[英]How can I use Haar cascade for face detection and kalman filter for tracking?
我是计算机视觉的新手,我正在尝试使用 haar 级联进行人脸检测和卡尔曼滤波进行跟踪我打算在 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,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()
在 deepface 中运行人脸检测非常容易。 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', 'ssd', 'dlib', '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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.