赞
踩
在进行百度ai人脸检测检测到无人脸的时遇到的:
{‘error_code‘: 222202, ‘error_msg‘: ‘pic not has face‘, ‘log_id‘: 1017599795575, ‘timestamp‘: 1624}
需求:遇到没有人脸的图片数据需要进行跳过或者删除图片处理
一开始使用的是
- """有bug代码"""
- if response:
- for i in response.json()["result"]["face_list"]:
- probability = i["face_probability"]
- if probability > 0.8:
- print("人脸的概率高",probability )
- return "same"
-
-
-
- """
- 遇到没有人脸的图片数据报错
- for i in response.json()["result"]["face_list"]:
- TypeError: 'NoneType' object is not subscriptable
- """
看到NoneType 还以为是昨天刚遇到的问题,于是就用 is None 来进行解决但是还是没有解决
- """有bug代码代码"""
- if response is None:
- print("没有人脸")
-
- """
- 此时不管是有人脸还是没有人脸都走不进来
- """
最后重回思考了下,不管有没有人脸的图片数据response都是有数据的,所以此时使用 is None 来判断response是不合适的
以下是对response数据进行分析得来的思路过程
- """"有人脸的response"""
-
- {'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}}]}}
-
-
- """没有人脸的response"""
-
- {'error_code': 222202, 'error_msg': 'pic not has face', 'log_id': 1010011015575, 'timestamp': 1624160360, 'cached': 0, 'result': None}
-
- """
- 进行观察可以发现
- 有人脸的response[error_msg]='SUCCESS'
- 没有人脸的response[error_msg]='pic not has face'
- 于是我们可以使用这个数值来对图片进行一个过滤
- """
最终的代码:实现了没有人脸的处理,以及人脸概率的再次比对
- if response:
- # print("没有人脸")
- print(response.json())
- if response.json()["error_msg"] == "SUCCESS":
- print("有人脸")
- for i in response.json()["result"]["face_list"]:
- probability = i["face_probability"]
- if probability > 0.8:
- print("人脸的概率高",probability )
- return True
- else:
- print("人脸的概率低", probability)
- return False
- if response.json()["error_msg"] == "pic not has face":
- print("没有人脸")
- return False

完整的人脸检测代码:
- import requests
- import json
- import base64
- class BaiduAI():
- def __init__(self,AK="RxlPVRY7OS0PvxrG4pqZEBuT",SK="dSHHDw7N6nRsbhAd29AMz6XHX0Gzazoz"):
- self.AK = AK
- self.SK = SK
- self.access_tocken = self.get_acess_token()
- def get_acess_token(self):
- host = 'https://aip.baidubce.com/oauth/2.0/token? grant_type=client_credentials&client_id={}&client_secret={}'.format(self.AK,self.SK)
- response = requests.get(host)
- if response:
- return response.json()["access_token"]
-
- def face_detect(self,img_path):
- '''
- 人脸检测与属性分析
- '''
- f = open(img_path, 'rb')
- img = base64.b64encode(f.read())
- f.close()
- print("img_path", img_path)
- request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
- params = json.dumps({"image": str(img, 'utf-8'),
- "image_type": "BASE64",
- # "face_field": "gender,age,beauty,race,expression,emotion",
- "face_type": "LIVE"
- })
- print("params")
- request_url = request_url + "?access_token=" + self.access_tocken
- headers = {'content-type': 'application/json'}
- response = requests.post(request_url, data=params, headers=headers)
- if response:
- # print("没有人脸")
- print(response.json())
- if response.json()["error_msg"] == "SUCCESS":
- print("有人脸")
- for i in response.json()["result"]["face_list"]:
- probability = i["face_probability"]
- if probability > 0.8:
- print("人脸的概率高",probability )
- return True
- else:
- print("人脸的概率低", probability)
- return False
- if response.json()["error_msg"] == "pic not has face":
- print("没有人脸")
- return False
-
-
- if __name__ == "__main__":
- ai = BaiduAI()
- ret = ai.face_detect("final_un_mask/unmask.0.jpg")
- ret = ai.face_detect("final_un_mask/unmask.6185.jpg")

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。