def Face(filename): appid = '1256531249' secret_id = 'AKIDGK9iwx8IbgYCcoNW1IK8h27wVosg8ZfV' secret_key = 'iAMIxNxLTKuFJ7fx3uVqwlKdjbEcmhOq' bucket = 'ocrbucket-1256531249' client = Client(appid, secret_id, secret_key, bucket) client.use_http() client.set_timeout(30) print '人脸识别' if client.face_detect(CIFile(filename), 1).get('message') == 'OK': #如果监测到人脸进行如下操作 print 'indect face' # 数据库连接 #conn = pymssql.connect('localhost', 'root', '123456', 'album') db = MySQLdb.connect("localhost", "root", "123456", "album", charset='utf8') cursor = db.cursor() # 数据库查询操作 cursor.execute('SELECT name FROM names') row = cursor.fetchone() flag = 0 id = 1 # 对于每一个查询到的人名 while row: id = id + 1 # print("ID=%d, Name=%s" % (row[0], row[1])) if client.face_verify(row[0], CIFile(filename)).get('data').get( 'confidence') > 90: # 若跟这个人置信度大于90 print '\t\tget ' + row[0] client.face_addface(row[0], CIFiles([ filename, ])) # 就加脸 sql = "update picture set namelabel='" + row[ 0] + "' where pictureid='" + filename.replace( '/home/ubuntu/album/public/', "") + "'" try: cursor.execute(sql) db.commit() print 'update namelabel success' except: db.rollback() print "update namelabel error" flag = 1 break else: # 取下个人名继续判断 row = cursor.fetchone() # 若判断完仍未找到对的人 if flag == 0: client.face_newperson('people' + str(id), [ 'group1', ], CIFile(filename)) # 就添加新人 try: # 并将其插入到数据库中 cursor.execute("INSERT INTO names VALUES( '" + 'people' + str(id) + "')") # 如果没有指定autocommit属性为True的话就需要调用commit()方法 db.commit() print 'insert names success' except: db.rollback() print 'insert names error' try: cursor.execute( "UPDATE picture SET namelabel ='" + 'people' + str(id) + "' where pictureid='" + filename.replace('/home/ubuntu/album/public/', "") + "'") db.commit() print 'update picture.namelabel success' except: db.rollback() print 'update picture.namelabel success' db.close()
print( client.face_newperson( 'person111', [ 'group2', ], CIUrl('http://img3.a0bi.com/upload/ttq/20160814/1471155260063.png'), 'xiaoxin')) #创建一个Person, 使用图片file print(client.face_newperson('person211', [ 'group2', ], CIFile('./hot2.jpg'))) #将单个或者多个Face的url加入到一个Person中 print( client.face_addface( 'person111', CIUrls([ 'http://jiangsu.china.com.cn/uploadfile/2015/1102/1446443026382534.jpg', 'http://n.sinaimg.cn/fashion/transform/20160704/flgG-fxtspsa6612705.jpg' ]))) #将单个或者多个Face的file加入到一个Person中 print(client.face_addface('person211', CIFiles([ './test.jpg', ]))) #删除人脸,删除一个person下的face print(client.face_delface('person111', [ 'person111', ])) #设置信息 print(client.face_setinfo('person111', 'hello'))