所以我有一個跟蹤相機的專案,通過檢測他們的臉來計算進入跟蹤的人。沒有任何電源可用,因此出于電源原因我只能使用 Raspberry Pi 3 B 。我目前正在使用 opencv 和 Haar 級聯來檢測人臉。我的問題有兩個。
- 第一個是我的計數器更像一個計時器。在它鎖定在它認為是一張臉的任何東西上的整個程序中,它都會繼續增加。我需要的行為是讓它在得到檢測時只增加一次,而不是在檢測丟失然后重新初始化之前再增加一次。如果檢測到多個面孔,我也需要它來作業。
- 第二個問題是 Haar cascades 不擅長檢測人臉。我一直在玩引數,但似乎無法獲得很好的結果。還嘗試了其他方法,例如 Dlib,但幀速率使其在 pi 3 上幾乎無法使用。
我將在下面發布我的代碼(通過結合一些示例拼湊在一起)。目前,它也被設定為使用執行緒(另一個試圖擠出更多性能的實驗)。據我所知,執行緒正在作業,但似乎并沒有真正改善任何事情。非常感謝你們為解決反問題或優化 Haar Cascades 以在 Pi 上使用提供的任何幫助。** 還應注意使用 Rasp Pi 高品質相機和 Ardu Cam 鏡頭。
from __future__ import print_function
from imutils.video import VideoStream
from imutils.video.pivideostream import PiVideoStream
from imutils.video import FPS
from picamera.array import PiRGBArray
from picamera import PiCamera
import argparse
import imutils
import time
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-c", "--cascade", type=str,
default="haarcascade_frontalface_default.xml",
help="path to haar cascade face detector")
args = vars(ap.parse_args())
detector = cv2.CascadeClassifier(args["cascade"])
size = 40
counter = 0
# created threaded video
print("[INFO] using threaded frames")
vs = PiVideoStream().start()
time.sleep(2.0)
# loop over some frames...this time using the threaded stream
while True:
# grab the frame from the threaded video stream and resize it
# to have a maximum width of 400 pixels (trying larger frame size)
frame = vs.read()
frame = imutils.resize(frame, width=450)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# perform face detection
rects = detector.detectMultiScale(gray, scaleFactor=1.05,
minNeighbors=6, minSize=(size, size),
flags=cv2.CASCADE_SCALE_IMAGE)
# loop over the bounding boxes
for (x, y, w, h) in rects:
# draw the face bounding box on the image
cv2.rectangle(frame, (x, y), (x w, y h), (0, 255, 0), 2)
# Increment counter when face is found
counter = 1
print(counter)
# show the output frame
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()
uj5u.com熱心網友回復:
首先,您可以按照您所說的那樣使用 Dlib,但您必須使用“HOG”方法(定向梯度直方圖)而不是“CNN”來提高性能。
locations = face_recognition.face_locations(frame, model="hog")
但是,如果您真的想獲得更快的性能,我會建議您為此目的使用 Mediapipe。
在您的 rpi3 上下載 Mediapipe:
sudo pip3 install mediapipe-rpi3
這是來自 Mediapipe 檔案的人臉檢測器示例代碼:
import cv2
import mediapipe as mp
mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils
# For webcam input:
cap = cv2.VideoCapture(0)
with mp_face_detection.FaceDetection(
model_selection=0, min_detection_confidence=0.5) as face_detection:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
continue
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = face_detection.process(image)
# Draw the face detection annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.detections:
for detection in results.detections:
mp_drawing.draw_detection(image, detection)
# Flip the image horizontally for a selfie-view display.
cv2.imshow('MediaPipe Face Detection', cv2.flip(image, 1))
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
我不確定你會得到多少 FPS(但肯定比 Dlib 好,而且非常準確),但你可以通過每隔三幀而不是全部檢測人臉來加快性能。
其次,您可以采取一種可能會正常作業的天真的方式。您可以在最后一次檢測中提取邊界框的中心,如果前一幀的中心位于當前幀中人臉的邊界框內,則可能是同一個人。
您可以通過確定當前幀中面部的新中心(他的邊界框的中心)是否足夠接近最后一幀中的最后一個中心之一,通過您選擇的偏移量來更準確地做到這一點。如果是這樣,它可能是同一張臉,所以不要計算它。
可視化:

希望它對你有用!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/471832.html
上一篇:在面具上繪制輪廓
下一篇:如何檢測帶顏色的圓圈
