[b8dc97]: / Create_inputImage.py

Download this file

58 lines (45 with data), 1.6 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import cv2
import os
import sys
import time
# Reading input and setting the frames
inp = cv2.VideoCapture("C:\\Users\\Vishal R\\Downloads\\DownwardDog.mp4")
# Check if the video file opened successfully
if not inp.isOpened():
print("Error: Unable to open video file.")
sys.exit()
# Get total frame count to ensure we're processing all frames
total_frames = int(inp.get(cv2.CAP_PROP_FRAME_COUNT))
print(f"Total frames in video: {total_frames}")
# Setting the starting frame (you can change this if needed)
inp.set(1, 400)
# Check if the directory for images exists, if not, create it
try:
if not os.path.exists('inp_images'):
os.makedirs('inp_images')
except OSError:
print('Error creating input directory')
# Current frame number
curr_frame = 0
# Reads the frame and stores it in the directory
while True:
inp.set(cv2.CAP_PROP_POS_FRAMES, curr_frame) # Set the current frame manually
ret, frame = inp.read()
# Check if frame is read successfully
if not ret:
print(f"Failed to read frame {curr_frame} or end of video.")
break
# Save the frame as an image
name = './inp_images/inp_image_' + str(curr_frame) + '.jpg'
print(f"Creating... {name}")
cv2.imwrite(name, frame)
# Increment the frame counter
curr_frame += 1
# Optional: add a small delay to help with frame processing
time.sleep(0.1)
# Stop if we've processed all frames
if curr_frame >= total_frames:
break
# Release the video capture object and close any OpenCV windows
inp.release()
cv2.destroyAllWindows()