This is a recreation of my old project that used raspberry pi 0 to detect faces through a small camera and directed servos to aim a laser at that face. This recreation is now made with raspberry pi 4
This is the hardware setup:
2 servos (Waveshare MG996R servo)
camera (Raspberry Pi Camera V2)
laser module (KY-008)
buzzer (FC-07)

And here is what it can do with the simple 2D_no_optimatization software: https://github.com/aizej/Face-targeting-laser-turret/assets/61479273/417483f3-e175-42ac-8c8f-4eb6831cfa0c
Now for the code:
First, I imported drivers for the servos and Opencv face recognition. I chose Opencv because it is one of the fastest, I also considered using retinaface but the preformance cost was too high.
Then we set some variables for our hardware such as camera resolution or pins for our servos.
After that, it is really important to get the angle of FOV of our camera and its cuttof(it only shows about 74.6% of horizontal image because of the set resolution)
Then there is our function that calculates to what angle each servo should move from where the face is on the image and what real-life offsets it has defined. This function takes in two numbers for x and y from 0 to 1 as. This represents where on the picture the face is [1,1] = bottom right [1,0] = up, right [0.5,0.5] = in the middle
Then we just setup loop where we take picture from the camera run it trough the face detection get middle of our detected face and pass it to the function. Next step is just to pass output from our function to the servos.




