如何使用Haar级联进行人脸检测并使用Kalman过滤器进行跟踪?

问题描述

我是计算机视觉的新手,我试图使用haar级联进行面部检测,并使用kalman滤波进行跟踪,我打算在raspBerry pi 3B上运行代码。因此,不能使用任何深度学习方法进行跟踪。 如何在我的代码中使用cv2.kalmanfilter()(https://docs.opencv.org/trunk/dd/d6a/classcv_1_1KalmanFilter.html)进行跟踪并为遍历路径画一条线? 如果有人可以指导我,那将是很大的帮助 我的代码是:

from __future__ import print_function
import numpy as np
import cv2
from imutils.video import WebcamVideoStream
from imutils.video import FPS
import argparse
import imutils
import cv2
 
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#faceCascade = cv2.CascadeClassifier('haarcascade_profileface.xml')
fps = FPS().start()
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height

while True:
    ret,img = cap.read()
    #img = cv2.flip(img,-1)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
        gray,scaleFactor=1.2,minNeighbors=5,minSize=(20,20)
    )

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0),2)
        roi_gray = gray[y:y+h,x:x+w]
        roi_color = img[y:y+h,x:x+w]  

    cv2.imshow('video',img)
    fps.update()
    k = cv2.waitKey(30) & 0xff
    if k == 27: # press 'ESC' to quit
        break
fps.stop()
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
cap.release()
cv2.destroyAllWindows()

解决方法

在深脸中运行人脸检测非常容易。 detectFace函数分别在背景中应用人脸检测和对齐。

#!pip install deepface
from deepface import DeepFace
backends = ['opencv','ssd','dlib','mtcnn']
detected_face = DeepFace.detectFace("img.jpg",detector_backend = backends[0])

您也可以手动运行检测和对齐。如果对齐不在您的范围内,则可以这种方式跳过此步骤。

from deepface.commons import functions
img = functions.load_image("img.jpg")
backends = ['opencv','mtcnn']

detected_face = functions.detect_face(img = img,detector_backend = backends[3])
plt.imshow(detected_face)

aligned_face = functions.align_face(img = img,detector_backend = backends[3])
plt.imshow(aligned_face)

processed_img = functions.detect_face(img = aligned_face,detector_backend = backends[3])
plt.imshow(processed_img)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...