1

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.

5
  • if original file has size different than (450,250) then you have to resize image before writing with size (450,250). And this can make problem
    – furas
    Commented May 31, 2022 at 3:18
  • if you want to write file then you have to use video.write(img) INSIDE for-loop. But your code runs video.write(img) after for-loop and it may write only last image/frame.
    – furas
    Commented May 31, 2022 at 3:22
  • you have ret to check if you get image - so instead of if (type(img) == type(None)): you can check if ret is False: break. And if you really have to check img then if img is None: break
    – furas
    Commented May 31, 2022 at 3:23
  • @furas thanks! I made the following changes - Changed the size to match the source video size - put the video.write(img) command inside the for loop (video.release() is outside the loop) This solved the issue and I can see the results. Each frame shows the bounding boxes for the objects detected. Is there a quick way to add the count of bounding boxes to the video file (for each frame)?
    – falkon
    Commented May 31, 2022 at 3:41
  • do you mean cv2.putText() ?
    – furas
    Commented May 31, 2022 at 4:02

1 Answer 1

2

I see two possible problems:

  1. you have write() in wrong place. It has to be inside for-loop. You write only last image.

  2. if original file has size different than (450,250) then you have to resize image before writing. It will NOT resize it automatically but it will skip image if it has wrong size. And finally it will create file without images/frames - so it will create broken file.


import cv2

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))

# --- loop ---

while True:

    ret, img = cap.read()

    if ret is False:
        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)

    img = cv2.resize(img, (450, 250))
    video.write(img)

# --- after loop ---

video.release()
cap.release()
2
  • You don't need if ret is False: break, but some do. Not widely. Commented May 31, 2022 at 13:00
  • @toyotaSupra if you don't use if ret is False: break then it will raise error after end of file - when img will be None and it will try change color in None - cv2.cvtColor(None, ...), or when it will try detectMultiScale(None, ...) or cv2.rectangle(None, ...), etc. So I would rather say: some people don't use it but they should use it.
    – furas
    Commented May 31, 2022 at 13:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.