Skip to content
Advertisement

OpenCV finding number of rectangles and writing to video

I have a code that detects cars using haar cascade car model, draws rectangles around the detected cars, and generates a video file. I am trying to update the code to show the number of detected cars in each frame.

JavaScript

When I execute this, some frames have numbers overlaid on each other. I want only one count written on each frame. car count detection

Not sure why this is happening, but I tried to set a delay timer and adjust video write frame speed without luck. I don’t know if this method of using cnt to count cars is accurate, but if anyone knows how to fix the number overlay issue or suggest a better way to count total identified cars in each frame that would be great.

Advertisement

Answer

I think the problem you have is that for every detected car you call the function cv2.putText(img, str(cnt), (10,200), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 255, 0), 2, cv2.LINE_AA) and it draws a different number for every car. Instead of doing so you should put the line outside the for loop.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement