-
Notifications
You must be signed in to change notification settings - Fork 0
/
yellowObjectDetector.py
73 lines (57 loc) · 2.31 KB
/
yellowObjectDetector.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
import cv2
import numpy as np
# Define target colors
targets = {
"Yellow": ([20, 100, 100], [30, 255, 255]), # Yellow color range in HSV
"Red": ([(0, 100, 100), (10, 255, 255)], [(160, 100, 100), (180, 255, 255)]), # Red color range in HSV
"Blue": ([100, 100, 100], [130, 255, 255]) # Blue color range in HSV
}
# Color names and corresponding BGR colors for labeling
color_labels = {
"Yellow": (0, 255, 255),
"Red": (0, 0, 255),
"Blue": (255, 0, 0)
}
# Function to detect and label colors
def detect_colors(frame):
# Convert the frame to HSV
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Detect and label colors
for color_name, (lower, upper) in targets.items():
if isinstance(lower[0], tuple): # Check if it's a range with two tuples
for (lower_range, upper_range) in zip(lower, upper):
# Create a mask for the current color range
mask = cv2.inRange(hsv_frame, np.array(lower_range), np.array(upper_range))
process_mask(mask, frame, color_name)
else:
mask = cv2.inRange(hsv_frame, np.array(lower), np.array(upper))
process_mask(mask, frame, color_name)
return frame
# Function to process the mask and add labels
def process_mask(mask, frame, color_name):
# Find contours in the mask
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if contours:
# Find the largest contour
max_contour = max(contours, key=cv2.contourArea)
# Get the bounding box of the largest contour
x, y, w, h = cv2.boundingRect(max_contour)
# Draw a rectangle and put text on the frame
cv2.rectangle(frame, (x, y), (x + w, y + h), color_labels[color_name], 2)
cv2.putText(frame, color_name, (x - 20, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color_labels[color_name], 2)
# Open the camera
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# Detect and label colors in the frame
result_frame = detect_colors(frame)
# Display the result
cv2.imshow("Color Detection", result_frame)
# Exit the loop on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the camera and close all OpenCV windows
cap.release()
cv2.destroyAllWindows()