Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,70 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from ultralytics import YOLO
|
| 3 |
-
import torch
|
| 4 |
-
|
| 5 |
-
model_id = "mosesb/best-comic-panel-detection"
|
| 6 |
-
model = YOLO("best.pt")
|
| 7 |
-
|
| 8 |
-
def detect_panels(pil_image):
|
| 9 |
-
"""
|
| 10 |
-
Takes a PIL image, runs YOLOv12 object detection
|
| 11 |
-
and returns the annotated image with bounding boxes.
|
| 12 |
-
"""
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# --- Gradio Interface ---
|
| 26 |
-
title = "YOLOv12 Comic Panel Detection"
|
| 27 |
-
description = """
|
| 28 |
-
This demo showcases a **YOLOv12 object detection model** that has been fine-tuned to detect panels in comic book pages.
|
| 29 |
-
Upload an image of a comic page, and the model will draw bounding boxes around each detected panel.
|
| 30 |
-
This can be a useful first step for downstream tasks like Optical Character Recognition (OCR) or character analysis within comics.
|
| 31 |
-
"""
|
| 32 |
-
|
| 33 |
-
article = f"""
|
| 34 |
-
<div style='text-align: center;'>
|
| 35 |
-
<p style='text-align: center'>Model loaded from <a href='https://huggingface.co/{model_id}' target='_blank'>{model_id}</a></p>
|
| 36 |
-
<p style='text-align: center'>For more details on the training process, check out the project repository: <a href='https://github.com/mosesab/YOLOV12-Comic-Panel-Detection/blob/main/comic-boundary-detection.ipynb' target='_blank'>Comic Boundary Detection</a></p>
|
| 37 |
-
</div>
|
| 38 |
-
"""
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
).launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
model_id = "mosesb/best-comic-panel-detection"
|
| 6 |
+
model = YOLO("best.pt")
|
| 7 |
+
|
| 8 |
+
def detect_panels(pil_image, conf_threshold, iou_threshold):
|
| 9 |
+
"""
|
| 10 |
+
Takes a PIL image and thresholds, runs YOLOv12 object detection,
|
| 11 |
+
and returns the annotated image with bounding boxes.
|
| 12 |
+
"""
|
| 13 |
+
# Run inference on the image with the specified thresholds
|
| 14 |
+
results = model.predict(pil_image, conf=conf_threshold, iou=iou_threshold, verbose=False)
|
| 15 |
+
annotated_image = results[0].plot()
|
| 16 |
+
|
| 17 |
+
# Gradio's gr.Image component expects an RGB image. The .plot() method
|
| 18 |
+
# returns a BGR image, so we convert it.
|
| 19 |
+
annotated_image_rgb = annotated_image[..., ::-1]
|
| 20 |
+
|
| 21 |
+
return annotated_image_rgb
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# --- Gradio Interface ---
|
| 26 |
+
title = "YOLOv12 Comic Panel Detection"
|
| 27 |
+
description = """
|
| 28 |
+
This demo showcases a **YOLOv12 object detection model** that has been fine-tuned to detect panels in comic book pages.
|
| 29 |
+
Upload an image of a comic page, and the model will draw bounding boxes around each detected panel.
|
| 30 |
+
This can be a useful first step for downstream tasks like Optical Character Recognition (OCR) or character analysis within comics.
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
article = f"""
|
| 34 |
+
<div style='text-align: center;'>
|
| 35 |
+
<p style='text-align: center'>Model loaded from <a href='https://huggingface.co/{model_id}' target='_blank'>{model_id}</a></p>
|
| 36 |
+
<p style='text-align: center'>For more details on the training process, check out the project repository: <a href='https://github.com/mosesab/YOLOV12-Comic-Panel-Detection/blob/main/comic-boundary-detection.ipynb' target='_blank'>Comic Boundary Detection</a></p>
|
| 37 |
+
</div>
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
# Define the input components for the Gradio interface
|
| 41 |
+
inputs = [
|
| 42 |
+
gr.Image(type="pil", label="Upload Comic Page Image"),
|
| 43 |
+
gr.Slider(
|
| 44 |
+
minimum=0.0,
|
| 45 |
+
maximum=1.0,
|
| 46 |
+
value=0.25, # The default confidence threshold in ultralytics
|
| 47 |
+
step=0.05,
|
| 48 |
+
label="Confidence Threshold",
|
| 49 |
+
info="Filters detections. Only boxes with confidence above this value will be shown."
|
| 50 |
+
),
|
| 51 |
+
gr.Slider(
|
| 52 |
+
minimum=0.0,
|
| 53 |
+
maximum=1.0,
|
| 54 |
+
value=0.7, # The default IoU threshold in ultralytics
|
| 55 |
+
step=0.05,
|
| 56 |
+
label="IoU Threshold",
|
| 57 |
+
info="Controls merging of overlapping boxes. Higher values allow more overlap."
|
| 58 |
+
)
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
gr.Interface(
|
| 62 |
+
fn=detect_panels,
|
| 63 |
+
inputs=inputs,
|
| 64 |
+
outputs=gr.Image(type="pil", label="Detected Panels"),
|
| 65 |
+
title=title,
|
| 66 |
+
description=description,
|
| 67 |
+
article=article,
|
| 68 |
+
examples=[ "aura_farmer_1.jpg", "aura_farmer_2.jpg", "aura_farmer_3.jpg", "aura_farmer_4.jpg" ],
|
| 69 |
+
allow_flagging="auto"
|
| 70 |
).launch()
|