-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
60 lines (43 loc) · 1.48 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
from flask import Flask, jsonify, request
import torch
import sys
import os
import re
app = Flask(__name__)
model = torch.hub.load("ultralytics/yolov5", "custom", path="./yolov5_exp11_best.pt",
force_reload=False) # or yolov5n - yolov5x6, custom
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'POST':
data = request.json
img = data['imagePath']
# Inference
results = model(img)
# Checks Operating system
if sys.platform.startswith('win'):
img_short = img.split('\\')[-1]
elif sys.platform.startswith('darwin'):
img_short = img.split('/')[-1]
elif sys.platform.startswith('linux'):
img_short = img.split('/')[-1]
img_folder = img_short.split('.')[0]
img_name = re.sub(r'\..+$', '', img_short)
# Results
# or .show(), .save(), .crop(), .pandas(), .print() etc
conditions = results.pandas().xyxy[0].to_json(orient = "records")
results.save(save_dir=f'./{img_folder}/')
if sys.platform.startswith('win'):
path = f'{os.getcwd()}\{img_folder}\{img_short}'
else:
path = f'/{img_folder}/{img_short}'
return jsonify({
'status': "OK",
'imagePath': path,
'conditions': conditions
})
return({
"status": "OK",
"msg": "AI server is running"
})
if __name__ == "__main__":
app.run()