Skip to content

Commit 53dd9f7

Browse files
Fix API & network graphing
1 parent ba0426a commit 53dd9f7

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

TapWork.py

+23-25
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import itertools
99
import cv2
1010
import copy
11+
import numpy as np
1112
from tqdm import tqdm
1213

1314
def get_taps_demand(taps,houses):
@@ -54,42 +55,39 @@ def total_demand(tap_demands):
5455

5556
def draw_network(houses,taps,image, display = True):
5657
pos = {}
57-
names = []
58-
edges = []
58+
names = {}
59+
g = nx.Graph()
5960
for i in range(len(houses)):
60-
pos[str(houses[i][0])]=(houses[i][1][0],houses[i][1][1])
61-
edges.append((str(houses[i][0]),str(houses[i][0])))
61+
g.add_node(i)
62+
pos[i]=(houses[i][1][0],houses[i][1][1])
63+
names[i]=str(round(houses[i][0]))
6264

63-
G = nx.Graph()
64-
G.add_edges_from(edges)
65-
66-
nx.draw_networkx(G, pos=pos, node_color='r',node_size = 20,font_size=10)
65+
nx.draw_networkx(g, pos=pos, labels=names, node_color='r',node_size = 15,font_size=10)
6766

6867
pos = {}
69-
edges = []
70-
for i in range(len(taps)):
71-
pos[(i+1)]=(taps[i][0],taps[i][1])
72-
edges.append((i+1,i+1))
73-
74-
G = nx.Graph()
75-
G.add_edges_from(edges)
68+
names = {}
69+
g = nx.Graph()
70+
for i in range(len(houses)):
71+
g.add_node(i)
72+
pos[i]=(taps[i][0],taps[i][1])
73+
names[i]= str(i + 1)
7674

77-
nx.draw_networkx(G, pos=pos, node_color='b',node_size=42,fontsize=15)
75+
nx.draw_networkx(g, pos=pos, names=names, node_color='b',node_size=90,fontsize=8, font_color='w')
7876
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
7977
fig1 = plt.gcf()
8078
plt.axis('off')
8179
plt.imshow(image)
8280
# plt.show()
8381

84-
fig1.savefig('figureTaps.png',bbox_inches='tight')
85-
im = Image.open('figureTaps.png')
86-
bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
87-
diff = ImageChops.difference(im, bg)
88-
diff = ImageChops.add(diff,diff,2.0,-100)
89-
bbox = diff.getbbox()
90-
image = im.crop(bbox)
91-
image.save('figureTaps.png')
92-
return image
82+
fig1.tight_layout(pad=0)
83+
fig1.canvas.draw()
84+
img = np.frombuffer(fig1.canvas.tostring_rgb(), dtype=np.uint8)
85+
img = img.reshape(fig1.canvas.get_width_height()[::-1] + (3,))
86+
img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
87+
88+
crop_img = img[5:455, 94:544]
89+
# cv2.imwrite("figureTaps.png",crop_img)
90+
return crop_img
9391

9492
def total(demands):
9593
totalval = 0

api.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
cors = CORS(app)
1111

1212
def toBase64(image):
13-
# image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
1413
_, buffer = cv2.imencode('.png', image)
1514
png_as_text = base64.b64encode(buffer).decode('utf-8')
1615
return png_as_text
@@ -22,7 +21,6 @@ def process(image,max_size=200,taps=5):
2221
houses = contours.get_contour_nodes(recoloured_image)
2322
tap_locations = TapWork.greedy_brute(houses,taps,(height,width))
2423
image = TapWork.draw_network(houses,tap_locations,resized_image,display=False)
25-
image = cv2.imread('figureTaps.png')
2624
return toBase64(image)
2725

2826
@app.route('/giveLocation', methods=['GET'])
@@ -51,4 +49,4 @@ def giveLocation():
5149
return jsonify(image=base64_payload)
5250

5351
if __name__ == '__main__':
54-
app.run(port=25565,host='0.0.0.0')
52+
app.run(port=25565,host='0.0.0.0')

figureTaps.png

150 KB
Loading

0 commit comments

Comments
 (0)