-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColor of object.py
49 lines (39 loc) · 1.17 KB
/
Color of object.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
#This was used to print color of object which it saw
import cv2
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
height, width, _ = frame.shape
cx = int(width / 2)
cy = int(height / 2)
pixel_center = hsv_frame[cy, cx]
hue_value = pixel_center[0]
color = "Undefined"
if hue_value ==0:
color="BLACK"
elif hue_value < 5:
color = "RED"
elif hue_value < 22:
color = "ORANGE"
elif hue_value < 33:
color = "YELLOW"
elif hue_value < 78:
color = "GREEN"
elif hue_value < 131:
color = "BLUE"
elif hue_value < 170:
color = "VIOLET"
else:
color = "WHITE"
pixel_center_bgr = frame[cy, cx]
b, g, r = int(pixel_center_bgr[0]), int(pixel_center_bgr[1]), int(pixel_center_bgr[2])
cv2.rectangle(frame, (cx - 220, 10), (cx + 200, 120), (255, 255, 255), -1)
cv2.putText(frame, color, (cx - 200, 100), 0, 3, (b, g, r), 5)
cv2.circle(frame, (cx, cy), 5, (25, 25, 25), 3)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1)
if key == ord('d'):
break
cap.release()
cv2.destroyAllWindows()