Skip to content

Commit c6815c3

Browse files
Tidied code
1 parent 8d6f12b commit c6815c3

14 files changed

+46
-237
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,7 @@ dmypy.json
123123
### Python Patch ###
124124
.venv/
125125

126-
# End of https://www.gitignore.io/api/python
126+
# End of https://www.gitignore.io/api/python
127+
128+
# Visual Studio Code
129+
.vscode/

TapWork.py

+5-137
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,13 @@
55
import cv2
66
import copy
77

8-
9-
10-
taps = [(29,29),(10,10)]
11-
houses = [[4,(10,10)],[7,(29,29)],[8,(18,6)],[15,(12,16)],[6,[23,20]],[7,[6,19]]]
12-
13-
14-
15-
16-
178
def get_taps_demand(taps,houses):
189
tap_demand = []
1910
houses_tap = []
2011
for tap in taps:
2112
tap_demand.append(0)
2213

2314
for house in houses:
24-
# print(house)
2515
lowest_distance = 99999999999999999
2616
house_tap = 0
2717
housex = house[1][0]
@@ -38,9 +28,6 @@ def get_taps_demand(taps,houses):
3828

3929
for tap in houses_tap:
4030
tap_demand[tap[0]]+=tap[1]
41-
42-
# print(houses_tap)
43-
# print(tap_demand)
4431
return tap_demand
4532

4633
def work_done(distance):
@@ -61,44 +48,13 @@ def total_demand(tap_demands):
6148
total+=tap
6249
return total
6350

64-
# taps_d = get_taps_demand(taps,houses)
65-
# print(total_differnces(taps_d))
66-
#
67-
68-
69-
# tick = 0
70-
# for x1 in range(15):
71-
# print(tick/810000)
72-
# for y1 in (range(30)):
73-
# for x2 in (range(30)):
74-
# for y2 in (range(30)):
75-
# # print(x1,y1,x2,y2)
76-
# tick += 1
77-
# taps = [[x1, y1], [x2, y2]]
78-
# tap_demand = get_taps_demand(taps, houses)
79-
# total_differnce = total_demand(tap_demand)
80-
# if total_differnce < min_total_differnces:
81-
# happy_taps = []
82-
# happy_taps.append(taps)
83-
# min_total_differnces = total_differnce
84-
# if total_differnce == min_total_differnces:
85-
# happy_taps.append(taps)
86-
87-
88-
# print(happy_taps)
89-
# print(min_total_differnces)
90-
91-
taps = [[18,16],[29,29]]
92-
93-
def draw_network(houses,taps,image = "null"):
51+
def draw_network(houses,taps,image):
9452
pos = {}
9553
names = []
9654
edges = []
9755
for i in range(len(houses)):
9856
pos[str(houses[i][0])]=(houses[i][1][0],houses[i][1][1])
9957
edges.append((str(houses[i][0]),str(houses[i][0])))
100-
print(pos)
101-
print(edges)
10258

10359
G = nx.Graph()
10460
G.add_edges_from(edges)
@@ -109,48 +65,23 @@ def draw_network(houses,taps,image = "null"):
10965
edges = []
11066
for i in range(len(taps)):
11167
pos[(i+1)]=(taps[i][0],taps[i][1])
112-
print(pos)
11368
edges.append((i+1,i+1))
114-
print(pos)
115-
print(edges)
11669

11770
G = nx.Graph()
11871
G.add_edges_from(edges)
11972

12073
nx.draw_networkx(G, pos=pos, node_color='b',node_size=30,fontsize=50)
121-
# G.add_edges_from([(1, 1), (2, 2)])
122-
# pos = {1: (taps[0][0], taps[0][1]), 2: (taps[1][0], taps[1][1])}
123-
# #
124-
# nx.draw_networkx(G, pos=pos, node_color='b')
125-
print(image)
126-
if image != "null":
127-
img = cv2.imread(image)
128-
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
129-
plt.imshow(img)
74+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
75+
plt.imshow(image)
13076
plt.show()
13177

