Skip to main content

imgscan

这是 sapic 图床(v2)的**图片鉴黄钩子**:把已入库的图片交给可切换的服务商判定, 只返回拦截结论,不写库、不删对象。模块名 imgscan,扩展类型 moderator。

  • 钩子名(模块名):imgscan

  • 扩展类型:moderator(先发后审,不阻塞上传)

  • 适用版本:sapic 2.x

服务商

通过后台的 provider 下拉框切换,新增服务商只需在 imgscan.py 的 PROVIDERS 里注册 review / ping 两个函数。

  • qiniu(默认):七牛云内容审核,只提交色情场景 pulp,使用 AK/SK 签名

  • http:通用 Bearer Token + JSON 接口,方便接自建服务或临时替换

  • upyun:又拍云内容识别(无存储),只提交色情识别;服务是否开放以控制台为准

服务商无关的配置:

  • threshold:0~1,默认 0.8;涉黄分数达到该值判 block。填 0 或越界值会回退默认,不会把全部图片判为拦截

  • timeout:单次请求超时秒数,默认 10

安装

  • 正式版本

    $ pip install -U imgscan

  • 本地开发

    $ cd imgscan && pip install -U .

依赖:只用 Python 标准库(Qiniu 的 QBox 签名用 hmac 实现),不需要额外安装包。

七牛云配置

在 后台管理-钩子插件 中导入模块名 imgscan(或 POST /api/v2/hooks/import),然后填写:

  • qiniu_ak:七牛 AccessKey

  • qiniu_sk:七牛 SecretKey(保存后不回显)

  • qiniu_endpoint:默认 http://ai.qiniuapi.com/v3/image/censor,一般不用改

请求体固定为:

{"data": {"uri": "https://.../raw/xxx.png"}, "params": {"scenes": ["pulp"]}}

Authorization 按七牛 QBox 规则对「方法 + path + Host + Content-Type + body」做 HMAC-SHA1 + urlsafe base64,签名用的是实际发送的 body,改配置后无需额外操作。

判定时读 result.scenes.pulp:场景级 suggestion=block直接判 block; 否则逐条看 details,只有 suggestion=block 或涉黄标签(label=pulp)的条目 才计入色情分,达到 threshold 判 block。

「检查配置」按钮会用空 uri 发一次带签名的请求:HTTP 400 说明服务与签名都通了, 401 说明密钥不对,且不会消耗检测配额。

又拍云配置

使用**内容识别(无存储)**的 ClientKey / ClientSecret(不是云存储操作员账号):

  • upyun_client_key:ClientKey

  • upyun_client_secret:ClientSecret(保存后不回显)

  • upyun_endpoint:默认 https://banma.api.upyun.com/image/url/check

请求体为 {"url": "https://.../raw/xxx.png"},签名按官方文档取 POST&<URI>&<GMT Date>做 HMAC-SHA1 + Base64,放在 Authorization: UPYUN <key>:<sign>, 同时发送同值的 Date 头。默认 endpoint 不带 ?act=——接口默认就是色情识别; 若要显式带 ?act=porn,签名仍按文档用不带 query 的 URI。

响应取 porn 对象:label 为 0 正常 / 1 色情 / 2 性感,scores 依次是 正常 / 色情 / 性感概率。判定规则:label=1 直接 block,色情概率或性感概率达到 threshold 也 block;label=0 时 rate 是「正常概率」,不参与拦截判断。 「检查配置」用空 url 请求一次:400 = 服务与签名正常,401 = 密钥错误。

自定义 HTTP 配置

  • http_endpoint(必填):接收 POST JSON {"url": "..."} 的地址

  • http_token:可选,以 Authorization: Bearer <token> 发送

  • http_score_key:响应里的分数字段,支持点号路径如 data.nsfw,默认 score

接口也可以直接返回 {"verdict": "pass" | "block", "message": "..."}, 此时优先采用 verdict,忽略分数与阈值。

判定契约

review_image(image, config=None) 返回:

{"code": 0, "verdict": "block", "message": "qiniu porn: pulp 0.93"}
  • code != 0:本轮判不了(没配对外地址、密钥错误、超时、响应格式不对), 核心不会猜 pass,下一轮再试

  • verdict:只有 pass / block 有效;任一钩子 block 即拦截

  • message:写进 images.review_note,管理员在审核队列里能看到

视频默认直接 pass(本扫描器不处理视频)。多个审核钩子并存时按核心的合并规则: 任一 block 即 block,任何失败都让 pass 降级为「不落结论」。

运行

审核是**先发后审**:上传接口不受影响,入库后由定时任务扫描未审图片(默认 5 分钟一轮, 由 APScheduler 驱动)。手动跑一批:

# 在 server/ 下
../.venv/bin/flask sa review-pending -n 50

注意

  • 核心只把公开可见的图片送审;image["url"] 优先是 sapic_external_base_url + /raw/<sha>, 没配时回退到登记外链或公开副本直链。三者都拿不到时钩子返回「判不了」;审核服务商仍需能访问这个地址

  • 只提交了 pulp 场景,其他内容维度不参与判定,减少误判和费用

  • 钩子不写库、不删对象;block 只是从公开口径摘除,误判由管理员在 /api/v2/review 里恢复

  • 如果存储后端是公有读桶,src 那条 CDN 直链仍然可访问;要彻底撤回只能 purge 或把后端设为 visibility=private

  • 审核钩子运行在调度进程里,没有 Flask 请求上下文;不要依赖 request / g

Metadata

Release files for imgscan 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for imgscan 0.1.0
File Size Uploaded Provenance Actions
imgscan-0.1.0.tar.gz 14.7 kB Without provenance Details for imgscan-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for imgscan 0.1.0
File Interpreter ABI Platform Actions
imgscan-0.1.0-py3-none-any.whl Python 3 none any Details for imgscan-0.1.0-py3-none-any.whl

Total release size: 29.8 kB

Release files / imgscan-0.1.0.tar.gz

Download URL imgscan-0.1.0.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
09959ef2e6d0b7b521af8866b62233abaa436e6284b09d81df37e70f0e8625f9
BLAKE2b-256 checksum
How to use checksums
ff9f38dc42fa97bc1dfabb4bfd5c0d66aba6b9a2437bced94b7c4279b57451e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.9

Release files / imgscan-0.1.0-py3-none-any.whl

Download URL imgscan-0.1.0-py3-none-any.whl
Size 15.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca4ba943a89d71a0239831b686a3ac3e822fce94a57c4dd32b13bf117340a905
BLAKE2b-256 checksum
How to use checksums
78f5d3e51f71e9ef2a06dfa0099061ad1f1c68f283467d056c2f51ed277366b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.9

Security for imgscan 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page