forked from mzsmakr/PGSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crop.py
executable file
·280 lines (248 loc) · 14.6 KB
/
crop.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
import cv2
import numpy as np
from pathlib import Path
import os
import time
import datetime
from logging import getLogger
import shutil
import math
import sys
from sys import argv
import importlib
import hashlib
import signal
LOG = getLogger('')
class Crop:
def __init__(self):
if len(argv) >= 2:
self.config = importlib.import_module(str(argv[1]))
else:
self.config = importlib.import_module('config')
self.screenshot_path = Path(self.config.SCREENSHOT_SAVE_PATH)
self.crop_save_path = os.getcwd() + '/process_img/'
self.not_find_path = os.getcwd() + '/not_find_img/'
self.web_img_path = os.getcwd()+'/web_img/'
# Create directories if not exists
file_path = os.path.dirname(self.crop_save_path)
if not os.path.exists(file_path):
LOG.info('process_img directory created')
os.makedirs(file_path)
# Create directories if not exists
file_path = os.path.dirname(self.not_find_path)
if not os.path.exists(file_path):
LOG.info('not_find_img directory created')
os.makedirs(file_path)
# Create directories if not exists
file_path = os.path.dirname(self.web_img_path)
if not os.path.exists(file_path):
LOG.info('web_img directory created')
os.makedirs(file_path)
self.init_crop_py = False
self.last_crop1 = np.zeros((10, 10, 3), np.uint8)
self.last_crop2 = np.zeros((10, 10, 3), np.uint8)
self.last_crop3 = np.zeros((10, 10, 3), np.uint8)
self.last_crop4 = np.zeros((10, 10, 3), np.uint8)
self.last_crop5 = np.zeros((10, 10, 3), np.uint8)
self.last_crop6 = np.zeros((10, 10, 3), np.uint8)
self.diff_threshold = 10000
def crop_img(self, fullpath_filename):
now = datetime.datetime.now()
unix_time = int(now.timestamp())
file_update_time = int(os.stat(str(fullpath_filename)).st_mtime)
if unix_time - file_update_time > 1800:
LOG.info('Attachment File is too old')
os.remove(fullpath_filename)
return
filename = os.path.basename(fullpath_filename)
filename, ext = os.path.splitext(filename)
img = cv2.imread(str(fullpath_filename), 3)
if img is not None:
if img.dtype == 'uint16':
# print('16 bit image')
img = (img / 256).astype('uint8')
height, width, channels = img.shape
find_size_config = False
for size in self.config.RAID_NEARBY_SIZE:
if width == size['width'] and height == size['height']:
find_size_config = True
LOG.debug('ext = {}'.format(ext))
ref_b = 156
ref_g = 194
ref_r = 252
dif3_1 = pow(img[size['comp3_y']][size['comp3_x']][0] - ref_b, 2)
dif3_2 = pow(img[size['comp3_y']][size['comp3_x']][1] - ref_g, 2)
dif3_3 = pow(img[size['comp3_y']][size['comp3_x']][2] - ref_r, 2)
error3 = math.sqrt(dif3_1+dif3_2+dif3_3)
dif2_1 = pow(img[size['comp2_y']][size['comp2_x']][0] - ref_b, 2)
dif2_2 = pow(img[size['comp2_y']][size['comp2_x']][1] - ref_g, 2)
dif2_3 = pow(img[size['comp2_y']][size['comp2_x']][2] - ref_r, 2)
error2 = math.sqrt(dif2_1+dif2_2+dif2_3)
dif1_1 = pow(img[size['comp1_y']][size['comp1_x']][0] - ref_b, 2)
dif1_2 = pow(img[size['comp1_y']][size['comp1_x']][1] - ref_g, 2)
dif1_3 = pow(img[size['comp1_y']][size['comp1_x']][2] - ref_r, 2)
error1 = math.sqrt(dif1_1+dif1_2+dif1_3)
LOG.debug('comp dif 3+:{}, dif 2:{}, dif 1:{} ' .format(error3, error2, error1))
scale = size['width'] / 1536
match = False
if error3 <= 15:
match = True
LOG.info('screenshot with {}x{} found with 3+ raids'.format(width, height))
crop1 = img[size['crop_y1']:size['crop_y1']+size['crop_h'],
size['crop3_x1']:size['crop3_x1']+size['crop_w']]
crop2 = img[size['crop_y1']:size['crop_y1']+size['crop_h'],
size['crop3_x2']:size['crop3_x2']+size['crop_w']]
crop3 = img[size['crop_y1']:size['crop_y1']+size['crop_h'],
size['crop3_x3']:size['crop3_x3']+size['crop_w']]
crop4 = img[size['crop_y2']:size['crop_y2']+size['crop_h'],
size['crop3_x1']:size['crop3_x1']+size['crop_w']]
crop5 = img[size['crop_y2']:size['crop_y2']+size['crop_h'],
size['crop3_x2']:size['crop3_x2']+size['crop_w']]
crop6 = img[size['crop_y2']:size['crop_y2']+size['crop_h'],
size['crop3_x3']:size['crop3_x3']+size['crop_w']]
elif error2 <= 15:
match = True
LOG.info('screenshot with {}x{} found with 2 raids'.format(width, height))
crop1 = img[size['crop_y1']:size['crop_y1']+size['crop_h'],
size['crop2_x1']:size['crop2_x1']+size['crop_w']]
crop2 = img[size['crop_y1']:size['crop_y1']+size['crop_h'],
size['crop2_x2']:size['crop2_x2']+size['crop_w']]
crop3 = None
crop4 = None
crop5 = None
crop6 = None
elif error1 <= 15:
match = True
LOG.info('screenshot with {}x{} found with 1 raid'.format(width, height))
crop1 = img[size['crop_y1']:size['crop_y1']+size['crop_h'],
size['crop1_x1']:size['crop1_x1']+size['crop_w']]
crop2 = None
crop3 = None
crop4 = None
crop5 = None
crop6 = None
if match:
if not self.init_crop_py:
self.last_crop1 = crop1
self.last_crop2 = crop2
self.last_crop3 = crop3
self.last_crop4 = crop4
self.last_crop5 = crop5
self.last_crop6 = crop6
if crop1 is not None and int(crop1.mean()) < 240:
if self.last_crop1 is not None and self.last_crop1.shape == crop1.shape:
s = cv2.norm(crop1, self.last_crop1, cv2.NORM_L1)
LOG.debug('crop1 s={} scale={}'.format(s, scale))
if s >= self.diff_threshold*scale*scale or not self.init_crop_py:
cv2.imwrite(self.crop_save_path+filename+'_01.png', crop1)
self.last_crop1 = crop1
LOG.debug('New Image. crop1 saved. s={} scale={}'.format(s, scale))
else:
cv2.imwrite(self.crop_save_path + filename + '_01.png', crop1)
self.last_crop1 = crop1
LOG.debug('New Image. crop1 saved.')
if crop2 is not None and int(crop2.mean()) < 240:
if self.last_crop2 is not None and self.last_crop2.shape == crop2.shape:
s = cv2.norm(crop2, self.last_crop2, cv2.NORM_L1)
LOG.debug('crop2 s={} scale={}'.format(s, scale))
if s >= self.diff_threshold*scale*scale or not self.init_crop_py:
cv2.imwrite(self.crop_save_path+filename+'_02.png', crop2)
self.last_crop2 = crop2
LOG.debug('New Image. crop2 saved. s={} scale={}'.format(s, scale))
else:
cv2.imwrite(self.crop_save_path + filename + '_02.png', crop2)
self.last_crop2 = crop2
LOG.debug('New Image. crop2 saved.')
if crop3 is not None and int(crop3.mean()) < 240:
if self.last_crop3 is not None and self.last_crop3.shape == crop3.shape:
s = cv2.norm(crop3, self.last_crop3, cv2.NORM_L1)
LOG.debug('crop3 s={} scale={}'.format(s, scale))
if s >= self.diff_threshold*scale*scale or not self.init_crop_py:
cv2.imwrite(self.crop_save_path+filename+'_03.png', crop3)
self.last_crop3 = crop3
LOG.debug('New Image. crop3 saved. s={} scale={}'.format(s, scale))
else:
cv2.imwrite(self.crop_save_path + filename + '_03.png', crop3)
self.last_crop3 = crop3
LOG.debug('New Image. crop3 saved.')
if crop4 is not None and int(crop4.mean()) < 240:
if self.last_crop4 is not None and self.last_crop4.shape == crop4.shape:
s = cv2.norm(crop4, self.last_crop4, cv2.NORM_L1)
LOG.debug('crop4 s={} scale={}'.format(s, scale))
if s >= self.diff_threshold*scale*scale or not self.init_crop_py:
cv2.imwrite(self.crop_save_path+filename+'_04.png', crop4)
self.last_crop4 = crop4
LOG.debug('New Image. crop4 saved. s={} scale={}'.format(s, scale))
else:
cv2.imwrite(self.crop_save_path + filename + '_04.png', crop4)
self.last_crop4 = crop4
LOG.debug('New Image. crop4 saved.')
if crop5 is not None and int(crop5.mean()) < 240:
if self.last_crop5 is not None and self.last_crop5.shape == crop5.shape:
s = cv2.norm(crop5, self.last_crop5, cv2.NORM_L1)
LOG.debug('crop5 s={} scale={}'.format(s, scale))
if s >= self.diff_threshold*scale*scale or not self.init_crop_py:
cv2.imwrite(self.crop_save_path+filename+'_05.png', crop5)
self.last_crop5 = crop5
LOG.debug('New Image. crop5 saved. s={} scale={}'.format(s, scale))
else:
cv2.imwrite(self.crop_save_path + filename + '_05.png', crop5)
self.last_crop5 = crop5
LOG.debug('New Image. crop5 saved.')
if crop6 is not None and int(crop6.mean()) < 240:
if self.last_crop6 is not None and self.last_crop6.shape == crop6.shape:
s = cv2.norm(crop6, self.last_crop6, cv2.NORM_L1)
LOG.debug('crop6 s={} scale={}'.format(s, scale))
if s >= self.diff_threshold*scale*scale or not self.init_crop_py:
cv2.imwrite(self.crop_save_path+filename+'_06.png', crop6)
self.last_crop6 = crop6
LOG.debug('New Image. crop6 saved. s={} scale={}'.format(s, scale))
else:
cv2.imwrite(self.crop_save_path + filename + '_06.png', crop6)
self.last_crop6 = crop6
LOG.debug('New Image. crop6 saved.')
self.init_crop_py = True
break
else:
LOG.info('screenshot with {}x{} found without raid'.format(width, height))
# os.remove(fullpath_filename)
if not find_size_config:
shutil.copy2(fullpath_filename, self.not_find_path+'Screen_' + str(width) + 'x' + str(height) + ext)
LOG.info('No size matching config found in RAID_NEARBY_SIZE')
LOG.info('Check not_find_img directory and add RAID_NEARBY_SIZE in config for the screenshot iamge')
split = filename.split('_')
if len(split) > 1:
device_uuid = split[0]
save_file_path = self.web_img_path + 'Device_' + device_uuid + '.png'
shutil.copy(fullpath_filename, save_file_path)
os.remove(fullpath_filename)
time.sleep(0.1)
def crop_task(self, raidscan, task_id):
try:
LOG.info('Crop screenshot task started for process {}'.format(task_id+1))
LOG.debug('Screenshot path:{}'.format(self.screenshot_path))
process_count = self.config.CROP_PROCESSES
while True:
for fullpath_filename in self.screenshot_path.glob('*.jpg'):
if process_count > 1 and not \
int(hashlib.md5(str(fullpath_filename).encode('utf-8')).hexdigest(), 16)\
% process_count == task_id:
continue
self.crop_img(fullpath_filename)
for fullpath_filename in self.screenshot_path.glob('*.png'):
if process_count > 1 and not \
int(hashlib.md5(str(fullpath_filename).encode('utf-8')).hexdigest(), 16)\
% process_count == task_id:
continue
self.crop_img(fullpath_filename)
time.sleep(0.1) # task runs every 0.1 seconds
except KeyboardInterrupt:
os.killpg(0, signal.SIGINT)
sys.exit(1)
except Exception as e:
LOG.error('Unexpected Exception in crop Process: {}'.format(e))
if raidscan is not None:
raidscan.restart_crop(task_id)
else:
os.killpg(0, signal.SIGINT)
sys.exit(1)