-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
167 lines (120 loc) · 4.65 KB
/
app.py
File metadata and controls
167 lines (120 loc) · 4.65 KB
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
import argparse
import io
import urllib.request
import os
import datetime
from flask import Flask, request
from PIL import Image
import torch
import config
import pymysql
import json
import firebase_admin
from firebase_admin import credentials, storage
from yolov5.detect import run
cred = credentials.Certificate("./serviceAccountKey.json")
firebase_admin.initialize_app(cred, {'storageBucket': config.fb_storage_bucket})
bucket = storage.bucket()
app = Flask(__name__)
models = {}
conn = pymysql.connect(
host = config.db['host'],
port = config.db['port'],
user = config.db['user'],
password = config.db['password'],
db = config.db['database']
)
bug_list = {
"cockroach": 1,
"scutigeridae": 2,
"centipede": 4,
"diplopoda": 5,
"silverfish": 6,
"dermaptera": 7
}
# API - Search
@app.route("/model/search/<model>", methods=['POST'])
def predict(model):
if request.method != 'POST':
return
if request.files.get('image'):
im_file = request.files['image']
im_bytes = im_file.read()
im = Image.open(io.BytesIO(im_bytes))
print("image ok")
if model in models:
results = models[model](im, size=320) # reduce size=320 for faster inference
json_data = {}
if (len(results.pandas().xyxy[0]) == 0):
json_data["species"] = "발견된 해충이 없음"
json_data["description"] = ""
else:
r = json.loads(results.pandas().xyxy[0]["name"].to_json(orient="records"))
if(r[0] in bug_list):
bug_id = bug_list[r[0]]
sql = "select * from bug where id = %s"
cursor = conn.cursor()
cursor.execute(sql, bug_id)
info = cursor.fetchone()
species = info[1]
description = info[2]
json_data["species"] = species
json_data["description"] = description
conn.commit()
return json_data
return "fail"
# API - Create & Save Scenario for Report
@app.route("/model/video", methods=['POST'])
def analyze2():
if request.method != 'POST':
return
created_at = datetime.datetime.now()
params = request.get_json()
user_id = params["user_id"]
video_url = params["url"]
video_path = os.path.join('./videos', 'input_video.mp4')
urllib.request.urlretrieve(video_url, video_path)
try :
result = run(weights='./yolov5/runs/train/model_v3/weights/best.pt', source=video_path)
img_dir_path = result[0]
object_name = result[1]
# 영상 분석 결과, 발견된 해충 없는 경우
if(object_name not in bug_list):
return "fail"
bug_id = bug_list[object_name]
# Firebase Storage에 이미지 업로드
firebase_img_url = uploadImgToFirebase(img_dir_path, user_id)
# RDS에 Scenario INSERT
return saveScenario(user_id, bug_id, firebase_img_url, created_at)
except:
return "fail"
# Firebase Storage에 결과 이미지 업로드 후 url 경로 리턴
def uploadImgToFirebase(img_dir_path, user_id):
local_file_path = "./yolov5/runs/detect/" + img_dir_path + "/input_video.png"
remote_file_path = "Report/" + str(user_id) + "/" + img_dir_path
blob = bucket.blob(remote_file_path)
blob.upload_from_filename(filename=local_file_path, content_type='image/png') # Uplad Image to Firebase Strage
blob.make_public() # Get Public URL from blob
return blob.public_url
# RDS에 Scenario 저장
def saveScenario(user_id, bug_id, img_url, created_at):
cursor = conn.cursor()
cursor.execute(
"INSERT INTO scenario (user_id, bug_id, image, created_at) VALUES(%s, %s, %s, %s)",
(user_id, bug_id, img_url, created_at)
)
conn.commit()
return "success"
# Config 설정
def create_app():
app.config.from_pyfile("config.py")
return app
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Flask API exposing YOLOv5 model')
parser.add_argument('--port', default=5000, type=int, help='port number')
parser.add_argument('--model', nargs='+', default=['yolov5s'], help='model(s) to run, i.e. --model yolov5n yolov5s')
opt = parser.parse_args()
for m in opt.model:
models[m] = torch.hub.load('ultralytics/yolov5', 'custom', './yolov5/runs/train/model_v3/weights/best.pt', force_reload=True, skip_validation=True)
create_app().run('0.0.0.0', port=5000, debug=False)
#app.run(host='0.0.0.0', port=opt.port) # debug=True causes Restarting with stat