import cv2 import serial import time ml = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") webcam = cv2.VideoCapture(0) #serial setup ser = serial.Serial('COM4', 9600) time.sleep(2) crosshair_x, crosshair_y = None, None while True: succes, frame = webcam.read() if not succes: print("Goruntu Basarisiz") break frame_height, frame_width = frame.shape[:2] crosshair_x = frame_width // 2 crosshair_y = frame_height // 2 gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = ml.detectMultiScale(gray_img) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 3) face_center_x = x + w // 2 face_center_y = y + h // 2 cv2.circle(frame, (face_center_x, face_center_y), 5, (0, 0, 255), -1) faceY = f"{face_center_y}\n" crossY = f"{crosshair_y}\n" crossX = f"{crosshair_x}\n" faceX = f"{face_center_x}\n" ser.write(faceY.encode('utf-8')) ser.write(faceX.encode('utf-8')) ser.write(crossY.encode('utf-8')) ser.write(crossX.encode('utf-8')) print(faceY.encode('utf-8')) print(crossY.encode('utf-8')) crosshair_size = 10 # Size of the crosshair cv2.line(frame, (crosshair_x - crosshair_size, crosshair_y), (crosshair_x + crosshair_size, crosshair_y), (0, 255, 0), 2) # Horizontal line (Red) cv2.line(frame, (crosshair_x, crosshair_y - crosshair_size), (crosshair_x, crosshair_y + crosshair_size), (0, 255, 0), 2) # Vertical line (Red) cv2.imshow("video", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break8 webcam.release() cv2.destroyAllWindows()