-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextractFeatures.py
More file actions
139 lines (56 loc) · 1.91 KB
/
extractFeatures.py
File metadata and controls
139 lines (56 loc) · 1.91 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
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
import os
#это для AMD
#os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
import keras
import cv2
import numpy as np
import math
import tensorflow
def _checkIndex(video, index):
totalFrames = video.get(cv2.CAP_PROP_FRAME_COUNT)
if index > totalFrames or index < 0:
print('Кадр за границами видео')
return False
video.set(cv2.CAP_PROP_POS_FRAMES, index)
return True
def readImageFromVideo(video, index):
if(not _checkIndex(video,index)):
return
ret, frame = video.read()
return frame
def kerasFeatureExtract(fv, shots, modelString, imageSize, shotParts, shotIndex):
model = eval(modelString)
predictedFrames = []
width = imageSize
height = imageSize
for shot in shots:
offset = ((shot[1] - shot[0]) * shotIndex) / shotParts
try:
imageIndex = shot[0] + offset
frame = readImageFromVideo(fv, imageIndex)
img = cv2.resize(frame,(width,height))
reshapedImage = np.reshape(img,[1,width,height,3])
middleFrame = model.predict(reshapedImage)
predictedFrames.append(middleFrame[0])
except Exception as e:
print(e)
print('Ошибка с получением особенностей шота, кадр ' + str(imageIndex))
return predictedFrames
def HSVHistFeatureExtract(fv, shots, shotParts, shotIndex):
predictedFrames = []
for shot in shots:
offset = ((shot[1] - shot[0]) * shotIndex) / shotParts
try:
imageIndex = shot[0] + offset
image = readImageFromVideo(fv, imageIndex)
hsvImage = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hist = cv2.calcHist([hsvImage],[0],None,[256],[0,256])
unpackedHist = []
for i in range(256):
unpackedHist.append(hist[i][0])
predictedFrames.append(unpackedHist)
except Exception as e:
print(e)
print('Ошибка с получением особенностей шота, кадр ' + str(imageIndex))
return predictedFrames
return