基於face++的人臉檢測(一)

畢設剛開始搞,所有還是參考了一些網友的現有代碼。
首先可以先去face++上註冊,有免費試用的,我覺得還不錯。註冊之後會有個key和secret
這個是我註冊得到的,你們也可以自己去申請

參考了http://blog.csdn.net/tinyzhao/article/details/55224183 的API調用代碼,可以實現輸出檢測結果的字典。
這個是我的圖片的檢測結果
我打算使用opencv把人臉表示出來。
下面是完整的代碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/1/1 16:41
# @Author  : He Hangjiang
# @Site    : 
# @File    : face++例程.py
# @Software: PyCharm

import requests
from json import JSONDecoder
import cv2

http_url = "https://api-cn.faceplusplus.com/facepp/v3/detect"
key = "2PX56nNhKpVd57QNKc9sQt9ASRczWlIr" 
secret = "7gwhn-gVp6J3we_vdj_bIlL1bGpuZdlp"
filepath = "face.jpg"

data = {"api_key": key, "api_secret": secret, "return_landmark": "1"}
files = {"image_file": open(filepath, "rb")}
response = requests.post(http_url, data=data, files=files)

req_con = response.content.decode('utf-8')
req_dict = JSONDecoder().decode(req_con)

print(req_dict)

#進過測試前面的程序會返回一個字典,其中指出了人臉所在的矩形的位置和大小等,所以直接進行標註
# mydict = eval(req_dict)
faces = req_dict["faces"]
faceNum = len(faces)
print("識別到了%d個人臉"%(faceNum))

img = cv2.imread(filepath)

for i in range(faceNum):
    face_rectangle = faces[i]['face_rectangle']
    width =  face_rectangle['width']
    top =  face_rectangle['top']
    left =  face_rectangle['left']
    height =  face_rectangle['height']
    start = (left, top)
    end = (left+width, top+height)
    color = (55,255,155)
    thickness = 3
    cv2.rectangle(img, start, end, color, thickness)

cv2.namedWindow("識別後")
cv2.imshow("識別後", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

# print type(resp)

參考博客:
http://blog.csdn.net/tinyzhao/article/details/55224183
http://blog.csdn.net/hongbin_xu/article/details/74981819

發佈了27 篇原創文章 · 獲贊 12 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章