BOWOWEEEEE commited on
Commit
2794086
·
verified ·
1 Parent(s): df5eb9e

fix: detect/preview도 class_mapping.json으로 약품명 변환

Browse files
Files changed (1) hide show
  1. api/detect_preview.py +11 -1
api/detect_preview.py CHANGED
@@ -1,4 +1,6 @@
1
  import io
 
 
2
  from typing import Any
3
 
4
  from fastapi import APIRouter, File, Header, HTTPException, Request, UploadFile
@@ -8,6 +10,14 @@ from services.auth import auth
8
 
9
  router = APIRouter()
10
 
 
 
 
 
 
 
 
 
11
 
12
  @router.post("/detect/preview")
13
  async def detect_preview(
@@ -45,7 +55,7 @@ async def detect_preview(
45
  class_id = int(box.cls[0].item())
46
  confidence = float(box.conf[0].item())
47
  x1, y1, x2, y2 = [float(v) for v in box.xyxy[0].tolist()]
48
- name = result.names.get(class_id, f"class_{class_id}")
49
  detected_drugs.append({
50
  "name": name,
51
  "confidence": confidence,
 
1
  import io
2
+ import json
3
+ from pathlib import Path
4
  from typing import Any
5
 
6
  from fastapi import APIRouter, File, Header, HTTPException, Request, UploadFile
 
10
 
11
  router = APIRouter()
12
 
13
+ _CLASS_MAPPING: dict[int, str] = {}
14
+ try:
15
+ _mapping_path = Path(__file__).parent.parent / "configs" / "class_mapping.json"
16
+ with open(_mapping_path) as _f:
17
+ _CLASS_MAPPING = {e["class_id"]: e["name"] for e in json.load(_f)["classes"]}
18
+ except Exception:
19
+ pass
20
+
21
 
22
  @router.post("/detect/preview")
23
  async def detect_preview(
 
55
  class_id = int(box.cls[0].item())
56
  confidence = float(box.conf[0].item())
57
  x1, y1, x2, y2 = [float(v) for v in box.xyxy[0].tolist()]
58
+ name = _CLASS_MAPPING.get(class_id) or result.names.get(class_id, f"class_{class_id}")
59
  detected_drugs.append({
60
  "name": name,
61
  "confidence": confidence,