人脸识别代码_Python人脸识别源代码可自动识别出年龄、性别等
生活随笔
收集整理的這篇文章主要介紹了
人脸识别代码_Python人脸识别源代码可自动识别出年龄、性别等
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
利用百度AI進(jìn)行人臉識別,可自動識別出年齡、性別等信息,準(zhǔn)確率非常高。
運(yùn)行程序前請安裝需要的模塊。
import base64from aip import AipFaceimport cv2# 配置百度aip參數(shù)APP_ID = '19484855'API_KEY = 'V2mDOleCsk3yEE6P5MgVwSjI'SECRET_KEY = 'RbRMAuPmz8QpDweikrbpfGQjXUm7HiCD'a_face = AipFace(APP_ID, API_KEY, SECRET_KEY)image_type = 'BASE64'options = {'face_field': 'age,gender,beauty', "max_face_num": 10}max_face_num = 10def get_file_content(file_path): """獲取文件內(nèi)容""" with open(file_path, 'rb') as fr: content = base64.b64encode(fr.read()) return content.decode('utf8')def face_score(file_path): """臉部識別分?jǐn)?shù)""" result = a_face.detect(get_file_content(file_path), image_type, options) return result# 圖片地址,圖片與程序同一目錄下file_path = "timg.jpg"result = face_score(file_path)# #從文件讀取圖像并轉(zhuǎn)為灰度圖像img = cv2.imread(file_path)# 圖片放文字# 設(shè)置文件的位置、字體、顏色等參數(shù)font = cv2.FONT_HERSHEY_DUPLEX# font = ImageFont.truetype("simhei.ttf", 20, encoding="utf-8")color = (0, 0, 255)for item in result['result']['face_list']: x = int(item['location']['left']) y = int(item['location']['top']) w = item['location']['width'] h = item['location']['height'] age = item['age'] beauty = item['beauty'] gender = item['gender']['type'] cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 3) cv2.putText(img, 'age:%s' % age, (x, y + h + 10), font, 1, color, 1) cv2.putText(img, 'beauty:%s' % beauty, (x, y + h + 30), font, 1, color, 1) cv2.putText(img, 'gender:%s' % gender, (x, y + h + 50), font, 1, color, 1)cv2.imshow('Image', img)# 按任意鍵退出key = cv2.waitKey()if key == 27: # 銷毀所有窗口 cv2.destroyAllWindows()總結(jié)
以上是生活随笔為你收集整理的人脸识别代码_Python人脸识别源代码可自动识别出年龄、性别等的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python编写一个程序、实现文件的复制
- 下一篇: 方舟原始恐惧代码_源代码分支管理模式丨中