-
Notifications
You must be signed in to change notification settings - Fork 16
/
batchapi.py
285 lines (239 loc) · 8.75 KB
/
batchapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!bin/evn python
# -*-coding:utf8-*-
import base64
import sys
import os
import logging
import datetime
import re
import multiprocessing
# from pylab import *
from PIL import Image
import cv2
from bceocrapi import BceOCRAPI
from bin.python.models.youyuan_log import YouyuanLog
from nude import Nude
import imagehash
from bin.python.utils import logger
from bin.python.models.redis_results import RedisResults
import matlab_wrapper
Image.LOAD_TRUNCATED_IMAGES = True
reload(sys)
sys.setdefaultencoding('utf-8')
# IMAGE_DIR = "/Users/fengxuting/Downloads/testphoto/"
IMAGE_DIR = "public/uploads/api/"
class Api:
def __init__(self):
self.IMAGE_HASH = ""
# 获取图片哈希值
def get_image_hash(self,file):
img = Image.open(file)
h = str(imagehash.dhash(img))
return h
# 人脸识别
def face(self,file):
matlab = matlab_wrapper.MatlabSession()
matlab.put('filename', file)
matlab.put('IMAGE_DIR', IMAGE_DIR)
matlab.eval('face')
count = matlab.get('count')
has_crop = matlab.get('has_crop')
return int(count),int(has_crop)==1
#黑白处理
def blackWhite(self,filename):
image_file = Image.open(IMAGE_DIR+filename) # open colour image
#exception : Premature end of JPEG file . IOError: image file is truncated (1 bytes not processed)
try:
image_file = image_file.convert('L') # convert image to black and white
except Exception as e:
raise
return IMAGE_DIR+filename
dst_path = IMAGE_DIR+"wb"+filename
image_file.save(dst_path)
return dst_path
#数字识别
def ocr(self,file):
ocr = BceOCRAPI("02fbe03acf3042a1b40e067bba1971f7", "bb1d4aafe7924fc0829fc33fa26b3347");
#黑白处理
# newImg = IMAGE_DIR +file
newImg = self.blackWhite(file)
#图像压缩处理
disImg = IMAGE_DIR +"ocrdis"+file
newImg = self.resizeImg(ori_img=newImg,dst_img=disImg,dst_w=1600,dst_h=1600,save_q=100)
with open(newImg, 'rb') as f:
content = f.read()
content = base64.b64encode(content)
try:
# ocr
# result = ocr.get_ocr_text(content, language='CHN_ENG')
result = ocr.get_ocr_text(content, language='ENG')
# print("file:"+file+"----------result:"+result)
return result
except Exception as e:
raise
# 图片如果宽或高大于300则等比例压缩
def resizeImg(self,**args):
args_key = {'ori_img': '', 'dst_img': '', 'dst_w': '', 'dst_h': '', 'save_q': 75}
arg = {}
for key in args_key:
if key in args:
arg[key] = args[key]
im = Image.open(arg['ori_img'])
ori_w, ori_h = im.size
widthRatio = heightRatio = None
ratio = 1
if (ori_w and ori_w > arg['dst_w']) or (ori_h and ori_h > arg['dst_h']):
if arg['dst_w'] and ori_w > arg['dst_w']:
widthRatio = float(arg['dst_w']) / ori_w # 正确获取小数的方式
if arg['dst_h'] and ori_h > arg['dst_h']:
heightRatio = float(arg['dst_h']) / ori_h
if widthRatio and heightRatio:
if widthRatio < heightRatio:
ratio = widthRatio
else:
ratio = heightRatio
if widthRatio and not heightRatio:
ratio = widthRatio
if heightRatio and not widthRatio:
ratio = heightRatio
newWidth = int(ori_w * ratio)
newHeight = int(ori_h * ratio)
else:
newWidth = ori_w
newHeight = ori_h
im.resize((newWidth, newHeight), Image.ANTIALIAS).save(arg['dst_img'], quality=arg['save_q'])
return arg['dst_img']
#鉴别黄色图片
def isnude(self,file):
#图像压缩处理
imagePath = IMAGE_DIR + file
nudeImg = IMAGE_DIR +"nude_"+file
# self.resizeImg(ori_img=imagePath,dst_img=nudeImg,dst_w=300,dst_h=300,save_q=100)
# faces = self.face("nude_"+file)
# self.cropImg("nude_"+file,faces)
n = Nude(nudeImg)
# n.setFaces(faces)
# n.resize(1000,1000)
n.parse()
# print n.result
return 1 if n.result else 0
# 统计数字个数
def countdigits(self,s):
digitpatt = re.compile('\d')
return len(digitpatt.findall(s))
# 删除图片
def delImg(self,file):
#黑白的
wbImg = IMAGE_DIR+"wb"+file
ocrImg300 = IMAGE_DIR +"dis"+file
#大于1600的
ocrImg1600 = IMAGE_DIR +"ocrdis"+file
nudeImg = IMAGE_DIR +"nude_"+file
if os.path.isfile(wbImg):
os.remove(wbImg)
if os.path.isfile(ocrImg300):
os.remove(ocrImg300)
if os.path.isfile(ocrImg1600):
os.remove(ocrImg1600)
# 鉴黄裁剪图
if os.path.isfile(nudeImg):
os.remove(nudeImg)
#删除原文件
# os.remove(IMAGE_DIR+file)
def one(self,file):
filepath = IMAGE_DIR+file
self.resizeImg(ori_img=filepath,dst_img=filepath,dst_w=480,dst_h=640,save_q=100)
if(os.path.isfile(filepath)):
# self.IMAGE_HASH = self.get_image_hash(filepath)
# redis_result = self.get_result_from_redis(self.IMAGE_HASH)
# if(redis_result):
# #删除图像
# self.delImg(file)
# print redis_result
# sys.exit(0)
is_pass = 1
#人脸检测
count,has_crop = self.face(file)
print("count:")
print(count)
print("has_crop:")
print(has_crop)
# 如果人脸不是1则 ocr和鉴黄不用检测
if(count!=1):
l = -1
is_nude = -1
is_pass = 0
else:
#ocr
text = ""
text = self.ocr(file)
text = text.encode("utf-8")
l = self.countdigits(text)
if (l > 6):
is_nude = -1
is_pass = 0
else:
#鉴黄 没有截图通过
if not has_crop:
is_nude = 0
is_pass = 1
else:
is_nude = self.isnude(file)
if(is_nude==1):
is_pass = 0
else:
is_pass = 1
#删除图像
self.delImg(file)
# print {"face_count":len(fc),"digital_count":l,"is_nude":is_nude,"pass":is_pass}
result = str(count)+","+str(l)+","+str(is_nude)+","+str(is_pass)
# 结果保存redis数据库
self.save_redis(self.IMAGE_HASH,result)
print result
return {"is_face":count,"is_qq":l,"is_nude":is_nude,"is_pass":is_pass}
else:
print("error:",file, "is not a img file")
return {"is_face":-1,"is_qq":-1,"is_nude":-1,"is_pass":-1}
# 保存redis
def save_redis(self,hash,result):
rr = RedisResults()
rr.save(hash,result)
# redis数据是否存在,并返回检测结果
def get_result_from_redis(self,hash):
rr = RedisResults()
return rr.get(hash)
# 多进程
def main(self):
count = multiprocessing.cpu_count()-1
pool = multiprocessing.Pool(processes=count)
# images = Images().findByNude(1)
youyuan = YouyuanLog().findByFace(0)
print("file count:"+str(len(youyuan)))
# sys.exit(0)
for f in youyuan:
# print f['name']
if(not os.path.isfile(IMAGE_DIR + f['name'])):
print(IMAGE_DIR + f['name'], " not exist")
else:
# self.detect(f['name'])
# pool.map(self.detect,f['name'] )
pool.apply_async(detect, (f['name'],)) # 维持执行的进程总数为processes,当一个进程执行完毕后会添加新的进程进去
# detect(f['name'])
print "Mark~ Mark~ Mark~~~~~~~~~~~~~~~~~~~~~~"
pool.close()
pool.join() # 调用join之前,先调用close函数,否则会出错。执行完close后不会有新的进程加入到pool,join函数等待所有子进程结束
print "Sub-process(es) done."
# 检测并保存数据库
def detect(file):
print file
api = Api()
result = api.one(file)
# 更新数据库
YouyuanLog().update(file, result)
if __name__ == '__main__':
api = Api()
api.main()
# api.one(sys.argv[1])
# api.one("9d27d550-4beb-11e6-aefd-4f827560e966.png")
# api.one("91787150-4bf1-11e6-aefd-4f827560e966.png")
pass