Esempio n. 1
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. 2
0
def add_new_blobs(boxes, classes, confidences, blobs, frame, tracker, mcdf):
    '''
	Add new blobs or updates existing ones.
	'''
    matches = _match_boxes_new(boxes, classes, confidences, blobs)
    box2blob_matches = {m[0]: m[1] for m in matches}
    #box2blob_matches={m[1]:m[0] for m in matches}
    matched_blob_ids = set([m[1] for m in matches])
    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)

        if i in box2blob_matches:  # or use try catch?
            _id = box2blob_matches[i]
            blob = blobs[_id]
            blob.num_consecutive_detection_failures = 0

            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})

        else:  # not match_found for this box
            _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. 3
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. 4
0
def add_new_blobs(boxes, classes, confidences, blobs, frame, tracker,
                  counting_line, line_position, mcdf):
    # add new blobs or update existing ones
    matched_blob_ids = []
    for i in range(len(boxes)):
        _type = classes[i] if classes != None else None
        _confidence = confidences[i] if confidences != None else None
        _tracker = get_tracker(tracker, boxes[i], frame)

        box_centroid = get_centroid(boxes[i])
        box_area = get_area(boxes[i])
        match_found = False
        for _id, blob in blobs.items():
            if blob.counted == False and get_iou(boxes[i],
                                                 blob.bounding_box) > 0.5:
                match_found = True
                if _id not in matched_blob_ids:
                    blob.num_consecutive_detection_failures = 0
                    matched_blob_ids.append(_id)
                blob.update(boxes[i], _type, _confidence, _tracker)

                log_info(
                    'Blob updated.', {
                        '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(boxes[i], _type, _confidence, _tracker)
            blob_id = generate_vehicle_id()
            blobs[blob_id] = _blob

            log_info(
                'Blob created.', {
                    '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