|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# $File: chaxun.py |
| 4 | + |
| 5 | +API_KEY = '187aa2ee8fc784972b7302f3b31becab' |
| 6 | +API_SECRET = 'F1czaFXaRJ5VzRcdVNZppUwJpDg4zjUY' |
| 7 | + |
| 8 | +# 导入系统库并定义辅助函数 |
| 9 | +import time |
| 10 | +from pprint import pformat |
| 11 | +from facepp import API |
| 12 | +import facepp |
| 13 | +import os |
| 14 | +#此部分是为了中文的处理 |
| 15 | +import sys |
| 16 | +reload(sys) |
| 17 | +sys.setdefaultencoding( "utf-8" ) |
| 18 | + |
| 19 | +def print_result(hint, result): |
| 20 | + def encode(obj): |
| 21 | + if type(obj) is unicode: |
| 22 | + return obj.encode('utf-8') |
| 23 | + if type(obj) is dict: |
| 24 | + return {encode(k): encode(v) for (k, v) in obj.iteritems()} |
| 25 | + if type(obj) is list: |
| 26 | + return [encode(i) for i in obj] |
| 27 | + return obj |
| 28 | + print hint |
| 29 | + result = encode(result) |
| 30 | + print '\n'.join([' ' + i for i in pformat(result, width = 75).split('\n')]) |
| 31 | + |
| 32 | +api = API(API_KEY, API_SECRET) |
| 33 | +#face = api.detection.detect(img = facepp.File('3.jpg')) |
| 34 | + |
| 35 | +# IMAGE_DIR 此部分则是选择出需要进行分析的图片。。管理员模式下不用设置实用。(可能有文件打开错误的bug,如果本身没有end.jpg的话。) |
| 36 | +TARGET_IMAGE = facepp.File('end.jpg') |
| 37 | +print ("已经读取图片,正在进行人脸识别,根据网络环境速度不定。") |
| 38 | +#识别end.jpg图中的Face |
| 39 | +rst = api.recognition.identify(group_name = 'test', img = TARGET_IMAGE) |
| 40 | +#print_result('识别结果:', rst) |
| 41 | + |
| 42 | +print '=' * 60 |
| 43 | +print '识别结果最匹配的对象为:', \ |
| 44 | + rst['face'][0]['candidate'][0]['person_name'] |
| 45 | + |
| 46 | +print ("正在写入kaoqin.txt中,当前时间为:"+" "+ time.asctime()) |
| 47 | +print '=' * 60 |
| 48 | +fp = open("kaoqin.txt",'a') |
| 49 | +fp.write("姓名:"+" "+rst['face'][0]['candidate'][0]['person_name']+"签到时间:"+" "+time.asctime()+"\n") |
| 50 | +fp.close() |
| 51 | + |
| 52 | + |
| 53 | +# 最终,删除无用的person和group |
| 54 | +#api.group.delete(group_name = 'test') |
| 55 | +#api.person.delete(person_name = FACES.iterkeys()) |
| 56 | + |
| 57 | + |
| 58 | + |
0 commit comments