-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultipleColor_Detction.py
More file actions
151 lines (120 loc) · 5.32 KB
/
Copy pathmultipleColor_Detction.py
File metadata and controls
151 lines (120 loc) · 5.32 KB
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
import cv2
import numpy as np
from PIL import Image
# Define the HSV color ranges for each color
def get_limits(color):
if color == [0, 255, 255]: # yellow in BGR
lower = np.array([20, 100, 100], dtype=np.uint8)
upper = np.array([30, 255, 255], dtype=np.uint8)
elif color == [255, 0, 0]: # blue in BGR
lower = np.array([100, 150, 0], dtype=np.uint8)
upper = np.array([140, 255, 255], dtype=np.uint8)
elif color == [0, 255, 0]: # green in BGR
lower = np.array([40, 40, 40], dtype=np.uint8)
upper = np.array([70, 255, 255], dtype=np.uint8)
else:
lower = upper = None
return lower, upper
# Define colors in BGR color space
colors = {
'yellow': [0, 255, 255],
'blue': [255, 0, 0],
'green': [0, 255, 0]
}
# Open a connection to the default camera (index 0)
cap = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame from the camera
ret, frame = cap.read()
# Check if frame was captured successfully
if not ret:
print("Error: Could not read frame.")
break
# Convert the captured frame from BGR to HSV color space
hsvImage = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Iterate over defined colors
for color_name, bgr_value in colors.items():
# Get the lower and upper HSV limits for the current color
lowerLimit, upperLimit = get_limits(color=bgr_value)
# Create a mask for the current color
mask = cv2.inRange(hsvImage, lowerLimit, upperLimit)
# Convert the mask to a PIL Image object to use getbbox()
mask_ = Image.fromarray(mask)
# Get the bounding box coordinates of the non-zero regions in the mask
bbox = mask_.getbbox()
# If a bounding box is found, draw a rectangle on the original frame
if bbox is not None:
x1, y1, x2, y2 = bbox
rect_color = tuple(bgr_value) # Set the rectangle color to match the detected color
frame = cv2.rectangle(frame, (x1, y1), (x2, y2), rect_color, 5)
# Add the color name label near the top-left corner of the rectangle
cv2.putText(frame, color_name, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, rect_color, 2)
# Display the frame with the rectangles and labels (if drawn)
cv2.imshow('frame', frame)
# Break the loop if the 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the camera and close all OpenCV windows
cap.release()
cv2.destroyAllWindows()
# import cv2
# import numpy as np
# from PIL import Image
# # Define the HSV color ranges for each color
# def get_limits(color):
# if color == [0, 255, 255]: # yellow in BGR
# lower = np.array([25, 150, 150], dtype=np.uint8)
# upper = np.array([35, 255, 255], dtype=np.uint8)
# elif color == [255, 0, 0]: # blue in BGR
# lower = np.array([90, 100, 100], dtype=np.uint8)
# upper = np.array([130, 255, 255], dtype=np.uint8)
# elif color == [0, 255, 0]: # green in BGR
# lower = np.array([50, 100, 100], dtype=np.uint8)
# upper = np.array([80, 255, 255], dtype=np.uint8)
# else:
# lower = upper = None
# return lower, upper
# # Define colors in BGR color space
# colors = {
# 'yellow': [0, 255, 255],
# 'blue': [255, 0, 0],
# 'green': [0, 255, 0]
# }
# # Open a connection to the default camera (index 0)
# cap = cv2.VideoCapture(0)
# while True:
# # Capture frame-by-frame from the camera
# ret, frame = cap.read()
# # Check if frame was captured successfully
# if not ret:
# print("Error: Could not read frame.")
# break
# # Convert the captured frame from BGR to HSV color space
# hsvImage = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# # Apply Gaussian blur to reduce noise
# hsvImage = cv2.GaussianBlur(hsvImage, (5, 5), 0)
# # Iterate over defined colors
# for color_name, bgr_value in colors.items():
# # Get the lower and upper HSV limits for the current color
# lowerLimit, upperLimit = get_limits(color=bgr_value)
# # Create a mask for the current color
# mask = cv2.inRange(hsvImage, lowerLimit, upperLimit)
# # Convert the mask to a PIL Image object to use getbbox()
# mask_ = Image.fromarray(mask)
# # Get the bounding box coordinates of the non-zero regions in the mask
# bbox = mask_.getbbox()
# # If a bounding box is found, draw a rectangle on the original frame
# if bbox is not None:
# x1, y1, x2, y2 = bbox
# rect_color = tuple(bgr_value) # Set the rectangle color to match the detected color
# frame = cv2.rectangle(frame, (x1, y1), (x2, y2), rect_color, 5)
# # Add the color name label near the top-left corner of the rectangle
# cv2.putText(frame, color_name, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, rect_color, 2)
# # Display the frame with the rectangles and labels (if drawn)
# cv2.imshow('frame', frame)
# # Break the loop if the 'q' key is pressed
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# # Release the camera and close all OpenCV windows
# cap.release()
# cv2.destroyAllWindows()