-
Notifications
You must be signed in to change notification settings - Fork 1
/
FrameExtractionFromVideo.py
57 lines (45 loc) · 1.35 KB
/
FrameExtractionFromVideo.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 14 09:47:58 2019
@author: Jo
"""
#Doing with open cv2
#Import Cv2 library
import cv2,os
from moviepy.editor import VideoFileClip
inputfilepath="Dataset/vid1.mov"
#For saving output
if not os.path.exists('Frames'):
os.makedirs('Frames')
#location of video file
video=cv2.VideoCapture(inputfilepath)
#For Getting Clip duration
clip = VideoFileClip(inputfilepath)
print(clip.duration,"seconds")
def extractframe(sec):
# cap.set(cv2.CAP_PROP_POS_MSEC,sec*1000) is responsible for
#skipping directly to the sec in the video (sec*1000th millisecond)
video.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
#reading frame
hasframes,image = video.read()
if hasframes:
#Write to location , increasing the count to avoid name conflict of images
#
cv2.imwrite("Frames/image"+str(count)+".jpg", image) # save frame as JPG file
return hasframes
#starting from 0th second
sec = 0
#Setting fps , here it will capture image in each 0.5 second , 2fps
frameRate = 0.5
count=1
#to check whether frame are their in video or not.
success = extractframe(sec)
while success:
#increasing counter to name conflick
count = count + 1
#setting sec
sec = sec + frameRate
sec = round(sec, 2)
print(sec)
success = extractframe(sec)