fahmiaziz98 commited on
Commit
665e8bb
·
1 Parent(s): 32814be

frist commit

Browse files
Files changed (1) hide show
  1. router/image_clf.py +19 -4
router/image_clf.py CHANGED
@@ -1,7 +1,10 @@
1
  import os
2
  import time
3
- from typing import Union
4
- from fastapi import APIRouter
 
 
 
5
  from scripts.data_model import ImageInput, ImageOutput
6
  from utils.pipeline import load_model
7
 
@@ -10,6 +13,16 @@ router = APIRouter()
10
  BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
11
  MODEL_PATH = os.path.join(BASE_DIR, "ml-models", "vit-human-pose-classification/")
12
 
 
 
 
 
 
 
 
 
 
 
13
  @router.post(
14
  "/image_classification",
15
  response_model=ImageOutput,
@@ -19,11 +32,13 @@ MODEL_PATH = os.path.join(BASE_DIR, "ml-models", "vit-human-pose-classification/
19
  def image_classification(input: ImageInput)-> ImageOutput:
20
  try:
21
  pipe = load_model(MODEL_PATH, is_image_model=True)
 
 
22
  start = time.time()
23
- output = pipe(input.url)
24
  end = time.time()
 
25
  prediction_time = int((end-start)*1000)
26
-
27
  labels_and_scores = [{"label": x['label'], "score": x['score']} for x in output]
28
 
29
  return ImageOutput(
 
1
  import os
2
  import time
3
+ import requests
4
+ from PIL import Image
5
+ from io import BytesIO
6
+
7
+ from fastapi import APIRouter, HTTPException
8
  from scripts.data_model import ImageInput, ImageOutput
9
  from utils.pipeline import load_model
10
 
 
13
  BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
14
  MODEL_PATH = os.path.join(BASE_DIR, "ml-models", "vit-human-pose-classification/")
15
 
16
+ def download_image(url):
17
+ """Download and open an image from a URL."""
18
+ try:
19
+ response = requests.get(url)
20
+ response.raise_for_status()
21
+ return Image.open(BytesIO(response.content)).convert("RGB")
22
+ except Exception as e:
23
+ raise HTTPException(status_code=400, detail=f"Failed to download image: {e}")
24
+
25
+
26
  @router.post(
27
  "/image_classification",
28
  response_model=ImageOutput,
 
32
  def image_classification(input: ImageInput)-> ImageOutput:
33
  try:
34
  pipe = load_model(MODEL_PATH, is_image_model=True)
35
+ image = download_image(input.url)
36
+
37
  start = time.time()
38
+ output = pipe(image)
39
  end = time.time()
40
+
41
  prediction_time = int((end-start)*1000)
 
42
  labels_and_scores = [{"label": x['label'], "score": x['score']} for x in output]
43
 
44
  return ImageOutput(