forked from yosinski/deep-visualization-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 6
/
run_webui.py
69 lines (48 loc) · 1.8 KB
/
run_webui.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
#! /usr/bin/env python
import os
import thread
from live_vis import LiveVis
from bindings import bindings
try:
import settings
except:
print '\nError importing settings.py. Check the error message below for more information.'
print "If you haven't already, you'll want to open the settings_model_selector.py file"
print 'and edit it to point to your caffe checkout.\n'
raise
if not os.path.exists(settings.caffevis_caffe_root):
raise Exception('ERROR: Set caffevis_caffe_root in settings.py first.')
import cv2
from flask import Flask, render_template, Response
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
def gen():
while True:
frame = get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
def get_frame():
# We are using Motion JPEG, but OpenCV defaults to capture raw images,
# so we must encode it into JPEG in order to correctly display the
# video stream.
global lv
ret, jpeg = cv2.imencode('.jpg', lv.window_buffer[:,:,::-1])
return jpeg.tobytes()
@app.route('/video_feed')
def video_feed():
return Response(gen(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
global lv
def someFunc():
print "someFunc was called"
lv.run_loop()
if os.environ.get("WERKZEUG_RUN_MAIN") == "true":
# The reloader has already run - do what you want to do here
lv = LiveVis(settings)
help_keys, _ = bindings.get_key_help('help_mode')
quit_keys, _ = bindings.get_key_help('quit')
print '\n\nRunning toolbox. Push %s for help or %s to quit.\n\n' % (help_keys[0], quit_keys[0])
thread.start_new_thread(someFunc, ())
app.run(host='127.0.0.1', debug=True)