File size: 835 Bytes
65aa0cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from .provider import model
from io import BytesIO
from PIL import Image


def detect_object(image: bytes):
    # Convert bytes to PIL image
    image = Image.open(BytesIO(image))

    detections = model.predict(image)

    # Format detections
    bboxs = detections[0].boxes.data
    labels = detections[0].names

    # Get boxs result
    boxs_result = []
    for box in bboxs:
        result_conf = box[4].item()
        if result_conf > model.conf:
            boxs_result.append({
                "class_name": labels[int(box[5].item())],
                "confidence": result_conf,
                "xmin": box[0].item(),
                "ymin": box[1].item(),
                "xmax": box[2].item(),
                "ymax": box[3].item()
            })

    print(boxs_result)

    return {
        "detections": boxs_result
    }