-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
270 lines (211 loc) · 8.66 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
import os
import os.path
import flask
import werkzeug
import cv2
from flask import Flask, jsonify, send_file, request
from flask_restful import Resource, Api, reqparse
from werkzeug.sansio.multipart import MultipartEncoder
from flask import redirect, url_for, send_from_directory, render_template
import json
from werkzeug.utils import secure_filename
import yolov5.detect
app = Flask(__name__)
api = Api(app)
class Upload(Resource):
def post(self):
parser = reqparse.RequestParser()
print("parser")
print(parser)
parser.add_argument('file', type=werkzeug.datastructures.FileStorage, location='files')
args = parser.parse_args()
print("args")
print(args)
file_object = args['file']
print(file_object)
if file_object is None:
return jsonify({'result': 'fail'})
else:
file_path = os.path.join(os.getcwd(), file_object.filename)
file_object.save(dst=file_object.filename)
print(file_path)
breed = yolov5.detect.run(weights="yolov5/runs/train/dogtest5_yolov5m_results2/weights/best.pt", source=file_path)
safety = yolov5.detect.run(weights="yolov5/runs/train/dogsafety/best_safety.pt", source=file_path)
muzzle = yolov5.detect.run(weights="yolov5/runs/train/dogmuzzle/dog-muzzle-best.pt", source=file_path)
# print("result", breed, safety, muzzle)
# 기본 세팅
result_breed = False
result_safety = False
result_muzzle = False
print("======result_breed======")
print(breed["result"])
count_breed_check1 = 0
count_breed_check2 = 0
count_breed_check3 = 0
count_breed_check4 = 0
for x in breed["result"]:
if "Rottweiler" in x:
count_breed_check1 = count_breed_check1 + 1
# result_muzzle = True
# print("result_muzzle = True")
elif "tosa" in x:
count_breed_check2 = count_breed_check2 + 1
# result_muzzle = True
# print("result_muzzle = True")
elif "dangerous_dog" in x:
count_breed_check3 = count_breed_check3 + 1
# result_muzzle = True
# print("result_muzzle = True")
else:
count_breed_check4 = count_breed_check4 + 1
# result_muzzle = False
# print("result_muzzle = False")
print("======result_breed222======")
print("count_breed_check1 ")
print(count_breed_check1)
print("count_breed_check2 ")
print(count_breed_check2)
print("count_breed_check3 ")
print(count_breed_check3)
print("count_breed_check4 ")
print(count_breed_check4)
max_count = max(count_breed_check1, count_breed_check2, count_breed_check3, count_breed_check4)
print("max_count ")
print(max_count)
print("======result_breed222======")
if max_count == count_breed_check1:
result_breed = True
print("result_breed = True")
elif max_count == count_breed_check2:
result_breed = True
print("result_breed = True")
elif max_count == count_breed_check3:
result_breed = True
print("result_breed = True")
else:
result_breed = False
print("result_breed = False")
print(breed["path"])
# img_breed = cv2.imread(breed["path"], cv2.IMREAD_COLOR)
# cv2.imshow('img_breed', img_breed)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
print("======result_breed======")
# for abc in breed['result']:
# if "Dog" in abc:
# print(abc)
#
# for abc in breed['result']:
# print(abc)
print("======result_safety======")
print(safety["result"])
count_safety_check1 = 0
count_safety_check2 = 0
for x in safety["result"]:
if "leash-detect-not" in x:
count_safety_check2 = count_safety_check2 + 1
else:
count_safety_check1 = count_safety_check1 + 1
print("======result_safety222======")
print("count_safety_check1 ")
print(count_safety_check1)
print("count_safety_check2 ")
print(count_safety_check2)
max_count_safety = max(count_safety_check1, count_safety_check2)
print("max_count_safety ")
print(max_count_safety)
print("======result_safety222======")
if max_count_safety == count_safety_check1:
result_safety = True
print("result_safety = True")
else:
result_safety = False
print("result_safety = False")
print(safety["path"])
# img_safety = cv2.imread(safety["path"], cv2.IMREAD_COLOR)
# cv2.imshow('img_safety', img_safety)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
print("======result_safety======")
print("======result_muzzle======")
print(muzzle["result"])
count_muzzle_check1 = 0
count_muzzle_check2 = 0
for x in muzzle["result"]:
if "no-muzzle" in x:
count_muzzle_check2 = count_muzzle_check2 + 1
else:
count_muzzle_check1 = count_muzzle_check1 + 1
print("======result_muzzle222======")
print("count_muzzle_check1 ")
print(count_muzzle_check1)
print("count_muzzle_check2 ")
print(count_muzzle_check2)
max_count_muzzle = max(count_muzzle_check1, count_muzzle_check2)
print("max_count_muzzle ")
print(max_count_muzzle)
print("======result_muzzle222======")
if max_count_muzzle == count_muzzle_check2:
result_muzzle = False
print("result_muzzle = True")
else:
result_muzzle = True
print("result_muzzle = False")
print(muzzle["path"])
# img_muzzle = cv2.imread(muzzle["path"], cv2.IMREAD_COLOR)
# cv2.imshow('img_muzzle', img_muzzle)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
print("======result_muzzle======")
# img = cv2.imread('lena.jpg', cv2.IMREAD_COLOR)
# return send_file(
# img_breed, mimetype='image/jpeg'
# # img_safety,
# # img_muzzle
# )
# return send_file(
# img_muzzle,
# as_attachment = True,
# attachment_filename = 'test.jpg',
# mimetype = 'image/jpeg'
# )
# m = MultipartEncoder(fields={'markdown': "> Block quotes are written like so in markdown.",
# 'toPersonEmail': '[email protected]',
# 'files': (img_muzzle, open(img_muzzle, 'rb'),
# 'image.png')})
#
# return (m.to_string(), {'Content-Type': m.content_type})
return jsonify({
'result': file_path,
'result_breed': result_breed,
'result_muzzle': result_muzzle,
'result_safety': result_safety,
'result_breed_imgPath': breed['path'],
'result_muzzle_imgPath': muzzle['path'],
'result_safety_imgPath': safety['path']
})
# return send_file(breed["path"])
api.add_resource(Upload, '/upload')
@app.route('/download/breed')
def breed():
json = request.get_json()
return send_file(json['path'])
@app.route('/download/muzzle')
def muzzle():
json = request.get_json()
return send_file(json['path'])
@app.route('/download/safety')
def safety():
json = request.get_json()
return send_file(json['path'])
@app.route('/')
def hello_world(): # put application's code here
return 'Hello World!'
class HelloWorld(Resource):
def get(self): # GET 요청시 리턴 값에 해당 하는 dict를 JSON 형태로 반환
return "test"
api.add_resource(HelloWorld, '/test')
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=80)
if __name__ == '__main__':
app.run()