根据位置(从json)绘制人脸框图

今天做了一个人脸检测的网站,调用了face++的API。效果还不错。

github地址:https://github.com/JameyWoo/face_detect


网站界面

在这里插入图片描述
在这里插入图片描述


API返回的是一个json字符串,其中有一项属性是人脸的位置
在这里插入图片描述
解析json文件的时候会出现一个问题是:它返回的引号是单引号。有两种解决方法,一种是替换,另一种是先使用eval+json.dumps方法,再使用json.loads方法。

json文件的解析参考代码

str = '''
{'attributes': {'age': {'value': 24},
                             'beauty': {'female_score': 75.175,
                                        'male_score': 75.144},
                             'blur': {'blurness': {'threshold': 50.0,
                                                   'value': 9.103},
                                      'gaussianblur': {'threshold': 50.0,
                                                       'value': 9.103},
                                      'motionblur': {'threshold': 50.0,
                                                     'value': 9.103}},
                             'emotion': {'anger': 32.633,
                                         'disgust': 0.33,
                                         'fear': 0.087,
                                         'happiness': 0.863,
                                         'neutral': 65.645,
                                         'sadness': 0.122,
                                         'surprise': 0.319},
                             'ethnicity': {'value': 'INDIA'},
                             'eyestatus': {'left_eye_status': {'dark_glasses': 0.007,
                                                               'no_glass_eye_close': 0.198,
                                                               'no_glass_eye_open': 80.633,
                                                               'normal_glass_eye_close': 0.295,
                                                               'normal_glass_eye_open': 18.677,
                                                               'occlusion': 0.191},
                                           'right_eye_status': {'dark_glasses': 6.296,
                                                                'no_glass_eye_close': 0.182,
                                                                'no_glass_eye_open': 92.223,
                                                                'normal_glass_eye_close': 0.003,
                                                                'normal_glass_eye_open': 1.248,
                                                                'occlusion': 0.048}},
                             'facequality': {'threshold': 70.1,
                                             'value': 0.006},
                             'gender': {'value': 'Male'},
                             'glass': {'value': 'None'},
                             'headpose': {'pitch_angle': -6.716119,
                                          'roll_angle': -14.588619,
                                          'yaw_angle': -78.17652},
                             'mouthstatus': {'close': 0.211,
                                             'open': 97.742,
                                             'other_occlusion': 2.047,
                                             'surgical_mask_or_respirator': 0.0},
                             'skinstatus': {'acne': 11.584,
                                            'dark_circle': 1.84,
                                            'health': 21.948,
                                            'stain': 3.273},
                             'smile': {'threshold': 50.0, 'value': 4.688}},
              'face_rectangle': {'height': 140,
                                 'left': 558,
                                 'top': 284,
                                 'width': 140},
              'face_token': 'd9b1aae1a746a57d2d91859b8b864f27'}
'''

# str = str.replace("'", "\"")
print(type(str))
data = json.loads(json.dumps(eval(str)))
print(data)

写一个函数解析出返回的json中的人脸位置信息,添加到一个列表中以便批量处理。

import json
def get_locations(json_name):
    with open(json_name, 'r') as file:
        txt = file.read()
        # data = json.loads(json.dumps(eval(txt))) # 两种方法解决单引号问题
        data = json.loads(txt.replace('\'', '"'))
        print(type(data))
    locations = []
    for each in data['faces']:
        height = each['face_rectangle']['height']
        left = each['face_rectangle']['left']
        top = each['face_rectangle']['top']
        width = each['face_rectangle']['width']
        locations.append([height, left, top, width])
    return locations

使用PIL库绘制人脸框图
参考 https://blog.csdn.net/caobin0825/article/details/80338438

from PIL import Image

def draw4lines(location, filename): # location为元素为[height, left, top, width](人脸位置)的列表
    img = Image.open(filename)
    line = (255, 0, 0, 0)
    for each in location:
        height, left, top, width = tuple(each)
        # 加粗
        for i in [top, top-1, top - 2, height + top, height + top + 1, height + top + 2]:
            for j in range(left - 2, left + width + 3):
                img.putpixel((j, i), line)
        for i in [left, left - 1, left - 2, left + width, left + width + 1, left + width + 2]:
            for j in range(top, top + height):
                img.putpixel((i, j), line)
    img.save("result_" + filename)

有一下几点记录

1、API返回的属性有很多,人脸的位置只是其中一个。其它属性还有比如人种、颜值打分等等。挺有意思的。不过图片中人太多了无法完全检测出来。

2、注意文件的名字,卧槽,一个py文件本来是find_faces.py,结果在php里面写成了find_face.py,一直没发现导致出问题!好在我是火眼金睛最后还是发现了。

3、注意文件改打点的打点,不打点就是/目录下了。

4、限制img显示的大小,用width,height设置即可。如果不限制各种图片各种大小,会导致网站很难看。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值