-
Notifications
You must be signed in to change notification settings - Fork 49
/
color-5.py
executable file
·127 lines (109 loc) · 3.33 KB
/
color-5.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
#!/usr/bin/env python
"""color-5.py: Similar to color-4 but inside a class."""
"""
Performance @ 640x480 resolution:
RMBP -> 0.005s each detection or 20hz
RPI 2 -> 0.15s each detection or 6.66hz
RPI 3 -> 0.12s each detection or 8.33hz
"""
__author__ = "Aldo Vargas"
__copyright__ = "Copyright 2015 Altax.net"
__license__ = "GPL"
__version__ = "1"
__maintainer__ = "Aldo Vargas"
__email__ = "[email protected]"
__status__ = "Development"
import numpy as np
import cv2
import time, threading
class vision:
"""
1st argumet:
Colors:
- red
- blue
- green
- white
2nd argument:
- True -> if you want to see camera output
- False -> if you dont want to see camera output
"""
def __init__(self, targetcolor, show):
#self.cam = cv2.VideoCapture(0)
self.cam = cv2.VideoCapture('roomba.mp4')
self.position = {'color':targetcolor,'found':False,'x':0,'y':0,'rate':0.0,'serX':0.0,'serY':0.0}
self.resX=640
self.resY=480
self.cam.set(3,self.resX)
self.cam.set(4,self.resY)
self.targetcolor = targetcolor
self.show = show
def findcolor(self):
while True:
t1 = time.time()
ret, frame = self.cam.read()
hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
if self.targetcolor is 'red':
color = cv2.inRange(hsv,np.array([0,150,0]),np.array([5,255,255]))
elif self.targetcolor is 'blue':
color=cv2.inRange(hsv,np.array([100,50,50]),np.array([140,255,255]))
elif self.targetcolor is 'green':
color=cv2.inRange(hsv,np.array([40,50,50]),np.array([80,255,255]))
else: # white is default
sensitivity = 10
color = cv2.inRange(hsv,np.array([0,0,255-sensitivity]),np.array([255,sensitivity,255]))
image_mask=color
element = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
image_mask = cv2.erode(image_mask,element, iterations=2)
image_mask = cv2.dilate(image_mask,element,iterations=2)
image_mask = cv2.erode(image_mask,element)
contours, hierarchy = cv2.findContours(image_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
maximumArea = 0
bestContour = None
for contour in contours:
currentArea = cv2.contourArea(contour)
if currentArea > maximumArea:
bestContour = contour
maximumArea = currentArea
#Create a bounding box around the biggest color object
if bestContour is not None:
x,y,w,h = cv2.boundingRect(bestContour)
cv2.rectangle(frame, (x,y),(x+w,y+h), (0,0,255), 3)
t2 = time.time()
self.position['found']=True
self.position['x']=x+(w/2)
self.position['y']=y+(h/2)
self.position['serX'] = (self.position['x']-(self.resX/2.0))*(50.0/(self.resX/2))
self.position['serY'] = (self.position['y']-(self.resY/2.0))*(50.0/(self.resX/2))
self.position['rate']=round(1.0/(t2-t1),1)
print self.position
else:
self.position['found']=False
self.position['serX']=0.0
self.position['serY']=0.0
print self.position
if self.show:
cv2.imshow( 'vision' ,frame)
if cv2.waitKey(1) == 27:
break
test = vision('white',True)
def aldo():
global test
#test = vision('white',True)
test.findcolor()
try:
#test = vision('red',True)
#test.findcolor()
aldo()
#testThread = threading.Thread(target=test.findcolor)
#testThread.daemon=True
#testThread.start()
#testThread.join()
while True:
print test.position
if cv2.waitKey(1) == 27:
break
time.sleep(0.1)
pass
except Exception,error:
print "Error in main: "+str(error)