示例#1
0
def add_new_blobs(boxes, classes, confidences, blobs, frame, tracker,
                  counting_line, line_position, mcdf):
    # add new blobs to existing blobs
    matched_blob_ids = []
    for _idx in range(len(boxes)):
        _type = classes[_idx] if classes != None else None
        _confidence = confidences[_idx] if confidences != None else None

        box_centroid = get_centroid(boxes[_idx])
        box_area = get_area(boxes[_idx])
        match_found = False
        for _id, blob in blobs.items():
            if blob.counted == False and get_iou(boxes[_idx],
                                                 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[_idx], _type, _confidence, frame,
                    tracker)  # TODO: update blob w/o creating temp blob
                blob.update(temp_blob.bounding_box, _type, _confidence,
                            temp_blob.tracker)
                break

        if not match_found and not is_passed_counting_line(
                box_centroid, counting_line, line_position):
            _blob = create_blob(boxes[_idx], _type, _confidence, frame,
                                tracker)
            blob_id = generate_vehicle_id()
            blobs[blob_id] = _blob

    blobs = remove_stray_blobs(blobs, matched_blob_ids, mcdf)
    return blobs
示例#2
0
def add_new_blobs(boxes, blobs, frame, tracker, current_blob_id, counting_line,
                  line_position, mcdf):
    # add new blobs to existing blobs
    matched_blob_ids = []
    for box in boxes:
        box_centroid = get_centroid(box)
        box_area = get_area(box)
        match_found = False
        for _id, blob in blobs.items():
            if blob.counted == False and box_contains_point(
                    box, blob.centroid):
                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(
                    box, frame,
                    tracker)  # TODO: update blob w/o creating temp blob
                blob.update(temp_blob.bounding_box, temp_blob.tracker)
                break

        if not match_found and not is_passed_counting_line(
                box_centroid, counting_line, line_position):
            _blob = create_blob(box, frame, tracker)
            blobs[current_blob_id] = _blob
            current_blob_id += 1

    blobs = remove_stray_blobs(blobs, matched_blob_ids, mcdf)
    return blobs, current_blob_id
示例#3
0
 def __init__(self, _bounding_box, _tracker):
     self.bounding_box = _bounding_box
     self.centroid = get_centroid(_bounding_box)
     self.area = get_area(_bounding_box)
     self.tracker = _tracker
     self.num_consecutive_tracking_failures = 0
     self.counted = False
示例#4
0
 def update(self, _bounding_box, _tracker=None):
     self.bounding_box = _bounding_box
     self.centroid = get_centroid(_bounding_box)
     self.area = get_area(_bounding_box)
     self.frames += 1
     if _tracker:
         self.tracker = _tracker
示例#5
0
 def update(self, _bounding_box, _tracker=None):
     self.bounding_box = _bounding_box
     self.centroid = get_centroid(_bounding_box)
     self.area = get_area(_bounding_box)
     self.classID = get_classID(_bounding_box)
     if _tracker:
         self.tracker = _tracker
示例#6
0
 def update(self, _bounding_box, _type=None, _confidence=None, _tracker=None):
     self.bounding_box = _bounding_box
     self.type = _type if _type != None else self.type
     self.type_confidence = _confidence if _confidence !=None else self.type_confidence
     self.centroid = get_centroid(_bounding_box)
     self.area = get_area(_bounding_box)
     if _tracker:
         self.tracker = _tracker
示例#7
0
 def __init__(self, _bounding_box, _tracker, _type):
     self.bounding_box = _bounding_box
     self.centroid = get_centroid(_bounding_box)
     self.area = get_area(_bounding_box)
     self.vehicle_type = _type
     self.tracker = _tracker
     self.num_consecutive_tracking_failures = 0
     self.num_consecutive_detection_failures = 0
     self.counted = False
示例#8
0
 def __init__(self, _bounding_box, _type, _confidence, _tracker):
     self.bounding_box = _bounding_box
     self.type = _type
     self.type_confidence = _confidence
     self.centroid = get_centroid(_bounding_box)
     self.area = get_area(_bounding_box)
     self.tracker = _tracker
     self.num_consecutive_tracking_failures = 0
     self.num_consecutive_detection_failures = 0
     self.counted = False
     self.position_first_detected = tuple(self.centroid)
