首页 > 解决方案 > 网络摄像头 openCV 错误:(-215) ssize.width > 0 && ssize.height > 0 in function resize

问题描述

我已经看到有关此完全相同错误的其他堆栈交换帖子,但是我的脚本不是从图像或图像列表中读取,而是从网络摄像头中读取。顺便说一句,这个脚本被复制了,我试图让它作为一个例子让我了解它是如何工作的。

import numpy as np
import cv2

# set up HOG person detector and create hog object
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

cv2.startWindowThread()

# set up video capture, make cap video stream object
cap = cv2.VideoCapture(0)

# write file output to output.avi in 640x480 size
out = cv2.VideoWriter(
    'output.avi',
    cv2.VideoWriter_fourcc(*'MJPG'),
    15.,
    (640,480))
if cap is not None:
    while(True):
        # read the webcam
        ret, frame = cap.read()

        # resizing for faster detection
        frame = cv2.resize(frame, (640, 480))
        # using a greyscale picture, also for faster detection
        gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)

        # detect people in the image
        # returns the bounding boxes for the detected objects
        boxes, weights = hog.detectMultiScale(frame, winStride=(8,8) )

        boxes = np.array([[x, y, x + w, y + h] for (x, y, w, h) in boxes])

        for (xA, yA, xB, yB) in boxes:
            # display the detected boxes in the colour picture
            cv2.rectangle(frame, (xA, yA), (xB, yB),
            (0, 255, 0), 2)

        # Write the output video 
        out.write(frame.astype('uint8'))
        # Display the resulting frame
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # When everything done, release the capture
    cap.release()
    # and release the output
    out.release()
    # finally, close the window
    cv2.destroyAllWindows()
    cv2.waitKey(1)

我知道这个错误是因为 resize 函数没有图像来调整大小,所以我添加了if cap is not None:语句,但我仍然得到同样的错误。如何在此脚本中解决此问题?

标签: pythonopencvwebcam

解决方案


根据我从您上面的帖子中收集到的信息,您不想以任何方式使用网络摄像头。如果是这样,您可能会在这部分脚本中遇到一些错误

# set up video capture, make cap video stream object
cap = cv2.VideoCapture(0)

# write file output to output.avi in 640x480 size
out = cv2.VideoWriter(
'output.avi',
cv2.VideoWriter_fourcc(*'MJPG'),
15.,
(640,480))

在这里,您正在设置视频流,然后加载 avi。如果您只想阅读图像,可以使用以下代码。

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('inputImage.jpg',0)

#Display the input image
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

您可以在下面找到有关图像和 Opencv 的更多信息。

https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html


推荐阅读