python调用BaiDu-Api和opencv进行人脸检测

BaiDu-API人脸检测:

import cv2  
from aip import AipFace  
""" 你的 APPID AK SK """  
APP_ID = ' '  
API_KEY = ' '  
SECRET_KEY = ' '  
client = AipFace(APP_ID, API_KEY, SECRET_KEY)  
def get_file_content(filePath):  
    with open(filePath, 'rb') as fp:  
        return fp.read()  
image = get_file_content(r'2.jpg')  
""" 如果有可选参数 """  
options = {}  
options["max_face_num"] = 30  
""" 带参数调用人脸检测 """  
result=client.detect(image, options)  
print(result['result_num'])  
img=cv2.imread(r'2.jpg')  
for i in xrange(result['result_num']):  
    x=result['result'][i]['location']['left']  
    h=result['result'][i]['location']['height']  
    y=result['result'][i]['location']['top']-int(h*0.5)  
    w=result['result'][i]['location']['width']  
    cv2.rectangle(img,(x,y),(x+w,y+h+int(h*0.5)),(0,0,255),2)  
cv2.imshow("result",img)  
cv2.waitKey(0) 

opencv人脸检测:

import cv2  
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')  
img = cv2.imread('2.jpg')  
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  
faces = face_cascade.detectMultiScale(gray, 1.1, 3)  
for (x,y,w,h) in faces:  
  img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)  
cv2.imshow('img',img)  
cv2.waitKey(0) 

BaiDu-API脸检测结果:


opencv人脸检测结果:


猜你喜欢

转载自blog.csdn.net/jkjj2015/article/details/80059174
今日推荐