-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoImgExtract.py
More file actions
29 lines (28 loc) · 1.15 KB
/
VideoImgExtract.py
File metadata and controls
29 lines (28 loc) · 1.15 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
# Necessary imports
import os
import cv2
# Change the directory to your video path
os.chdir('Your\VideoFolder\path')
# Try to create a folder if found already, continue;
try:
if not os.path.exists('Name_your_folder_for_the_frames'):
os.makedirs('Name_your_folder_for_the_frames')
except OSError:
print('Error: Creating directory of data')
# To ensure the differentiation in images
currentFrame = 0
kat = cv2.VideoCapture('Your_Video_Name.extension')
ret, frame = kat.read()
ret = True
while ret:
kat.set(cv2.CAP_PROP_POS_MSEC, (currentFrame * 1000)) # You can set your frames per second 1000 represents 1 frame/second if you wanna change the FPS do the math this way
ret, frame = kat.read()
if not ret: break
name = 'Your\VideoFolder\path\\anyNameForTheFrame' + str(currentFrame) + '.png'# can also use.jpeg,.bmp but using png will result have less loss in quality
print('Creating...' + name) # This is for reference not necessary line of code
cv2.imwrite(name, frame)
# To stop duplicate images
currentFrame += 1
# When the process is done, release the capture
kat.release()
cv2.destroyAllWindows()