当前位置:   article > 正文

百度ai人脸检测以及处理{‘error_code‘: 222202}_pic not has face

pic not has face

在进行百度ai人脸检测检测到无人脸的时遇到的:

{‘error_code‘: 222202, ‘error_msg‘: ‘pic not has face‘, ‘log_id‘: 1017599795575, ‘timestamp‘: 1624}

 

需求:遇到没有人脸的图片数据需要进行跳过或者删除图片处理

 

一开始使用的是

  1. """有bug代码"""
  2. if response:
  3. for i in response.json()["result"]["face_list"]:
  4. probability = i["face_probability"]
  5. if probability > 0.8:
  6. print("人脸的概率高",probability )
  7. return "same"
  8. """
  9. 遇到没有人脸的图片数据报错
  10. for i in response.json()["result"]["face_list"]:
  11. TypeError: 'NoneType' object is not subscriptable
  12. """

看到NoneType 还以为是昨天刚遇到的问题,于是就用 is None 来进行解决但是还是没有解决

  1. """有bug代码代码"""
  2. if response is None:
  3. print("没有人脸")
  4. """
  5. 此时不管是有人脸还是没有人脸都走不进来
  6. """

最后重回思考了下,不管有没有人脸的图片数据response都是有数据的,所以此时使用 is None 来判断response是不合适的

 

以下是对response数据进行分析得来的思路过程

  1. """"有人脸的response"""
  2. {'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 5575201050012, 'timestamp': 1624160360, 'cached': 0, 'result': {'face_num': 1, 'face_list': [{'face_token': 'ae01b8fe10afa0c9a536c27c58f62b04', 'location': {'left': 37.94, 'top': 69.26, 'width': 216, 'height': 199, 'rotation': 3}, 'face_probability': 1, 'angle': {'yaw': 0.5, 'pitch': 11.86, 'roll': 1.05}}]}}
  3. """没有人脸的response"""
  4. {'error_code': 222202, 'error_msg': 'pic not has face', 'log_id': 1010011015575, 'timestamp': 1624160360, 'cached': 0, 'result': None}
  5. """
  6. 进行观察可以发现
  7. 有人脸的response[error_msg]='SUCCESS'
  8. 没有人脸的response[error_msg]='pic not has face'
  9. 于是我们可以使用这个数值来对图片进行一个过滤
  10. """

 

最终的代码:实现了没有人脸的处理,以及人脸概率的再次比对

  1. if response:
  2. # print("没有人脸")
  3. print(response.json())
  4. if response.json()["error_msg"] == "SUCCESS":
  5. print("有人脸")
  6. for i in response.json()["result"]["face_list"]:
  7. probability = i["face_probability"]
  8. if probability > 0.8:
  9. print("人脸的概率高",probability )
  10. return True
  11. else:
  12. print("人脸的概率低", probability)
  13. return False
  14. if response.json()["error_msg"] == "pic not has face":
  15. print("没有人脸")
  16. return False

 

完整的人脸检测代码:

  1. import requests
  2. import json
  3. import base64
  4. class BaiduAI():
  5. def __init__(self,AK="RxlPVRY7OS0PvxrG4pqZEBuT",SK="dSHHDw7N6nRsbhAd29AMz6XHX0Gzazoz"):
  6. self.AK = AK
  7. self.SK = SK
  8. self.access_tocken = self.get_acess_token()
  9. def get_acess_token(self):
  10. host = 'https://aip.baidubce.com/oauth/2.0/token? grant_type=client_credentials&client_id={}&client_secret={}'.format(self.AK,self.SK)
  11. response = requests.get(host)
  12. if response:
  13. return response.json()["access_token"]
  14. def face_detect(self,img_path):
  15. '''
  16. 人脸检测与属性分析
  17. '''
  18. f = open(img_path, 'rb')
  19. img = base64.b64encode(f.read())
  20. f.close()
  21. print("img_path", img_path)
  22. request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
  23. params = json.dumps({"image": str(img, 'utf-8'),
  24. "image_type": "BASE64",
  25. # "face_field": "gender,age,beauty,race,expression,emotion",
  26. "face_type": "LIVE"
  27. })
  28. print("params")
  29. request_url = request_url + "?access_token=" + self.access_tocken
  30. headers = {'content-type': 'application/json'}
  31. response = requests.post(request_url, data=params, headers=headers)
  32. if response:
  33. # print("没有人脸")
  34. print(response.json())
  35. if response.json()["error_msg"] == "SUCCESS":
  36. print("有人脸")
  37. for i in response.json()["result"]["face_list"]:
  38. probability = i["face_probability"]
  39. if probability > 0.8:
  40. print("人脸的概率高",probability )
  41. return True
  42. else:
  43. print("人脸的概率低", probability)
  44. return False
  45. if response.json()["error_msg"] == "pic not has face":
  46. print("没有人脸")
  47. return False
  48. if __name__ == "__main__":
  49. ai = BaiduAI()
  50. ret = ai.face_detect("final_un_mask/unmask.0.jpg")
  51. ret = ai.face_detect("final_un_mask/unmask.6185.jpg")

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/703404
推荐阅读
相关标签
  

闽ICP备14008679号