· 9 years ago · Oct 12, 2016, 07:30 AM
1import os
2import sys
3import json
4import base64
5from image.check import ImageCheckAPIDemo
6
7SECRET_ID = '***'
8SECRET_KEY = '***'
9BUSINESS_ID = '***'
10
11DIR_PIC = '***'
12CHECK = ImageCheckAPIDemo(SECRET_ID, SECRET_KEY, BUSINESS_ID)
13
14
15def get_files(dname=DIR_PIC):
16 for fname in os.listdir(dname):
17 path = os.path.join(dname, fname)
18 with open(path) as f:
19 encoded_string = base64.b64encode(f.read())
20 yield fname, encoded_string
21
22
23def check_one(data):
24 images = []
25 fname, pic = data
26 images.append({
27 'name': fname,
28 'type': 2,
29 'data': pic
30 })
31
32 print(fname, len(json.dumps(images)))
33
34 params = {
35 'images': json.dumps(images),
36 'account': 'python@163.com',
37 'ip': '123.115.77.137'
38 }
39
40 ret = CHECK.check(params)
41
42 if ret['code'] == 200:
43 results = ret['result']
44 for result in results:
45 print result
46 else:
47 print 'ERROR: ', ret
48
49
50if __name__ == '__main__':
51 path = sys.argv[1] if len(sys.argv) > 1 else DIR_PIC
52
53 for c in get_files(path):
54 check_one(c)