使用 OpenCV 与 Face++ 人脸识别
2016-04-13 23:45
399 查看
今天看到一篇文章《使用 OpenCV 与 Face++ 实现人脸解锁》,感觉挺好玩,就照着作者的讲解,写了一下。详细内容还请看原作者文章。
参考文献:《使用 OpenCV 与 Face++ 实现人脸解锁》:http://python.jobbole.com/84666/
# *^_^* coding:utf-8 *^_^* from __future__ import print_function __author__ = 'stone' __date__ = '16-4-13' """ http://www.cnblogs.com/asmer-stone/p/5389383.html """ import cv2 import numpy as np import time import requests import os import mimetypes face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") API_KEY = 'xxx' API_SECRET = 'xxx' BASE_URL = 'http://apicn.faceplusplus.com/v2' //用opencv检测人脸的函数,注释部分内容是检测并画出人脸区域。 def detect_face(): cap = cv2.VideoCapture(0) next_capture_time = time.time() faces = [] if not cap.isOpened(): print("Capture Opening ERROR!") while 1: ret, img = cap.read() # img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # if next_capture_time < time.time(): # next_capture_time += 0.1 # faces = face_cascade.detectMultiScale(img_gray, 1.3, 5) # if faces is not None: # for x, y, w, h in faces: # img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.imshow("face_detect", img) k = cv2.waitKey(1) & 0xFF if k == ord('s'): cv2.imwrite("image/face2.jpg", img) //打开摄像头按下‘s’键即可保存图片 if k == 27: break cap.release() cv2.destroyAllWindows() def update_img(file_dir, oneface=True): //上传图片到face++ 并返回face_id url = '%s/detection/detect?api_key=%s&api_secret=%s&attribute=none' % (BASE_URL, API_KEY, API_SECRET) if oneface: url += '&mode=oneface' files = {'img': (os.path.basename(file_dir), open(file_dir, 'rb'), mimetypes.guess_type(file_dir)[0])} r = requests.post(url, files=files) faces = r.json().get('face') if faces is None: print("No face") else: return faces[0]['face_id'] def compare(faceID1, faceID2): //比较两张图片中的人脸的相似度函数 url = '%s/recognition/compare?api_key=%s&api_secret=%s&face_id1=%s&face_id2=%s' % ( BASE_URL, API_KEY, API_SECRET, faceID1, faceID2) r = requests.get(url) return r.json() if __name__ == "__main__": faceID1 = update_img('image/lena.jpg') faceID2 = update_img('image/17-1m.bmp') if faceID2 and faceID1: compare_json = compare(faceID1, faceID2) print(compare_json) if (compare_json['similarity'] > 90): //相似在90以上打印yes,否则no print("yes") else: print("no") else: print("Something wrong with the pictures!")
参考文献:《使用 OpenCV 与 Face++ 实现人脸解锁》:http://python.jobbole.com/84666/
相关文章推荐
- full backup of Linux OS
- openGL学习笔记2——视图和模型变换
- 1.1 Linux是什么
- iOS使用Category添加@property变量
- Ubuntu下建立openwrt-linux交叉编译环境
- arm9+linux fl2440 实现RT3070无线网卡AP模式并使开发板接入Wifi上网
- linux内核I2C子系统学习(三)
- Windows客户端 SSH 远程连接Windows服务器(PowerShell Server)
- 20135302魏静静——linux课程第八周实验及总结
- ubuntu使用下载的deb文件制作本地源
- centos内核编译与其签名机制
- 恢复软件环境时遇到的关于opencv的几个错误
- 所有W版本的函数都在wchar.h文件(_wfopen),和stdlib.h文件(wcstombs),和stdio.h文件(vwprintf)
- 相比较Windows,Linux系统的优点体现在哪里?
- Handler、Looper,Message传递机制(方法)
- 【转载】tomcat结合nginx使用小结
- OpenSSL
- Hadoop集群搭建
- tomcat 能在控制台成功启动,程序确定无错误,但运行结果是404错误。
- Hadoop学习之FileSystem