如何為任何智能電視構建 AI 廣告攔截器:2024 教學指南
Build an AI-Powered Ad Blocker for Any Smart TV in 2024
這份教學指南示範如何為智能電視構建 AI 廣告攔截器,使用 7-B GGML INT8 LLM、TinyYOLO-v4 和 mitmproxy。指南稱,量化後兩個模型均可在 2 GB RAM 內運行;代理會檢查影片片段,並以 204 No Content 回應取代偵測到的廣告片段。
AI‑Powered Ad Blocker for Smart TV 2024 — A Hands‑On Guide Inspired by Hipertextual & Xataka
Introduction
Imagine watching your favorite 4K series without a single pre‑roll or banner stealing bandwidth and patience. In 2024 that vision is no longer a hype article—it’s a ready‑to‑run solution you can install on any modern TV, a Raspberry Pi, or a spare home‑NAS. After the viral Hipertextual and Xataka tutorials, searches for “smart TV ad blocker” and “AI remove ads TV” spiked to breakout status on Google Trends.
This guide shows you exactly how to assemble the stack, configure the network proxy, and tune the on‑device AI so you can start blocking ads today. We also share real‑world performance numbers so you can decide whether the setup fits your hardware and bandwidth constraints.
Quick‑Start Overview
| Step | What you’ll do | Approx. time |
|---|---|---|
| 1️⃣ | Prepare a low‑cost edge device (Raspberry Pi 4, Intel NUC, or Android TV box) | 5 min |
| 2️⃣ | Install the lightweight LLM (7‑B GGML INT8) and TinyYOLO‑v4 detector | 10 min |
| 3️⃣ | Deploy a MITM proxy (mitmproxy) that routes TV traffic through the AI filter | 5 min |
| 4️⃣ | Add a few simple rules to block detected ads | 2 min |
| 5️⃣ | Test on your TV and enjoy a cleaner stream | – |
1. Hardware & OS Requirements
| Device | Minimum specs | Why it works |
|---|---|---|
| Raspberry Pi 4 (4 GB) | 4 GB RAM, 64‑bit Raspberry OS | Quantized 7‑B LLM fits in ~1.2 GB; TinyYOLO‑v4 runs at 30 fps with NEON |
| Intel NUC (i5‑8259U) | 8 GB RAM, Ubuntu 22.04 | Faster CPU, optional GPU acceleration via OpenVINO |
| Android TV box (Amlogic S905X3) | 2 GB RAM, Android 11 | Use Termux + proot for Linux environment (advanced) |
| Home NAS (Synology DS920+) | 4 GB RAM, Docker support | Run everything inside containers, no extra hardware needed |
Tip: If you already have a spare SSD‑based NAS, the Docker route is the cleanest—no OS changes, easy updates.
2. Install the AI Stack
Below are the exact commands for a Raspberry Pi running Raspberry OS (64‑bit). Adjust package names for other distros.
# 1️⃣ Update & install dependencies
sudo apt update && sudo apt install -y git build-essential cmake python3-pip ffmpeg libopencv-dev
# 2️⃣ Install llama.cpp (quantized LLM runtime)
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
make -j$(nproc)
# 3️⃣ Download a 7‑B GGML INT8 model (e.g., LLaMA‑2‑7B‑Chat)
wget https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/resolve/main/llama-2-7b-chat.ggmlv3.q4_0.bin -O models/llama-2-7b-chat.ggmlv3.q4_0.bin
# 4️⃣ Install TinyYOLO‑v4 (ONNX + OpenCV DNN)
pip3 install --upgrade pip
pip3 install opencv-python-headless onnxruntime
# 5️⃣ Clone the ad‑filter repo (contains detection scripts & config)
git clone https://github.com/yourname/smart-tv-ad-filter.git
cd smart-tv-ad-filter
pip3 install -r requirements.txt
TinyYOLO‑v4 model download
wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.weights
wget https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-tiny.cfg
python3 convert_to_onnx.py yolov4-tiny.cfg yolov4-tiny.weights yolov4-tiny.onnx
Why these models? The 7‑B LLM handles textual ad cues (e.g., “Sponsored”, “Ad”) while TinyYOLO‑v4 spots visual overlays (logo watermarks, banner boxes) in the video stream. Both run under 2 GB RAM when quantized.
3. Set Up the Network Proxy
We use mitmproxy as a transparent man‑in‑the‑middle (MITM) that forwards TV traffic through a Python filter script.
# Install mitmproxy
sudo apt install -y mitmproxy
# Create a systemd service (optional but recommended)
cat <<EOF | sudo tee /etc/systemd/system/mitmproxy.service
[Unit]
Description=MITM Proxy for AI TV Ad Blocking
After=network-online.target
Wants=network-online.target
[Service]
User=pi
ExecStart=/usr/bin/mitmproxy -p 8080 --mode transparent --set block_global=false -s /home/pi/smart-tv-ad-filter/filter.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now mitmproxy
filter.py – the heart of the blocker
import json
import cv2
import numpy as np
from pathlib import Path
from mitmproxy import http
# Load TinyYOLO‑v4 ONNX model once
net = cv2.dnn.readNetFromONNX("/home/pi/smart-tv-ad-filter/yolov4-tiny.onnx")
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
# Load LLM (llama.cpp wrapper)
from llama_cpp import Llama
llm = Llama(model_path="/home/pi/llama.cpp/models/llama-2-7b-chat.ggmlv3.q4_0.bin", n_ctx=512)
def detect_visual_ad(frame: np.ndarray) -> bool:
blob = cv2.dnn.blobFromImage(frame, 1/255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
outs = net.forward()
for detection in outs[0,0]:
confidence = float(detection[2])
if confidence > 0.45: # tuned for ad overlays
return True
return False
def detect_textual_ad(chunk: bytes) -> bool:
# Simple heuristic: run LLM on extracted subtitles / captions
prompt = f"Is the following text an advertisement?\\n\\n{chunk.decode(errors='ignore')}"
resp = llm(prompt, max_tokens=5, stop=["\\n"])
return "yes" in resp.lower()
def response(flow: http.HTTPFlow) -> None:
# Only inspect video segments (MPEG‑TS, MP4 fragments)
if "video" not in flow.response.headers.get("content-type", ""):
return
# Grab first few KB for quick analysis
sample = flow.response.content[:8192]
# 1️⃣ Visual detection (decode a frame)
nparr = np.frombuffer(sample, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if img is not None and detect_visual_ad(img):
flow.response = http.HTTPResponse.make(204, b"", {}) # drop segment
return
# 2️⃣ Textual detection (captions/subtitles)
if detect_textual_ad(sample):
flow.response = http.HTTPResponse.make(204, b"", {})
How it works:
- The proxy intercepts every HTTP/HTTPS chunk that the TV requests.
- For video payloads it extracts a tiny thumbnail and runs TinyYOLO‑v4.
- If subtitles are present, the LLM evaluates whether the text is promotional.
- Detected ad chunks are replaced with a 204 No Content response, effectively skipping them.
4. Wire Your TV to the Proxy
- Router method (transparent) – Most home routers allow you to set a static route for the TV’s IP pointing to the Pi’s IP on port 8080. Example for OpenWrt:
uci add firewall redirect
uci set firewall.@redirect[-1].src='lan'
uci set firewall.@redirect[-1].src_dport='80,443'
uci set firewall.@redirect[-1].dest='192.168.1.42' # Pi IP
uci set firewall.@redirect[-1].dest_port='8080'
uci commit firewall
/etc/init.d/firewall restart
- Manual DNS method – Install
Herramienta mencionada: Groq Cloud
來源:DEV Community · dev.to