mosesb/best-comic-panel-detection
8
1import gradio as gr2from ultralytics import YOLO3import torch4 5model_id = "mosesb/best-comic-panel-detection"6model = YOLO("best.pt")7 8def 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 thresholds14 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() method18 # returns a BGR image, so we convert it.19 annotated_image_rgb = annotated_image[..., ::-1]20 21 return annotated_image_rgb22 23 24 25# --- Gradio Interface ---26title = "YOLOv12 Comic Panel Detection"27description = """28This demo showcases a **YOLOv12 object detection model** that has been fine-tuned to detect panels in comic book pages.29Upload an image of a comic page, and the model will draw bounding boxes around each detected panel. 30This can be a useful first step for downstream tasks like Optical Character Recognition (OCR) or character analysis within comics.31"""32 33article = 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 <p>If you like this demo, consider leaving a star on the <a href='https://github.com/mosesab/YOLOV12-Comic-Panel-Detection' target='_blank'>Github repo</a> or a like on the <a href='https://huggingface.co/{model_id}' target='_blank'>Hugging Face model</a>. It helps me know people are interested and motivates further development.</p>38</div>39"""40 41# Define the input components for the Gradio interface42inputs = [43 gr.Image(type="pil", label="Upload Comic Page Image"),44 gr.Slider(45 minimum=0.0,46 maximum=1.0,47 value=0.25, # The default confidence threshold in ultralytics48 step=0.05,49 label="Confidence Threshold",50 info="Filters detections. Only boxes with confidence above this value will be shown."51 ),52 gr.Slider(53 minimum=0.0,54 maximum=1.0,55 value=0.7, # The default IoU threshold in ultralytics56 step=0.05,57 label="IoU Threshold",58 info="Controls merging of overlapping boxes. Higher values allow more overlap."59 )60]61 62examples = [63 ["aura_farmer_1.jpg", 0.25, 0.7],64 ["aura_farmer_2.jpg", 0.25, 0.7],65 ["aura_farmer_3.jpg", 0.25, 0.7],66 ["aura_farmer_4.jpg", 0.25, 0.7],67]68 69gr.Interface(70 fn=detect_panels,71 inputs=inputs,72 outputs=gr.Image(type="pil", label="Detected Panels"),73 title=title,74 description=description,75 article=article,76 examples=examples,77 allow_flagging="auto"78).launch()