-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
144 lines (103 loc) · 3.59 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
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
from flask import Flask, flash, request, render_template, Response
import sys
from pathlib import Path
from config import UPLOAD_FOLDER, REMOVE_TIME, CAMERA_SOURCE, GPU_NUM, VIDEO_RESOLUTION, PORT
from utils import remove_old_files, model_predictions, draw_predictions
import cv2
import os
import multiprocessing as mp
import warnings
warnings.filterwarnings("ignore")
os.environ['CUDA_VISIBLE_DEVICES'] = GPU_NUM
app = Flask(__name__)
app.secret_key = "your_secret_key"
app.config['UPLOAD_FOLDER'] = 'static/uploads'
action_event = mp.Event()
name_to_save = ''
stop_event = mp.Event()
file = Path(__file__).resolve()
parent, root = file.parent, file.parents[1]
sys.path.append(str(root))
try:
sys.path.remove(str(parent))
except ValueError:
pass
@app.route('/')
def upload_form():
# global stop_event
# if not stop_event.is_set():
# stop_event.set()
return render_template('home.html')
def webcam_capture(frame_queue, detection_queue):
camera = cv2.VideoCapture(CAMERA_SOURCE)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, VIDEO_RESOLUTION[0])
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, VIDEO_RESOLUTION[1])
global stop_event
while True:
global action_event, name_to_save
if stop_event.is_set():
break
ret, frame = camera.read()
if not ret:
break
else:
if frame_queue.empty():
frame_queue.put(frame)
if detection_queue.empty():
continue
if action_event.is_set(): # if save_event TRUE
img_name = name_to_save + '.jpg'
cv2.imwrite(os.path.join(UPLOAD_FOLDER, img_name), frame)
action_event.clear()
if detection_queue.empty():
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
boxes, names = detection_queue.get()
draw_predictions(frame, boxes, names)
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')
camera.release()
stop_event.clear()
def process_frames(frame_queue, detection_queue):
global stop_event
while True:
if stop_event.is_set():
break
if frame_queue.empty():
continue
frame = frame_queue.get()
# TODO edit model_predictions in utils.py
boxes, names = model_predictions(frame)
detection_queue.put((boxes, names))
@app.route('/videoo', methods=["GET", "POST"])
def videoo():
return render_template('videoo.html')
@app.route('/requests_task', methods=["GET", "POST"])
def tasks():
global action_event, name_to_save
if request.method == 'POST':
input_text = request.form['text']
name_to_save = input_text
print(f'Saving: {name_to_save}')
action_event.set() # TRUE
return videoo()
elif request.method == 'GET':
return videoo()
return videoo()
@app.route('/video')
def video():
remove_old_files(UPLOAD_FOLDER, REMOVE_TIME)
global stop_event, action_event
stop_event.clear()
frame_queue = mp.Queue()
detection_queue = mp.Queue()
p1 = mp.Process(target=webcam_capture, args=(frame_queue, detection_queue))
p2 = mp.Process(target=process_frames, args=(frame_queue, detection_queue))
p1.start()
p2.start()
mp.freeze_support()
return Response(webcam_capture(frame_queue, detection_queue), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=PORT)