-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathapp.py
64 lines (51 loc) · 2.28 KB
/
app.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
from flask import Flask, request, Response, abort, send_file, jsonify, render_template, url_for, redirect, send_from_directory
from werkzeug import secure_filename
import os, subprocess, re, sys, difflib
app = Flask(__name__)
video_file = sys.argv[1]
@app.route('/')
def index():
return render_template('homepage.html');
@app.route('/<path:filename>')
def uploaded_file(filename):
return send_from_directory('/', filename)
@app.route("/upload", methods=['POST'])
def upload():
print "Video request"
age = request.form['age']
gender = request.form['gender']
if age and gender:
print "Medical use case: Age " + age + ", " + gender
file = request.files['file']
if file:
filename = secure_filename(file.filename)
file.save(os.path.join('/', filename))
client_proc_cmd = 'python heart_rate_client.py ' + url_for('uploaded_file', filename=filename) + ' ' + str(age) + ' ' + str(gender)
client_proc = subprocess.Popen(client_proc_cmd.split(' '), stdout=subprocess.PIPE)
header = client_proc.stdout.readline()
print header
[width, height, fps] = header.split(' ')
cmd = 'ffmpeg -f rawvideo -pix_fmt bgr24 -s WIDTHxHEIGHT -r 30 -i - -i ' + url_for('uploaded_file', filename=filename) + ' -f ogg -qscale:v 10 pipe:1'
cmd = cmd.replace('WIDTH', width).replace('HEIGHT', height)
cmd = cmd.split(' ')
# lets you skip forward
start = request.args.get("start") or 0
def generate():
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=client_proc.stdout)
try:
f = proc.stdout
byte = f.read(512)
while byte:
yield byte
byte = f.read(512)
finally:
proc.kill()
return Response(response=generate(),
status=200,
mimetype='video/ogg',
headers={'Access-Control-Allow-Origin': '*',
"Content-Type": "video/ogg",
"Content-Disposition": "inline",
"Content-Transfer-Enconding": "binary"})
if __name__ == '__main__':
app.run(host='0.0.0.0', threaded=True, debug=True)