132-
133-
# import copy
134-
#
135-
# taps = [(10, 16), (18, 6), (19, 19)]
136-
# demands = get_taps_demand(taps,houses)
137-
# print(demands)
138-
#
139-
# print(total_demand(demands))
140-
#
141-
# taps = [(10, 16), (10,10), (19, 19)]
142-
# demands = get_taps_demand(taps,houses)
143-
# print(demands)
144-
#
145-
# print(total_demand(demands))
146-
14778
def total(demands):
14879
totalval = 0
14980
for demand in demands:
15081
totalval += demand
15182
return totalval
15283

153-
def greedy_brute(houses,amount_of_taps,grid_size,image="null"):
84+
def greedy_brute(houses,amount_of_taps,grid_size,image):
15485
tick = 0
15586
total_tick = amount_of_taps*grid_size[0]*grid_size[1]
15687
happy_taps = []
@@ -182,67 +113,4 @@ def greedy_brute(houses,amount_of_taps,grid_size,image="null"):
182113

183114
min_total_differnces = total_differnce
184115
stored_taps.append(happy_taps[-1])
185-
draw_network(houses, stored_taps,image)
186-
187-
188-
189-
190-
def brute_tap_position(houses,amount_of_taps,grid_size,image="null"):
191-
192-
193-
min_total_differnces = 999999999999999
194-
happy_taps = []
195-
possible_coords = []
196-
197-
for x in range(grid_size[0]+1):
198-
for y in range(grid_size[1]+1):
199-
possible_coords.append((x, y))
200-
201-
202-
203-
204-
205-
pairs = list(itertools.combinations_with_replacement(possible_coords,amount_of_taps))
206-
print(pairs[-1])
207-
total_ticks = (len(list(copy.copy(pairs))))
208-
#total_ticks = (grid_size[0]**(2*amount_of_taps))
209-
print(total_ticks)
210-
tick = 0
211-
212-
for pair in pairs:
213-
214-
tick += 1
215-
if tick % (int(total_ticks/1000)) == 0:
216-
print((tick / total_ticks)*100)
217-
taps = pair
218-
tap_demand = get_taps_demand(taps, houses)
219-
total_differnce = total_demand(tap_demand)
220-
221-
222-
if total_differnce == min_total_differnces:
223-
happy_taps.append(taps)
224-
225-
226-
if total_differnce < min_total_differnces:
227-
happy_taps = []
228-
happy_taps.append(taps)
229-
min_total_differnces = total_differnce
230-
231-
print(happy_taps)
232-
233-
for gtaps in happy_taps:
234-
draw_network(houses, gtaps,image=image)
235-
236-
237-
238-
# #brute_tap_position(houses,3,(20,20))
239-
#
240-
# houses = [(133.0, (130, 282)), (168.5, (55, 272)), (106.0, (72, 262)), (301.5, (86, 230)), (480.5, (14, 222)), (657.0, (93, 200)), (394.5, (108, 155)), (84.0, (228, 116)), (117.0, (115, 114)), (274.0, (44, 116)), (561.0,(212, 88)), (194.0, (80, 50)), (295.5, (124, 15))]
241-
#
242-
# taps = [(103, 159), (168, 65), (56, 240), (107, 29), (214, 92), (44, 116), (96, 218),(14, 222)]
243-
# draw_network(houses,taps,image="resized.jpg")
244-
245-
246-
247-
248-
116+
draw_network(houses, stored_taps,image)

address of the file location

-1
This file was deleted.

bigo_nodes.txt

-1
This file was deleted.

contours.py

+2-21
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
# import the necessary packages
2-
import argparse
32
import imutils
43
import cv2
54

