用 Python 鉴别色色的图片
2018-05-14 本文已影响624人
伪君子_
0 前言
实话实说啊,这个标题起得就有点标题党,识别是识别,准确率就有点玄学了。
1 环境说明
Win10 系统下 Python3,编译器是 Pycharm,需要安装 nonude 这个库。
Pycharm 安装第三方库的方法。


2 代码
2.1 识别少量图片
先导入 nonude 这个库(我也很奇怪为什么 import 的是 nude)
import nude
再写出代码
print(nude.is_nude("godfather.jpg"))
print(nude.is_nude("leisheng.jpg"))
print(nude.is_nude("qiaoba.png"))
代码运行的结果居然是 False、True、True
让我们来看一下图片。



对了,我这里是直接把图片放在了项目的路径里的

如果不喜欢这样,可以换一个写法
import nude
print(nude.is_nude("E:/Images/OOXX/00mitai.jpg"))
print(nude.is_nude("E:\Images\OOXX/00zkted.jpg"))
print(nude.is_nude("E:/Images\OOXX/011idk8.jpg"))
代码运行的结果是 False、True、False



说一下,路径里面的斜杠 / 可以是反斜杠 \,如果后面有数字的话,请使用斜杠 /,不要用反斜杠 \。
2.2 识别文件夹中的图片
先导入要用的库
import glob
import itertools
from nude import Nude
这里用 glob 返回匹配指定模式的文件名
images_format = ['jpg', 'png', 'gif'] # 图片格式
images_jpg = glob.glob("E:/Images/OOXX/*.jpg") # 返回匹配指定模式的文件名
images_png = glob.glob("E:/Images/OOXX/*.png")
images_gif = glob.glob("E:/Images/OOXX/*.gif")
itertools.chain 把迭代对象串联起来,形成一个更大的迭代器,说白了就是把 images_jpg、images_png、images_gif 弄在一起,变成了images_list。
然后进行循环,在 images_list 里面一个一个对图片进行识别。
images_list = itertools.chain(images_jpg, images_png, images_gif)
for i in images_list:
print(i) # 输出照片的路径
n = Nude(i) # 对图片进行识别
n.parse()
print(n.result) # 输出结果
print(n.message) # 输出判断信息
print(n.inspect()) # 输出更加详细的判断信息
运行结果如下图

我们来看一下下图片,准确率嘛,有点玄学。

3 说明
识别是不是色色的图片的有依据的,根据给出的信息来看,是皮肤暴露的百分比来判断的,暴露的比例大于 15% 就判断为色色的图片。这里只是很粗浅的说明,还有一些我没有看懂。
nonude 的 GitHub 链接:https://github.com/gearsystems/nonude/
nonude 的最近一次更新是两年前了,感觉有点可惜,希望作者能继续维护。