OpenCV Web Cam视频捕获问题

时间:2017-12-20 23:27:34

标签: python python-3.x opencv opencv3.0

我也是OpenCV和Python的新手。 我的代码中出现了一些错误,我已经尽力了但是......我希望有人能帮助我。我希望当我运行代码时它应该每秒捕获10-15帧...下面是我得到的错误...任何人都可以帮我改变 代码...

import cv2
import time
import sys
import numpy as np

if __name__ == '__main__':
    faceCascade = cv2.CascadeClassifier('C:\\Users\\Mudit\\Desktop\\Thesis\\CNN-master\\haarcascade_frontalface_default')
    faceNeighborsMax = 10
    neighborStep = 1

    # Start default camera
    video = cv2.VideoCapture(0);

    # Number of frames to capture
    num_frames = 10;
    print("Capturing {0} frames".format(num_frames))

    # Start time
    start = time.time()

    # Grab a few frames
    for i in range(0, num_frames):
        ret, frame = video.read()

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

    for neigh in range(1, faceNeighborsMax, neighborStep):
        faces = faceCascade.detectMultiScale(frameGray, 1.2, neigh)
        frameClone = np.copy(frame)

        # Display the image
        for (x, y, w, h) in faces:
            cv2.rectangle(frameClone, (x, y), (x + w, y + h), (255, 0, 0), 2)

        cv2.putText(frameClone, "# Neighbors = {}".format(neigh), (10, 50),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 4)
        cv2.imshow('Face Detection', frameClone)
        if cv2.waitKey(500) & 0xFF == 27:
            cv2.destroyAllWindows()
            sys.exit()

    # End time
    end = time.time()

    # Time elapsed
    seconds = end - start
    print("Time taken : {0} seconds".format(seconds))

    # Calculate frames per second
    fps = num_frames / seconds;
    print("Estimated frames per second : {0}".format(fps))

    # Release video
    video.release()

错误:

Traceback (most recent call last):   File "C:/Users/Mudit/PycharmProjects/CNN/findframerateofacamera/webcam.py", line 28, in <module>
    faces = faceCascade.detectMultiScale(frameGray, 1.2, neigh) cv2.error: C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale

1 个答案:

答案 0 :(得分:0)

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

不会导致断言,我认为图像捕获工作正常。你可以添加

cv2.imshow('Gray', frameGray)

验证灰度图像是否为空

下一个嫌疑人是级联分类器faceCascade。检查路径和文件名是否正确并且是否已正确加载。