python - 网络摄像头 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:
语句,但我仍然得到同样的错误。如何在此脚本中解决此问题?
解决方案
根据我从您上面的帖子中收集到的信息,您不想以任何方式使用网络摄像头。如果是这样,您可能会在这部分脚本中遇到一些错误
# 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 的更多信息。
推荐阅读
- javascript - Angularjs将json响应合并为一个
- typescript - 将 JSON 文件的输出保存在 Map Object Angular 6 中
- ios - 如何使用 DeviceCheck API swift 获取唯一 ID?
- ionic-framework - 我们如何使用 Appium 处理 Selenium 中的数字签名
- php - LearnPress 为课程内容创建模板 - 不同的流程
- javascript - 没有为键 X 定义路由 - 两个 StackNavigators
- java - JavaFX 场景生成器的奇怪屏幕尺寸
- oracle - oracle如何查找修改用户密码的人
- ruby-on-rails - RSpec:如何编写单元测试用例以接收在私有方法中引发的异常
- java - Java Spring中的单例bean并发