File size: 953 Bytes
4986f6d
b245237
aaa1b64
 
7bf08cb
7cfde67
4986f6d
b245237
4986f6d
 
 
 
 
a5c51fb
4986f6d
0890b20
 
aaa1b64
 
7cfde67
 
4f3205b
4986f6d
 
 
 
 
 
 
 
 
 
aaa1b64
 
 
 
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
import cv2

from fastapi import APIRouter, File, Response
from app.detector import detector
from mmcv import imfrombytes
from app import logger

router = APIRouter(prefix="/image", tags=["Image"])


@router.post("")
async def handleImageRequest(

    file: bytes = File(...),

    threshold: float = 0.5,

):
    try:
        img = imfrombytes(file, cv2.IMREAD_COLOR)

        img = inference_image(img, threshold)
    except Exception as e:
        logger.error(e)
        return Response(content="Failed to read image", status_code=400)

    ret, jpeg = cv2.imencode(".jpg", img)

    if not ret:
        return Response(content="Failed to encode image", status_code=500)
    jpeg_bytes: bytes = jpeg.tobytes()

    return Response(content=jpeg_bytes, media_type="image/jpeg")


def inference_image(img, threshold):
    detector.set_conf_threshold(threshold)
    detector(img)
    return detector.draw_detections(img)