-
Notifications
You must be signed in to change notification settings - Fork 1
/
RepnetFlask.py
288 lines (232 loc) · 8.9 KB
/
RepnetFlask.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import cv2
import os
import random
import math
import datetime as dt
from collections import deque
import pandas as pd
import matplotlib.pyplot as plt
from numpy.lib.scimath import sqrt # used for hoF
from numpy import arctan2 # used for hoF
from scipy import pi, cos, sin # used for HoF
from scipy.ndimage import uniform_filter # used for hoF
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import sklearn.metrics as metrics
from tensorflow.keras.layers import *
from tensorflow.keras.models import Sequential, Model, load_model
from tensorflow.keras.utils import to_categorical, plot_model
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint,CSVLogger
from tensorflow.keras.losses import categorical_crossentropy
from tensorflow.keras.optimizers import Adam
import tensorflow as tf
print("Tensorflow version: ", tf.__version__)
print(tf.test.gpu_device_name())
import torch
# for repnet
# tensowflow==1.15
# python=3.5,this is problem.i can't success in 3.9
import sys
#print(sys.path)
#sys.path.insert(1, 'C:\\Users\\Grace\\MTech Jupyter\\Intelligent Sensing Systems\\PracticeMod\\FineDiving\\itsspm_fenglei\\models')
#print(sys.path)
from models.repnet import get_repnet_model
from models.repnet import video_transform_image
from models.repnet import slow_video_generate
from models.repnet import get_counts
from models.repnet import read_video
tf.compat.v1.enable_v2_behavior()
# In[ ]:
#Global Variables
dive_action_labels = ['Entry', 'Flight', 'Takeoff']
temp_segment_model = None
autoscore_model = None
ss_twist_classifier_model = None
somersault_model = None
twist_model = None
angle_of_entry_model = None
splash_model = None
linear_regression_model = None
folderpath = 'modelcheckpoints/'
#set parameter
# FPS while recording video from webcam.
WEBCAM_FPS = 16#@param {type:"integer"}
# Time in seconds to record video on webcam.
RECORDING_TIME_IN_SECONDS = 8. #@param {type:"number"}
# Threshold to consider periodicity in entire video.
THRESHOLD = 0.001#@param {type:"number"}
# Threshold to consider periodicity for individual frames in video.
WITHIN_PERIOD_THRESHOLD = 0.005#@param {type:"number"}
# Use this setting for better results when it is
# known action is repeating at constant speed.
CONSTANT_SPEED = False#@param {type:"boolean"}
# Use median filtering in time to ignore noisy frames.
MEDIAN_FILTER = True#@param {type:"boolean"}
# Use this setting for better results when it is
# known the entire video is periodic/reapeating and
# has no aperiodic frames.
FULLY_PERIODIC = True#@param {type:"boolean"}
# Plot score in visualization video.
PLOT_SCORE = False#@param {type:"boolean"}
# Visualization video's FPS.
VIZ_FPS = 30#@param {type:"integer"}
def load_somersault_model():
print('loading somersault model')
global somersault_model
PATH_TO_CKPT = folderpath+'repnet_ckpt'
model = get_repnet_model(PATH_TO_CKPT)
somersault_model=model
def load_twist_model():
print('loading twist model')
global twist_model, somersault_model
twist_model=somersault_model
def ensureDirectoryClean(dirpath):
if not os.path.exists(dirpath):
os.makedirs(dirpath)
else:
for f in os.listdir(dirpath):
os.remove(os.path.join(dirpath, f))
def extractFolderAndFileNameFromAbsPath(absFilePath):
filename_sep = absFilePath.rindex('\\')
extension_sep = absFilePath.rindex(".")
folder = absFilePath[0: filename_sep]
shortfilename = absFilePath[filename_sep+1:extension_sep]
ext = absFilePath[extension_sep+1:len(absFilePath)]
return folder, shortfilename, ext
def extractEventNoAndDiveNo(folderPath):
tokens = folderPath.split("\\")
diveno = tokens[len(tokens)-1]
eventno = tokens[len(tokens)-2]
return eventno, diveno
def createVideo(image_folder, video_folder, divephase, vidname, resizeFrame=False, resizeFrameDim=[64,64]):
images = []
#folders = [image_folder+"\\Ntakeoff", image_folder+"\\Nflight", image_folder+"\\Nentry"]
subfolder_images = sorted(os.listdir(image_folder))
for subfolder_image in subfolder_images:
if subfolder_image.endswith(".jpg"):
images.append(image_folder+"\\"+subfolder_image)
if (len(images)==0):
return
frame = cv2.imread(images[0])
height, width, layers = frame.shape
if (resizeFrame == True):
height = resizeFrameDim[1]
width = resizeFrameDim[0]
vidFullName = video_folder+'\\'+vidname+"_"+divephase+".mp4"
print('writing video to ', vidFullName , ' framewidth ', width, ' frameheight ', height)
fps = 25
video = cv2.VideoWriter(vidFullName, cv2.VideoWriter_fourcc('M', 'P', '4', 'V'), fps, (width,height))
for image in images:
frame = cv2.imread(image)
if (resizeFrame == True):
frame = cv2.resize(frame, (height, width))
video.write(frame)
cv2.destroyAllWindows()
video.release()
return vidFullName
def predict_num_somersaults(ssVidPath, out_dir):
ensureDirectoryClean(out_dir)
print('predict num somersaults ', ssVidPath, out_dir)
video_transform_image(ssVidPath, out_dir) # when merge, must use imgfolder format
print('predict num somersaults video transform image done')
output_video=slow_video_generate(ssVidPath, out_dir)
print('predict num somersaults slow_video_generate done')
imgs, vid_fps = read_video(output_video)
print('Running RepNet...')
(pred_period, pred_score, within_period,
per_frame_counts, chosen_stride) = get_counts(
somersault_model,
imgs,
strides=[1,2,3,4],
batch_size=20,
threshold=THRESHOLD,
within_period_threshold=WITHIN_PERIOD_THRESHOLD,
constant_speed=CONSTANT_SPEED,
median_filter=MEDIAN_FILTER,
fully_periodic=FULLY_PERIODIC)
frames=imgs
count=per_frame_counts
if isinstance(count, list):
counts = len(frames) * [count/len(frames)]
else:
counts = count
sum_counts = np.cumsum(counts)
num= round(float(sum_counts[-1]),2)
unique_somes = [1.5, 2.0, 2.5, 3.5, 4.5, 3.0]
modify_num= min(unique_somes, key=lambda x: abs(x - num))
return num,modify_num
def predict_num_twists(twVidPath, out_dir):
ensureDirectoryClean(out_dir)
video_transform_image(twVidPath, out_dir) # when merge, must use imgfolder format
output_video=slow_video_generate(twVidPath, out_dir)
imgs, vid_fps = read_video(output_video)
print('Running RepNet...')
(pred_period, pred_score, within_period,
per_frame_counts, chosen_stride) = get_counts(
twist_model,
imgs,
strides=[1,2,3,4],
batch_size=20,
threshold=THRESHOLD,
within_period_threshold=WITHIN_PERIOD_THRESHOLD,
constant_speed=CONSTANT_SPEED,
median_filter=MEDIAN_FILTER,
fully_periodic=FULLY_PERIODIC)
frames=imgs
count=per_frame_counts
if isinstance(count, list):
counts = len(frames) * [count/len(frames)]
else:
counts = count
sum_counts = np.cumsum(counts)
num= round(float(sum_counts[-1]),2)
unique_twist = [0.5, 1.5, 2.0, 3.0, 1.0, 2.5, 3.5]
modify_num= min(unique_twist, key=lambda x: abs(x - num))
return num,modify_num
def processVideo(vidpath):
print('processing ', vidpath)
folder, shortfilename, _ = extractFolderAndFileNameFromAbsPath(vidpath)
numSomersaults = predict_num_somersaults(".\\images\\"+shortfilename+"\\Flight"+shortfilename+"_Flight_ss.mp4",
".\\images\\"+shortfilename+"\\repnet_tmp")
numTwists = predict_num_twists(".\\images\\"+shortfilename+"\\Flight"+shortfilename+"_Flight_tw.mp4",
".\\images\\"+shortfilename+"\\repnet_tmp")
print(
'numSomersaults: ', numSomersaults,
', numTwists: ', numTwists)
return numSomersaults, numTwists
def main():
load_somersault_model()
load_twist_model()
main()
import os
import urllib.request
from flask import Flask, flash, request, redirect, url_for, render_template
from werkzeug.utils import secure_filename
from werkzeug.wrappers import Request, Response
import json
app = Flask(__name__)
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = '.\\uploads\\'
@app.route('/sstwist', methods=['POST'])
def predict_ss_twist():
if 'videoname' not in request.form:
return {"error" : "no videoname in request"}
file = request.form['videoname']
print('file : ', file)
numSomersaults, numTwists = processVideo(app.config['UPLOAD_FOLDER']+file)
result = {
"file" : file,
"numSomersaults" : str(numSomersaults),
"numTwists" : str(numTwists)
}
result_json = json.dumps(result)
print('result', result_json)
return result_json
if __name__ == "__main__":
from werkzeug.serving import run_simple
run_simple('localhost', 5001, app)