def add_new_objects(boxes, classes, confidences, objects, frame, tracker, mcdf,
                    overlap_threshold):
    """
    This function create new objects by the detected boxes or update existing one.
    decide to update existing one if the overlap between them is grater than the overlap_threshold
    :param boxes: the bounding boxes of the detected objects (the new ones)
    :param classes: the classes of the objects
    :param confidences: the confidence of the objects
    :param objects: list of the objects that exists (that was updated by the tracker)
    :param frame: the frame that we detect.
    :param tracker: the tracker.
    :param mcdf: the maximum detection failures, remove objects that have mcdf greater than this param.
    :param overlap_threshold: the maximum overlap between the detected object (boxes) to the tracked object (from objects) in order to update the object and not create new one.
    :return: the objects.
    """
    matched_object_ids = []
    for i, box in enumerate(boxes):
        _type = classes[i] if classes is not None else None
        _confidence = confidences[i] if confidences is not None else None
        _tracker = get_tracker(tracker, box, frame)

        match_found = False
        for _id, object in objects.items():
            if get_overlap(box, object.bounding_box) >= overlap_threshold:
                match_found = True
                if _id not in matched_object_ids:
                    object.num_consecutive_detection_failures = 0
                    matched_object_ids.append(_id)
                object.update(box, _type, _confidence, _tracker)

                logger.info('Object updated.',
                            extra={
                                'meta': {
                                    'cat': 'Object updated',
                                    'people_id': _id,
                                    'bounding_box': object.bounding_box,
                                    'type': object.type,
                                    'type_confidence': object.type_confidence,
                                },
                            })
                break
        if not match_found:
            # create new object
            _object = Object(box, _type, _confidence, _tracker)
            object_id = generate_people_id()
            objects[object_id] = _object

            logger.info('Object created.',
                        extra={
                            'meta': {
                                'cat': 'Object created',
                                'people_id': object_id,
                                'bounding_box': _object.bounding_box,
                                'type': _object.type,
                                'type_confidence': _object.type_confidence,
                            },
                        })

    objects = remove_stray_objects(objects, matched_object_ids, mcdf)
    return remove_duplicates(objects, overlap_threshold)
def remove_duplicates(objects, overlap_threshold):
    """
    Removes duplicate objects i.e objects that point to an already detected and tracked people.
    :param objects: the detected objects.
    :param overlap_threshold: the maximum overlap between the objects in order to consider one of them as duplicate.
    :return: the objects
    """
    for _id, object_a in list(objects.items()):
        for _, object_b in list(objects.items()):
            if object_a == object_b:
                break
            overlap = get_overlap(object_a.bounding_box, object_b.bounding_box)
            logger.info('overlap info:',
                        extra={
                            'meta': {
                                'cat': 'OBJECT_UPDATE',
                                'people_id_a': _id,
                                'bounding_box_a': object_a.bounding_box,
                                'people_id_b': _,
                                'bounding_box_b': object_b.bounding_box,
                                'overlap': overlap
                            },
                        })
            if overlap >= overlap_threshold and _id in objects:
                logger.info('People tracker delete',
                            extra={
                                'meta': {
                                    'cat': 'TRACKER_UPDATE',
                                    'people_id': _id,
                                    'bounding_box': object_a.bounding_box,
                                    'overlap': overlap
                                },
                            })
                del objects[_id]
    return objects
Esempio n. 3
0
def remove_duplicates(blobs):
    for blob_id, blob_a in list(blobs.items()):
        for _, blob_b in list(blobs.items()):
            if blob_a == blob_b:
                break

            if get_overlap(blob_a.bounding_box,
                           blob_b.bounding_box) >= 0.6 and blob_id in blobs:
                del blobs[blob_id]
    return blobs
Esempio n. 4
0
def remove_duplicates(blobs):
    '''
    Removes duplicate blobs i.e blobs that point to an already detected and tracked vehicle.
    '''
    for _id, blob_a in list(blobs.items()):
        for _, blob_b in list(blobs.items()):
            if blob_a == blob_b:
                break

            if get_overlap(blob_a.bounding_box, blob_b.bounding_box) >= 0.7 and _id in blobs:
                del blobs[_id]
    return blobs
Esempio n. 5
0
def _match_boxes_simple(boxes, classes, confidences, blobs):
    '''
	old algorithm used to match boxes with blobs
	I'm keep it so I can compare results
	'''
    # Note: in this algorithm multiple boxes can match the same blob
    matches = []
    for i, box in enumerate(boxes):
        for _id, blob in blobs.items():
            if get_overlap(box, blob.bounding_box) >= 0.6:
                matches.append((i, _id))
    return matches
