bakisae commited on
Commit
93f2aa0
·
verified ·
1 Parent(s): 0f6c1ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -41
app.py CHANGED
@@ -1,58 +1,76 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
- from PIL import ImageFilter
4
- import spaces
 
5
 
6
- # استخدام النموذج المتقدم الذي يكشف البيكيني والإغراء
7
- pipe = pipeline("image-classification", model="prithivMLmods/Mature-Content-Detection")
8
 
9
- @spaces.GPU
10
- def predict(image, sensitivity=0.30):
 
 
 
 
 
 
 
 
 
 
11
  if image is None:
12
- return None, {"nsfw": False, "score": 0.0}
13
 
14
- results = pipe(image)
 
 
15
 
16
- # حساب نسبة العري أو الإغراء والبيكيني
17
- bad_score = 0.0
18
- detected_labels = {}
19
 
20
- for r in results:
21
- label = r['label']
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
- if is_nsfw:
34
- filtered_image = image.filter(ImageFilter.GaussianBlur(radius=45))
35
- else:
36
- filtered_image = image
37
 
38
- return filtered_image, {
39
- "is_nsfw": is_nsfw,
40
- "total_bad_score": round(bad_score, 3),
41
- "details": detected_labels,
42
- "status": "🚨 تم الحجب والتغبيش" if is_nsfw else "✅ آمنة"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  }
44
 
45
  demo = gr.Interface(
46
- fn=predict,
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()