-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.py
386 lines (356 loc) · 13.1 KB
/
web.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from flask import Flask, render_template, Response, request, send_from_directory
import cv2
import datetime, time
import os, sys
import numpy as np
from time import perf_counter
from threading import Thread
from animations.main import MU_effect
from animations.trial import tiktok_animation
from animations.segmentation import Segmentation
from animations.doctorStrange import doctor_strange
app=Flask(__name__, static_url_path='/static/', template_folder='static/templates')
app.static_folder = 'static'
app.config['UPLOAD_FOLDER'] = 'Upload'
global global_frame, video_camera, rec, cap
rec, cap = 1, 1
video_camera = None
global_frame = None
def generate_frames_1():
camera=cv2.VideoCapture(0)
i = 0
while True:
## read the camera frame
success,frame=camera.read()
if not success:
break
else:
frame = tiktok_animation(frame, 1, i)
try:
i+=1
ret, buffer=cv2.imencode('.jpg',frame)
frame=buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
except Exception as e:
pass
def record_generate_frames_1():
camera=cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('Upload/Anima1.avi', fourcc, 20.0, (1080, 500))
i = 0
while True:
## read the camera frame
success,frame=camera.read()
if not success:
print('Not sucess')
break
else:
frame = tiktok_animation(frame, 1, i)
out.write(frame)
try:
i+=1
ret, buffer=cv2.imencode('.jpg',frame)
frame=buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
except Exception as e:
pass
out.release()
def generate_frames_2():
camera=cv2.VideoCapture(0)
camera.get(cv2.CAP_PROP_BUFFERSIZE)
while True:
## read the camera frame
success,frame=camera.read()
if not success:
break
else:
frame = MU_effect(frame)
try:
ret, buffer=cv2.imencode('.jpg',frame)
frame=buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
except Exception as e:
pass
def record_generate_frames_2():
camera=cv2.VideoCapture(0)
camera.get(cv2.CAP_PROP_BUFFERSIZE)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('Upload/Anima2.avi', fourcc, 20.0, (640, 480))
while True:
## read the camera frame
success,frame=camera.read()
if not success:
break
else:
frame = MU_effect(frame)
out.write(frame)
try:
ret, buffer=cv2.imencode('.jpg',frame)
frame=buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
except Exception as e:
pass
out.release()
def generate_frames_3():
camera=cv2.VideoCapture(0)
fps = camera.get(cv2.CAP_PROP_FPS)
height = camera.get(cv2.CAP_PROP_FRAME_HEIGHT)
scale_fact = 1
segment_count = fps*3
segment_height = int(height*scale_fact/segment_count)
frames = []
t1 = perf_counter()
while True:
success,frame=camera.read()
if not success:
break
else:
if scale_fact != 1:
frame = cv2.resize(frame, (int(frame.shape[1]*scale_fact), int(frame.shape[0]*scale_fact)))
frames.append(frame)
if len(frames) >= segment_count:
segments = []
for i,frame in enumerate(frames):
segments.append(frame[i*segment_height:(i+1)*segment_height])
frame = np.concatenate(segments, axis=0)
frames.pop(0)
t2 = perf_counter()
delay = int(1000/fps - (t2-t1)*1000)
delay = delay if delay >1 else 1
t1 = perf_counter()
try:
ret, buffer=cv2.imencode('.jpg',frame)
frame=buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
except Exception as e:
pass
def record_generate_frames_3():
camera=cv2.VideoCapture(0)
fps = camera.get(cv2.CAP_PROP_FPS)
height = camera.get(cv2.CAP_PROP_FRAME_HEIGHT)
scale_fact = 1
segment_count = fps*3
segment_height = int(height*scale_fact/segment_count)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('Upload/Anima3.avi', fourcc, 20.0, (640, 450))
frames = []
temp = None
t1 = perf_counter()
count = 0
while True:
count+=1
success,frame=camera.read()
X = frame
if not success:
break
else:
if scale_fact != 1:
frame = cv2.resize(frame, (int(frame.shape[1]*scale_fact), int(frame.shape[0]*scale_fact)))
frames.append(frame)
# print('scale fact: ', scale_fact, ' segment height: ', segment_height)
if len(frames) >= segment_count:
segments = []
for i,frame in enumerate(frames):
X = frame[i*segment_height:(i+1)*segment_height]
print(X)
segments.append(X)
frame = np.concatenate(segments, axis=0)
cv2.imwrite(f'video/pic_{count}.jpg', frame)
out.write(frame)
frames.pop(0)
t2 = perf_counter()
delay = int(1000/fps - (t2-t1)*1000)
delay = delay if delay >1 else 1
t1 = perf_counter()
try:
ret, buffer=cv2.imencode('.jpg',frame)
frame=buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
except Exception as e:
pass
out.release()
def generate_frames_4():
camera=cv2.VideoCapture(0)
while True:
## read the camera frame
success,frame=camera.read()
if not success:
break
else:
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
img_blur = cv2.GaussianBlur(frame, (23, 23), 0, 0)
img_blend = cv2.divide(frame, img_blur, scale=200)
try:
ret, buffer=cv2.imencode('.jpg',img_blend)
frame=buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + img_blend + b'\r\n')
except Exception as e:
pass
def record_generate_frames_4():
camera=cv2.VideoCapture(0)
camera.get(cv2.CAP_PROP_BUFFERSIZE)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('Upload/Anima4.avi', fourcc, 40.0, (640, 480))
while True:
success,frame= camera.read()
if not success:
break
else:
frame = doctor_strange(frame)
out.write(frame)
try:
ret, buffer=cv2.imencode('.jpg', frame)
frame=buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
except Exception as e:
pass
out.release()
def generate_frames_rmBG():
camera=cv2.VideoCapture(0)
imgBg = cv2.imread("Photo/9.jpg")
seg = Segmentation()
while True:
# read the camera frame
success,frame=camera.read()
if not success:
break
else:
frame, imgBg = cv2.resize(frame, (640, 480)), cv2.resize(imgBg, (640, 480))
frame = seg.removeBG(frame, imgBg, threshold=0.55)
try:
ret, buffer=cv2.imencode('.jpg',frame)
frame=buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
except Exception as e:
pass
def record_generate_frames_rmBG():
camera=cv2.VideoCapture(0)
imgBg = cv2.imread("Photo/9.jpg")
seg = Segmentation()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('Upload/rmBG.avi', fourcc, 40.0, (640, 480))
while True:
success,frame= camera.read()
if not success:
break
else:
frame, imgBg = cv2.resize(frame, (640, 480)), cv2.resize(imgBg, (640, 480))
frame = seg.removeBG(frame, imgBg, threshold=0.55)
out.write(frame)
try:
ret, buffer=cv2.imencode('.jpg', frame)
frame=buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
except Exception as e:
pass
out.release()
@app.route('/')
def Home():
return render_template('home.html')
@app.route('/download/<path:filename>', methods=['GET', 'POST'])
def download(filename):
# Appending app path to upload folder path within app root folder
uploads = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'])
return send_from_directory(directory=uploads, path=filename)
@app.route('/animation_1', methods=['POST','GET'])
def Animation1():
global rec, cap
if request.method == 'POST':
if request.form.get('cap') == "Turn on/off Webcam":
cap *= -1
return render_template('Animation1.html', cap=cap)
elif request.form.get('rec') == "Recording":
rec *= -1
return render_template('Animation1.html', rec=rec)
else:
return render_template('Animation1.html')
@app.route('/animation_2', methods=['POST','GET'])
def Animation2():
global rec, cap
if request.method == 'POST':
if request.form.get('cap') == "Turn on/off Webcam":
cap *= -1
return render_template('Animation2.html', cap=cap)
elif request.form.get('rec') == "Recording":
rec *= -1
return render_template('Animation2.html', rec=rec)
else:
return render_template('Animation2.html')
@app.route('/animation_3', methods=['POST','GET'])
def Animation3():
global rec, cap
if request.method == 'POST':
if request.form.get('cap') == "Turn on/off Webcam":
cap *= -1
return render_template('Animation3.html', cap=cap)
elif request.form.get('rec') == "Recording":
rec *= -1
return render_template('Animation3.html', rec=rec)
else:
return render_template('Animation3.html')
@app.route('/animation_4', methods=['POST','GET'])
def Animation4():
global rec, cap
if request.method == 'POST':
if request.form.get('cap') == "Turn on/off Webcam":
cap *= -1
return render_template('Animation4.html', cap=cap)
elif request.form.get('rec') == "Recording":
rec *= -1
return render_template('Animation4.html', rec=rec)
else:
return render_template('Animation4.html')
@app.route('/Background_removal', methods=['POST','GET'])
def Background_removal():
global rec, cap
if request.method == 'POST':
if request.form.get('cap') == "Turn on/off Webcam":
cap *= -1
return render_template('Background-removal.html', cap=cap)
elif request.form.get('rec') == "Recording":
rec *= -1
return render_template('Background-removal.html', rec=rec)
else:
return render_template('Background-removal.html')
@app.route('/file/video_animation_1')
def video_1():
return Response(generate_frames_1(),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/file/video_animation_2')
def video_2():
return Response(generate_frames_2(),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/file/video_animation_3')
def video_3():
return Response(generate_frames_3(),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/file/video_animation_4')
def video_4():
return Response(generate_frames_4(),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/file/BG_removal')
def video_BG_removal():
return Response(generate_frames_rmBG(),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/recording/video_animation_1')
def record_video_1():
return Response(record_generate_frames_1(),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/recording/video_animation_2')
def record_video_2():
return Response(record_generate_frames_2(),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/recording/video_animation_3')
def record_video_3():
return Response(record_generate_frames_3(),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/recording/video_animation_4')
def record_video_4():
return Response(record_generate_frames_4(),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/recording/BG_removal')
def record_video_BG_removal():
return Response(record_generate_frames_rmBG(),mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run()