File size: 1,494 Bytes
db2db2a
 
665e8bb
 
 
 
 
db2db2a
 
540c05e
db2db2a
 
 
7312439
 
db2db2a
 
 
 
 
 
 
 
 
 
40ea044
 
 
665e8bb
db2db2a
40ea044
db2db2a
665e8bb
db2db2a
 
 
 
 
 
 
 
 
 
 
 
 
540c05e
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
import os
import time
import requests
from PIL import Image
from io import BytesIO

from fastapi import APIRouter, HTTPException
from scripts.data_model import ImageInput, ImageOutput
from utils.pipeline import load_model
from utils.log import logger

router = APIRouter()

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  
MODEL_PATH = os.path.join(BASE_DIR, "ml-models", "vit-human-pose-classification/")

@router.post(
    "/image_classification",
    response_model=ImageOutput,
    summary="Image Classification",
    description="Classify the image using a pre-trained model."
)
def image_classification(input: ImageInput)-> ImageOutput:
    try:
        pipe = load_model(MODEL_PATH, is_image_model=True)
        image = input.url
        
        logger.info(f"Image URLs: {image[-1]}")
        
        start = time.time()
        output = pipe(image[-1])
        end = time.time()
        
        prediction_time = int((end-start)*1000)
        labels_and_scores = [{"label": x['label'], "score": x['score']} for x in output]

        return ImageOutput(
            user_id=input.user_id,
            url=input.url,
            model_name="vit-human-pose-classification",
            labels=[x['label'] for x in labels_and_scores],
            scores=[x['score'] for x in labels_and_scores],
            prediction_time=prediction_time
        )
   
    except Exception as e:
        return {"error": f"Failed to process image classification: {str(e)}"}, 500