Esempio n. 6
0
def add_new_blobs(boxes, classes, confidences, blobs, frame, tracker, mcdf):
    '''
    Add new blobs or updates existing ones.
    '''
    matched_blob_ids = []
    for i, box in enumerate(boxes):
        _type = classes[i] if classes is not None else None
        _confidence = confidences[i] if confidences is not None else None
        _tracker = get_tracker(tracker, box, frame)

        match_found = False
        for _id, blob in blobs.items():
            if get_overlap(box, blob.bounding_box) >= 0.6:
                match_found = True
                if _id not in matched_blob_ids:
                    blob.num_consecutive_detection_failures = 0
                    matched_blob_ids.append(_id)
                blob.update(box, _type, _confidence, _tracker)

                blob_update_log_meta = {
                    'label': 'BLOB_UPDATE',
                    'object_id': _id,
                    'bounding_box': blob.bounding_box,
                    'type': blob.type,
                    'type_confidence': blob.type_confidence,
                }
                if settings.LOG_IMAGES:
                    blob_update_log_meta['image'] = get_base64_image(
                        get_box_image(frame, blob.bounding_box))
                logger.debug('Blob updated.',
                             extra={'meta': blob_update_log_meta})
                break

        if not match_found:
            _blob = Blob(box, _type, _confidence, _tracker)
            blob_id = generate_object_id()
            blobs[blob_id] = _blob

            blog_create_log_meta = {
                'label': 'BLOB_CREATE',
                'object_id': blob_id,
                'bounding_box': _blob.bounding_box,
                'type': _blob.type,
                'type_confidence': _blob.type_confidence,
            }
            if settings.LOG_IMAGES:
                blog_create_log_meta['image'] = get_base64_image(
                    get_box_image(frame, _blob.bounding_box))
            logger.debug('Blob created.', extra={'meta': blog_create_log_meta})

    blobs = _remove_stray_blobs(blobs, matched_blob_ids, mcdf)
    return blobs
Esempio n. 7
0
def add_new_blobs(boxes, classes, confidences, blobs, frame, tracker, counting_line, line_position, mcdf):
    '''
    Adds new blobs or updates existing ones.
    '''
    matched_blob_ids = []
    for i, box in enumerate(boxes):
        _type = classes[i] if classes is not None else None
        _confidence = confidences[i] if confidences is not None else None
        _tracker = get_tracker(tracker, box, frame)

        box_centroid = get_centroid(box)
        match_found = False
        for _id, blob in blobs.items():
            if not blob.counted and get_overlap(box, blob.bounding_box) >= 0.7:
                match_found = True
                if _id not in matched_blob_ids:
                    blob.num_consecutive_detection_failures = 0
                    matched_blob_ids.append(_id)
                blob.update(box, _type, _confidence, _tracker)

                logger.debug('Blob updated.', extra={
                    'meta': {
                        'cat': 'BLOB_UPSERT',
                        'vehicle_id': _id,
                        'bounding_box': blob.bounding_box,
                        'type': blob.type,
                        'type_confidence': blob.type_confidence,
                        'image': get_base64_image(get_box_image(frame, blob.bounding_box)),
                    },
                })
                break

        if not match_found and not is_passed_counting_line(box_centroid, counting_line, line_position):
            _blob = Blob(box, _type, _confidence, _tracker)
            blob_id = generate_vehicle_id()
            blobs[blob_id] = _blob

            logger.debug('Blob created.', extra={
                'meta': {
                    'cat': 'BLOB_UPSERT',
                    'vehicle_id': blob_id,
                    'bounding_box': _blob.bounding_box,
                    'type': _blob.type,
                    'type_confidence': _blob.type_confidence,
                    'image': get_base64_image(get_box_image(frame, _blob.bounding_box)),
                },
            })

    blobs = remove_stray_blobs(blobs, matched_blob_ids, mcdf)
    return blobs
Esempio n. 8
0
def add_new_blobs(boxes, classes, confidences, blobs, frame, tracker, mcdf):
    matched_blob_ids = []
    for i, box in enumerate(boxes):
        _type = classes[i] if classes is not None else None
        _confidence = confidences[i] if confidences is not None else None
        _tracker = get_tracker(tracker, box, frame)

        match_found = False
        for _id, blob in blobs.items():
            if get_overlap(box, blob.bounding_box) >= 0.6:
                match_found = True
                if _id not in matched_blob_ids:
                    blob.num_consecutive_detection_failures = 0
                    matched_blob_ids.append(_id)
                blob.update(box, _type, _confidence, _tracker)

                logger.debug('Blob updated.',
                             extra={
                                 'meta': {
                                     'label': 'BLOB_UPDATE',
                                     'vehicle_id': _id,
                                     'bounding_box': blob.bounding_box,
                                     'type': blob.type,
                                     'type_confidence': blob.type_confidence,
                                 },
                             })
                break

        if not match_found:
            _blob = Blob(box, _type, _confidence, _tracker)
            blob_id = generate_vehicle_id()
            blobs[blob_id] = _blob
            logger.debug('Blob created.',
                         extra={
                             'meta': {
                                 'label': 'BLOB_CREATE',
                                 'vehicle_id': blob_id,
                                 'bounding_box': _blob.bounding_box,
                                 'type': _blob.type,
                                 'type_confidence': _blob.type_confidence,
                             },
                         })

    blobs = _remove_stray_blobs(blobs, matched_blob_ids, mcdf)
    return blobs