-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathledshield.py
More file actions
193 lines (167 loc) · 5.7 KB
/
ledshield.py
File metadata and controls
193 lines (167 loc) · 5.7 KB
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
#!/usr/bin/env python
import psutil, os, imutils
from artnetmatrix import (
ArtNetMatrix,
START_PIXEL_TOP,
START_PIXEL_RIGHT,
START_PIXEL_BOTTOM,
START_PIXEL_LEFT,
DIRECTION_COLUMNS,
LAYOUT_SNAKE,
COLOR_ORDER_RGB,
)
from effects import airdraw
OPTS = START_PIXEL_TOP | START_PIXEL_RIGHT | DIRECTION_COLUMNS | LAYOUT_SNAKE
import cv2
import numpy as np
from collections import deque
def blit(dest, src, loc):
pos = [i if i >= 0 else None for i in loc]
neg = [-i if i < 0 else None for i in loc]
target = dest[tuple([slice(i, None) for i in pos])]
src = src[tuple([slice(i, j) for i, j in zip(neg, target.shape)])]
target[tuple([slice(None, i) for i in src.shape])] = src
return dest
def main():
matrix = ArtNetMatrix((16, 40), OPTS, COLOR_ORDER_RGB)
matrix.fill((255, 0, 0))
# matrix.fill((255,255,255))
# matrix.fill((255,255,255))
for i in range(128, -1, -1):
matrix.fill((i, i, 255))
# img = cv2.cvtColor(matrix.surface.astype('uint8'), cv2.COLOR_BGR2RGB)
matrix.update()
img = matrix.surface
# print(img[0][0])
cv2.imshow("shield", cv2.resize(img, (320, 800)))
k = cv2.waitKey(1) & 0xFF
if k == 27:
cv2.destroyAllWindows()
return
# time.sleep(.1)
print("done")
while 1:
k = cv2.waitKey(30) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
def testopencv2():
print("initializing matrix")
SHIELD_WIDTH = 16
SHIELD_HEIGHT = 40
matrix = ArtNetMatrix((SHIELD_WIDTH, SHIELD_HEIGHT), OPTS, COLOR_ORDER_RGB)
print("initializing camera")
cap = cv2.VideoCapture(0)
camwidth = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
capheight = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fgbg = cv2.createBackgroundSubtractorMOG2(
history=12000, varThreshold=32, detectShadows=False
)
while 1:
ret, frame = cap.read()
# fgmask = fgbg.apply(firstFrame)
fgmask = fgbg.apply(frame)
outputFrame = cv2.bitwise_and(frame, frame, mask=fgmask)
outputFrame = cv2.resize(outputFrame, (16, 40), fx=0, fy=0)
# cv2.imshow('shield',cv2.resize(blit(matrix.surface, outputFrame, (0,0)),(320,800), interpolation=cv2.INTER_NEAREST))
# matrix.update()
# just the mask
# cv2.imshow('frame',fgmask)
# color image, bg removed via mask
# cv2.imshow('frame', outputFrame)
k = cv2.waitKey(30) & 0xFF
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
def testopencv3():
cap = cv2.VideoCapture(0)
while 1:
ret, frame = cap.read()
imgray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
# im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if imutils.is_cv2():
contours, _ = cv2.findContours(
thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
elif imutils.is_cv3():
_, contours, _ = cv2.findContours(
thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
img = frame # cv2.cvtColor(imgray, cv2.COLOR_BGR2GRAY)
img2 = cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
cv2.imshow("frame", img2)
k = cv2.waitKey(30) & 0xFF
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
def testopencv3():
cap = cv2.VideoCapture(0)
fgbg = cv2.createBackgroundSubtractorMOG2(
history=500, varThreshold=16, detectShadows=False
)
i = 0
ARC_THRESHOLD = 10
while 1:
i += 1
ret, frame = cap.read()
fgmask = fgbg.apply(frame)
contours, hierarchy = cv2.findContours(
fgmask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE
)
# defects = cv2.convexityDefects(contours,hull)
img = frame
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
# indexes of hullpoints w/ defects
# hullIndexes = [cv2.convexHull(contour, returnPoints=False) for contour in contours]
# print(hullIndexes)
hulls = [
cv2.convexHull(contour)
for contour in contours
if cv2.arcLength(contour, True) > ARC_THRESHOLD
]
# hulls = [cv2.convexHull(contour) for contour in contours]
cv2.drawContours(img, hulls, -1, (255, 0, 0), 3)
cv2.imshow("frame", cv2.flip(img, 1))
# cv2.imshow('frame',cv2.flip(fgmask, 1))
k = cv2.waitKey(30) & 0xFF
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
def testairdraw():
print("starting air draw effect")
outputSize = (16, 40)
matrix = ArtNetMatrix((16, 40), OPTS, COLOR_ORDER_RGB)
effect = airdraw.AirDraw()
effect.showCanvas = True
effect.showMask = False
effect.start()
tick_samples = deque()
output_samples = deque()
current_fps = 0
MAX_SAMPLES = 10
process = psutil.Process(os.getpid())
while effect.running:
effect.tick()
k = cv2.waitKey(10) & 0xFF
if k == 27:
effect.stop()
# cv2.imshow("Canvas", effect.canvas)
outputSurface = cv2.resize(effect.canvas, outputSize)
cv2.imshow("Matrix Scale", outputSurface)
blit(matrix.surface, cv2.cvtColor(outputSurface, cv2.COLOR_BGR2RGB), (0, 0))
matrix.update()
if not effect.tick_count % 100:
print("mem:", process.memory_info().rss / 1024 / 1024)
print(effect.timers.get("loop"))
# print("\r", end="")
if __name__ == "__main__":
print("starting air draw effect")
# main()
# testopencv()
# testopencv2()
# testopencv3()
testairdraw()