Skip to content

Commit 4075ccd

Browse files
Merge pull request #1578 from SwastikGorai/framext
Frame-Extractor
2 parents deb923b + bc03b56 commit 4075ccd

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

Frame-Extract/Demo/frames.png

610 KB
Loading

Frame-Extract/Demo/place.png

123 KB
Loading

Frame-Extract/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
# Framext
3+
A python script that extracts frames from video
4+
5+
6+
## Setup instructions
7+
8+
To use this script, get a copy of the script into your system and pass in the required arguments
9+
10+
## Detailed explanation of script, if needed
11+
12+
The Script takes the following Arguments:
13+
14+
~~~
15+
16+
python framext.py -h
17+
18+
usage: Frame Extractor [-h] -i VIDEO_FILE -o OUTPUT_DIR [-s SAMPLE_RATE]
19+
20+
options:
21+
-h, --help show this help message and exit
22+
-i VIDEO_FILE, --video_file VIDEO_FILE
23+
Location of video file
24+
-o OUTPUT_DIR, --output_dir OUTPUT_DIR
25+
Output directory to store frames
26+
~~~
27+
28+
The script works by extracting the FPS cap from the metadata of the video file and the length of the video. Then it uses OpenCV and counter to extract frames using the FPSCap and length of video.
29+
30+
31+
## Output
32+
The supplied video file and the created folder:
33+
<img src = "Demo/place.png">
34+
35+
The Frames extracted from the video
36+
37+
<img src = "Demo/frames.png">
38+
39+
40+
## Author
41+
42+
Swastik Gorai
43+
44+
## Disclaimers, if any
45+
46+
This script has no assoicated Disclaimers

Frame-Extract/framext.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import os
2+
from os import path
3+
import math
4+
import cv2
5+
import argparse
6+
7+
8+
class Extractor:
9+
"""Extract frames from video file and saves them in a directory
10+
11+
Arguments:
12+
vid_filename {str} -- Location of video file
13+
output_dir {str} -- Output directory to store frames
14+
15+
16+
"""
17+
18+
global args
19+
20+
def __init__(self, vid_filename, output_dir, verbose=False):
21+
22+
self.verbose = verbose
23+
self.output_dir = output_dir
24+
if not os.path.exists(self.output_dir):
25+
os.makedirs(self.output_dir)
26+
print(self.output_dir)
27+
if path.exists(vid_filename):
28+
self.vid_filename = vid_filename
29+
else:
30+
raise FileNotFoundError(f"File {vid_filename} not found")
31+
32+
# initialize video capture
33+
self.cap = cv2.VideoCapture(self.vid_filename)
34+
35+
# get video fps
36+
self.fps = self.cap.get(cv2.CAP_PROP_FPS)
37+
38+
# Video length in frames
39+
self.vid_length = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
40+
41+
42+
def extractor(self):
43+
is_ok, frame = self.cap.read()
44+
count = 0
45+
print(self.fps)
46+
while is_ok:
47+
# is_ok, frame = self.cap.read()
48+
print(f"Extracting frame {count} of {self.vid_length-1}") if self.verbose else {}
49+
# save current frame
50+
cv2.imwrite(os.path.join(
51+
self.output_dir, f"{count}.jpg"), frame)
52+
print("COMPLETED") if count == self.vid_length-1 else {}
53+
54+
is_ok, frame = self.cap.read()
55+
56+
count += 1
57+
58+
def extract_frames(file):
59+
global args
60+
61+
# check whether file is greather than 0 bytes using path
62+
if os.stat(path.join(args.input_dir, file)).st_size > 0:
63+
# extract frames
64+
extractor = Extractor(path.join(args.input_dir, file), path.join(
65+
args.output_dir, file), args.sampling_rate)
66+
extractor.extractor()
67+
else:
68+
print(f"File {file} is empty")
69+
70+
71+
def main():
72+
# setup parsers for CLI arguments
73+
parser = argparse.ArgumentParser("Frame Extractor")
74+
parser.add_argument("-i", "--video_file", type=str,
75+
help="Location of video file", required=True)
76+
parser.add_argument("-o", "--output_dir", type=str,
77+
help="Output directory to store frames", required=True)
78+
parser.add_argument("-v", "--verbose", type = bool,
79+
help="Display details of extraction of frames", default=False)
80+
args = parser.parse_args()
81+
# print(args)
82+
# Extract frames from video file
83+
if args.video_file:
84+
name = args.video_file.split(".")[0]
85+
ext = args.video_file.split(".")[1]
86+
if ext != "mp4":
87+
raise argparse.ArgumentTypeError(
88+
f"File {args.video_file} is not a video(.mp4) file")
89+
output_dir = os.path.join(args.output_dir, name)
90+
extract = Extractor(args.video_file, args.output_dir, args.verbose)
91+
extract.extractor()
92+
93+
94+
if __name__ == '__main__':
95+
main()

Frame-Extract/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
opencv-python

0 commit comments

Comments
 (0)