我正在进行的项目是面部识别。人脸识别位工作正常。然而,当识别出面部时,伺服机构打开门并在几秒钟后关闭。当摄像机前面没有人时,伺服器会一直打开门。打开和关闭门后5-10次停止。首先我认为睡眠将是解决方案,但这不起作用,我尝试没有for循环,但仍然,伺服保持打开。我认为OpenCV会在我站在镜头前的几秒钟内拍摄帧。有什么办法我只能在相机前有人(识别的脸)时打开门。我正在使用ip camera
由于
import cv2
import time
import RPi.GPIO as GPIO
import numpy as np
import threading
class main:
def servo(self):
print ("door open")
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
pwm=GPIO.PWM(11,50)
pwm.start(8)
time.sleep(5.20)
pwm.ChangeDutyCycle(3)
def face(self):
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainner/trainner.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
num = 1
while True:
ret, frame =cam.read()
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces=faceCascade.detectMultiScale(gray, 1.2,5)
for(x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(225,0,0),2)
Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
if(conf >= 50):
if(Id==1):
Id="Person 1"
elif(Id==2):
Id="Person 2"#
else:
Id="Unknown"
cv2.putText(frame,str(Id), (x, y+h), font, 4,(255,255,255),2,cv2.LINE_AA)
cv2.imshow('im',frame)
if cv2.waitKey(1) ==ord('q'):
break
cam.release()
cv2.destroyAllWindows()
app = main()
app.face()
答案 0 :(得分:0)
我认为你需要的是跟踪脸部直到它离开框架,当你第一次检测到你称之为功能的脸部时,你应该保持门打开直到脸部不再被跟踪(离开框架)