-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
205 lines (166 loc) · 5.62 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
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
from ultralytics import YOLO
import time
import numpy as np
import cv2
from flask import Flask, render_template, request, Response, session, redirect, url_for
from flask_socketio import SocketIO
import yt_dlp as youtube_dl
model_object_detection = YOLO("yolov8n.pt")
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode='threading')
stop_flag = False
class VideoStreaming(object):
def __init__(self):
super(VideoStreaming, self).__init__()
print ("*********************************Video Streaming******************************")
# self.VIDEO = cv2.VideoCapture(0)
# self.VIDEO.set(10, 200)
self._preview = False
self._flipH = False
self._detect = False
self._model = False
self._confidence = 75.0
@property
def confidence(self):
return self._confidence
@confidence.setter
def confidence(self, value):
self._confidence = int(value)
@property
def preview(self):
return self._preview
@preview.setter
def preview(self, value):
self._preview = bool(value)
@property
def flipH(self):
return self._flipH
@flipH.setter
def flipH(self, value):
self._flipH = bool(value)
@property
def detect(self):
return self._detect
@detect.setter
def detect(self, value):
self._detect = bool(value)
def show(self, url):
print(url)
self._preview = False
self._flipH = False
self._detect = False
self._confidence = 75.0
ydl_opts = {
"quiet": True,
"no_warnings": True,
"format": "best",
"forceurl": True,
}
# Create a youtube-dl object
ydl = youtube_dl.YoutubeDL(ydl_opts)
# Extract the video URL
info = ydl.extract_info(url, download=False)
url = info["url"]
cap = cv2.VideoCapture(url)
while True:
if self._preview:
if stop_flag:
print("Process Stopped")
return
grabbed, frame = cap.read()
if not grabbed:
break
if self.flipH:
frame = cv2.flip(frame, 1)
if self.detect:
print(self._confidence)
# frame = cv2.cvtColor(snap, cv2.COLOR_BGR2RGB)
# frame = cv2.resize(frame, (500,500
# ))
# Detect objects
results = model_object_detection.predict(frame, conf=self._confidence/100)
frame, labels = results[0].plot()
list_labels = []
# labels_confidences
for label in labels:
confidence = label.split(" ")[-1]
label = (label.split(" "))[:-1]
label = " ".join(label)
list_labels.append(label)
list_labels.append(confidence)
socketio.emit('label', list_labels)
# frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
frame = cv2.imencode(".jpg", frame)[1].tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
else:
snap = np.zeros((
1000,
1000
), np.uint8)
label = "Streaming Off"
H, W = snap.shape
font = cv2.FONT_HERSHEY_PLAIN
color = (255, 255, 255)
cv2.putText(snap, label, (W//2 - 100, H//2),
font, 2, color, 2)
frame = cv2.imencode(".jpg", snap)[1].tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
# check_settings()
VIDEO = VideoStreaming()
@app.route('/', methods=['GET', 'POST'])
def homepage():
return render_template('hompage.html')
@app.route('/index', methods=['GET', 'POST'])
def index():
print("index")
global stop_flag
stop_flag = False
if request.method == 'POST':
print("Index post request")
url = request.form['url']
print("index: ", url)
session['url'] = url
return redirect(url_for('index'))
return render_template('index.html')
@app.route('/video_feed')
def video_feed():
url = session.get('url', None)
print("video feed: ", url)
if url is None:
return redirect(url_for('homepage'))
return Response(VIDEO.show(url), mimetype='multipart/x-mixed-replace; boundary=frame')
# * Button requests
@app.route("/request_preview_switch")
def request_preview_switch():
VIDEO.preview = not VIDEO.preview
print("*"*10, VIDEO.preview)
return "nothing"
@app.route("/request_flipH_switch")
def request_flipH_switch():
VIDEO.flipH = not VIDEO.flipH
print("*"*10, VIDEO.flipH)
return "nothing"
@app.route("/request_run_model_switch")
def request_run_model_switch():
VIDEO.detect = not VIDEO.detect
print("*"*10, VIDEO.detect)
return "nothing"
@app.route('/update_slider_value', methods=['POST'])
def update_slider_value():
slider_value = request.form['sliderValue']
VIDEO.confidence = slider_value
return 'OK'
@app.route('/stop_process')
def stop_process():
print("Process stop Request")
global stop_flag
stop_flag = True
return 'Process Stop Request'
@socketio.on('connect')
def test_connect():
print('Connected')
if __name__ == "__main__":
socketio.run(app, debug=True)