Hi, I am trying to detect face and eyes using the haar inbuilt by OpenMV but i keep getting no face detected and also, the boundary regions defined are not shown. Am i doing anything wrong? Working with arduino Nicla vision board
import sensor, image, time, tf
Initialize camera sensor
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000) # Let the camera adjust
Load TensorFlow Lite model
net = tf.load(“model.tflite”)
Initialize face and eye detection
face_cascade = image.HaarCascade(“frontalface”, stages=25)
eye_cascade = image.HaarCascade(“eye”, stages=25)
while True:
img = sensor.snapshot()
# Perform face detection
faces = img.find_features(face_cascade, threshold=0.5, scale_factor=1.1)
# Check if any faces are detected
if faces:
print(f"Faces detected: {len(faces)}")
else:
print("No faces detected")
for face in faces:
# Draw rectangle around detected face
img.draw_rectangle(face)
# Perform eye detection within the detected face region
eyes = img.find_features(eye_cascade, roi=face, threshold=0.5, scale_factor=1.1)
# Check if any eyes are detected within the face region
if eyes:
print(f"Eyes detected: {len(eyes)} in face region")
else:
print("No eyes detected in face region")
for eye in eyes:
# Draw rectangle around detected eye
img.draw_rectangle(eye)
# Extract and preprocess the eye region for TensorFlow Lite model
eye_roi = img.copy(roi=eye).to_grayscale() # Convert to grayscale
eye_roi = eye_roi.resize(24, 24) # Resize to match model input size
eye_array = eye_roi.to_bytes()
# Perform inference using TensorFlow Lite model
output = net.classify(eye_array)[0].output() # Get output probabilities
# Print the raw output of the model for debugging
print("Model output:", output)
# Determine if eye is open or closed based on model output
prediction = output.index(max(output))
# Print the prediction label
label = "Closed" if prediction == 0 else "Open"
print("Eye prediction:", label)