У меня есть камера с поворотным наклоном, которую я хочу использовать, чтобы следить за любым человеческим лицом, которое она видит. Он подключен к Raspberry Pi 4 4B с 4 ГБ оперативной памяти и разогнан до 2 ГГц.
Я написал скрипт на python, и пока он работает, но слишком медленно, чтобы его можно было использовать. Я получаю около 1 кадра в секунду. Я хотел бы реализовать многопроцессорность, чтобы увеличить скорость обнаружения, но я никогда не делал этого раньше. Я посмотрел вокруг, но я не уверен, с чего начать. Вот код, который у меня есть:
#GPIO dependencies
import RPi.GPIO as GPIO
from time import sleep
import os
import pigpio
#Camera dependencies
import face_recognition
import picamera
import numpy as np
# Using BCM numbers
GPIO.setmode(GPIO.BCM)
#GPIO number allocation
servo_horizontal = 19
servo_vertical = 12
#we setup the pins
pi = pigpio.pi()
#we create variables to store servo position
horizontal_position = 1500
vertical_position = 1750
#We position the servos in neutral position.
pi.set_servo_pulsewidth(19, 1500)
pi.set_servo_pulsewidth(12, 1750)
sleep(0.2)
pi.set_servo_pulsewidth(19, 0)
pi.set_servo_pulsewidth(12, 0)
#We initialize the camera
camera = picamera.PiCamera()
camera.resolution = (320, 240)
output = np.empty((240, 320, 3), dtype=np.uint8)
# Initialize some variables
face_locations = []
face_encodings = []
# main loop
while True:
print("Capturing image.")
# Grab a single frame of video from the RPi camera as a numpy array
camera.capture(output, format="rgb")
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(output)
#print("Found {} faces in image.".format(len(face_locations)))
if len(face_locations) == 1:
print("Only one face detected. Switching to camera pilot mode.")
camera_detection = 1
motion_detection = 0
face_detected_since = 0
face_location = face_locations[0]
face_top = face_location[0]
face_right = face_location[1]
face_bottom = face_location[2]
face_left = face_location[3]
print("Top: ",face_top," - Right: ",face_right," - Bottom: ",face_bottom," - Left: ",face_left,)
horizontal_image = (face_right + face_left)/2
vertical_image = (face_top + face_bottom)/2
print("Horizontal position: ",horizontal_position," Vertical position: ",vertical_position)
horizontal_offset = 160 - horizontal_image
vertical_offset = 120 - vertical_image
if horizontal_offset > 30 and horizontal_position > 500 and horizontal_position < 2500:
horizontal_position = horizontal_position + 100
print("Moving to position ",horizontal_position)
pi.set_servo_pulsewidth(servo_horizontal, horizontal_position)
if horizontal_offset < -30 and horizontal_position > 500 and horizontal_position < 2550:
horizontal_position = horizontal_position - 100
print("Moving to position ",horizontal_position)
pi.set_servo_pulsewidth(servo_horizontal, horizontal_position)
if vertical_offset > 30 and vertical_position > 1000 and vertical_position <= 2000:
vertical_position = vertical_position + 50
print("Moving to position ",vertical_position)
pi.set_servo_pulsewidth(servo_vertical, vertical_position)
if vertical_offset < -30 and vertical_position > 1000 and vertical_position <= 2050:
vertical_position = vertical_position - 50
print("Moving to position ",vertical_position)
pi.set_servo_pulsewidth(servo_vertical, vertical_position)