-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart.py
185 lines (143 loc) · 5.42 KB
/
smart.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
import argparse
import asyncio
import functools
import io
import json
import re
import subprocess
import time
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from threading import Thread
import janus
import numpy as np
import tesserocr
import websockets
from skimage.color import rgb2gray
from skimage import filters
from skimage import util
from scipy import ndimage as ndi
from PIL import Image
WEBSOCKET_HOST = 'localhost'
WEBSOCKET_PORT = 8779
TESSDATA = '/usr/share/tesseract-ocr/tessdata'
def _normalize_whitespace(string):
return re.sub(r'(\s)\1{1,}', r'\1', string).strip()
def invert_button_colors(img):
""" Find the buttons, invert their colors """
# Thanks, Andras
options = util.invert(img)
label, num_features = ndi.label(options)
for feat in range(1, num_features + 1):
inds = np.where(label == feat)
if (0 in inds[0] or options.shape[0]-1 in inds[0]
or 0 in inds[1] or options.shape[1]-1 in inds[1]):
options[inds] = 0
return options
def optimize(img):
""" Convert to grayscale and apply the threshold """
img = rgb2gray(img)
return img >= filters.threshold_minimum(img)
def ocr(question, answers):
""" Perform the OCR """
start = time.perf_counter()
question = Image.fromarray((question * 255).astype(np.uint8))
answers = Image.fromarray((answers * 255).astype(np.uint8))
with ThreadPoolExecutor() as executor:
a = executor.submit(tesserocr.image_to_text, question,
lang='rus+eng', path=TESSDATA, psm=6)
b = executor.submit(tesserocr.image_to_text, answers,
lang='rus+eng', path=TESSDATA, psm=4)
question, answers = a.result(), b.result()
question = _normalize_whitespace(question.lower())
# The first line is noise
try:
_, question = question.split('\n', 1)
except ValueError:
pass
question = re.sub(r'\bне\b', '', question, flags=re.I)
question = question.translate(str.maketrans('«»\n', '"" '))
answers = _normalize_whitespace(answers.lower())
answers = answers.split('\n')
print('OCR completed in', time.perf_counter() - start)
print(f'Clean question: {question!r}')
print('Answers:', answers)
return question, answers
def frame_processor(queue, done):
prev_loaded = None
while True:
frame = queue.get()
frame = np.asarray(frame)
height, width, _ = frame.shape
# Once the bottom part of the third button is white, we know
# the answers (and the question) have finished loading
if np.any(frame[int(0.54 * height), width // 4:width // 4 * 3] != 255):
continue
# Excludes the viewer count and the countdown timer
question = optimize(frame[int(0.11 * height):int(0.32 * height)])
# Check similarity
# Each question should be processed once
if prev_loaded is None or np.sum(prev_loaded == question) / question.size <= 0.99:
prev_loaded = question
# Empty the queue
for _ in range(queue.qsize()):
try:
queue.get_nowait()
except janus.SyncQueueEmpty:
break
buttons = optimize(frame[int(0.32 * height):int(0.56 * height)])
answers = invert_button_colors(buttons)
result = ocr(question, answers)
done(result)
async def ws_handler(queues, websocket, path):
""" Handle WebSocket connections """
result_queue = janus.Queue()
queues.append(result_queue)
try:
while True:
question, answers = await result_queue.async_q.get()
# Generate search queries
queries = [question]
queries += [f'{question} {a}' for a in answers]
asyncio.ensure_future(websocket.send(json.dumps(queries)))
finally:
queues.remove(result_queue)
def notify_all(queues, result):
""" Send the result to all connected clients """
for x in queues:
x.sync_q.put_nowait(result)
def create_stream(queue):
""" Start the stream, extract JPEG frames, send them to the queue """
script = Path(__file__).with_name('stream.sh')
stream = subprocess.Popen(['sh', str(script)], stdout=subprocess.PIPE)
content = b''
frame_count = 0
last_frame = time.perf_counter()
while True:
chunk = stream.stdout.read(8_192)
content += chunk
soi = content.find(b'\xFF\xD8')
eoi = content.find(b'\xFF\xD9')
if soi != -1 and eoi != -1:
frame_count += 1
end = time.perf_counter()
print(f'[#{frame_count:>5}]', 'Since last frame:', end - last_frame)
last_frame = end
img = Image.open(io.BytesIO(content[soi:eoi+2]))
queue.put(img)
content = content[eoi+2:]
async def main():
frame_queue = janus.Queue(maxsize=100)
client_queues = []
# Wait for frames in another thread
on_done = functools.partial(notify_all, client_queues)
Thread(target=frame_processor, args=(frame_queue.sync_q, on_done)).start()
# Actually start the stream
Thread(target=create_stream, args=(frame_queue.sync_q,)).start()
# Start the WS server
ws = functools.partial(ws_handler, client_queues)
server = await websockets.serve(ws, WEBSOCKET_HOST, WEBSOCKET_PORT)
# Keep it running
await server.wait_closed()
if __name__ == '__main__':
asyncio.run(main())