datacollector.py 4.4 KB
Newer Older
Z
zhiboniu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import copy
17
from collections import Counter
Z
zhiboniu 已提交
18 19 20 21 22 23 24 25 26


class Result(object):
    def __init__(self):
        self.res_dict = {
            'det': dict(),
            'mot': dict(),
            'attr': dict(),
            'kpt': dict(),
27
            'video_action': dict(),
Z
zhiboniu 已提交
28
            'skeleton_action': dict(),
J
JYChen 已提交
29 30 31
            'reid': dict(),
            'det_action': dict(),
            'cls_action': dict(),
32 33
            'vehicleplate': dict(),
            'vehicle_attr': dict()
Z
zhiboniu 已提交
34 35 36 37 38 39 40 41 42 43
        }

    def update(self, res, name):
        self.res_dict[name].update(res)

    def get(self, name):
        if name in self.res_dict and len(self.res_dict[name]) > 0:
            return self.res_dict[name]
        return None

Z
zhiboniu 已提交
44 45 46
    def clear(self, name):
        self.res_dict[name].clear()

Z
zhiboniu 已提交
47 48 49

class DataCollector(object):
    """
Z
zhiboniu 已提交
50
  DataCollector of Pipeline, collect results in every frames and assign it to each track ids.
Z
zhiboniu 已提交
51 52 53 54 55 56 57 58 59 60 61
  mainly used in mtmct.
  
  data struct:
  collector:
    - [id1]: (all results of N frames)
      - frames(list of int): Nx[int]
      - rects(list of rect): Nx[rect(conf, xmin, ymin, xmax, ymax)]
      - features(list of array(256,)): Nx[array(256,)]
      - qualities(list of float): Nx[float]
      - attrs(list of attr): refer to attrs for details
      - kpts(list of kpts): refer to kpts for details
Z
zhiboniu 已提交
62
      - skeleton_action(list of skeleton_action): refer to skeleton_action for details
Z
zhiboniu 已提交
63 64 65 66 67
    ...
    - [idN]
  """

    def __init__(self):
Z
zhiboniu 已提交
68
        #id, frame, rect, score, label, attrs, kpts, skeleton_action
Z
zhiboniu 已提交
69 70 71 72 73 74 75
        self.mots = {
            "frames": [],
            "rects": [],
            "attrs": [],
            "kpts": [],
            "features": [],
            "qualities": [],
Z
zhiboniu 已提交
76 77
            "skeleton_action": [],
            "vehicleplate": []
Z
zhiboniu 已提交
78 79 80 81 82 83 84
        }
        self.collector = {}

    def append(self, frameid, Result):
        mot_res = Result.get('mot')
        attr_res = Result.get('attr')
        kpt_res = Result.get('kpt')
Z
zhiboniu 已提交
85
        skeleton_action_res = Result.get('skeleton_action')
Z
zhiboniu 已提交
86
        reid_res = Result.get('reid')
Z
zhiboniu 已提交
87
        vehicleplate_res = Result.get('vehicleplate')
Z
zhiboniu 已提交
88

89 90 91 92 93 94
        rects = []
        if reid_res is not None:
            rects = reid_res['rects']
        elif mot_res is not None:
            rects = mot_res['boxes']

95
        for idx, mot_item in enumerate(rects):
Z
zhiboniu 已提交
96 97 98 99 100 101 102 103
            ids = int(mot_item[0])
            if ids not in self.collector:
                self.collector[ids] = copy.deepcopy(self.mots)
            self.collector[ids]["frames"].append(frameid)
            self.collector[ids]["rects"].append([mot_item[2:]])
            if attr_res:
                self.collector[ids]["attrs"].append(attr_res['output'][idx])
            if kpt_res:
104 105
                self.collector[ids]["kpts"].append(
                    [kpt_res['keypoint'][0][idx], kpt_res['keypoint'][1][idx]])
Z
zhiboniu 已提交
106 107 108
            if skeleton_action_res and (idx + 1) in skeleton_action_res:
                self.collector[ids]["skeleton_action"].append(
                    skeleton_action_res[idx + 1])
Z
zhiboniu 已提交
109 110
            else:
                # action model generate result per X frames, Not available every frames
Z
zhiboniu 已提交
111
                self.collector[ids]["skeleton_action"].append(None)
Z
zhiboniu 已提交
112 113 114 115 116
            if reid_res:
                self.collector[ids]["features"].append(reid_res['features'][
                    idx])
                self.collector[ids]["qualities"].append(reid_res['qualities'][
                    idx])
117
            if vehicleplate_res and vehicleplate_res['plate'][idx] != "":
Z
zhiboniu 已提交
118
                self.collector[ids]["vehicleplate"].append(vehicleplate_res[
Z
zhiboniu 已提交
119
                    'plate'][idx])
Z
zhiboniu 已提交
120 121 122

    def get_res(self):
        return self.collector
123 124 125 126 127 128 129 130 131

    def get_carlp(self, trackid):
        lps = self.collector[trackid]["vehicleplate"]
        counter = Counter(lps)
        carlp = counter.most_common()
        if len(carlp) > 0:
            return carlp[0][0]
        else:
            return None