【问题标题】:Webcam is not even opening OpenCV is just printing the output网络摄像头甚至没有打开 OpenCV 只是打印输出
【发布时间】:2019-06-14 22:07:59
【问题描述】:

我按照一些教程创建了一个 OpenCV 函数,该函数从我的网络摄像头捕获 100 帧并将其存储在我提到的路径中,但是当我尝试检查网络摄像头是否与 OpenCV 正确集成时 我已经运行了这段代码

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

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

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

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

这一切都很好,我可以在灰色的框架中看到自己

import cv2
import numpy as np

# Load HAAR face classifier
face_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml')

# Load functions
def face_extractor(img):
    # Function detects faces and returns the cropped face
    # If no face detected, it returns the input image

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces = face_classifier.detectMultiScale(gray, 1.3, 3)

    if faces is ():
        return None

    # Crop all faces found
    for (x,y,w,h) in faces:
        cropped_face = img[y:y+h, x:x+w]

    return cropped_face

# Initialize Webcam
cap = cv2.VideoCapture(0)
# ret, frame = cap.read()
count = 0

# Collect 100 samples of your face from webcam input
while(True):

    ret, frame = cap.read()
    print(type(frame))
    print(frame.shape)
#     if ret==True:
    if face_extractor(frame) is not None:
        count += 1
        face = cv2.resize(face_extractor(frame), (200, 200))
        face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)

        # Save file in specified directory with unique name
        file_name_path = r'C:\Users\madhumani\path\\' + str(count) + '.jpg'
        cv2.imwrite(file_name_path, face)

        # Put count on images and display live count
        cv2.putText(face, str(count), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
        cv2.imshow('Face Cropper', face)

    else:
        print("Face not found")

#     else:
#         print("no camera")
    if cv2.waitKey(1) == 13 or count == 100: #13 is the Enter Key
        break

cap.release()
cv2.destroyAllWindows()      
print("Collecting Samples Complete")

根据该教程,网络摄像头应捕获该特定人的 100 帧并将其存储在路径中,但我只是将 face not found 打印为输出

这是输出:

<type 'numpy.ndarray'>
(480L, 640L, 3L)
Face not found
<type 'numpy.ndarray'>
(480L, 640L, 3L)
Face not found
<type 'numpy.ndarray'>
(480L, 640L, 3L)
Face not found
<type 'numpy.ndarray'>
(480L, 640L, 3L)
Face not found
<type 'numpy.ndarray'>
(480L, 640L, 3L)
Face not found

【问题讨论】:

  • 我运行了你的代码,它可以正常工作,没有错误。不知何故,方法face_extractor 在您的情况下返回None。你能分享print(len(faces))if faces is () 上方调用的结果吗?如果为 0,则分类器有问题。
  • @Bhoke 0 是输出
  • 那么分类器有什么问题??
  • 我怀疑您的 xml 文件可能已损坏。您也可以尝试将行 cv2.imshow('window',gray)cv2.waitKey() 添加到上面的同一位置,以查看如何获取帧。

标签: python opencv


【解决方案1】:

您的代码正在运行,并将文件写入指定路径。我认为您唯一缺少的是给定路径 C:\Users\madhumani\path\ 不存在,您应该先创建目录。

您可以通过添加以下代码来创建目录:

import os
dir = r'C:\Users\madhumani\path\\'
if not os.path.exists(dir):
    os.makedirs(dir)

或手动创建。

【讨论】:

  • 不,你误会我了,问题不在于路径,但是当我运行代码时,代码没有打开网络摄像头并开始捕捉我
  • 哈哈,我只读到“但我只是在变脸”。我会在解决问题时编辑答案。
猜你喜欢
  • 2017-02-09
  • 2017-09-25
  • 2012-12-20
  • 1970-01-01
  • 2017-01-29
  • 2020-09-10
  • 2019-06-20
  • 1970-01-01
相关资源
最近更新 更多