-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
53 lines (42 loc) · 1.5 KB
/
app.py
File metadata and controls
53 lines (42 loc) · 1.5 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
from flask import Flask, request, jsonify
from io import BytesIO
from PIL import Image
import requests
import numpy as np
import logging
from mmdet.apis import inference_detector, init_detector
app = Flask(__name__)
logger = logging.getLogger("gunicorn.error")
# Load the model and configurations
model = init_detector(config='faster_rcnn_config.py', checkpoint='mmdetection/checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth', device='cuda:1')
# Function to process image from URL and get the best class
def url_to_best_class(image):
result = inference_detector(model, image)
max_score = -1
best_class = None
for bbox in result:
if bbox.shape[0] != 0:
class_score = bbox[0, 4]
if class_score > max_score:
max_score = class_score
best_class = int(bbox[0, 0])
return best_class
# Route to accept image URL and process it
@app.route("/process_image", methods=['POST'])
def process_image():
if request.method == 'POST':
# Get the image URL from the JSON request with key 'image_url'
image_file = request.files['image']
best_class = url_to_best_class(image_file)
return jsonify({
{
"header": {},
"body": {
"best_class": best_class,
}
}
})
else:
return 'Backend-server Connect'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8081, debug=True)