forked from jaflo/ProjectWinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contours.py
154 lines (137 loc) · 4.52 KB
/
contours.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
"""
Lots of code by glooga, although he did not commit
"""
import cv2
import time
import numpy as np
videoCapture = cv2.VideoCapture(0)
calibrating = True
counter = -1 #how many clicks before calibration ends
color = 0 # color from click
colors = []
trytime = [] #will contain the time to process a frame
currentstep = "Click the background"
class Finder:
x = 0
y = 0
w = 0
h = 0
color_min = np.array([110, 100, 100], np.uint8)
color_max = np.array([130, 255, 255], np.uint8)
def __init__(self, color_min, color_max):
self.color_min = color_min
self.color_max = color_max
self.previouswidths = []
self.previousheights = []
def update(self, frame, hsv_img, a = 0, b = 255, ccCccc = 0):
frame_threshed = cv2.inRange(hsv_img, self.color_min, self.color_max)
ret,thresh = cv2.threshold(frame_threshed, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
def recupdate(contours):
areas = [cv2.contourArea(c) for c in contours]
def showprev():
cv2.rectangle(frame, (self.x, self.y), (self.x + self.w, self.y + self.h), (a, b, ccCccc), 2)
cv2.rectangle(hsv_img, (self.x, self.y), (self.x + self.w, self.y + self.h), (a, b, ccCccc), 2)
if areas != []:
threshhold = 0.9
oflast = 30 #frames
max_index = np.argmax(areas)
cnt = contours[max_index]
nowx, nowy, noww, nowh = cv2.boundingRect(cnt)
self.previouswidths.append(noww)
self.previousheights.append(nowh)
if (threshhold<(float(noww)/float(np.median(self.previouswidths[-oflast:])))<(1/threshhold)) and (threshhold<(float(nowh)/float(np.median(self.previousheights[-oflast:])))<(1/threshhold)):
Height, Width, trash = frame.shape
self.x, self.y, self.w, self.h = cv2.boundingRect(cnt)
if (0.95 < self.w/self.h < 1/0.95) or len(contours) == 1:
cv2.rectangle(frame, (self.x, self.y), (self.x + self.w, self.y + self.h), (a, b, ccCccc), 2)
cv2.rectangle(hsv_img, (self.x, self.y), (self.x + self.w, self.y + self.h), (a, b, ccCccc), 2)
else:
contours = contours[:max_index] + contours[max_index + 1:]
recupdate(contours)
else:
showprev()
else:
showprev()
recupdate(contours)
def clicker(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
global color
global colors
color = hsv_img[y][x]
colors = colors + [color]
global counter
counter += 1
while True:
start = time.time()
ret, frame = videoCapture.read()
hsv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
def text(text):
x = 10
y = 20
padding = 1
global frame
size = cv2.getTextSize(text, cv2.FONT_HERSHEY_PLAIN, 1, 25)
cv2.rectangle(frame, (x-padding,y+padding),
(x+size[0][0]-15,y-size[0][1]/2-padding), 0, -99)
cv2.putText(frame, text, (10,20), cv2.FONT_HERSHEY_PLAIN, 1, [255,255,255])
if calibrating:
text(currentstep)
cv2.setMouseCallback("frame", clicker)
if counter/4 == 0:
if counter%4 == 3:
if colors != []:
currentstep = "Click the blue frame"
h = [a[0] for a in colors]
h.sort()
s = [a[1] for a in colors]
s.sort()
v = [a[2] for a in colors]
v.sort()
whiteMin = np.array([h[0]-10, s[0]-10, v[0]-10], np.uint8)
whiteMax = np.array([h[3]+10, s[3]+10, v[3]+10], np.uint8)
white = Finder(whiteMin, whiteMax)
colors = []
elif counter/4 == 1:
if counter%4 == 3:
if colors != []:
currentstep = "Click the green frame"
h = [a[0] for a in colors]
h.sort()
s = [a[1] for a in colors]
s.sort()
v = [a[2] for a in colors]
v.sort()
blueMin = np.array([h[0]-10, s[0]-10, v[0]-10], np.uint8)
blueMax = np.array([h[3]+10, s[3]+10, v[3]+10], np.uint8)
blue = Finder(blueMin, blueMax)
colors = []
elif counter/4 == 2:
if counter%4 == 3:
h = [a[0] for a in colors]
h.sort()
s = [a[1] for a in colors]
s.sort()
v = [a[2] for a in colors]
v.sort()
greenMin = np.array([h[0]-10, s[0]-10, v[0]-10], np.uint8)
greenMax = np.array([h[3]+10, s[3]+10, v[3]+10], np.uint8)
green = Finder(greenMin, greenMax)
calibrating = False
colors = []
else:
# Capture frame-by-frame
# and show the outlines
white.update(frame, hsv_img, 0, 0, 0)
blue.update(frame, hsv_img, 255, 0, 0)
green.update(frame, hsv_img, 0, 255, 0)
#print white.color_min
trytime.append(time.time()-start)
text("FPS: "+str(round(1/np.mean(trytime[-20:]),2)))
cv2.imshow('frame', frame)
#cv2.imshow('hsv_img', hsv_img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(1/10.)
video_capture.release()
cv2.destroyAllWindows()