/
obj_detect_stream_server.py
205 lines (156 loc) · 7.5 KB
/
obj_detect_stream_server.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
#!/usr/bin/env python
# import the necessary packages
from __future__ import print_function
from flask import Flask, render_template, Response, request, jsonify, url_for
import sys
import argparse
import numpy as np
import cv2
import tensorflow as tf
from scipy import misc
import traceback
import os
import imutils
from imutils.video import WebcamVideoStream
import core_process as cp
###########
# Create and flask app
app = Flask(__name__)
# Render main html pages
@app.route('/stream')
def stream():
return render_template('stream.html')
@app.route('/image')
def image():
return render_template('image.html')
@app.route('/youtube')
def youtube():
return render_template('youtube.html')
@app.route('/')
def index():
return render_template('index.html')
##
# This subroutine activates youtube-dl with the input link, in the form inside youtube.html
# and, save the file into the /tmp folder, naming it as "video_to_process.mp4"
@app.route('/video_url', methods=['GET', 'POST'])
def process_on_video():
if request.method=='POST':
print('aaaaaaaaaaaaaaaaauehauehuhaeuhaeuea')
filename = get_youtube_video()
def get_frame():
with model.as_default():
with tf.Session(graph=model) as sess:
cap = cv2.VideoCapture('./static/{}'.format(filename))
while cap.isOpened():
# grab the frame from the threaded video stream and process it
ret, src_frame = cap.read()
# Process image
out_frame = cp.process_frame(src_frame,model,sess,category_index, display=True)
imgencode=cv2.imencode('.jpg',out_frame)[1]
stringData=imgencode.tostring()
yield (b'--frame\r\n'
b'Content-Type: text/plain\r\n\r\n'+stringData+b'\r\n')
return Response(get_frame(), mimetype='multipart/x-mixed-replace; boundary=frame')
# return render_template('youtube.html', video_name = filename)
def get_youtube_video():
'''
video_examples
https://www.youtube.com/watch?v=PhFcl72nhiM (person talking)
https://www.youtube.com/watch?v=jjlBnrzSGjc (traffic camera)
https://www.youtube.com/watch?v=MiN_kgpkn-U (people walking)
https://www.youtube.com/watch?v=GKuG4fftJdk (dogs playing 01)
https://www.youtube.com/watch?v=GKuG4fftJdk (dogs playing 02)
'''
filename = 'video_to_process.mp4'
if os.path.isfile(('/home/odj_detect_app/static/{}'.format(filename))):
print('here UHAuhAUhUAHUHA')
os.system('rm /home/odj_detect_app/static/{}'.format(filename))
os.system('youtube-dl -f 18 -o ' + '"/home/odj_detect_app/static/video_to_process.%(ext)s" ' + request.form['video_url'])
return filename
###########
@app.route('/calc')
def calc():
def get_frame():
vs = WebcamVideoStream(src=src).start()
with model.as_default():
with tf.Session(graph=model) as sess:
while True:
# grab the frame from the threaded video stream and process it
src_frame = vs.read()
# Process image
out_frame = cp.process_frame(src_frame,model,sess,category_index, display=True)
imgencode=cv2.imencode('.jpg',out_frame)[1]
stringData=imgencode.tostring()
yield (b'--frame\r\n'
b'Content-Type: text/plain\r\n\r\n'+stringData+b'\r\n')
vs.stop()
return Response(get_frame(),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/predict', methods=['POST'])
def predict_frame():
if request.method=='POST':
# get uploaded image file if it exists
file = request.files['image']
# read in file as raw pixels values
src_frame = np.array(misc.imread(file))
print("\n\nsrc_frame:",src_frame.shape,"\n\n")
with model.as_default():
with tf.Session(graph=model) as sess:
try:
#Process image
prediction = cp.process_frame(src_frame, model, sess, category_index, display=False)
return jsonify(prediction)
except Exception, e:
return jsonify({'error': str(e), 'trace': traceback.format_exc()})
def init():
# created a *threaded *video stream, allow the camera senor to warmup,
# and start the FPS counter
print("[INFO] sampling THREADED frames from webcam...")
if( len(args['source']) == 1 ):
src = int(args['source'])
# Donwload Model by its name
cp.download_weights(args['model'])
# Path to frozen detection graph. This is the actual model that is used for the object detection.
model = cp.load_model(args['model'])
# List of the strings that is used to add correct label for each box.
category_index = cp.load_label_map(args['decoder'])
return src,model,category_index
if __name__ == '__main__':
# Asterisk arguments
parser = argparse.ArgumentParser(description='')
parser.add_argument('--source', default="0", type=str)
parser.add_argument('--host', default='localhost', type=str)
parser.add_argument("--port", action="store", default=8080, type=int)
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = '/tensorflow/models/research/object_detection/data/mscoco_label_map.pbtxt'
parser.add_argument('--decoder', default=PATH_TO_LABELS, type=str)
# Name of the model to be downloaded and loaded
# MODEL_NAME = "ssd_mobilenet_v1_coco_2018_01_28"
MODEL_NAME = "ssd_mobilenet_v1_0.75_depth_300x300_coco14_sync_2018_07_03"
# MODEL_NAME = "ssd_mobilenet_v1_quantized_300x300_coco14_sync_2018_07_18"
# MODEL_NAME = "ssd_mobilenet_v1_0.75_depth_quantized_300x300_coco14_sync_2018_07_18"
#MODEL_NAME = "ssd_mobilenet_v1_ppn_shared_box_predictor_300x300_coco14_sync_2018_07_03"
# MODEL_NAME = "ssd_mobilenet_v1_fpn_shared_box_predictor_640x640_coco14_sync_2018_07_03"
# MODEL_NAME = "ssd_resnet50_v1_fpn_shared_box_predictor_640x640_coco14_sync_2018_07_03"
#MODEL_NAME = "ssd_mobilenet_v2_coco_2018_03_29"
# MODEL_NAME = "ssdlite_mobilenet_v2_coco_2018_05_09"
#MODEL_NAME = "ssd_inception_v2_coco_2018_01_28"
# MODEL_NAME = "faster_rcnn_inception_v2_coco_2018_01_28"
#MODEL_NAME = "faster_rcnn_resnet50_coco_2018_01_28"
# MODEL_NAME = "faster_rcnn_resnet50_lowproposals_coco_2018_01_28"
#MODEL_NAME = "rfcn_resnet101_coco_2018_01_28"
# MODEL_NAME = "faster_rcnn_resnet101_coco_2018_01_28"
# MODEL_NAME = "faster_rcnn_resnet101_lowproposals_coco_2018_01_28"
# MODEL_NAME = "faster_rcnn_inception_resnet_v2_atrous_coco_2018_01_28"
# MODEL_NAME = "faster_rcnn_inception_resnet_v2_atrous_lowproposals_coco_2018_01_28"
#MODEL_NAME = "faster_rcnn_nas_coco_2018_01_28"
# MODEL_NAME = "faster_rcnn_nas_lowproposals_coco_2018_01_28"
# MODEL_NAME = "mask_rcnn_inception_resnet_v2_atrous_coco_2018_01_28"
# MODEL_NAME = "mask_rcnn_inception_v2_coco_2018_01_28"
# MODEL_NAME = "mask_rcnn_resnet101_atrous_coco_2018_01_28"
# MODEL_NAME = "mask_rcnn_resnet50_atrous_coco_2018_01_28"
parser.add_argument('--model', default=MODEL_NAME, type=str)
args = vars(parser.parse_args())
#Init Model (Download and Load into memory)
src,model,category_index = init()
# Accessible at
app.run(host=args['host'], port=args['port'], debug=False, threaded=True)