I am trying to use use this code (https://github.com/Aman-Preet-Singh-Gulati/Vehicle-count-detect) to detect the number of vehicles and write a video file with bounding boxes. I have successfully used this code on the image file, but the code is not generating a video file for some reason.
I am using haar cascade car model (verified the .xml file works on a picture) and writing a video file using the following code:
cascade_src = 'cars.xml'
video_src = 'Cars.mp4'
cap = cv2.VideoCapture(video_src)
car_cascade = cv2.CascadeClassifier(cascade_src)
video = cv2.VideoWriter('result.avi',cv2.VideoWriter_fourcc(*'DIVX'), 15, (450,250))
The resulting file is a few kilobytes and cannot be played. I am trying to get some help to figure out how to make this step work.
Assuming this step works, the next block of code is supposed to generate bounding boxes and write to a file. Will this create a new file or overwrite the previously generated video file with the bounding boxes?
while True:
ret, img = cap.read()
if (type(img) == type(None)):
break
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectMultiScale(gray, 1.1, 2)
for (x,y,w,h) in cars:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,255),2)
video.write(img)
video.release()
Thanks a lot! I am a total beginner in Python so appreciate the help.
(450,250)
then you have to resize image before writing with size(450,250)
. And this can make problemvideo.write(img)
INSIDEfor
-loop. But your code runsvideo.write(img)
afterfor
-loop and it may write only last image/frame.ret
to check if you get image - so instead ofif (type(img) == type(None)):
you can checkif ret is False: break
. And if you really have to checkimg
thenif img is None: break
cv2.putText()
?