Python Cinsiyet Algılama (Python Gender Detection)
Python ile cinsiyet algılama
import cv2
import numpy as np
# Load pre-trained face detection model
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Load pre-trained gender classification model
gender_model = cv2.dnn.readNetFromCaffe("deploy_gender.prototxt", "gender_net.caffemodel")
# Load labels for gender classification
gender_labels = ['Male', 'Female']
# Initialize video capture
cap = cv2.VideoCapture(0)
while True:
# Read frame from video capture
_, frame = cap.read()
# Convert frame to gray scale for face detection
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the frame
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
# Extract face region of interest
roi = frame[y:y + h, x:x + w]
# Preprocess face for gender classification
blob = cv2.dnn.blobFromImage(roi, 1, (227, 227), (78.4263377603, 87.7689143744, 114.895847746), swapRB=False)
# Pass the face through gender classification model
gender_model.setInput(blob)
gender_preds = gender_model.forward()
# Get predicted gender label
gender_idx = np.argmax(gender_preds)
gender_label = gender_labels[gender_idx]
# Draw rectangle around the detected face
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Put predicted gender label and probability on the frame
label = f'{gender_label}: {gender_preds[0][gender_idx] * 100:.2f}%'
cv2.putText(frame, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
# Display the frame
cv2.imshow('Gender Detection', frame)
# Quit if ESC is pressed
if cv2.waitKey(1) == 27:
break
Bu Yazıya Tepkin Nedir?