تشخیص رنگ در پایتون
کدهای پروژه
import cv2
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1288)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 728)
color = “”
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)
#Pick pixel value
pixel_center = hsv_frame[cy , cx]
hue_value = pixel_center[0]
if 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 = “RED”
print(pixel_center)
cv2.putText(frame, color, (cx – 200, 100), 0, 3, (255,0,0), 5)
cv2.circle(frame,(cx,cy) , 5 , (255,0,0) , 3)
cv2.imshow(“frame” , frame)
# wait milliseconds until => press any button => return the ascii code
key = cv2.waitKey(1)
if key == 27:
break
# Closes video file or capturing device
cap.release()
cv2.destroyWindow()
دیدگاهتان را بنویسید