Python Göz Rengi Algılama (Python Eye Color Detection)
Python ile göz rengi algılama
import cv2
# Load the pre-trained models for face and eye detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
# Set the video capture source (0 for webcam)
cap = cv2.VideoCapture(0)
while True:
# Read the video stream frame by frame
ret, frame = cap.read()
# Convert the frame to grayscale for face and eye detection
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
# Loop over each face found
for (x, y, w, h) in faces:
# Draw rectangle around the face
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Extract the region of interest (ROI) within the face rectangle
roi_gray = gray[y:y + h, x:x + w]
roi_color = frame[y:y + h, x:x + w]
# Detect eyes in the ROI
eyes = eye_cascade.detectMultiScale(roi_gray)
# Loop over each eye found
for (ex, ey, ew, eh) in eyes:
# Draw rectangle around the eyes
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
# Extract the region of interest (ROI) within the eye rectangle
eye_roi = roi_gray[ey:ey + eh, ex:ex + ew]
# Perform eye color detection based on the average pixel intensity
avg_pixel_intensity = cv2.mean(eye_roi)[0]
# Determine the eye color based on the average pixel intensity
if avg_pixel_intensity < 90: # Dark eye color
eye_color = "Dark"
elif avg_pixel_intensity < 120: # Brown eye color
eye_color = "Brown"
elif avg_pixel_intensity < 150: # Hazel eye color
eye_color = "Hazel"
elif avg_pixel_intensity < 180: # Green eye color
eye_color = "Green"
else: # Blue eye color
eye_color = "Blue"
# Display the detected eye color
cv2.putText(frame, eye_color, (x + ex, y + ey - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# Display the resulting frame with the detected eyes and eye color
cv2.imshow('Eye Color Detection', frame)
# Break the infinite loop when 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture object and close all windows
cap.release()
cv2.destroyAllWindows()
Bu Yazıya Tepkin Nedir?