crowdnet基于paddlepaddle

该博客介绍了一个使用PaddlePaddle框架复现CrowdNet模型的人群密度估计Demo,尽管效果一般,但详细探讨了论文中的实现细节。
摘要由CSDN通过智能技术生成

 做人群密度估计的demo,基本复现了 crowdnet,效果一般,具体细节看论文实现

 
from matplotlib import pyplot as plt
from paddle import fluid
import paddle
import json
import random, sys, time
# from scipy import misc
import numpy as np
import cv2
import scipy.spatial, scipy.ndimage

json_stage1_path = '/home/aistudio/data/cus_data/annotation_train_stage1.json'
json_stage2_path = '/home/aistudio/data/cus_data/annotation_train_stage2.json'

def create_density_map(annos, imshape):
    gt = np.zeros(imshape, dtype='uint8')

    for dot in annos:
        try:
            gt[int(dot[1]), int(dot[0])] = 1
        except IndexError:
            print (dot[1], dot[0], sys.exc_info())

    density = np.zeros(gt.shape, dtype=np.float32)
    gt_count = np.count_nonzero(gt)
    if gt_count == 0:
        return density
    pts = np.array(zip(np.nonzero(gt)[1], np.nonzero(gt)[0]))
    # leafsize = 2048
    # tree = scipy.spatial.KDTree(pts.copy(), leafsize=leafsize)
    # k = gt_count
    # if gt_count>=5:
    #     k = 5
    # distances, locations = tree.query(pts, k=k, eps=20.)

    for i, pt in enumerate(pts):
        pt2d = np.zeros(gt.shape, dtype=np.float32)
        pt2d[pt[1], pt[0]] = 1.
        if gt_count > 1:
            # d_avg = distances[i].sum() / float(k-1)
            # sigma = d_avg * 0.18 /
            # # sigma = distances[i][1] * 1
            # print(sigma)
            sigma=10
        else:
            sigma = np.average(np.array(gt.shape)) / 2. /2.
        density += scipy.ndimage.filters.gaussian_filter(pt2d, sigma, mode='constant')
    return density

with open(json_stage1_path, 'r') as f:
    dict1 = json.loads(f.read())
with open(json_stage2_path, 'r') as f:
    dict2 = json.loads(f.read())

if dict1 is not None:
    l1 = [(key['name'], key['type'], key['annotation'], key['num'], key['id'], key['ignore_region']) for key in
          dict1['annotations']]  # 2859
    print(len(l1))
if dict2 is not None:
    l2 = [(key['name'], key['type'], key['annotation'], key['num'], key['id'], key['ignore_region']) for key in
          dict2['annotations']]  # 2859
    print(len(l2))
l1.extend(l2)  # 6477
print(len(l1))
train_data, test_data = l1[:6000], l1[6000:]  # [xxxx][yyyy]
index_np = np.empty(shape=(len(l1), 2), dtype='|S41')

resize_shape = (256,256)

for i, d in enumerate(l1):
#         print(d[0])
    img = cv2.imread('/home/aistudio/data/cus_data/train/' + d[0].split('/')[-1])
#         print(img)
    if d[5]:
        pts = np.array([[anno['x'], anno['y']] for anno in d[5][0]], dtype=np.int32)
        cv2.fillPoly(img, [pts], (0, 0, 0))

    original_shape = img.shape[:2]
    img = cv2.resize(img, resize_shape).astype('float32') / 255.0
    annos = []
    if d[1] == 'bbox':
        annos = [((anno['x'] + anno['w'] / 2) * resize_shape[1] / original_shape[1],
                  (anno[&#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值