Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,76 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
import
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
if image is None:
|
| 12 |
-
return None, {"
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# ح
|
| 17 |
-
|
| 18 |
-
detected_labels = {}
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
score = r['score']
|
| 23 |
-
detected_labels[label] = round(score, 3)
|
| 24 |
-
|
| 25 |
-
# إذا كانت اللقطة عري أو ملابس كاشفة وبيكيني
|
| 26 |
-
if label in ['Pornography', 'Enticing or Sensual', 'Hentai']:
|
| 27 |
-
bad_score += score
|
| 28 |
-
|
| 29 |
-
# إذا تجاوزت نسبة المحتوى غير اللائق أو الكاشف الحساسية
|
| 30 |
-
is_nsfw = bad_score >= sensitivity
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
filtered_image = image
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
|
| 45 |
demo = gr.Interface(
|
| 46 |
-
fn=
|
| 47 |
-
inputs=
|
| 48 |
-
gr.Image(type="pil", label="الصورة الأصلية"),
|
| 49 |
-
gr.Slider(minimum=0.10, maximum=0.90, value=0.30, step=0.05, label="حساسية الحجب")
|
| 50 |
-
],
|
| 51 |
outputs=[
|
| 52 |
-
gr.Image(type="pil", label="النتيجة ب
|
| 53 |
-
gr.JSON(label="
|
| 54 |
],
|
| 55 |
-
title="Safe Watch AI -
|
| 56 |
)
|
| 57 |
|
| 58 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from nudenet import NudeDetector
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
# تشغيل كاشف الأجزاء الحساسة الموضعي
|
| 8 |
+
detector = NudeDetector()
|
| 9 |
|
| 10 |
+
# قائمة الأجزاء التي نريد حجبها فقط (بيكيني، عري، عورات)
|
| 11 |
+
UNSAFE_PARTS = [
|
| 12 |
+
"FEMALE_BREAST_EXPOSED",
|
| 13 |
+
"FEMALE_BREAST_COVERED", # ملابس السباحة والبيكيني وحمالات الصدر
|
| 14 |
+
"BUTTOCKS_EXPOSED",
|
| 15 |
+
"BUTTOCKS_COVERED",
|
| 16 |
+
"FEMALE_GENITALIA_EXPOSED",
|
| 17 |
+
"MALE_GENITALIA_EXPOSED",
|
| 18 |
+
"BELLY_EXPOSED"
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
def censor_target_only(image):
|
| 22 |
if image is None:
|
| 23 |
+
return None, {"status": "لا توجد صورة"}
|
| 24 |
|
| 25 |
+
# تحويل الصورة لمعالجتها
|
| 26 |
+
img_np = np.array(image.convert("RGB"))
|
| 27 |
+
img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
| 28 |
|
| 29 |
+
# كشف إحداثيات الأجزاء الحساسة في الصورة
|
| 30 |
+
detections = detector.detect(img_np)
|
|
|
|
| 31 |
|
| 32 |
+
censored_count = 0
|
| 33 |
+
details = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
for item in detections:
|
| 36 |
+
part_name = item['class']
|
| 37 |
+
score = item['score']
|
| 38 |
+
box = item['box'] # [x, y, width, height]
|
|
|
|
| 39 |
|
| 40 |
+
# إذا وجد جزءاً حساساً بنسبة ثقة أعلى من 40%
|
| 41 |
+
if part_name in UNSAFE_PARTS and score >= 0.40:
|
| 42 |
+
censored_count += 1
|
| 43 |
+
details.append(f"{part_name} ({round(score * 100)}%)")
|
| 44 |
+
|
| 45 |
+
x, y, w, h = box
|
| 46 |
+
x, y = max(0, x), max(0, y)
|
| 47 |
+
x2 = min(img_cv.shape[1], x + w)
|
| 48 |
+
y2 = min(img_cv.shape[0], y + h)
|
| 49 |
+
|
| 50 |
+
# تطبيق التغبيش على هذا الجزء فقط بدون باقي الصورة
|
| 51 |
+
target_area = img_cv[y:y2, x:x2]
|
| 52 |
+
if target_area.size > 0:
|
| 53 |
+
# تغبيش قوي جداً للمنطقة المحددة
|
| 54 |
+
blurred = cv2.GaussianBlur(target_area, (65, 65), 35)
|
| 55 |
+
img_cv[y:y2, x:x2] = blurred
|
| 56 |
+
|
| 57 |
+
result_rgb = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)
|
| 58 |
+
result_image = Image.fromarray(result_rgb)
|
| 59 |
+
|
| 60 |
+
return result_image, {
|
| 61 |
+
"censored_parts": censored_count,
|
| 62 |
+
"details": details,
|
| 63 |
+
"status": f"🚨 تم حجب {censored_count} موضع فقط" if censored_count > 0 else "✅ الصورة آمنة"
|
| 64 |
}
|
| 65 |
|
| 66 |
demo = gr.Interface(
|
| 67 |
+
fn=censor_target_only,
|
| 68 |
+
inputs=gr.Image(type="pil", label="الصورة الأصلية"),
|
|
|
|
|
|
|
|
|
|
| 69 |
outputs=[
|
| 70 |
+
gr.Image(type="pil", label="النتيجة (حجب الموضع فقط مع بقاء الصورة واضحة)"),
|
| 71 |
+
gr.JSON(label="المواضع المحجوبة")
|
| 72 |
],
|
| 73 |
+
title="Safe Watch AI - الحجب الموضعي الذكي للأجزاء الحساسة فقط"
|
| 74 |
)
|
| 75 |
|
| 76 |
demo.launch()
|