-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorDetection.py
82 lines (63 loc) · 2.49 KB
/
ColorDetection.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
from utils import get_limits
import cv2
from threading import Thread
"""
Define the resolution of the frame bellow:
"""
USR_WIDTH = 1920
USR_HEIGTH = 1080
class VideoStream:
def __init__(self,src=0):
self.stream = cv2.VideoCapture(src)
self.ret, self.frame = self.stream.read()
self.thread.daemon = True
self.thread.start()
def update(self):
while(True):
self.ret, self.frame = self.stream.read()
def read(self):
return self.frame
colors = {
'red': [0, 0, 255],
'green': [0, 255, 0],
'blue': [255, 0, 0],
}
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
frame_heigt, frame_width = frame.shape[:2]
if frame is not None:
frame = cv2.resize(frame,(USR_WIDTH,USR_HEIGTH))
else:
print("Error while resizing the frame")
############ ROI DEFINITION ###################
roi_size = 200
roi_top = int(frame_heigt - roi_size / 2)
roi_bottom = int(frame_heigt + roi_size / 2)
roi_left = int(frame_width - roi_size / 2)
roi_rigth = int(frame_width + roi_size / 2)
##############################################
hsvImage = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
for color_name, color in colors.items():
lowerLimit, upperLimit = get_limits(color=color)
mask = cv2.inRange(hsvImage, lowerLimit, upperLimit)
# Operações Morfológicas para reduzir o ruído.
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15))
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.rectangle(frame,(roi_left,roi_top),(roi_rigth,roi_bottom),(0,233,255),2)
cv2.putText(frame, "Detection Area", (roi_left - 12,roi_top - 10), cv2.FONT_HERSHEY_SIMPLEX,1,(0,233,255),2)
for contour in contours:
if cv2.contourArea(contour) > 500:
x, y, w, h = cv2.boundingRect(contour)
center_x = x + w / 2
center_y = y + h / 2
if roi_left < center_x < roi_rigth and roi_top < center_y < roi_bottom:
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.putText(frame, color_name, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
cv2.imshow('frame', frame)
# Tecla para parar a execução do código
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()