【问题标题】:Saving image from live feed of detected faces从检测到的人脸的实时馈送中保存图像
【发布时间】:2017-03-19 22:23:35
【问题描述】:
import cv2
import sys

faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

video_capture = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=2.0,
        minNeighbors=5,A        minSize=(30, 30),
        flags = 0
        #flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    # Display the resulting frame
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        # Write frame in file
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)        
            cv2.imwrite('only_face.jpg')
        break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

上面的代码是从相机的实时馈送中检测人脸并只保存一张人脸的图像。我想保存从提要中检测到的多个面孔的图像。它只保存一张脸的图像

【问题讨论】:

  • 您应该包含一个 for 循环以在每次保存新图像时增加图像
  • TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S3') dtype('S3') dtype('S3') 它显示了这个错误

标签: python opencv face-detection


【解决方案1】:

查看我在此处包含的内容:

import cv2
import sys

faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

video_capture = cv2.VideoCapture(0)
increment = 0  #---Declare a variable that will increment for every image saved
while True:
# Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(gray, scaleFactor=2.0, minNeighbors=5,A        minSize=(30, 30), flags = 0
    #flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

# Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Display the resulting frame
cv2.imshow('Video', frame)
    increment = increment + 1  #--- Initiate the increment variable
    if cv2.waitKey(1) & 0xFF == ord('q'):
    # Write frame in file
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)        
            cv2.imwrite('only_face' + str(increment) + '.jpg')  #--- Here is where the increment variable is placed. It will be incremented for every face and thus saving every face that gets detected.
        break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

我已经包含了increment 变量,它会随着检测到的每一张脸而递增,从而分别保存每张图片。

【讨论】:

    猜你喜欢
    • 2018-05-30
    • 2021-01-22
    • 2012-03-08
    • 2013-04-04
    • 2019-12-01
    • 2022-11-24
    • 2013-04-06
    • 2016-06-27
    • 2017-12-22
    相关资源
    最近更新 更多