modesty-nsfw-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
nsfw_cli.py,sha256=ptzvdhDHamyAzQt1-k6LtGjB5uJ_Xlky30_hCxToESk,6854
|
|
2
|
+
modesty_nsfw_cli-0.1.0.dist-info/METADATA,sha256=HU0-opuRwNMJWIiB4Yc18alEgmYB52Oc5H0QzIRZig8,222
|
|
3
|
+
modesty_nsfw_cli-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
4
|
+
modesty_nsfw_cli-0.1.0.dist-info/entry_points.txt,sha256=K2M5ODjPftm8flu_mZYQ8AymBL1clCPij3-UEB0P-04,78
|
|
5
|
+
modesty_nsfw_cli-0.1.0.dist-info/top_level.txt,sha256=7bgKCg8N72EHWb2chmzvPeV6qdiBtFJABKzrVzJpTGE,9
|
|
6
|
+
modesty_nsfw_cli-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
nsfw_cli
|
nsfw_cli.py
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"""Modesty CLI: thin command line over the nsfw_scanner library.
|
|
2
|
+
|
|
3
|
+
All scanning lives in the lib; this package only forwards argv.
|
|
4
|
+
Works installed (`pip install ./packages/nsfw-cli`, entry points
|
|
5
|
+
`modesty-nsfw-scan` and the compatibility alias `nsfw-scan`) or straight
|
|
6
|
+
from the checkout.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import os
|
|
11
|
+
import sys
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _bootstrap():
|
|
15
|
+
try:
|
|
16
|
+
import nsfw_scanner # noqa: F401
|
|
17
|
+
return
|
|
18
|
+
except ImportError:
|
|
19
|
+
pass
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
candidate = Path(__file__).resolve().parents[2] / "nsfw-lib" / "src"
|
|
22
|
+
if candidate.is_dir():
|
|
23
|
+
sys.path.insert(0, str(candidate))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
_bootstrap()
|
|
27
|
+
|
|
28
|
+
from nsfw_scanner import main as lib_main # noqa: E402
|
|
29
|
+
from nsfw_scanner.scanner import ( # noqa: E402
|
|
30
|
+
evaluate_image, get_sessions, get_face_session, get_binary_session,
|
|
31
|
+
analyze_image_file,
|
|
32
|
+
)
|
|
33
|
+
from PIL import Image # noqa: E402
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _cmd_scan_eval(argv):
|
|
37
|
+
"""scan-eval TARGET [--crops] — full model evaluation (no thresholds)."""
|
|
38
|
+
import argparse
|
|
39
|
+
parser = argparse.ArgumentParser(prog="scan-eval",
|
|
40
|
+
description="Run every model without "
|
|
41
|
+
"thresholds, with optional "
|
|
42
|
+
"half/half crops for "
|
|
43
|
+
"low-quality images")
|
|
44
|
+
parser.add_argument("target", help="Local image file to evaluate")
|
|
45
|
+
parser.add_argument("--crops", action="store_true",
|
|
46
|
+
help="Also run half-vertical / half-horizontal crops "
|
|
47
|
+
"on low-quality images")
|
|
48
|
+
parser.add_argument("--low-quality-dim", type=int, default=None,
|
|
49
|
+
help="Pixel dimension below which an image is "
|
|
50
|
+
"considered low quality (default: 1024, or "
|
|
51
|
+
"NSFW_LOW_QUALITY_MAX_DIM env var)")
|
|
52
|
+
parser.add_argument("--early-stop", action="store_true",
|
|
53
|
+
help="Stop at the first crop that detects a female "
|
|
54
|
+
"(default: scan all crops)")
|
|
55
|
+
args = parser.parse_args(argv)
|
|
56
|
+
|
|
57
|
+
nsfw_sess, gender_sess = get_sessions()
|
|
58
|
+
face_sess = get_face_session()
|
|
59
|
+
binary_sess = get_binary_session()
|
|
60
|
+
|
|
61
|
+
img = Image.open(args.target)
|
|
62
|
+
result = evaluate_image(img, nsfw_session=nsfw_sess,
|
|
63
|
+
gender_session=gender_sess,
|
|
64
|
+
face_session=face_sess,
|
|
65
|
+
binary_session=binary_sess,
|
|
66
|
+
crops=args.crops,
|
|
67
|
+
fallback_url=args.target,
|
|
68
|
+
low_quality_max_dim=args.low_quality_dim,
|
|
69
|
+
early_stop=args.early_stop)
|
|
70
|
+
print(json.dumps(result, indent=2, default=str))
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _cmd_scan_new(argv):
|
|
74
|
+
"""scan-new TARGET — newer rating function (analyze_pil_image)."""
|
|
75
|
+
import argparse
|
|
76
|
+
parser = argparse.ArgumentParser(prog="scan-new",
|
|
77
|
+
description="Run the regular rating "
|
|
78
|
+
"function (analyze_pil_image) "
|
|
79
|
+
"on an image")
|
|
80
|
+
parser.add_argument("target", help="Local image file to scan")
|
|
81
|
+
parser.add_argument("--details", action="store_true",
|
|
82
|
+
help="Print rating plus nsfw/female scores and gender")
|
|
83
|
+
parser.add_argument("--debug", action="store_true",
|
|
84
|
+
help="Explain the verdict (keyword hits, deciding stage)")
|
|
85
|
+
parser.add_argument("--crops", action="store_true",
|
|
86
|
+
help="Run half-vertical / half-horizontal crops on "
|
|
87
|
+
"low-quality images, stop at first female "
|
|
88
|
+
"detection (early stop)")
|
|
89
|
+
parser.add_argument("--low-quality-dim", type=int, default=None,
|
|
90
|
+
help="Pixel dimension below which an image is "
|
|
91
|
+
"considered low quality (default: 1024, or "
|
|
92
|
+
"NSFW_LOW_QUALITY_MAX_DIM env var)")
|
|
93
|
+
parser.add_argument("--high-accuracy", action="store_true",
|
|
94
|
+
help="Run YOLOv8 localized body-part detection after "
|
|
95
|
+
"the fast scanner to resolve middle-tier "
|
|
96
|
+
"over-protection (revealing 6 vs risque 7 vs "
|
|
97
|
+
"explicit 10). Costs ~570ms per image vs "
|
|
98
|
+
"~112ms for the fast path.")
|
|
99
|
+
args = parser.parse_args(argv)
|
|
100
|
+
|
|
101
|
+
nsfw_sess, gender_sess = get_sessions()
|
|
102
|
+
face_sess = get_face_session()
|
|
103
|
+
binary_sess = get_binary_session()
|
|
104
|
+
|
|
105
|
+
img = Image.open(args.target)
|
|
106
|
+
|
|
107
|
+
# With --crops, use evaluate_image with early_stop=True so it stops
|
|
108
|
+
# at the first crop that detects a female.
|
|
109
|
+
if args.crops:
|
|
110
|
+
result = evaluate_image(img, nsfw_session=nsfw_sess,
|
|
111
|
+
gender_session=gender_sess,
|
|
112
|
+
face_session=face_sess,
|
|
113
|
+
binary_session=binary_sess,
|
|
114
|
+
crops=True,
|
|
115
|
+
fallback_url=args.target,
|
|
116
|
+
low_quality_max_dim=args.low_quality_dim,
|
|
117
|
+
early_stop=True)
|
|
118
|
+
else:
|
|
119
|
+
result = analyze_image_file(args.target, nsfw_sess, gender_sess,
|
|
120
|
+
details=args.details, debug=args.debug,
|
|
121
|
+
face_session=face_sess,
|
|
122
|
+
binary_session=binary_sess,
|
|
123
|
+
high_accuracy=args.high_accuracy)
|
|
124
|
+
print(json.dumps(result, indent=2, default=str))
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _cmd_doctor(argv):
|
|
128
|
+
"""doctor — print every resolved path and whether it exists."""
|
|
129
|
+
import argparse
|
|
130
|
+
parser = argparse.ArgumentParser(prog="doctor",
|
|
131
|
+
description="Show resolved Modesty paths")
|
|
132
|
+
parser.parse_args(argv)
|
|
133
|
+
from nsfw_scanner.paths import doctor
|
|
134
|
+
print(json.dumps(doctor(), indent=2, default=str))
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def main(argv=None):
|
|
138
|
+
# Intercept new subcommands before forwarding to the lib
|
|
139
|
+
if argv is None:
|
|
140
|
+
argv = sys.argv[1:]
|
|
141
|
+
if argv and argv[0] in ("scan-eval", "scan-new", "doctor"):
|
|
142
|
+
cmd = argv[0]
|
|
143
|
+
rest = argv[1:]
|
|
144
|
+
if cmd == "scan-eval":
|
|
145
|
+
_cmd_scan_eval(rest)
|
|
146
|
+
elif cmd == "doctor":
|
|
147
|
+
_cmd_doctor(rest)
|
|
148
|
+
else:
|
|
149
|
+
_cmd_scan_new(rest)
|
|
150
|
+
return
|
|
151
|
+
|
|
152
|
+
# Force verbose details for production debugging
|
|
153
|
+
import argparse
|
|
154
|
+
if argv is None:
|
|
155
|
+
argv = sys.argv[1:]
|
|
156
|
+
# If no --details, inject it before lib call
|
|
157
|
+
has_details = '--details' in argv or '-d' in argv
|
|
158
|
+
if not has_details:
|
|
159
|
+
argv = argv + ['--details']
|
|
160
|
+
return lib_main(argv)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
if __name__ == "__main__":
|
|
164
|
+
raise SystemExit(main() or 0)
|