-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_mockup_opencv.py
78 lines (69 loc) · 2.59 KB
/
led_mockup_opencv.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
from time import sleep
import cv2
import numpy as np
import collections
from threading import Thread
w = 72
h = 16
scale = 5
class LedMatrixMockup:
def __init__(self):
self.img = np.zeros((h, w), np.uint8)
self.array0 = collections.deque(np.zeros(72), maxlen=72)
self.array1 = collections.deque(np.zeros(72), maxlen=72)
self.active_array = 0
self.escape = False
self.imS = np.zeros((h * scale, w * scale), np.uint8)
self.stopping = False
new_thread = Thread(target=self.windowLoop, )
new_thread.start()
def windowLoop(self):
while not self.stopping:
if cv2.waitKey(1) == ord('q'):
break
cv2.imshow('output', self.imS) # Show image
# press q to terminate the loop
cv2.destroyAllWindows()
raise KeyboardInterrupt
def write(self, data):
for i in data:
if i == ord('.'):
if self.escape:
self.escape = False
else:
self.escape = True
continue
elif self.escape:
if i == ord('s'):
if self.active_array == 0:
img = np.unpackbits(np.array([self.array0], dtype=np.uint8), axis=0, bitorder='little')
# replace first 8 rows with the new img:
self.img[0:8, :] = img * 255
else:
img = np.unpackbits(np.array([self.array1], dtype=np.uint8), axis=0, bitorder='little')
# replace last 8 rows with the new img:
self.img[8:16, :] = img * 255
im_s = cv2.resize(self.img, (w * scale, h * scale), interpolation=cv2.INTER_AREA) # Resize image
im_s = np.fliplr(im_s)
self.imS = np.pad(im_s, pad_width=10, mode='constant', constant_values=100)
self.escape = False
continue
elif i == ord('0'):
self.active_array = 0
self.escape = False
continue
elif i == ord('1'):
self.active_array = 1
self.escape = False
continue
else:
self.escape = False
continue
else:
if self.active_array == 0:
self.array0.append(i)
else:
self.array1.append(i)
def stop(self):
self.stopping = True
pass