人脸检测预测年龄性别
发布时间
阅读量:
阅读量
python3或python2环境。
2.安装百度包
pip install baidu-aip
python2 环境下:
# -*- coding: utf-8 -*-
# -- coding: UTF-8 --
# coding = UTF-8
# -*- coding: utf-8 -*-
import base64
import matplotlib.pyplot as plt
import cv2
import pandas as pd
from PIL import Image
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
import numpy as np
from aip import AipFace
import time
import dlib
import threading
def frame2base64(frame):
img = Image.fromarray(frame) # 将每一帧转为Image
output_buffer = BytesIO() # 创建一个BytesIO
img.save(output_buffer, format='JPEG') # 写入output_buffer
byte_data = output_buffer.getvalue() # 在内存中读取
base64_data = base64.b64encode(byte_data) # 转为BASE64
return base64_data # 转码成功 返回base64编码
def face_detection(face_data,im):
for i in face_data['result']['face_list']:
left = max(int(i['location']['left']),0)
top = max(int(i['location']['top']),0)
width = int(i['location']['width'])
height = int(i['location']['height'])
gender = str(i['gender']['type'])
age = str(i['age'])
cv2.rectangle(im, (left, top), (left+width , top+height),(0,0,255),2)
cv2.putText(im,gender+age, (left+10, top+10), cv2.FONT_HERSHEY_SIMPLEX,
0.7, (0, 255, 0), 1, cv2.LINE_AA)
return im
video_capture = cv2.VideoCapture(0)
while True :
ret, frame = video_capture.read()
frame = cv2.flip(frame, 1)
if cv2.waitKey(1) == ord('Q'):
break
APP_ID = 'test1'
API_KEY = '9OWZWm5tMrecGxYm4hSNbtYL'
SECRET_KEY = 'Y7Qw0dWX4u1B0WM76tiTrnM4tv17pvMQ'
imageType = 'BASE64'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
resized_image = cv2.resize(frame, (0, 0), fx=1, fy=1)
base64_2 = frame2base64(resized_image)
base64_2 = str(base64_2)
options = {}
options["max_face_num"] = 10
options["face_type"] = "LIVE"
options["liveness_control"] = "NORMAL"
options["face_field"] = "age,gender,beauty,"
face_data = client.detect(base64_2, imageType, options)
print face_data
if face_data['error_msg'] == 'SUCCESS':
frame = face_detection(face_data, resized_image)
cv2.imshow('fda', frame)
全部评论 (0)
还没有任何评论哟~
