| import gradio as gr |
| from ultralytics import YOLO |
| import cv2 |
| import numpy as np |
| from PIL import Image |
|
|
| print("🚀 Loading Manga Sniper Studio (Optimized for Auto-Cleaning)...") |
| |
| model = YOLO("best.pt") |
|
|
| def clean_manga_page(input_image): |
| try: |
| if input_image is None: |
| return None |
| |
| |
| img_cv = cv2.cvtColor(np.array(input_image), cv2.COLOR_RGB2BGR) |
| h, w = img_cv.shape[:2] |
| |
| |
| results = model(img_cv, conf=0.35) |
| bubble_mask = np.zeros((h, w), dtype=np.uint8) |
|
|
| if results[0].masks is not None: |
| for seg in results[0].masks.xy: |
| poly = seg.astype(np.int32) |
| |
| if 300 < cv2.contourArea(poly) < (h * w * 0.50): |
| cv2.fillPoly(bubble_mask, [poly], 255) |
| |
| |
| gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY) |
| |
| |
| ink = cv2.adaptiveThreshold( |
| gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, |
| cv2.THRESH_BINARY_INV, 15, 4 |
| ) |
| |
| |
| ink_in_bubble = cv2.bitwise_and(ink, bubble_mask) |
|
|
| |
| kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (4, 4)) |
| final_mask = cv2.dilate(ink_in_bubble, kernel, iterations=2) |
|
|
| |
| |
| cleaned_cv = cv2.inpaint(img_cv, final_mask, inpaintRadius=5, flags=cv2.INPAINT_TELEA) |
| |
| |
| cleaned_rgb = cv2.cvtColor(cleaned_cv, cv2.COLOR_BGR2RGB) |
| return Image.fromarray(cleaned_rgb) |
| |
| except Exception as e: |
| print(f"Error occurred: {str(e)}") |
| return input_image |
|
|
| |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: |
| gr.Markdown( |
| """ |
| # 🌌 AI Manga Cleaner Studio |
| قم برفع صفحة المانجا/المانهوا الخاصة بك ليقوم الذكاء الاصطناعي بتبييض ومسح النصوص تلقائياً مع الحفاظ على الرسوم الخلفية. |
| """ |
| ) |
| with gr.Row(): |
| with gr.Column(): |
| image_input = gr.Image(type="pil", label="الصورة الأصلية (Original Page)") |
| clean_btn = gr.Button("✨ بدء التبييض التلقائي", variant="primary") |
| with gr.Column(): |
| image_output = gr.Image(type="pil", label="النتيجة المبيّضة (Cleaned Page)") |
| |
| clean_btn.click(fn=clean_manga_page, inputs=image_input, outputs=image_output) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch(server_name="0.0.0.0", server_port=7860) |