【发布时间】:2018-06-17 21:32:42
【问题描述】:
在 Ubuntu 16.04 中,我尝试在实时视频中检测人脸并使用 OpenCV 和 Python 保存该图像。具体来说,我想为每个检测到的人脸只保存一张图像,直到我按下“q”。因此,对于检测到的每一张不同的面孔,都会拍摄另一张照片。在以下代码中,脚本每秒拍摄一张照片,直到我退出。
import cv2
# Import the cascade for face detection
face_cascade = cv2.CascadeClassifier('data/haarcascades/haarcascade_frontalface_default.xml')
# Access the webcam (every webcam has a number, the default is 0)
video = cv2.VideoCapture(0)
num = 0
while True:
# Capture frame-by-frame
ret, frame = video.read()
# Detect faces in video
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
# Draw rectangles around faces
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
# Display the image
cv2.imshow('Video', frame)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imwrite('opencv'+str(num)+'.jpg',frame)
num = num+1
# Press q for exit
if cv2.waitKey(1) & 0xFF == ord('q'):
# Write frame in file
break
video.release()
cv2.destroyAllWindows()
有什么建议吗?
【问题讨论】:
-
你说
So for each different face that is detected, another picture of it, is taken.这是什么意思?每个人脸一张人脸图像,还是一张带有人脸标签的整张图像?而cv2.waitKey(1)不会等待1 second。 -
@Silencer 每一张脸检测到一张完整的图像,没有剪切,如果同一张脸再次出现也没关系。所以,这个人出现了,拍一张快照。在此人从视频中消失之前不要再拍照,但前提是另一个人与此人一起出现或当然单独出现。我不确定这是否足够精确?我应该描述更多吗?
标签: python opencv ubuntu face-detection haar-classifier