-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
43 lines (34 loc) · 1.3 KB
/
server.py
File metadata and controls
43 lines (34 loc) · 1.3 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
from flask import Flask, request, Response
from PIL import Image
import io
import torch
import json
from requests_toolbelt import MultipartEncoder,MultipartDecoder
import os
app = Flask(__name__)
@app.route('/api/v1/trash', methods=['POST'])
def process_image():
if request.method == 'POST':
# YOLOv5 모델 로드
model = torch.hub.load('ultralytics/yolov5', 'custom', path='./best.pt')
# 멀티파트 이미지 파일 수신
image_file = request.files['multipartFile']
image = Image.open(image_file.stream)
# 이미지 처리 (YOLOv5 추론)
results = model(image)
results.save(filename)
# 결과에서 레이블 추출
labels = results.xyxy[0][:, -1].cpu().numpy()
label_names = list(set([results.names[int(label)] for label in labels]))
filename = './result/result.jpg'
body = MultipartEncoder(
fields={
"code": 200,
"message": "hi",
"trashAnalyzeImage": (os.path.basename(filename), open(filename, 'rb'), 'image'),
"trashInfoList": label_names
}
)
return Response(body.to_string(), mimetype=body.content_type)
if __name__ == '__main__':
app.run(debug=True, host='localhost', port=5000)