Pablo Biosca
Pablo Biosca

Reputation: 1

I need to name sequential images that are being capured from a video in order to procees them, sort them, and rebuild the video again. (Python)

I have a script in Python that captures frames from a video, then an ML model detects objects, and draws the bounding boxes on the frame. The scrip names each captured frame like "img" + secuencial_counter, and save them in the same folder. When I want to rebuild the video, the sort() function that I used as input to make a list, mixes up the files, because the counter hasn´t a fixed digit length. So:

list by sort() (img1, img10, img11....img19, img2, img20, img21,....img3, img30 ...etc)

what I need: (img1, img2, img3....img21, img22, img23, ......img125, img126....etc)

Is there any way that I can add to the file name a sequential number with a fixed number of digits? like:

(img001, img002, img003....img021, img022, img023, ......img125, img126....etc)

If does, the sort() function would make the job as I need.

Thanks in advance.

This is the code:

image_folder = 'dnn_model\images_for_video'
video_file = 'dnn_model\\video_output\\video_output.mp4'
image_size = (480, 848)
fps = 29.99

images = [img for img in os.listdir(image_folder) if img.endswith(".png")]
images.sort()

out = cv2.VideoWriter(video_file, cv2.VideoWriter_fourcc(*'MP4V'), fps, image_size)

img_array = []
for filename in images:
    img = cv2.imread(os.path.join(image_folder, filename))
    img_array.append(img)
    out.write(img)

out.release()

Upvotes: 0

Views: 43

Answers (1)

Pablo Biosca
Pablo Biosca

Reputation: 1

I have just found out how to solve my problem, it is not the best solution, but if I start my counter from an enough high number, like 100000 the sort() function sort in the right order. Since my fps is 30, I will have 1800 images per minute of video, so it is easy in this way to calculate the start counter number knowing the total length of the video.

Upvotes: 0

Related Questions