File size: 2,034 Bytes
8682e4d 4d51fb3 8682e4d 4d51fb3 8682e4d 4d51fb3 8682e4d 4d51fb3 8682e4d 4d51fb3 8682e4d 4d51fb3 8682e4d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# AdVision AI: Simple Ad Detector & Suggestion Tool for Hugging Face
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
# Load YOLOv8 model (assumes pretrained for ad/banner detection)
# You can also use your custom model from Roboflow or YOLO
model = YOLO("yolov8n.pt") # Change to custom ad-detection model if available
# Function to analyze ad positions and give suggestions
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
# Main prediction function
def detect_ads(image):
img = image.convert("RGB")
image_array = np.array(img)
results = model(image_array)[0] # First result
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
# Gradio Interface
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() |