4899| 3
|
[项目] 虚谷号体验(六) 人工智能案例体验:人脸识别 |
本帖最后由 szjuliet 于 2020-7-5 08:59 编辑 虚谷号体验(一)开箱及基本功能体验 虚谷号体验(二) 安装远程桌面及蓝牙设备 虚谷号体验(三) 主机模式体验 虚谷号体验(四)人工智能案例体验:文字识别 虚谷号体验(五)人工智能案例体验:动植物识别 虚谷号体验(六) 人工智能案例体验:人脸识别 摄像头获取图像来识别人脸,并输出人脸的属性:年龄、性别、表情、是否佩戴眼镜、情绪、人种、相貌、脸型等。 1.设备连接: 虚谷号连接高清电视、摄像头,通电: 2. 百度云中创建应用(如使用体验文档的KEY,则可以跳过这步)
3. 修改程序: 虚谷号U盘模式\vvBoard\Python\01.example\03.人工智能 目录下复制程序face_info.py到电脑上进行编辑: [mw_shl_code=python,true]#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Oct 18 13:02:42 2018 @author: james """ import cv2 import requests import json import threading import time import base64 import numpy as np import xugu # servo=xugu.Servo(12) # access_token="24.42017d996da0a23f3c4567b8dfd16a21.2592000.1550220300.282335-14563606" access_token ="" # API_KEY="ijoqlG1PdSsdxtXc7DNn68jh" API_KEY = "" # 输入自己的API_KEY # SECRET_KEY = "Erpr184wiWCG7ZZglFKKG3Zm3up6xUmi" SECRET_KEY = "" # 输入自己的SECRET_KEY face_num = 0 frame=None now_time=0 face_info={} def cvimg_to_b64(img): try: image = cv2.imencode('.jpg', img)[1] base64_data = str(base64.b64encode(image))[2:-1] return base64_data except Exception as e: return "error" def face_search(img64): url="https://aip.baidubce.com/rest/2.0/face/v3/search" url=url+"?access_token="+access_token data={ "image":img64, "image_type":"BASE64", "group_id_list":"test_group"} try: response=requests.post(url,files=None,data=data) res_text=response.text res_json=json.loads(res_text) return res_json except Exception: return "error" def get_ai_access_token(): url="https://aip.baidubce.com/oauth/2.0/token?grant_type=" +\ "client_credentials&client_id=%s&client_secret=%s" %(API_KEY,SECRET_KEY) try: response=requests.get(url) res_text=response.text res_json=json.loads(res_text) return str(res_json["access_token"]) except Exception: return "error" def get_face_info(img64): url="https://aip.baidubce.com/rest/2.0/face/v3/detect" url = url + "?access_token=" + access_token data = {"face_field": "age,beauty,emotion,face_shape,gender,glasses,race,qualities,expression", "image_type": "BASE64", "image": img64, "max_face_num": 5} try: response = requests.post(url,data=data) res_text=response.text res_json=json.loads(res_text) return res_json except Exception: return "error" def post_request(frame,face_num,nt): global face_info if(face_num>0) and (time.time()-nt>3): global now_time now_time=time.time() img64=cvimg_to_b64(frame) res=get_face_info(img64) print(res) try: status=res['error_msg'] except Exception: status="" if(status=="SUCCESS"): face_info['gender']= res['result']['face_list'][0]['gender']['type'] face_info['age']=res['result']['face_list'][0]['age'] face_info['beauty']=res['result']['face_list'][0]['beauty'] face_info['emotion'] = res['result']['face_list'][0]['emotion']['type'] face_info['faceshape'] = res['result']['face_list'][0]['face_shape']['type'] face_info['glasses'] = res['result']['face_list'][0]['glasses']['type'] face_info['expression'] = res['result']['face_list'][0]['expression']['type'] face_info['race'] = res['result']['face_list'][0]['race']['type'] # face_info['qualities'] = res['result']['face_list'][0]['qualities'] return else: face_info={} def faceDetect(img,face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')): size=img.shape[:2] divisor = 8 h,w=size minSize=(w//divisor,h//divisor) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.2, 1,cv2.CASCADE_SCALE_IMAGE,minSize) for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) return img,len(faces) def put_Text(cvimg,text,location,size=1): cvimg=cv2.putText(cvimg, text, location,cv2.FONT_HERSHEY_SIMPLEX, size, (51, 102, 255), 3) return cvimg def check_token(): print(int(time.time()) - 1547628298) if int(time.time()) - 1547628298 >= 2592000: global access_token token = get_ai_access_token() if token != "error": access_token = get_ai_access_token() def main(): check_token() cap = cv2.VideoCapture(0) global now_time now_time=time.time() while(True): global face_info global servo ret, frame = cap.read() if ret == True: frame1,face_num = faceDetect(frame) frame1=cv2.flip(frame1,1,dst=None) frame1=cv2.resize(frame1,(1280,800),interpolation=cv2.INTER_LINEAR) t = threading.Thread(target=post_request, args=( frame, face_num, now_time,), name='POST_REQUEST') t.start() print(face_info) if face_info=={} : frame1=put_Text(frame1,"Waiting...",(50,50)) else: try: frame1=put_Text(frame1,str(int(face_info['age'])),(300,50)) frame1=put_Text(frame1,str(face_info['gender']),(300,120)) frame1=put_Text(frame1,str(int(face_info['beauty'])),(300,190)) frame1=put_Text(frame1,"Age:",(50,50)) frame1=put_Text(frame1,"Gender:",(50,120)) frame1=put_Text(frame1,"Beauty:",(50,190)) frame1 = put_Text(frame1, str((face_info['expression'])), (300, 260)) frame1 = put_Text(frame1, str(face_info['faceshape']), (300, 330)) frame1 = put_Text(frame1, str((face_info['glasses'])), (300, 400)) frame1 = put_Text(frame1, "Expression:", (50, 260)) frame1 = put_Text(frame1, "Faceshape:", (50, 330)) frame1 = put_Text(frame1, "Glasses:", (50, 400)) frame1 = put_Text(frame1, str((face_info['emotion'])), (300, 470)) frame1 = put_Text(frame1, str(face_info['race']), (300, 540)) # frame1 = put_Text(frame1, str(int(face_info['qualities'])), (300, 610)) frame1 = put_Text(frame1, "Emotion:", (50, 470)) frame1 = put_Text(frame1, "Race:", (50, 540)) # frame1 = put_Text(frame1, "Qualities:", (50, 610)) # servo.angle(int(face_info['beauty']*3)) except Exception: pass cv2.imshow('Magic Image',frame1) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() main()[/mw_shl_code] 4. 将程序face_info上传到虚谷号桌面,同时将/home/scope/vvBoard/Python/01.example/03.人工智能/人脸识别目录下的依赖包也拖动到桌面 5. 在文件管理器中进入Desktop目录,运行LX终端 6. 输入命令python3 face_info.py,在当前目录下运行文字识别脚本 7. 很快会新建一个窗口,标题为Magic Image,用于显示采集的摄像头图像。 8. 输出结果 (不清楚为什么最后两行Emotion和Race为什么没有显示出来,检查了代码没发现问题)
参考资料: https://ai.baidu.com/download?sdkId=3,SDK下载地址 http://ai.baidu.com/docs#/Face-Python-SDK/top,SDK使用说明 至此,体验文档里要求的内容已经全部完成。 在体验过程中对虚谷好的好感和认同度不断增加。这几天也把虚谷号公众号里几篇访谈文章认认真真的读完,觉得谢作如、管雪枫等一大批中国创客教育的先行者做了许多了不起事情。由衷的感动,钦佩!! |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed