I've seen the other stack exchange posts about this exact same error, but instead of reading from an image or a list of images, my script reads from a webcam. BTW, this script is copied, and i'm trying to get it working as an example for me to learn how it all works.
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)
I understand that the error is caused because there is no image for the resize function to resize, so I added the if cap is not None:
statement, but I still get the same error. How can I fix this in this script?
From what I've gathered from your post above, you don't want to use a webcame in any sort of capacity. If so, You might be experiencing some errors with this portion of the script
# 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))
Here you are setting up a video stream, then loading in an avi. If you just want to read in an image you can use the below code.
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()
You can find more about images and Opencv below.