65
# construct the argument parse and parse the arguments
7-
def get_contour_nodes():
8-
args = {}
9-
args["image"] = "black_and_white.png"
10-
11-
# load the image, convert it to grayscale, blur it slightly,
12-
# and threshold it
13-
image = cv2.imread(args["image"])
6+
def get_contour_nodes(image):
147
height, width, channels = image.shape
158
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
169
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
@@ -27,23 +20,11 @@ def get_contour_nodes():
2720
for c in cnts:
2821
# compute the center of the contour
2922
try:
30-
print(cv2.contourArea(c))
3123
if cv2.contourArea(c) > 40:
3224
M = cv2.moments(c)
3325
cX = int(M["m10"] / M["m00"])
3426
cY = int(M["m01"] / M["m00"])
3527
nodes.append([cv2.contourArea(c), [cX, cY]])
36-
37-
# draw the contour and center of the shape on the image
38-
#cv2.drawContours(image, [c], -1, (0, 0, 255), 2)
39-
#cv2.circle(image, (cX, cY), 7, (255, 0, 0), -1)
40-
#cv2.putText(image, "center", (cX - 20, cY - 20),
41-
# cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
42-
43-
# show the image
44-
#cv2.imshow("Image", image)
45-
46-
4728
except:
4829
pass
49-
return nodes
30+
return nodes

downloaded.png

-2.33 MB
Binary file not shown.

google.jpg

-18 KB
Binary file not shown.

google.png

-2.54 MB
Binary file not shown.

main.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import resize
2-
import messingcolour
2+
import recolour
33
import contours
44
import TapWork
55
import map_downloader
6+
import cv2
7+
import numpy as np
68

7-
map_downloader.download_patch((13.323964, -15.9257),'AgqByPum4K6T5wlV3oIAhSDvFHVRuoPs6cwipRdvprWtmvqld0poyLI54AP0e6HI')
9+
response = map_downloader.download_patch((13.623299, -15.192386),)
10+
image_array = np.asarray(bytearray(response.content), dtype=np.uint8)
11+
map_image = cv2.imdecode(image_array, -1)
812
max_size = 200
9-
height, width = resize.resize('downloaded.png',max_size)
10-
print(height,width)
11-
messingcolour.find_silver()
12-
houses = contours.get_contour_nodes()
13-
TapWork.greedy_brute(houses,5,(height,width),image='resized.jpg')
13+
resized_image = resize.resize(map_image,max_size)
14+
height, width, _ = resized_image.shape
15+
recoloured_image = recolour.find_silver(resized_image)
16+
houses = contours.get_contour_nodes(recoloured_image)
17+
TapWork.greedy_brute(houses,5,(height,width),resized_image)

map_downloader.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,4 @@ def download_patch(lat_long,api_key,zoom=19,dimensions=(1000,1000),file_format='
66
URL = ('https://dev.virtualearth.net/REST/v1/Imagery/Map/Aerial/{},{}/'
77
'{}?mapSize={},{}&format={}&key={}').format(latitude,longitude,zoom,x,y,file_format,api_key)
88
response = requests.get(URL)
9-
f = open('downloaded.png', 'wb')
10-
f.write(response.content)
11-
return response
12-
13-
#file = download_patch((13.338863, -15.922574),'AgqByPum4K6T5wlV3oIAhSDvFHVRuoPs6cwipRdvprWtmvqld0poyLI54AP0e6HI').content
14-
9+
return response

messingcolour.py

-54
This file was deleted.

recolour.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import cv2
2+
import numpy
3+
4+
def find_silver(image):
5+
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
6+
h,s,v = cv2.split(hsv)
7+
8+
rgb = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
9+
10+
mask = cv2.inRange(rgb, (150,160,160), (255, 255, 255))
11+
image = cv2.bitwise_and(image, image, mask=mask)
12+
13+
for w in range(image.shape[0]):
14+
for h in range(image.shape[1]):
15+
# checks if not pure black
16+
if(all(rgb != 0 for rgb in image[w][h])):
17+
# makes pixel completely white
18+
for colour in range(image.shape[2]):
19+
image[w][h][colour] = 255
20+
return image

resize.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import cv2
22

33
def resize(image,max_size):
4-
img = cv2.imread(image)
5-
height, width, channels = img.shape
6-
print(height,width)
4+
height, width, channels = image.shape
75
scale = max_size/(max(height,width))
8-
small = cv2.resize(img, (0,0), fx=scale, fy=scale)
9-
height,width,channels = small.shape
10-
11-
cv2.imwrite('resized.jpg',small)
12-
13-
return height,width
6+
resized_image = cv2.resize(image, (0,0), fx=scale, fy=scale)
7+
return resized_image

village.png

-1.06 MB
Binary file not shown.

0 commit comments

Comments
 (0)