-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
318 lines (281 loc) · 9.96 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import base64
import os
import time
import cv2
import numpy as np
import torch
from flask import Flask, render_template, request, send_from_directory, url_for
from flask_socketio import SocketIO, emit
from ultralytics import YOLO
model = None
model_train = None
full_path = os.path.abspath(os.curdir)
# NOTE: 元の写真を引数として受け取り、バウンディングボックスを描画し、写真を保存する関数
def rial_time_make_box_and_photo(img, error_count):
global count, model_number
im1 = img
im1_cv = np.array(im1)
im1_cv = im1_cv[:, :, ::-1].copy()
boolean = False
items_train = []
# NOTE: 学習済みモデルを使用する場合
if model_number == 2:
results_train = model_train.predict(source=im1)
items_train = results_train[0]
cls_train = -1
score_train = 0
label_train = ""
x1_train = -1
y1_train = -1
x2_train = -1
y2_train = -1
cls_normal = -1
score_normal = 0
label_normal = ""
x1_normal = -1
y1_normal = -1
x2_normal = -1
y2_normal = -1
for item in items_train:
cls = int(item.boxes.cls) # cls, (N, 1)
cls_train = int(item.boxes.cls)
label_train = item.names[int(cls)]
score_train = item.boxes.conf.to("cuda").cpu().numpy()[0]
x1_train, y1_train, x2_train, y2_train = (
item.boxes.xyxy.to("cuda").cpu().numpy()[0]
)
print(
"train",
cls_train,
score_train,
label_train,
x1_train,
y1_train,
x2_train,
y2_train,
)
results = model.predict(source=im1) # save plotted images
items_normal = results[0]
for item in items_normal:
cls = int(item.boxes.cls) # cls, (N, 1)
cls_normal = int(item.boxes.cls) # cls, (N, 1)
label_normal = item.names[int(cls)]
score_normal = (
item.boxes.conf.to("cuda").cpu().numpy()[0]
) # confidence score, (N, 1)
x1_normal, y1_normal, x2_normal, y2_normal = (
item.boxes.xyxy.to("cuda").cpu().numpy()[0]
) # box with xyxy format, (N, 4)
print(
"normal",
cls_normal,
score_normal,
label_normal,
x1_normal,
y1_normal,
x2_normal,
y2_normal,
)
if cls_train == 0 and 0.7 <= score_train:
boolean = False
if error_count == 0:
cv2.rectangle(
im1_cv,
(int(x1_train), int(y1_train)),
(int(x2_train), int(y2_train)),
(0, 255, 0),
2,
)
else:
# NOTE: error_countが0でない場合は、赤色の枠を表示
cv2.rectangle(
im1_cv,
(int(x1_train), int(y1_train)),
(int(x2_train), int(y2_train)),
(0, 0, 255),
2,
)
return {
"boolean": boolean,
"img": im1_cv,
"label": label_normal,
"score": score_train,
}
else:
if cls_normal != 0 or 0.2 <= score_normal <= 0.7:
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Add this line
cv2.imwrite(
"{}/data/train/photos/".format(full_path)
+ str(round(time.time() * 1000))
+ ".jpg",
img,
)
count += 1
boolean = True
cv2.rectangle(
im1_cv,
(int(x1_normal), int(y1_normal)),
(int(x2_normal), int(y2_normal)),
(0, 255, 0),
2,
)
elif score_normal < 0.2:
pass
else:
boolean = False
if error_count == 0:
cv2.rectangle(
im1_cv,
(int(x1_normal), int(y1_normal)),
(int(x2_normal), int(y2_normal)),
(0, 255, 0),
2,
)
else:
# NOTE: error_countが0でない場合は、赤色の枠を表示
cv2.rectangle(
im1_cv,
(int(x1_normal), int(y1_normal)),
(int(x2_normal), int(y2_normal)),
(0, 0, 255),
2,
)
return {
"boolean": boolean,
"img": im1_cv,
"label": label_normal,
"score": score_normal,
}
# NOTE: 学習済みモデルを使用しない場合
if len(items_train) == 0:
results = model.predict(source=im1) # save plotted images
items = results[0]
for item in items:
cls = int(item.boxes.cls) # cls, (N, 1)
label = item.names[int(cls)]
score = (
item.boxes.conf.to("cuda").cpu().numpy()[0]
) # confidence score, (N, 1)
x1, y1, x2, y2 = (
item.boxes.xyxy.to("cuda").cpu().numpy()[0]
) # box with xyxy format, (N, 4)
print(cls, score, label, x1, y1, x2, y2)
if cls != 0 or 0.2 <= score <= 0.7:
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Add this line
cv2.imwrite(
"{}/data/train/photos/".format(full_path)
+ str(round(time.time() * 1000))
+ ".jpg",
img,
)
count += 1
boolean = True
cv2.rectangle(
im1_cv, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2
)
elif score < 0.2:
pass
else:
boolean = False
if error_count == 0:
cv2.rectangle(
im1_cv, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2
)
else:
# NOTE: error_countが0でない場合は、赤色の枠を表示
cv2.rectangle(
im1_cv, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2
)
return {"boolean": boolean, "img": im1_cv, "label": label, "score": score}
if len(items) == 0:
return {"boolean": True, "img": im1_cv, "label": "None", "score": 0}
app = Flask(__name__)
app.config["SECRET_KEY"] = "secret!"
socketio = SocketIO(app)
model_number = 0
@app.route("/")
def index():
return render_template("index.html")
@app.route("/select_model", methods=["GET"])
def select_model():
global model_number, model, model_train
selected_model = request.args.get("model", "")
if selected_model == "second":
model_number = 2
print(torch.cuda.memory_allocated())
model = YOLO("./yolov8n.pt")
model = model.to("cuda")
print(torch.cuda.memory_allocated())
model_train = YOLO("./yolov8_train.pt")
model_train = model_train.to("cuda")
print(torch.cuda.memory_allocated())
else:
model_number = 0
model = YOLO("./yolov8n.pt")
model = model.to("cuda")
# モデルの番号をコンソールに出力
print("選択されたモデルの番号:", model_number)
return render_template("select_model.html")
@app.route("/start_count")
def start_count():
# NOTE: カウントの初期化をおこなわないと、リスタートの際にカウントが残ってしまう
global count, flame, error_count
count = 0
flame = 0
error_count = 0
print("start_count", count, flame, error_count)
return render_template("start_count.html")
@app.route("/game")
def game():
return render_template("game.html")
@app.route("/gameover")
def gameover():
return render_template("gameover.html")
@app.route("/gameclear")
def gameclear():
return render_template("gameclear.html")
@app.route("/music/<path:filename>")
def play(filename):
return send_from_directory("music", filename)
flame = 0
error_count = 0
@socketio.on("image")
def handle_image(image):
global count, error_count
# 画像データをデコード
image_data = base64.b64decode(image.split(",")[1])
# OpenCVで画像を読み込む
nparr = np.frombuffer(image_data, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Add this line
dicts = rial_time_make_box_and_photo(img, error_count)
boolean = dicts["boolean"]
bounding_box_img = dicts["img"]
label = dicts["label"]
score = dicts["score"]
if boolean == False:
error_count += 1
else:
error_count = 0
if error_count >= 5:
print("終了")
emit(
"redirect", {"url": url_for("gameover")}
) # クライアントにリダイレクトを指示
return # この場合、処理を終了します
emit(
"label_and_score",
{"label": label, "score": float(score), "error_count": error_count},
)
count += 1
# 反転した画像をエンコードしてクライアントに送信
_, buffer = cv2.imencode(".jpg", bounding_box_img)
inverted_image_data = base64.b64encode(buffer)
inverted_image_str = "data:image/jpeg;base64," + inverted_image_data.decode("utf-8")
emit("processed_image", inverted_image_str)
if __name__ == "__main__":
# NOTE: カウントの初期化
count = 0
socketio.run(app, debug=True)