-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
70 lines (50 loc) · 1.92 KB
/
Copy pathapp.py
File metadata and controls
70 lines (50 loc) · 1.92 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
from flask import Flask, request, jsonify
from flask_cors import CORS,cross_origin
from PIL import Image
import pathlib
import numpy as np
from fastai.learner import load_learner
from icevision.all import *
from icevision.models import *
# from fastai.vision import load_image
app = Flask(__name__)
CORS(app, support_credentials=True)
# load the classifier
learn = load_learner('export.pkl')
classes = learn.dls.vocab
# load the object detection
modelPath = pathlib.Path('Fish_checkpoint.pth')
checkpoint_and_model = model_from_checkpoint(modelPath)
def predict_single(img_file):
img_file = Image.open(img_file)
model = checkpoint_and_model["model"]
model_type = checkpoint_and_model["model_type"]
class_map = checkpoint_and_model["class_map"]
img_size = checkpoint_and_model["img_size"]
img_size = checkpoint_and_model["img_size"]
valid_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(img_size), tfms.A.Normalize()])
# img_file = get_image_files(img_file)
preds = model_type.end2end_detect(img_file, valid_tfms, model, class_map=class_map, detection_threshold=0.5)
result = [];
for bbox in preds['detection']['bboxes']:
img1 = img_file.crop(bbox.xyxy)
# img_byte_arr = io.BytesIO()
# img1.save(img_byte_arr, format='PNG')
# img_byte_arr = img_byte_arr.getvalue()
prediction = learn.predict(np.asarray(img1))
probs_list = prediction[2].numpy()
result.append(
{
'bbox': bbox.xywh,
'category': classes[prediction[1].item()],
'probs': {c: round(float(probs_list[i]), 5) for (i, c) in enumerate(classes)},
'originalWidth': img_file.width,
}
)
return result;
# route for prediction
@app.route('/predict', methods=['POST'])
def predict():
return jsonify(predict_single(request.files['image'].stream))
if __name__ == '__main__':
app.run()