使用 OpenCV 与 Face++ 人脸识别

时间:2023-05-18 17:26:20

今天看到一篇文章《使用 OpenCV 与 Face++ 实现人脸解锁》,感觉挺好玩,就照着作者的讲解,写了一下。详细内容还请看原作者文章。

 # *^_^* 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/