python百度AI的人流量统计2区域划分

人流量统计2区域划分

# -*- coding: utf-8 -*-
# !/usr/bin/env python

import urllib
import base64
import json
import time
from flask import Flask

""" 你的 APPID AK SK """
APP_ID = '20096368'
API_KEY = 'AMVkPEj7MgKWHdglLjoYj1EL'
SECRET_KEY = 'NuhozgvzVFTKTFF2zfRYE8MF4pmxtdtR'


# 获取token
def get_token():
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=AMVkPEj7MgKWHdglLjoYj1EL&client_secret=NuhozgvzVFTKTFF2zfRYE8MF4pmxtdtR&'
    request = urllib.request.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib.request.urlopen(request)
    token_content = response.read()
    if token_content:
        token_info = json.loads(token_content)
        token_key = token_info['access_token']
    return token_key


# 保存图片
def save_base_image(img_str, filename):
    img_data = base64.b64decode(img_str)
    with open(filename, 'wb') as f:
        f.write(img_data)


# 人流量统计
# filename:原图片名(本地存储包括路径);dehazedfilename:处理后的文件保存名称(每个人打标)
def body_num_v2(filename,area,dehazedfilename):
    request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_num"

    # 二进制方式打开图片文件
    f = open(filename, 'rb')
    img = base64.b64encode(f.read())

    params = dict()
    params['image'] = img
    params['show'] = 'true'
    params['area'] = area
    params = urllib.parse.urlencode(params).encode("utf-8")
    # params = json.dumps(params).encode('utf-8')

    access_token = get_token()
    request_url = request_url + "?access_token=" + access_token
    request = urllib.request.Request(url=request_url, data=params)
    request.add_header('Content-Type', 'application/x-www-form-urlencoded')
    response = urllib.request.urlopen(request)
    content = response.read()
    if content:
        # print(content)
        content = content.decode('utf-8')
        # print(content)
        data = json.loads(content)
        # print(data)
        person_num = data['person_num']
        area_counts = data['area_counts']
        print('person_num', person_num)
        print('area_counts', area_counts)
        img_str = data['image']
        save_base_image(img_str, dehazedfilename)


area = '3,24,94,8,258,58,320,105,390,190,266,359,165,440,3,465,3,310,120,260,150,170'
body_num_v2("C:\\Users\\17983\\Desktop\\人1.jpg",area,"C:\\Users\\17983\\Desktop\\人群2_num.jpg")

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

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页