forked from touhidrahman/eyecatching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
352 lines (300 loc) · 12.6 KB
/
controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import subprocess
import os
import sys
import shutil
import imagehash
import cv2
import pandas
import time
import numpy as np
from PIL import Image
from urllib.parse import urlparse
from eyecatchingutil import MetaImage
from eyecatchingutil import FirefoxScreenshot
from eyecatchingutil import ChromeScreenshot
from eyecatchingutil import Coordinates
from eyecatchingutil import ImageComparator
from cv2 import VideoWriter, VideoWriter_fourcc, imread, resize
class Controller:
output_id = "_"
block_size = 20
width = 1280
threshold = 8
algorithm = "ahash"
ref = None # MetaImage
com = None # MetaImage
ref_screenshot = None # BrowserScreenshot
com_screenshot = None # BrowserScreenshot
url = None
def recursive(self, image1 = None, image2 = None):
self.normalize_images(image1, image2)
self.set_images(image1, image2)
self._rec_count = 0
self._rec_total_diff = 0
self._rec_total_area_marked = 0
start_time = time.time()
self.divide_recursive(self.ref.coordinates.as_tuple(), 0)
stop_time = time.time()
output_filename = self.save_output(self.ref.image, "recursive")
avg_dissimilarity = round(self._rec_total_diff / self._rec_count, 2) if self._rec_count != 0 else 0
print("Done:\tAverage dissimilarity: {0:.2f}%".format(avg_dissimilarity))
print("Done: \tDissimilar area: {0:.2f}%".format(
100 * self._rec_total_area_marked / self.ref.coordinates.get_area()
))
print("Done: \tExecution time: {0:.4f} seconds".format(stop_time - start_time))
return Image.open(output_filename)
def compare_recursive(self, patch_coords):
"""
Compares two image slice with given coordinates
"""
ref_img_slice = self.ref.image.crop(patch_coords)
com_img_slice = self.com.image.crop(patch_coords)
ic = ImageComparator(ref_img_slice, com_img_slice)
diff = ic.hamming_diff(self.algorithm)
if diff > 0:
self.divide_recursive(patch_coords, diff)
def divide_recursive(self, initial_coords, diff):
(x1, y1, x2, y2) = initial_coords
coords = Coordinates(x1, y1, x2, y2)
# return and save if image is less than block size
if (coords.width <= self.block_size or coords.height <= self.block_size) and diff != 0:
self.mark_image_recursive(initial_coords, diff)
# Divide the image with larger side
else:
self.compare_recursive(coords.first_half())
self.compare_recursive(coords.second_half())
def mark_image_recursive(self, patch_coords, diff):
(x1, y1, x2, y2) = patch_coords
coords = Coordinates(x1, y1, x2, y2)
patch = self.ref.image.crop(patch_coords)
opacity = round((100 * float(diff) / 64) / 100, 1)
blended = self.blend_image(patch, opacity)
self.ref.image.paste(blended, patch_coords)
self._rec_count += 1
self._rec_total_diff += opacity * 100
self._rec_total_area_marked += coords.get_area()
def save_output(self, image_obj:Image.Image, methodname:str):
method = methodname[:3]
output_name = "output_{0}_{1}_{2}_{3}_{4}.{5}".format(
method,
self.output_id,
self.ref.name,
self.com.name,
self.algorithm,
self.ref.ext
)
image_obj.save(output_name)
print("Done: \tOutput saved as: {0}".format(output_name))
return output_name
def linear(self, image1 = None, image2 = None):
self.normalize_images(image1, image2)
self.set_images(image1, image2)
return self.compare_linear()
def compare_linear(self):
"""
Compare two images block by block
"""
start_time = time.time()
counter = 0
counter_problem = 0
total_diff = 0
dissimilar_area = 0
edge = int(self.block_size)
for x in range(0, self.com.image.width, edge):
for y in range(0, self.com.image.height, edge):
coords = (x, y, x + edge, y + edge)
_coords_obj = Coordinates(x, y, x + edge, y + edge)
ref_tile = self.ref.get_cropped(coords)
com_tile = self.com.get_cropped(coords)
# compare with ref tile
ic = ImageComparator(ref_tile, com_tile)
hash_diff = ic.hash_diff(self.algorithm)
hash_diff_percent = ic.hash_diff_percent(self.algorithm)
# get an opacity value between 0 - 1
opacity = float(hash_diff_percent) / 100
if hash_diff >= self.threshold:
blended = self.blend_image(ref_tile, opacity)
self.ref.image.paste(blended, coords)
counter_problem += 1
dissimilar_area += _coords_obj.get_area()
del ref_tile, com_tile
total_diff += hash_diff_percent
counter += 1
stop_time = time.time()
self.save_output(self.ref.image, "linear")
print("Done: \tTotal blocks compared: {0}.".format(counter))
print("Done: \tNumber of blocks with dissimilarity: {0}".format(counter_problem))
print("Done: \tAverage dissimilarity {0:.2f}%.".format(round(total_diff / counter, 2)))
print("Done: \tDissimilar area: {0:.2f}%".format(
100 * dissimilar_area / self.ref.coordinates.get_area()
))
print("Done: \tExecution time: {0:.4f} seconds".format(stop_time - start_time))
return self.ref.image
def blend_image(self, image_obj, opacity, color = "salmon"):
img1 = image_obj.convert("RGB")
img2 = Image.new("RGB", img1.size, color)
return Image.blend(img1, img2, opacity)
def get_screenshot(self, url):
self.ref_screenshot.width = self.width
self.com_screenshot.width = self.width
self.ref_screenshot.take_shot(url)
self.com_screenshot.take_shot(url)
def set_images(self, ref_imagename = None, com_imagename = None):
if ref_imagename is None:
self.ref = MetaImage(self.ref_screenshot.imagename)
else:
self.ref = MetaImage(ref_imagename)
if com_imagename is None:
self.com = MetaImage(self.com_screenshot.imagename)
else:
self.com = MetaImage(com_imagename)
def normalize_images(self, image1, image2):
"""
Make 2 images equal height by adding white background to the smaller image
"""
img1 = MetaImage(image1)
img2 = MetaImage(image2)
print("Info: \t{0} image size: {1}x{2}".format(image1, img1.width, img1.height))
print("Info: \t{0} image size: {1}x{2}".format(image2, img2.width, img2.height))
print("Work:\tMaking both image size equal (as larger image)")
if img1.size == img2.size:
print("Info: \tImage sizes are already equal")
return
bigger_ht = img1.height if (img1.height >= img2.height) else img2.height
bigger_wd = img1.width if (img1.width >= img2.width) else img2.width
newimg = Image.new("RGB", (bigger_wd, bigger_ht), "white")
# which one is smaller
if img1.size == (bigger_wd, bigger_ht):
newimg.paste(img2.image)
newimg.save(image2)
else:
newimg.paste(img1.image)
newimg.save(image1)
print("Done: \t{0} and {1} both are now {2}x{3} pixels.".format(
image1, image2, bigger_wd, bigger_ht
))
def detect_shift(self, image1, image2):
"""
Detect shift of objects between two images
"""
self.normalize_images(image1, image2)
self.set_images(image1, image2)
print("Work:\tStarting shift detection process")
fourcc = VideoWriter_fourcc(*"XVID")
img1 = imread(image1)
img2 = imread(image2)
size = img1.shape[1], img1.shape[0]
output_vid = VideoWriter(
"output_vid.avi", # output_filename
fourcc, # codec
float(40), # fps
size, # framesize
True # write color frames
)
# make a white image for comparing
img_white = np.zeros((size[1], size[0], 3), np.uint8)
img_white.fill(255)
start_time = time.time()
# add frames to output video
output_vid.write(img_white)
output_vid.write(img1)
output_vid.write(img2)
output_vid.write(img1)
output_vid.release()
first_frame = None
video = cv2.VideoCapture("output_vid.avi")
step = 1
objects_ref = []
objects_com = []
def draw_rectangles(frame, is_ref_image):
color_red = (0, 0, 255)
color_green = (0, 255, 0)
if is_ref_image:
for i in objects_ref:
(x, y, w, h) = i
cv2.rectangle(
frame,
(x, y),
(x + w, y + h),
color_red,
2 # strokes
)
else:
for i in objects_com:
(x, y, w, h) = i
cv2.rectangle(
frame,
(x, y),
(x + w, y + h),
color_green,
2 # strokes
)
return frame
while True:
is_being_read, frame = video.read()
if is_being_read is True:
current_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
else:
break
# remove blur and noise
# kernel size (21, 21), std deviation = 0
current_frame = cv2.GaussianBlur(current_frame, (21, 21), 0)
if first_frame is None:
first_frame = current_frame
continue
# get the differences between current and ref frame
delta_frame = cv2.absdiff(first_frame, current_frame)
# convert background above threshold to white
threshold_frame = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]
# smoothen to remove sharp edges
# this frame now holds closed shapes with objects against background
threshold_frame = cv2.dilate(threshold_frame, None, iterations = 2)
(_, contours, _) = cv2.findContours(
threshold_frame.copy(),
cv2.RETR_EXTERNAL, # ignore inside contours
cv2.CHAIN_APPROX_SIMPLE # method for locating contours
)
# bigger for big objects, smaller for small
# 100 = 10 x 10px
shape_size_factor = 100
for contour in contours:
if cv2.contourArea(contour) < shape_size_factor:
continue
# get corresponding bounding for the detected contour
(x, y, w, h) = cv2.boundingRect(contour)
if step == 1:
objects_ref.append((x, y, w, h))
elif step == 2:
objects_com.append((x, y, w, h))
if step == 1:
frame = draw_rectangles(frame, True)
output_filename = "output_struct_{0}_{1}.{2}".format(
self.output_id,
self.ref.name,
self.ref.ext
)
elif step == 2:
frame = draw_rectangles(frame, False)
output_filename = "output_struct_{0}_{1}.{2}".format(
self.output_id,
self.com.name,
self.ref.ext
)
elif step == 3:
# green on top
frame = draw_rectangles(frame, True)
frame = draw_rectangles(frame, False)
output_filename = "output_shift_{0}_{1}_{2}.{3}".format(
self.output_id,
self.ref.name,
self.com.name,
self.ref.ext
)
cv2.imwrite(output_filename, frame)
step += 1
cv2.destroyAllWindows()
video.release()
stop_time = time.time()
print("Done:\tShift detection process completed")
print("Done:\tExecution time: {0:.4f} seconds".format(stop_time - start_time))
return Image.open(output_filename)