From fc9c49b5f323d56e2b8f1bcfb35edc16895a93dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=A7=84=ED=98=84?= Date: Wed, 18 Sep 2024 01:08:40 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20get=5Fmodel=5Finfo=20API=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ai/app/api/yolo/model.py | 14 ++++++++++++-- ai/app/services/load_model.py | 17 ++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/ai/app/api/yolo/model.py b/ai/app/api/yolo/model.py index e9517df..403faac 100644 --- a/ai/app/api/yolo/model.py +++ b/ai/app/api/yolo/model.py @@ -1,13 +1,23 @@ from fastapi import APIRouter, HTTPException from schemas.model_create_request import ModelCreateRequest from services.init_model import create_pretrained_model, create_default_model +from services.load_model import load_model router = APIRouter() # modelType(detection/segmentation/classification), (default, pretrained), labelCategories @router.get("/info") -def get_model_info(project_id:int, model_path:str): - pass +def get_model_info(model_path:str): + try: + model = load_model(model_path=model_path) + except FileNotFoundError: + HTTPException(status_code=404, + detail= f"Cannot found model") + except Exception as e: + raise HTTPException(status_code=500, detail="model load exception: " + str(e)) + pretrained = model.names == {0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'} + + return {"type": model.task, "pretrained":pretrained, "labelCategories":model.names} # @router.get("/list") diff --git a/ai/app/services/load_model.py b/ai/app/services/load_model.py index 8693bd8..3359ba2 100644 --- a/ai/app/services/load_model.py +++ b/ai/app/services/load_model.py @@ -54,4 +54,19 @@ def load_segmentation_model(model_path: str = "test-data/model/yolov8n-seg.pt", model.to("cpu") else: model.to(device) - return model \ No newline at end of file + return model + +def load_model(model_path: str): + if not os.path.exists(model_path): + raise FileNotFoundError(f"Model file not found at path: {model_path}") + + try: + model = YOLO(model_path) + if (torch.cuda.is_available()): + model.to("cuda") + print("gpu 사용") + else: + model.to("cpu") + return model + except: + raise Exception("YOLO model conversion failed: Unsupported architecture or invalid configuration.") \ No newline at end of file