-
Notifications
You must be signed in to change notification settings - Fork 0
/
Color_detection.py
154 lines (113 loc) · 5.37 KB
/
Color_detection.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
import cv2
import numpy as np
#capturing video through webcam
cap=cv2.VideoCapture(0)
while True:
ret, img = cap.read() #read the image through webcam
if not ret:
print("failed to grab the frame")
break
'''scale_percent = 50 # percent of original size
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
img= cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
cv2.imshow('original',img)'''
#converting frame(img i.e BGR) to HSV(Hue-Saturation-Value)
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('hsv format',hsv)
#defining the range of red Colour
red_lower=np.array([167,100,100],dtype=np.uint8)
red_upper=np.array([180,255,255],dtype=np.uint8)
#defining the range of the blue color
blue_lower=np.array([97,100,100],dtype=np.uint8)
blue_upper=np.array([103,255,255],dtype=np.uint8)
#defining the range of yellow color
yellow_lower=np.array([24,100,100],dtype=np.uint8)
yellow_upper=np.array([37,255,255],dtype=np.uint8)
#defining the range of green color
green_lower=np.array([42,100,100],dtype=np.uint8)
green_upper=np.array([75,255,255],dtype=np.uint8)
#defining the range of orange color
orange_lower=np.array([10,100,100],dtype=np.uint8)
orange_upper=np.array([16,250,250],dtype=np.uint8)
#defining the range of white color
white_lower=np.array([70,10,130],dtype=np.uint8)
white_upper=np.array([180,110,255],dtype=np.uint8)
#finding the range of red,blue,white,orange and yellow colour
red=cv2.inRange(hsv,red_lower,red_upper)
blue=cv2.inRange(hsv,blue_lower,blue_upper)
yellow=cv2.inRange(hsv,yellow_lower,yellow_upper)
green=cv2.inRange(hsv,green_lower,green_upper)
orange=cv2.inRange(hsv,orange_lower,orange_upper)
white=cv2.inRange(hsv,white_lower,white_upper)
#Morphological transfoorrmation ,Dilation
kernal = np.ones((5,5), "uint8")
red = cv2.dilate(red,kernal)
res = cv2.bitwise_and(img, img, mask = red)
blue = cv2.dilate(blue,kernal)
res1 = cv2.bitwise_and(img, img, mask = blue)
yellow = cv2.dilate(yellow,kernal)
res2 = cv2.bitwise_and(img, img, mask = yellow)
green = cv2.dilate(green,kernal)
res3 = cv2.bitwise_and(img, img, mask = green)
orange = cv2.dilate(orange,kernal)
res4 = cv2.bitwise_and(img, img, mask = orange)
white = cv2.dilate(white,kernal)
res5 = cv2.bitwise_and(img, img, mask = white)
#Tracking the Red Colour
(contours,hierarchy)=cv2.findContours(red,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area>300):
x,y,w,h = cv2.boundingRect(contour)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv2.putText(img,"RED",(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255))
#Tracking the Blue Colour
(contours,hierarchy)=cv2.findContours(blue,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area>300):
x,y,w,h = cv2.boundingRect(contour)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.putText(img,"BLUE",(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255))
#Tracking the Yellow Colour
(contours,hierarchy)=cv2.findContours(yellow,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area>300):
x,y,w,h = cv2.boundingRect(contour)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv2.putText(img,"Yellow",(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255))
#Tracking the Green Colour
(contours,hierarchy)=cv2.findContours(green,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area>300):
x,y,w,h = cv2.boundingRect(contour)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.putText(img,"Green",(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255))
#Tracking the orange Colour
(contours,hierarchy)=cv2.findContours(orange,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area>300):
x,y,w,h = cv2.boundingRect(contour)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.putText(img,"Orange ",(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255))
#Tracking the White Colour
(contours,hierarchy)=cv2.findContours(white,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area>300):
x,y,w,h = cv2.boundingRect(contour)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.putText(img,"White",(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255))
cv2.imshow("Color Tracking",img)
# Stop if escape key is pressed
k = cv2.waitKey(30) & 0xff
if k==27:
cap.release()
cv2.destroyAllWindows()
break