示例#9
0
def add_new_blobs(boxes, blobs, frame, tracker, current_blob_id):
    # add new blobs to existing blobs
    for box in boxes:
        box_centroid = get_centroid(box)
        box_area = get_area(box)
        match_found = False
        for _id, blob in blobs.items():
            if (blob.area >= box_area and box_contains_point(blob.bounding_box, box_centroid)) \
                    or (box_area >= blob.area and box_contains_point(box, blob.centroid)):
                match_found = True
                temp_blob = create_blob(
                    box, frame,
                    tracker)  # TODO: update blob w/o creating temp blob
                blob.update(temp_blob.bounding_box, temp_blob.tracker)
                break

        if not match_found:
            _blob = create_blob(box, frame, tracker)
            blobs[current_blob_id] = _blob
            current_blob_id += 1
    return blobs, current_blob_id
示例#10
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
        #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
                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
                cv2.putText(frame, 'person' + str(_id), (x, y - 2),
                            cv2.FONT_HERSHEY_DUPLEX, 0.5, (0, 0, 255), 1,
                            cv2.LINE_AA)
            else:
                blob.num_consecutive_tracking_failures += 1

            if blob.num_consecutive_tracking_failures >= MAX_CONSECUTIVE_TRACKING_FAILURES:
                del blobs[_id]

        # rerun detection, add new blobs
        if frame_counter >= DETECTION_FRAME_RATE:
            boxes = get_bounding_boxes(frame)

            for box in boxes:
                box_centroid = get_centroid(box)
                match_found = False
                for _id, blob in blobs.items():
                    dist = np.linalg.norm(
                        np.array(box_centroid) - np.array(blob.centroid))
                    if dist <= 5:  # 5 pixels
                        match_found = True
                        tracker = cv2.TrackerKCF_create()
                        tracker.init(frame, tuple(box))
                        blob.update(box, tracker)
                        break

                if not match_found:
                    blob_id += 1
                    tracker = cv2.TrackerCSRT_create()
                    tracker.init(frame, tuple(box))
def add_new_blobs(boxes, blobs, frame, tracker, current_blob_id, counting_line,
                  line_position, mcdf, detector):
    path = 'detectors\yolo'
    labelsPath = os.path.join(path, 'coco.names')
    LABELS = open(labelsPath).read().strip().split("\n")
    # add new blobs to existing blobs
    matched_blob_ids = []
    if detector == 'yolo':
        for box in boxes:
            #print(boxes)
            box_centroid = get_centroid(box[0])
            box_area = get_area(box[0])
            match_found = False
            for _id, blob in blobs.items():
                #print(blobs.items())
                if blob[0].counted == False and box_contains_point(
                        box[0], blob[0].centroid):
                    #((blob.area >= box_area and box_contains_point(blob.bounding_box, box_centroid)) \
                    #or (box_area >= blob.area and box_contains_point(box[0], blob.centroid))):
                    match_found = True
                    if _id not in matched_blob_ids:

                        blob[0].num_consecutive_detection_failures = 0

                        matched_blob_ids.append(_id)
                    temp_blob = create_blob(
                        box[0], frame,
                        tracker)  # TODO: update blob w/o creating temp blob
                    blob[0].update(temp_blob.bounding_box, temp_blob.tracker)
                    break

            if not match_found and not is_passed_counting_line(
                    box_centroid, counting_line, line_position):
                _blob = create_blob(box[0], frame, tracker)
                blobs[current_blob_id] = _blob, LABELS[box[1][0]]
                current_blob_id += 1
    else:
        for box in boxes:
            #print(boxes)
            box_centroid = get_centroid(box)
            box_area = get_area(box)
            match_found = False
            for _id, blob in blobs.items():
                #print(blobs.items())
                if blob.counted == False and box_contains_point(
                        box, blob.centroid):
                    #((blob.area >= box_area and box_contains_point(blob.bounding_box, box_centroid)) \
                    #or (box_area >= blob.area and box_contains_point(box[0], blob.centroid))):
                    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(
                        box, frame,
                        tracker)  # TODO: update blob w/o creating temp blob
                    blob.update(temp_blob.bounding_box, temp_blob.tracker)
                    break

            if not match_found and not is_passed_counting_line(
                    box_centroid, counting_line, line_position):
                _blob = create_blob(box, frame, tracker)
                blobs[current_blob_id] = _blob
                current_blob_id += 1

    blobs = remove_stray_blobs(blobs, matched_blob_ids, mcdf)
    return blobs, current_blob_id
示例#13
0
 def update(self, _bounding_box, _tracker=None):
     self.bounding_box = _bounding_box
     self.centroid = get_centroid(_bounding_box)
     if _tracker:
         self.tracker = _tracker
示例#14
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 = []
    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