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', 'vehicle_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_vehicle_id() blobs[blob_id] = _blob blog_create_log_meta = { 'label': 'BLOB_CREATE', 'vehicle_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
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
def test_generate_vehicle_id(): v_id = generate_vehicle_id() assert type(v_id) is str, "vehicle id is a string" assert v_id.startswith('veh_'), "vehicle id starts with 'veh_'" assert len(v_id) == 4 + 32, "vehicle id is 36 characters in length"
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 #print (confidences) if confidences != []: _confidence = confidences[i] else: _confidence=None box_centroid = get_centroid(boxes[i]) box_area = get_area(boxes[i]) match_found = False for _id, blob in blobs.items(): #print (_id,blob) 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) temp_blob = create_blob(boxes[i], _type, _confidence, frame, tracker) # TODO: update blob w/o creating temp blob blob.update(temp_blob.bounding_box, _type, _confidence, temp_blob.tracker) #blob.trajectory # Create a sequence of points to make a contour #contour=cv2.contourArea([(100,100),(200,200),(500,500)]) #cv2.pointPolygonTest([(100,100),(200,200),(500,500)], box_centroid, false) #area_of_left_left = np.array([[1, 1], [10, 50], [50, 50]], dtype=np.int32) # area_of_left_straight=np.array([[1, 1], [10, 50], [50, 50]], dtype=np.int32) # area_of_left_right=np.array([[1, 1], [10, 50], [50, 50]], dtype=np.int32) # area_of_left=np.array([[1, 1], [10, 50], [50, 50]], dtype=np.int32) #left_left = object_in_polygon(frame, area_of_left_left,box_centroid) #print(box_centroid) #print(left_left) log_info('Blob updated.', { 'event': 'BLOB_UPSERT', 'vehicle_id': _id, 'bounding_box': blob.bounding_box, 'type': blob.type, 'type_confidence': blob.type_confidence, 'center': box_centroid, #'direction': direction #'trajectory':trajectory.append #'image': get_base64_image(get_box_image(frame, blob.bounding_box)) }) f = './ProcessRecords/Blobupdated58.txt' with open(f, "a") as file: file.write( 'BLOB_UPSERT' + '-' + 'id' + str(_id) + '-' + 'bounding_box'+str(blob.bounding_box)+'-'+'type' + str(blob.type) + '-' + 'type_confidence' + str( blob.type_confidence) + '-' + 'center' + str( box_centroid) + "\n") break if not match_found and not is_passed_counting_line(box_centroid, counting_line, line_position): _blob = create_blob(boxes[i], _type, _confidence, frame, tracker) blob_id = generate_vehicle_id() blobs[blob_id] = _blob log_info('Blob created.', { 'event': 'BLOB_UPSERT', 'vehicle_id': blob_id, 'bounding_box': _blob.bounding_box, 'type': _blob.type, 'type_confidence': _blob.type_confidence, 'center': box_centroid #'image': get_base64_image(get_box_image(frame, _blob.bounding_box)) }) f = './ProcessRecords/Blobcreated58.txt' with open(f, "a") as file: file.write( 'BLOB_UPSERT' + '-' + 'id' + str(blob_id) + '-' + 'bounding_box' + str( _blob.bounding_box) + '-' + 'type' + str(_blob.type) + '-' + 'type_confidence' + str( _blob.type_confidence) + '-' + 'center' + str(box_centroid) + "\n") blobs = remove_stray_blobs(blobs, matched_blob_ids, mcdf) return blobs
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
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 = [] print(classes) print(boxes) for i in range(len(boxes)): if classes != []: print(classes[i]) _type = classes[i] else: _type=None print (confidences) if confidences != []: _confidence = confidences[i] else: _confidence=None box_centroid = get_centroid(boxes[i]) box_area = get_area(boxes[i]) match_found = False for _id, blob in blobs.items(): #print (_id,blob) 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) temp_blob = create_blob(boxes[i], _type, _confidence, frame, tracker) # TODO: update blob w/o creating temp blob blob.update(temp_blob.bounding_box, _type, _confidence, temp_blob.tracker) #blob.trajectory # Create a sequence of points to make a contour #contour=cv2.contourArea([(100,100),(200,200),(500,500)]) #cv2.pointPolygonTest([(100,100),(200,200),(500,500)], box_centroid, false) log_info('Blob updated.', { 'event': 'BLOB_UPSERT', 'vehicle_id': _id, 'bounding_box': blob.bounding_box, 'type': blob.type, 'type_confidence': blob.type_confidence, 'center': box_centroid, #'direction': direction #'trajectory':trajectory.append #'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 = create_blob(boxes[i], _type, _confidence, frame, tracker) blob_id = generate_vehicle_id() blobs[blob_id] = _blob log_info('Blob created.', { 'event': 'BLOB_UPSERT', 'vehicle_id': blob_id, 'bounding_box': _blob.bounding_box, 'type': _blob.type, 'type_confidence': _blob.type_confidence, 'centere': box_centroid #'image': get_base64_image(get_box_image(frame, _blob.bounding_box)) }) blobs = remove_stray_blobs(blobs, matched_blob_ids, mcdf) return blobs