Webcam openCV error: (-215) ssize.width > 0 && ssize.height > 0 in function resize
Webcam openCV error: (-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:
语句,但是还是报同样的错误。如何在此脚本中解决此问题?
根据我从您上面的 post 那里收集到的信息,您 不想 以任何形式使用网络摄像头。如果是这样,您可能在脚本的这一部分遇到了一些错误
# 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 的更多信息。
我看过其他堆栈交换帖子关于这个完全相同的错误,但我的脚本不是从图像或图像列表中读取,而是从网络摄像头中读取。顺便说一句,这个脚本是复制的,我试图让它作为一个例子让我了解它是如何工作的。
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:
语句,但是还是报同样的错误。如何在此脚本中解决此问题?
根据我从您上面的 post 那里收集到的信息,您 不想 以任何形式使用网络摄像头。如果是这样,您可能在脚本的这一部分遇到了一些错误
# 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 的更多信息。