Skip to content

Commit d0b9fbe

Browse files
Return more information with API
1 parent cb7b149 commit d0b9fbe

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

api.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ def toBase64(image):
1414
png_as_text = base64.b64encode(buffer).decode('utf-8')
1515
return png_as_text
1616

17-
def process(image,max_size=200,taps=5):
17+
def process(image,max_size=200,taps=3):
1818
resized_image = resize.resize(image,max_size)
1919
height, width, _ = resized_image.shape
2020
recoloured_image = recolour.find_silver(resized_image)
21-
houses = contours.get_contour_nodes(recoloured_image)
21+
houses, percentage = contours.get_contour_nodes(recoloured_image)
2222
tap_locations = TapWork.greedy_brute(houses,taps,(height,width))
2323
image = TapWork.draw_network(houses,tap_locations,resized_image,display=False)
24-
return toBase64(image)
24+
return dict(image=toBase64(image), houses=len(houses), taps=taps, percentage=percentage)
2525

2626
@app.route('/giveLocation', methods=['GET'])
2727
def giveLocation():
@@ -38,15 +38,15 @@ def giveLocation():
3838
image_array = numpy.asarray(bytearray(response.content), dtype=numpy.uint8)
3939
map_image = cv2.imdecode(image_array, -1)
4040
if max_size is not None and taps is not None:
41-
base64_payload = process(map_image,max_size=max_size,taps=taps)
41+
result = process(map_image,max_size=max_size,taps=taps)
4242
elif max_size is not None:
43-
base64_payload = process(map_image,max_size=max_size)
43+
result = process(map_image,max_size=max_size)
4444
elif taps is not None:
45-
base64_payload = process(map_image,taps=taps)
45+
result = process(map_image,taps=taps)
4646
else:
47-
base64_payload = process(map_image)
47+
result = process(map_image)
4848

49-
return jsonify(image=base64_payload)
49+
return jsonify(result)
5050

5151
if __name__ == '__main__':
5252
app.run(port=25565,host='0.0.0.0')

contours.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
# construct the argument parse and parse the arguments
66
def get_contour_nodes(image):
77
height, width = image.shape
8+
totalpixels = height * width;
9+
pixels = cv2.sumElems(image)[0] / 255;
10+
percentage = round((pixels / totalpixels) * 100)
811
# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
912
# blurred = cv2.GaussianBlur(gray, (5, 5), 0)
1013
# thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
@@ -27,4 +30,4 @@ def get_contour_nodes(image):
2730
nodes.append([cv2.contourArea(c), [cX, cY]])
2831
except:
2932
pass
30-
return nodes
33+
return nodes, percentage

main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# cv2.imshow("Window", recoloured_image)
2525
# cv2.waitKey(0)
2626

27-
houses = contours.get_contour_nodes(recoloured_image)
27+
houses, percentage = contours.get_contour_nodes(recoloured_image)
2828
print(houses)
2929
tap_locations = TapWork.greedy_brute(houses, 3, (height, width))
3030
print(tap_locations)

0 commit comments

Comments
 (0)