Skip to content

Commit 698dd3c

Browse files
fix: improve setup and visualization
1 parent fec9dfe commit 698dd3c

3 files changed

Lines changed: 25 additions & 23 deletions

File tree

Main.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,12 @@ def store_image(path, name, x, y):
5050
# Locate waldo in image
5151
def locate(img, filepath="Data/Raw/Train/"):
5252
num_sub_img = 100
53-
data = cv2.cvtColor(cv2.imread(filepath + img), cv2.COLOR_BGR2RGB)
53+
original_image = cv2.cvtColor(cv2.imread(filepath + img), cv2.COLOR_BGR2RGB).astype(np.uint8)
54+
data = np.array(original_image)
5455
gray = cv2.cvtColor(cv2.imread(filepath + img), cv2.COLOR_BGR2GRAY)
5556
coloured_heat = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
5657

5758
heatmap = heatmodel.predict(data.reshape(1, data.shape[0], data.shape[1], data.shape[2]))
58-
plt.imshow(heatmap[0, :, :, 0])
59-
plt.title("Heatmap")
60-
plt.show()
6159

6260
# Show 95 certainty heatmap and grayscale->color
6361
# plt.imshow(heatmap[0, :, :, 0] > 0.95, cmap="gray")
@@ -75,16 +73,23 @@ def locate(img, filepath="Data/Raw/Train/"):
7573
# for i, j in chosen_sub_images:
7674
# store_image('Data/Raw/Train/' + img, img, int(i*3),int(j*3)))
7775

76+
data_marked = np.array(data)
77+
7878
for i, j in zip(x, y):
7979
y_pos = j * 3
8080
x_pos = i * 3
81-
cv2.rectangle(data, (x_pos, y_pos), (x_pos + 64, y_pos + 64), (0, 0, 255), 5)
81+
cv2.rectangle(data_marked, (x_pos, y_pos), (x_pos + 64, y_pos + 64), (0, 0, 255), 5)
82+
marker_opacity = 0.6
83+
data = data_marked.astype(np.float64) * marker_opacity + data.astype(np.float64) * (1 - marker_opacity)
84+
orig_size = tuple(reversed(original_image.shape[:2]))
85+
data = cv2.resize(data.astype(np.uint8), orig_size)
8286

8387
coloured_heat = coloured_heat.astype(np.uint8)
8488
# Image.fromarray(coloured_heat).show()
8589
# if random.randint(0, 10) < 1:
8690
# store_image('Data/Raw/Train/'+img, img, i, j)
87-
return data, heatmap
91+
heatmap = cv2.normalize(cv2.resize(cv2.cvtColor(heatmap[0], cv2.COLOR_GRAY2BGR), orig_size), None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
92+
return original_image, data, heatmap
8893

8994

9095
def color_gray(image, heatmap, gray):
@@ -98,22 +103,8 @@ def color_gray(image, heatmap, gray):
98103

99104
# Predict all test images
100105
for img in os.listdir("Data/Raw/Test/"):
101-
print(img)
102106
try:
103-
annotated, heatmap = locate(img, filepath="Data/Raw/Test/")
104-
# Image.fromarray(annotated).show()
105-
Image.fromarray(annotated).save("README/" + img)
106-
Image.fromarray(heatmap).save("README/heat" + img)
107-
# plt.title("Augmented")
108-
# plt.imshow(annotated)
109-
# plt.show()
107+
image, annotated, heatmap = locate(img, filepath="Data/Raw/Test/")
108+
Image.fromarray(np.hstack((image, annotated, heatmap))).show()
110109
except Exception as e:
111110
print('exception', e)
112-
113-
# Predict a specific image
114-
annotated, heatmap = locate('16t.jpg', filepath="Data/Raw/Test/")
115-
Image.fromarray(annotated).show()
116-
print(annotated.shape[:2])
117-
plt.title("Augmented")
118-
plt.imshow(annotated)
119-
plt.show()

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ navigate into the project folder
1414
cd FindWaldo
1515
```
1616

17+
install the required libraries
18+
```bash
19+
python3.10 -m pip install -r requirements.txt
20+
```
21+
1722
if everything went well, you should now be able to run the code
1823
```bash
19-
python3 kerasfindwaldo.py
24+
python3.10 Main.py
2025
```
2126

2227
## Motivation

requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
matplotlib==3.9.2
2+
opencv-python==4.9.0.80
3+
pillow==10.3.0
4+
numpy==1.26.4
5+
keras==3.5.0
6+
tensorflow==2.17.0

0 commit comments

Comments
 (0)