-
Notifications
You must be signed in to change notification settings - Fork 0
/
Images_from_Video.py
105 lines (91 loc) · 3.03 KB
/
Images_from_Video.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import datetime
import os
import cv2
from DuplicateRemover import DuplicateRemover
class ImagesFromVideo:
"""
Extract the Images from video and
apply pre-processing algorithms
on images
"""
def __init__(self, logger, f_name=""):
self.filename = f_name
"""
Creating an object
"""
self.logger = logger
def readfile(self):
try:
"""
Read the video from specified path
"""
cam = cv2.VideoCapture(str(self.filename))
return cam
except Exception as e:
self.logger.error("File Not Found at this specific directory...")
def detect_blur_images(self, image):
"""
detect the blur image
"""
threshold = 60
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
fm = cv2.Laplacian(gray, cv2.CV_64F).var()
text = "Blurry"
if fm >= threshold:
text = "Not Blurry"
data = [text, image]
return data
data = [text, image]
return data
def get_frames_times(self,cam):
video_frames = cam.get(cv2.CAP_PROP_FRAME_COUNT)
fps = int(cam.get(cv2.CAP_PROP_FPS))
# calculate duration of the video
seconds = int(video_frames / fps)
video_time = str(datetime.timedelta(seconds=seconds))
return {
"video_time": video_time,
"no_of_frames": video_frames,
"frames_per_second": fps
}
def processing(self, cam):
currentframe = 0
try:
while True:
"""
reading from frames
"""
# cam.set(cv2.CAP_PROP_POS_MSEC, (currentframe * 1000))
ret, frame = cam.read()
if ret:
"""
set the name of frame
"""
try:
name = os.path.dirname(os.path.abspath(__file__)) + '/' +\
'frames/frame' + str(
currentframe) + '.jpg'
"""
writing the extracted images
"""
data = self.detect_blur_images(frame)
cv2.imwrite(name, data[1])
# if data[0] == "Not Blurry":
# cv2.imwrite(name, data[1])
"""
increasing counter so that it will
show how many frames are created
"""
currentframe += 1
except Exception as e:
self.logger.error(e)
else:
break
except Exception as e:
self.logger.error(e)
def detect_duplicate_images(self):
"""
Remove Duplicates Images
"""
dr = DuplicateRemover(os.path.dirname(os.path.abspath(__file__)) + '/' + "frames/", self.logger)
dr.find_duplicates()