This repository was archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreencast_detection.py
215 lines (174 loc) · 6.69 KB
/
screencast_detection.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
import functools
import json
from collections import defaultdict
from multiprocessing import cpu_count
import ahocorasick
import cv2
import numpy as np
import pytesseract
from utils import ThreadPool, crop_image, map_by_workers, video_reader
def simple_rect_detector(img, x_k=2, y_k=2):
"""Generates overlapping rectangles to cover whole image
x_k - how many overlapping rectangles should fit in img width
y_k - how many overlapping rectangles should fit in img height
returns a generator with rectangles
"""
x_k = x_k if x_k > 1 else 2
y_k = y_k if y_k > 1 else 2
h, w = img.shape[:2]
x_d = w / (x_k + 1)
y_d = h / (y_k + 1)
return (
(int(x), int(y), int(x + 2 * x_d), int(y + 2 * y_d))
for y in np.linspace(0, h - h / (y_k - 1), y_k)
for x in np.linspace(0, w - w / (x_k - 1), x_k)
)
def aho_create_automaton(keywords_dct):
"""Inserts all keys from dict into a Aho Corasik trie and creates state machine"""
aho_automaton = ahocorasick.Automaton()
process_app_keyword = defaultdict(
lambda: lambda a, b: (a, b)
) # will return app name for all apps except browser
process_app_keyword["browser"] = lambda a, b: (
a,
a,
) # will return keyword(link) for browser
for app_name, app_data in keywords_dct.items():
for key_word in app_data["keywords"]:
aho_automaton.add_word(
key_word, process_app_keyword[app_name](key_word, app_name)
)
aho_automaton.make_automaton()
return aho_automaton
def detect_anomalies_in_text(text, aho_automaton):
"""Matches text to key words from dict"""
found_apps = set()
for end_entry_idx, (found_word, value) in aho_automaton.iter(text.lower()):
found_apps.add(value)
return list(found_apps)
def recognize_text_in_image(image, try_inverted=False):
"""Recognizes text on given image"""
# -- psm 7 - assume text on image as signle line, just as EAST provides
# from https://medium.com/@jaafarbenabderrazak.info/ocr-with-tesseract-opencv-and-python-d2c4ec097866
# when testing on apps rects, change to 6 or 11 or leave without (3 by default)
config = "-l eng --oem 1 --psm 11"
detected_text = pytesseract.image_to_string(image, config=config)
if try_inverted:
inverted_text = pytesseract.image_to_string(255 - image, config=config)
detected_text += inverted_text
return detected_text
def detect_screenshot_anomalies(img, keywords_dct, rect_detector, parallel=True):
"""Detects anomalies in text on screenshot"""
rects = rect_detector(img)
text_images = tuple(crop_image(img, r) for r in rects)
if parallel:
texts = map_by_workers(
functools.partial(recognize_text_in_image, try_inverted=True), text_images
)
else:
texts = tuple(
recognize_text_in_image(area, try_inverted=True) for area in text_images
)
screenshot_text = "".join(set(texts))
warn_dct = keywords_dct["warn"]
ok_dct = keywords_dct["ok"]
warn_apps = detect_anomalies_in_text(
screenshot_text, aho_create_automaton(warn_dct)
)
ok_apps = detect_anomalies_in_text(screenshot_text, aho_create_automaton(ok_dct))
return {"warn": warn_apps, "ok": ok_apps}
def generate_anomalies_markup(
frame, frame_timestamp, frame_number, prev_frame_timestamp, keywords_dct
):
"""Creates dictionary containing frame anomalies, defined in keywords_dct"""
try:
frame_markup = detect_screenshot_anomalies(
frame, keywords_dct, simple_rect_detector, parallel=True
)
timestamps = {
"start_time": prev_frame_timestamp / 1000,
"end_time": frame_timestamp / 1000,
}
except Exception as e:
raise Exception(
f"Frame #{frame_number}, time: {frame_timestamp} failed, cause: {str(e)}"
)
else:
return {**timestamps, **frame_markup}
def detect_screencast_anomalies(video, keywords_dct, parallel=False):
"""Generates anomalies markup for screencast video"""
if not isinstance(video, cv2.VideoCapture): # assuming video is a string
video = cv2.VideoCapture(video)
fps = video.get(cv2.CAP_PROP_FPS)
markup = []
with ThreadPool(cpu_count()) as pool:
prev_timestamp = 0
wait_results = []
for frame, timestamp, frame_number in video_reader(video, sample_rate=fps // 2):
if parallel:
wait_results.append(
pool.apply_async(
generate_anomalies_markup,
(
frame.copy(),
timestamp,
frame_number,
prev_timestamp,
keywords_dct,
),
)
)
else:
markup.append(
pool.apply(
generate_anomalies_markup,
(
frame.copy(),
timestamp,
frame_number,
prev_timestamp,
keywords_dct,
),
)
)
prev_timestamp = timestamp
if parallel:
markup = tuple(
r.get() for r in wait_results
) # wait for results if parallel
return markup
if __name__ == "__main__":
"""usage of 'detect_screenshot_anomalies' example"""
import time
from argparse import ArgumentParser
from pprint import pprint
parser = ArgumentParser()
parser.add_argument(
"-s", "--source", type=str, default="video", help="type of source: video|image"
)
parser.add_argument("-p", "--path", type=str, help="path to input source")
parser.add_argument(
"-t",
"--parallel",
action="store_true",
help="amount of padding to add to each border of ROI",
)
args = vars(parser.parse_args())
f = open("./dictionary.json")
key_words = json.load(f)
f.close()
print("Processing started")
t = time.time()
if args["source"] == "video":
anomalies_markup = detect_screencast_anomalies(
args["path"], key_words, args["parallel"]
)
elif args["source"] == "image":
anomalies_markup = detect_screenshot_anomalies(
cv2.imread(args["path"]), key_words, simple_rect_detector, args["parallel"]
)
else:
print(f'Wrong source type, got {args["source"]}')
duration = time.time() - t
print(f"Processing of {args['path']} ended, duration - {duration}")
pprint(anomalies_markup)