Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import onnxruntime as ort
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
|
| 8 |
+
# --- 1. Configuration & Model Loading ---
|
| 9 |
+
# Replace with your "username/repo-name"
|
| 10 |
+
REPO_ID = "A123123/AnimeAutoCensor"
|
| 11 |
+
# Retrieved from Space's Secrets
|
| 12 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
# Download .onnx file
|
| 16 |
+
model_path = hf_hub_download(
|
| 17 |
+
repo_id=REPO_ID,
|
| 18 |
+
filename="model.onnx",
|
| 19 |
+
token=HF_TOKEN
|
| 20 |
+
)
|
| 21 |
+
# Download .onnx_data file (ONNX Runtime automatically looks for this in the same directory)
|
| 22 |
+
hf_hub_download(
|
| 23 |
+
repo_id=REPO_ID,
|
| 24 |
+
filename="model.onnx_data",
|
| 25 |
+
token=HF_TOKEN
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Initialize Inference Engine (Hugging Face Free Tier supports CPU only)
|
| 29 |
+
session = ort.InferenceSession(model_path, providers=['CPUExecutionProvider'])
|
| 30 |
+
input_name = session.get_inputs()[0].name
|
| 31 |
+
target_size = 640 # Your model training size
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Model loading failed. Please check Token and Repository Path: {e}")
|
| 34 |
+
|
| 35 |
+
# --- 2. Core Processing Functions ---
|
| 36 |
+
def apply_mosaic_mask(image_rgb, mask, mosaic_level=16):
|
| 37 |
+
h, w = image_rgb.shape[:2]
|
| 38 |
+
mask = (mask > 0).astype(np.uint8)
|
| 39 |
+
small = cv2.resize(image_rgb, (max(1, w // mosaic_level), max(1, h // mosaic_level)), interpolation=cv2.INTER_LINEAR)
|
| 40 |
+
mosaic_image = cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)
|
| 41 |
+
output_image = image_rgb.copy()
|
| 42 |
+
output_image[mask == 1] = mosaic_image[mask == 1]
|
| 43 |
+
return output_image
|
| 44 |
+
|
| 45 |
+
def process_image(input_img):
|
| 46 |
+
if input_img is None:
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
# Gradio passes images as RGB numpy arrays
|
| 50 |
+
h_orig, w_orig = input_img.shape[:2]
|
| 51 |
+
|
| 52 |
+
# A. Letterbox Preprocessing
|
| 53 |
+
scale = target_size / max(h_orig, w_orig)
|
| 54 |
+
new_h, new_w = int(h_orig * scale), int(w_orig * scale)
|
| 55 |
+
img_resized = cv2.resize(input_img, (new_w, new_h))
|
| 56 |
+
|
| 57 |
+
canvas = np.zeros((target_size, target_size, 3), dtype=np.uint8)
|
| 58 |
+
pad_y = (target_size - new_h) // 2
|
| 59 |
+
pad_x = (target_size - new_w) // 2
|
| 60 |
+
canvas[pad_y:pad_y+new_h, pad_x:pad_x+new_w] = img_resized
|
| 61 |
+
|
| 62 |
+
# B. Normalization & Inference
|
| 63 |
+
input_tensor = canvas.astype(np.float32) / 255.0
|
| 64 |
+
input_tensor = (input_tensor - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225]
|
| 65 |
+
input_tensor = input_tensor.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
|
| 66 |
+
|
| 67 |
+
outputs = session.run(None, {input_name: input_tensor})
|
| 68 |
+
pred = outputs[0][0][0]
|
| 69 |
+
|
| 70 |
+
# C. Post-processing (Restore Size)
|
| 71 |
+
mask_valid = pred[pad_y:pad_y+new_h, pad_x:pad_x+new_w]
|
| 72 |
+
mask_final = cv2.resize(mask_valid, (w_orig, h_orig))
|
| 73 |
+
binary_mask = (mask_final > 0.5).astype(np.uint8)
|
| 74 |
+
|
| 75 |
+
# D. Apply Mosaic
|
| 76 |
+
result_rgb = apply_mosaic_mask(input_img, binary_mask)
|
| 77 |
+
return result_rgb
|
| 78 |
+
|
| 79 |
+
# --- 3. Gradio Interface ---
|
| 80 |
+
with gr.Blocks(title="AI Anime Auto-Censor") as demo:
|
| 81 |
+
gr.Markdown("# 🎨 AI Anime Auto-Censor (Trial Version)")
|
| 82 |
+
gr.Markdown("This tool uses AI to automatically detect and censor specific content. The model weights are protected and not accessible to the public.")
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
with gr.Column():
|
| 86 |
+
input_i = gr.Image(type="numpy", label="Upload Image")
|
| 87 |
+
run_btn = gr.Button("Start Processing", variant="primary")
|
| 88 |
+
with gr.Column():
|
| 89 |
+
output_i = gr.Image(type="numpy", label="Censored Result")
|
| 90 |
+
|
| 91 |
+
run_btn.click(fn=process_image, inputs=input_i, outputs=output_i)
|
| 92 |
+
|
| 93 |
+
gr.Markdown("---")
|
| 94 |
+
gr.Markdown("### Instructions:\n1. Upload an image.\n2. Click 'Start Processing'.\n3. Download the result if satisfied.")
|
| 95 |
+
|
| 96 |
+
if __name__ == "__main__":
|
| 97 |
+
demo.launch()
|