调用android相机,现实人脸识别

末蓝、 2022-12-20 05:53 241阅读 0赞

准备工作:

  1. 安装opencv
  2. 注册百度API应用
  3. 获取appid,app_key,secrt_key并且获取access_token
  4. 下载百度图像识别sdk或者使用API调用

分两大块
核心思路:

  1. 使用opencv库cv2调用摄像头功能,使用ip连接到手机,循环获取一帧画面。
  2. 保存的一帧图像 使用base64.b64encode()解析为base64编码文件。
  3. 使用百度人脸识别api或者sdk,调用后识别返回结果:戴口罩,男女,五官的坐标等等。

    import cv2
    import base64
    def capture():

    1. url = 'rtsp://admin:admin@192.168.5.27:8554/live'
    2. cap = cv2.VideoCapture(url) # 带有摄像头的笔记本用户将url替换为 0 即可
    3. while(cap.isOpened()):
    4. ret, frame = cap.read() # frame为一帧图像,当frame为空时,ret返回false,否则为true
    5. cv2.imshow('frame',frame)
    6. # if cv2.waitKey(1) & 0xFF == ord('r'):
    7. # cv2.imwrite('C:/Users/Administrator/Desktop/我的照片.jpg', frame)
    8. # print('写入成功!')
    9. if cv2.waitKey(1) & 0xFF == ord('q'):
    10. cv2.imwrite('C:/Users/Administrator/Desktop/my.jpg', frame)
    11. print('写入成功!')
    12. break
    13. cap.release() # release the capture
    14. cv2.destroyAllWindows()
  1. # -------------------------------人脸检测------------------------------------------------
  2. # encoding:utf-8
  3. import requests
  4. def detect():
  5. '''
  6. 人脸检测与属性分析
  7. '''
  8. request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
  9. params = {
  10. 'image':file_64,
  11. 'image_type':'BASE64'
  12. }
  13. access_token = '24.b23aafe39fc60508b8c92a9dd38a0571.2592000.1607615861.282335-22960104'
  14. request_url = request_url + "?access_token=" + access_token
  15. headers = {
  16. 'content-type': 'application/json'}
  17. response = requests.post(request_url, data=params, headers=headers)
  18. if response:
  19. # print (response.json())
  20. for i in response.json()['result']['face_list']:
  21. print(i)
  22. if __name__=='__main__':
  23. capture()
  24. # -------------------------------图片准备------------------------------------------------
  25. with open('C:/Users/Administrator/Desktop/my.jpg', 'rb')as file:
  26. file_64 = base64.b64encode(file.read())
  27. detect()

运行代码效果图:
在这里插入图片描述
通过代码预定的Q键,结束捕捉,写入文件,然后转换base64,调用api解析,返回数据:

  1. 写入成功!
  2. {
  3. 'face_token': '1f0eb2764eeb9f21c98b2a0d715deb26',
  4. 'location': {
  5. 'left': 210.46, 'top': 163.33, 'width': 182, 'height': 183, 'rotation': 0},
  6. 'face_probability': 1,
  7. 'angle': {
  8. 'yaw': 0.77, 'pitch': 4.73, 'roll': -1.05}}

发表评论

表情:
评论列表 (有 0 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读