I am working on Course project (intruder detection) which if he detect my face open the the door and write in the terminal Authorized face else write Intruder detected and close the door using servo motors. I took some snapshots of me and saved it in D drive . the problem is the when i run the code he considered my face as intruder and during recognition the camera turn the background and my image into dark with some white shadows , i know i should use grayscale especially when you use haar cascade function. Note: i used a code that take snapshots of me as try to train the system to recognize my face
import sensor, image, time
from pyb import Servo, Pin
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
clock = time.clock()
servo = Servo(1) # Use P7 pin (Servo 1)
buzzer = Pin("P8", Pin.OUT_PP) # Use P8 pin for buzzer
known_faces = []
for i in range(20):
try:
face = image.Image("faces/face_%d.jpg" % i)
known_faces.append(face)
print("Loaded:", "faces/face_%d.jpg" % i)
except OSError:
print("Face file not found:", i)
break
background = sensor.snapshot().copy()
def is_known_face(test_img):
for face in known_faces:
match = test_img.find_lbp(face)
if match > 0:
return True
return False
def door_open():
servo.angle(90)
print("Door Opened")
def door_close():
servo.angle(0)
buzzer.high()
print("Door Closed")
time.sleep(1)
buzzer.low()
while True:
clock.tick()
img = sensor.snapshot()
diff = img.difference(background)
blobs = diff.find_blobs(thresholds=[(20, 255)], pixels_threshold=200, area_threshold=200)
if blobs:
print("Movement Detected")
face_cascade = image.HaarCascade("frontalface", stages=25)
faces = img.find_features(face_cascade, threshold=0.5, scale_factor=1.5)
if faces:
for face_rect in faces:
face_img = img.copy(roi=face_rect)
img.draw_rectangle(face_rect)
if is_known_face(face_img):
print("Authorized Face Detected")
door_open()
else:
print("Intruder Face Detected")
door_close()
else:
print("No face detected")
door_close()
time.sleep(1) # Delay for stability
this my intruder detection code
import sensor
import image
import random
import machine
import os
import time
try:
os.mkdir("/faces")
except OSError:
pass
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
led = machine.LED("LED_RED")
face_cascade = image.HaarCascade("frontalface", stages=25)
while True:
print("About to start detecting faces...")
sensor.skip_frames(time=2000)
print("Now detecting faces!")
diff = 10
while diff:
img = sensor.snapshot()
faces = img.find_features(face_cascade, threshold=0.5, scale_factor=1.5)
if faces:
diff -= 1
for r in faces:
img.draw_rectangle(r)
led.on()
print("Face detected! Saving image...")
filename = "/faces/face_%d.jpg" % random.getrandbits(16)
img = sensor.snapshot()
img.save(filename)
print("Saved image:", filename)
led.off()
time.sleep(1)
and this is the snapshots code
and this the error when i tried to fix it

