|
|
|
|
|
import gradio as gr |
|
from PIL import Image, ImageDraw |
|
import torch |
|
import requests |
|
from io import BytesIO |
|
import numpy as np |
|
from ultralytics import YOLO |
|
|
|
|
|
|
|
model = YOLO("yolov8n.pt") |
|
|
|
|
|
def analyze_ad_positions(detections, image_height): |
|
insights = [] |
|
for box in detections: |
|
x1, y1, x2, y2 = box[:4] |
|
center_y = (y1 + y2) / 2 |
|
|
|
if center_y < image_height * 0.33: |
|
pos = "Top" |
|
suggestion = "Good visibility β
" |
|
elif center_y < image_height * 0.66: |
|
pos = "Middle" |
|
suggestion = "Moderate visibility β οΈ Consider moving up" |
|
else: |
|
pos = "Bottom" |
|
suggestion = "Low visibility β Move above the fold" |
|
|
|
insights.append(f"Ad at {pos} β {suggestion}") |
|
return insights |
|
|
|
|
|
def detect_ads(image): |
|
img = image.convert("RGB") |
|
image_array = np.array(img) |
|
|
|
results = model(image_array)[0] |
|
detections = [] |
|
draw = ImageDraw.Draw(img) |
|
|
|
for box in results.boxes.xyxy: |
|
x1, y1, x2, y2 = map(int, box.tolist()) |
|
draw.rectangle([x1, y1, x2, y2], outline="red", width=3) |
|
draw.text((x1, y1 - 10), "Ad", fill="red") |
|
detections.append((x1, y1, x2, y2)) |
|
|
|
suggestions = analyze_ad_positions(detections, img.height) |
|
suggestions_text = "\n".join(suggestions) if suggestions else "No ads detected." |
|
|
|
return img, suggestions_text |
|
|
|
|
|
interface = gr.Interface( |
|
fn=detect_ads, |
|
inputs=gr.Image(type="pil"), |
|
outputs=[gr.Image(type="pil"), gr.Textbox()], |
|
title="AdVision AI", |
|
description="Upload a webpage screenshot. Detects ad placements and gives visibility suggestions." |
|
) |
|
|
|
interface.launch() |