@@ -50,14 +50,12 @@ def store_image(path, name, x, y):
5050# Locate waldo in image
5151def 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
9095def color_gray (image , heatmap , gray ):
@@ -98,22 +103,8 @@ def color_gray(image, heatmap, gray):
98103
99104# Predict all test images
100105for 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 ()
0 commit comments