import cv2
# Load the pre-trained Haar cascade classifier for detecting faces
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Start the video capture from the default camera (index 0)
video_capture = cv2.VideoCapture(0)
while True:
# Read a frame from the video capture
ret, frame = video_capture.read()
# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# Iterate over the detected faces and draw rectangles around them
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Calculate the coordinates of the person
center_x = x + w / 2
center_y = y + h / 2
# Display the coordinates on the frame
cv2.putText(frame, f"({center_x:.2f}, {center_y:.2f})", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# Display the frame with the detected faces
cv2.imshow('Video', frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture and close the OpenCV windows
video_capture.release()
cv2.destroyAllWindows()