johnlockejrr commited on
Commit
59259b5
·
verified ·
1 Parent(s): d038733

Update app.py for YOLO11 segmentation models

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -8,11 +8,11 @@ from ultralytics import YOLO
8
 
9
  # Define models
10
  MODEL_OPTIONS = {
11
- "YOLOv11-Nano": "medieval-yolov11n.pt",
12
- "YOLOv11-Small": "medieval-yolov11s.pt",
13
- "YOLOv11-Medium": "medieval-yolov11m.pt",
14
- "YOLOv11-Large": "medieval-yolov11l.pt",
15
- "YOLOv11-XLarge": "medieval-yolov11x.pt"
16
  }
17
 
18
  # Dictionary to store loaded models
@@ -21,14 +21,14 @@ models: Dict[str, YOLO] = {}
21
  # Load all models
22
  for name, model_file in MODEL_OPTIONS.items():
23
  model_path = hf_hub_download(
24
- repo_id="biglam/medieval-manuscript-yolov11",
25
  filename=model_file
26
  )
27
  models[name] = YOLO(model_path)
28
 
29
  # Create annotators
30
  LABEL_ANNOTATOR = sv.LabelAnnotator(text_color=sv.Color.BLACK)
31
- BOX_ANNOTATOR = sv.BoxAnnotator()
32
 
33
  def detect_and_annotate(
34
  image: np.ndarray,
@@ -50,12 +50,14 @@ def detect_and_annotate(
50
  boxes = results.boxes.xyxy.cpu().numpy()
51
  confidence = results.boxes.conf.cpu().numpy()
52
  class_ids = results.boxes.cls.cpu().numpy().astype(int)
 
53
 
54
  # Create Detections object
55
  detections = sv.Detections(
56
  xyxy=boxes,
57
  confidence=confidence,
58
- class_id=class_ids
 
59
  )
60
 
61
  # Create labels with confidence scores
@@ -65,9 +67,9 @@ def detect_and_annotate(
65
  in zip(class_ids, confidence)
66
  ]
67
 
68
- # Annotate image
69
  annotated_image = image.copy()
70
- annotated_image = BOX_ANNOTATOR.annotate(scene=annotated_image, detections=detections)
71
  annotated_image = LABEL_ANNOTATOR.annotate(scene=annotated_image, detections=detections, labels=labels)
72
 
73
  return annotated_image
@@ -142,4 +144,4 @@ with gr.Blocks() as demo:
142
  )
143
 
144
  if __name__ == "__main__":
145
- demo.launch(debug=True, show_error=True)
 
8
 
9
  # Define models
10
  MODEL_OPTIONS = {
11
+ "YOLOv11-Nano": "medieval-yolo11n-seg.pt",
12
+ "YOLOv11-Small": "medieval-yolo11s-seg.pt",
13
+ "YOLOv11-Medium": "medieval-yolo11m-seg.pt",
14
+ "YOLOv11-Large": "medieval-yolo11l-seg.pt",
15
+ "YOLOv11-XLarge": "medieval-yolo11x-seg.pt"
16
  }
17
 
18
  # Dictionary to store loaded models
 
21
  # Load all models
22
  for name, model_file in MODEL_OPTIONS.items():
23
  model_path = hf_hub_download(
24
+ repo_id="johnlockejrr/medieval-manuscript-yolov11-seg",
25
  filename=model_file
26
  )
27
  models[name] = YOLO(model_path)
28
 
29
  # Create annotators
30
  LABEL_ANNOTATOR = sv.LabelAnnotator(text_color=sv.Color.BLACK)
31
+ MASK_ANNOTATOR = sv.MaskAnnotator()
32
 
33
  def detect_and_annotate(
34
  image: np.ndarray,
 
50
  boxes = results.boxes.xyxy.cpu().numpy()
51
  confidence = results.boxes.conf.cpu().numpy()
52
  class_ids = results.boxes.cls.cpu().numpy().astype(int)
53
+ masks = results.masks.data.cpu().numpy() if results.masks is not None else None
54
 
55
  # Create Detections object
56
  detections = sv.Detections(
57
  xyxy=boxes,
58
  confidence=confidence,
59
+ class_id=class_ids,
60
+ mask=masks
61
  )
62
 
63
  # Create labels with confidence scores
 
67
  in zip(class_ids, confidence)
68
  ]
69
 
70
+ # Annotate image with masks and labels
71
  annotated_image = image.copy()
72
+ annotated_image = MASK_ANNOTATOR.annotate(scene=annotated_image, detections=detections)
73
  annotated_image = LABEL_ANNOTATOR.annotate(scene=annotated_image, detections=detections, labels=labels)
74
 
75
  return annotated_image
 
144
  )
145
 
146
  if __name__ == "__main__":
147
+ demo.launch(debug=True, show_error=True)