-
Notifications
You must be signed in to change notification settings - Fork 1
/
video_find_key frames_using_histogram(64-bin method)V3.py
124 lines (106 loc) · 3.57 KB
/
video_find_key frames_using_histogram(64-bin method)V3.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 20 19:02:59 2018
@author: Metin
"""
import numpy as np
import cv2
import os
"""RGB degerleri 0-255 arasında olacaktır. Bit_7 msb'yi(most significant bit) gösterir"""
def getMostSignificantBits(value):
bit_7 = int(value / 128)
value = value % 128
bit_6 = int(value / 64)
return bit_7, bit_6
def createRGBPixelValue(red, green, blue):
createNewBitSequence = ''
createNewBitSequence += '0'
createNewBitSequence += '0'
bit_7, bit_6 = getMostSignificantBits(red)
createNewBitSequence += str(bit_7)
createNewBitSequence += str(bit_6)
bit_7, bit_6 = getMostSignificantBits(green)
createNewBitSequence += str(bit_7)
createNewBitSequence += str(bit_6)
bit_7, bit_6 = getMostSignificantBits(blue)
createNewBitSequence += str(bit_7)
createNewBitSequence += str(bit_6)
return int(createNewBitSequence, 2)
def calculateHistogram(prevFrame, size):
hist = [0] * size
height, width = prevFrame.shape
for i in range(0, height):
for j in range(0, width):
hist[int(prevFrame[i][j]) - 1] += 1
return np.array(hist, dtype=int)
video = cv2.VideoCapture("0.avi")
fps = video.get(cv2.CAP_PROP_FPS)
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
count = 0
first = True
isFinish = False
prevHist = [0] * 64
prevFrame = np.zeros(shape=(height, width))
currentFrame = np.zeros(shape=(height, width))
### DEGİSİKLİK ###
cnt = 10
while(video.isOpened() and (isFinish == False)):
ret, frame = video.read()
if ret == True:
if first == False:
for i in range(0, height):
for j in range(0, width):
currentFrame[i][j] = createRGBPixelValue(frame[i][j][0], frame[i][j][1], frame[i][j][2])
currentHist = calculateHistogram(currentFrame, 64)
dist = np.linalg.norm(currentHist - prevHist)
### DEGİSİKLİK ###
dist = dist / (width * height)
print(dist)
cnt += 1
if(dist > 0.1):
cnt = 0
print("aa: ", float(count/fps))
if(cnt == 2):
print("hlelo")
cv2.imwrite("rgb_key_frame/%f.jpg" % float(count / fps), frame)
### DEGİSİKLİK ###
prevHist = currentHist
else:
cv2.imwrite("rgb_key_frame/%f.jpg" % float(count / fps), frame)
for i in range(0, height):
for j in range(0, width):
prevFrame[i][j] = createRGBPixelValue(frame[i][j][0], frame[i][j][1], frame[i][j][2])
prevHist = calculateHistogram(prevFrame, 64)
first = False
cv2.imshow('frame', frame)
count += 1
if cv2.waitKey(25) & 0xFF == ord('q'):
isFinish = True
else:
isFinish = True
video.release()
cv2.destroyAllWindows()
file_list = os.listdir("rgb_key_frame")
i = 0
size = len(file_list)
while i < size:
tmp = file_list[i].split('.')
file_list[i] = tmp[0] + "." + tmp[1]
i += 1
file_list.sort()
i = 0
next_frame = 10
while i < size:
number = float(file_list[i])
specific_list = [item for item in file_list if (float(item) >= number and number + 1 >= float(item))]
if (len(specific_list) > 2):
del specific_list[0]
del specific_list[-1]
j = 0
while j < len(specific_list):
file_list.remove(specific_list[j])
os.unlink("rgb_key_frame/%s" % specific_list[j] + ".jpg")
j = j + 1
size = len(file_list)
i = i + 1