Tài Liệu Văn Bản Mới

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

import cv2

import serial
# Khởi tạo đối tượng VideoCapture và cài đặt kích thước khung hình
cap = cv2.VideoCapture(1) # Sử dụng camera mặc định (index 0)
Arduino_Serial = serial.Serial('COM4'9600)
Arduino_Serial.readline()

while True:
# Đọc khung hình từ camera
ret, frame = cap.read()

# frame = cv2.flip(frame, 1)
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
height, width, ret = frame. shape
cx= int(width / 2)
cy= int(height / 2)
# Chọn gia tri diem anh
pixel_center = hsv_frame [cy, cx]
hue_value = pixel_center[0]
if hue_value < 9:
color = "RED"
else if hue_value> 25 and hue_value < 45:
color = "YELLOW"
else if hue_value> 45 and hue_value < 95: color = "GREEN"
color = "GREEN"

input_data = color
if (input_data == 'RED'):
Arduino_Serial.write('RED'.encode())
if (input data == 'YELLOW'):
Arduino_Serial.write('YELLOW'.encode())
if (input_data == 'GREEN'):
Arduino_Serial.write('GREEN'.encode())

cv2.imshow("Frame", frame)
if cv2.waitKey(1) & 8xff== ord('q');
break
cap.release()
cv2.destroyAllWindows()

You might also like