waveshaker commited on
Commit
705e0f6
·
verified ·
1 Parent(s): 27d0379

Upload 2 files

Browse files
Files changed (2) hide show
  1. comfy_dataset_prep_wf_v001.json +0 -0
  2. filter_nsfw.py +83 -0
comfy_dataset_prep_wf_v001.json ADDED
The diff for this file is too large to render. See raw diff
 
filter_nsfw.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import glob
4
+ from nudenet import NudeDetector
5
+ from PIL import Image, UnidentifiedImageError
6
+
7
+ def main():
8
+ current_dir = os.getcwd()
9
+ nsfw_dir = os.path.join(current_dir, "NSFW")
10
+
11
+ # 1. 创建 NSFW 文件夹
12
+ if not os.path.exists(nsfw_dir):
13
+ os.makedirs(nsfw_dir)
14
+ print(f"📁 已创建隔离文件夹: {nsfw_dir}")
15
+
16
+ # 2. 初始化检测器 (首次运行会自动下载模型)
17
+ print("⏳ 正在加载 NSFW 检测模型,首次运行可能需要下载...")
18
+ detector = NudeDetector()
19
+
20
+ # 定义属于 NSFW 的标签 (根据 nudenet 的标签库)
21
+ NSFW_LABELS = {
22
+ "FEMALE_BREAST_EXPOSED",
23
+ "FEMALE_GENITALIA_EXPOSED",
24
+ "MALE_GENITALIA_EXPOSED",
25
+ "ANUS_EXPOSED",
26
+ "BUTTOCKS_EXPOSED",
27
+ # 加入以下擦边标签:
28
+ "MALE_BREAST_EXPOSED", # 男士露胸(有时候会被误判)
29
+ }
30
+
31
+ # 3. 查找当前目录下所有的 jpg
32
+ jpg_files = glob.glob(os.path.join(current_dir, "*.jpg")) + glob.glob(os.path.join(current_dir, "*.jpeg"))
33
+
34
+ if not jpg_files:
35
+ print("❌ 当前目录下没有找到 .jpg 或 .jpeg 文件。")
36
+ return
37
+
38
+ print(f"🔍 共找到 {len(jpg_files)} 张图片,开始扫描...\n")
39
+
40
+ nsfw_count = 0
41
+
42
+ for img_path in jpg_files:
43
+ filename = os.path.basename(img_path)
44
+ base_name = os.path.splitext(filename)[0]
45
+ txt_path = os.path.join(current_dir, f"{base_name}.txt")
46
+
47
+ try:
48
+ # 检测图片
49
+ detections = detector.detect(img_path)
50
+
51
+ is_nsfw = False
52
+ found_labels = set()
53
+
54
+ for det in detections:
55
+ # 如果检测到的标签在 NSFW 列表中,且置信度 > 0.6
56
+ if det["class"] in NSFW_LABELS and det["score"] > 0.6:
57
+ is_nsfw = True
58
+ found_labels.add(det["class"])
59
+
60
+ if is_nsfw:
61
+ print(f"🚫 发现 NSFW: {filename} -> 特征: {', '.join(found_labels)}")
62
+
63
+ # 移动图片
64
+ shutil.move(img_path, os.path.join(nsfw_dir, filename))
65
+
66
+ # 移动同名 TXT (如果有的话)
67
+ if os.path.exists(txt_path):
68
+ shutil.move(txt_path, os.path.join(nsfw_dir, f"{base_name}.txt"))
69
+ else:
70
+ print(f" ⚠️ 警告: {filename} 没有对应的 txt 文件")
71
+
72
+ nsfw_count += 1
73
+
74
+ except UnidentifiedImageError:
75
+ print(f"⚠️ 跳过损坏的图片: {filename}")
76
+ except Exception as e:
77
+ print(f"⚠️ 检测 {filename} 时出错: {e}")
78
+
79
+ print("\n" + "="*40)
80
+ print(f"🎉 扫描完成!共处理 {len(jpg_files)} 张图,成功隔离 {nsfw_count} 张 NSFW 图片及其文本。")
81
+
82
+ if __name__ == "__main__":
83
+ main()