-
Notifications
You must be signed in to change notification settings - Fork 0
/
greenProto.py
173 lines (125 loc) · 3.89 KB
/
greenProto.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
#!/usr/bin/env python
# Python 2/3 compatibility
from __future__ import print_function
import cv2
import numpy as np
from difflib import SequenceMatcher
import sys
import RPi.GPIO as GPIO
import time
import CG
import threading
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
def nothing(*arg):
pass
def limit(inputVal,limits):
output=inputVal
for i,val in enumerate(inputVal,0):
if val>limits[i][1]:
val=limits[i][1]
elif val<limits[i][0]:
val=limits[i][0]
output[i]=val
return output
try:
fn = sys.argv[1]
except:
fn = 0
cap = cv2.VideoCapture(fn)
cv2.namedWindow('image')
thrs=50
cv2.createTrackbar('Hue', 'image', 61, 179, nothing)
cv2.createTrackbar('Sat', 'image', 235, 255, nothing)
cv2.createTrackbar('Val', 'image', 255, 255, nothing)
cv2.createTrackbar('filterThresh', 'image', 0, 100, nothing)
#sets how much to blur
filt=39
exitNow=0
pause=0
while True:
try:
flag, imgInit = cap.read()
imgBGR = cv2.resize(imgInit,(100, 100),cv2.INTER_AREA)
img=cv2.cvtColor(imgBGR, cv2.COLOR_BGR2HSV)
while True:
if exitNow==1:
break
hue = cv2.getTrackbarPos('Hue', 'image')
sat = cv2.getTrackbarPos('Sat', 'image')
val = cv2.getTrackbarPos('Val', 'image')
lower=[hue,sat,val]
lower=np.array(lower, dtype="uint8")
lower2=[[[hue,sat,val]]]
lower2=np.array(lower2, dtype="uint8")
chosenColor = cv2.cvtColor(lower2, cv2.COLOR_HSV2BGR)##Tr
upperBound=limit(lower+thrs/2,[[0,179],[0,255],[0,255]])
lowerBound=limit(lower-thrs/2,[[0,179],[0,255],[0,255]])
mask=np.uint8(cv2.inRange(img,lowerBound,upperBound))
vis = np.uint8(img.copy())
vis[mask==0]=(0,0,0)
gray2 = img[:,:,2] #only want black and white image
gray = vis[:,:,2]
blurred = cv2.GaussianBlur(gray, (filt, filt), 0)
thresholdValue = cv2.getTrackbarPos('filterThresh', 'image')
thresh = cv2.threshold(blurred, thresholdValue, 255, cv2.THRESH_BINARY)[1]
testArray=[(lower-thrs/2).tolist(),(lower+thrs/2).tolist(),lowerBound.tolist(),upperBound.tolist(),thresholdValue]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
areas = 1
if cnts is not None:
areas=int(len(cnts))
CG.win()
splotch = np.zeros((1,areas),dtype=np.uint8)
if cnts is None:
CG.runGame()
# loop over the contours
#THIS IS THE BLOCK THAT DETECTS GREEN
try:
for i,c in enumerate(cnts,0):
print(cnts)
M = cv2.moments(c)
splotch[0][i] = int(M["m00"])
try:
max1=np.argmax(splotch)
except:
max1=-1
original=vis.copy()
if max1>-1:
M = cv2.moments(cnts[max1])
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
cv2.drawContours(vis, [cnts[max1]], -1, (0, 255, 0), 2)
cv2.circle(vis, (cX, cY), 7, (255, 255, 255), -1)
cv2.putText(vis, "Green Light", (cX - 20, cY - 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
except:
pass
cc=(int(chosenColor[0][0][0]),int(chosenColor[0][0][1]),int(chosenColor[0][0][2]))
# cv2.circle(imgBGR, (50, 50), 50, cc, -1)
visBGR=cv2.cvtColor(vis, cv2.COLOR_HSV2BGR)
thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
cv2.imshow('image',np.hstack([imgBGR,thresh, visBGR])) #np.hstack([original, vis]))#np.hstack([thresh, gray2]))
ch=cv2.waitKey(1)
# print(ch)
if ch == 27:
exitNow=True
break
elif ch==112 and pause==0:
pause=1
print("paused")
elif ch==112 and pause ==1:
pause=0
print("unPaused")
break
elif pause==1:
pass
else:
break
except KeyboardInterrupt:
raise
except cv2.error as e:
print("Here it is \n",str(e), "\n")
if similar(str(e), " /home/pi/opencv-3.3.0/modules/imgproc/src/imgwarp.cpp:3483: error: (-215) ssize.width > 0 && ssize.height > 0 in function resize")>.8:
print("\n\n\n\n Your video appears to have ended\n\n\n")
break
cv2.destroyAllWindows()