Exemplo n.º 1
0
    def __init__(self, initial_frame, detector, tracker, droi, show_droi, mcdf,
                 mctf, di, counting_lines, show_counts):
        self.frame = initial_frame  # current frame of video
        self.detector = detector
        self.tracker = tracker
        self.droi = droi  # detection region of interest
        self.show_droi = show_droi
        self.mcdf = mcdf  # maximum consecutive detection failures
        self.mctf = mctf  # maximum consecutive tracking failures
        self.detection_interval = di
        self.counting_lines = counting_lines

        self.blobs = {}
        self.f_height, self.f_width, _ = self.frame.shape
        self.frame_count = 0  # number of frames since last detection
        self.counts = {
            counting_line['label']: {}
            for counting_line in counting_lines
        }  # counts of vehicles by type for each counting line
        self.show_counts = show_counts

        # create blobs from initial frame
        droi_frame = get_roi_frame(self.frame, self.droi)
        _bounding_boxes, _classes, _confidences = get_bounding_boxes(
            droi_frame, self.detector)
        self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences,
                                   self.blobs, self.frame, self.tracker,
                                   self.mcdf)
    def __init__(self, initial_frame, detector, tracker, droi, show_droi, mcdf,
                 mctf, di, cl_position):
        self.frame = initial_frame  # current frame of video
        self.detector = detector
        self.tracker = tracker
        self.droi = droi  # detection region of interest
        self.show_droi = show_droi
        self.mcdf = mcdf  # maximum consecutive detection failures
        self.mctf = mctf  # maximum consecutive tracking failures
        self.di = di  # detection interval
        self.cl_position = cl_position  # counting line position

        self.blobs = OrderedDict()
        self.blob_id = 1
        self.f_height, self.f_width, _ = self.frame.shape
        self.frame_count = 0  # number of frames since last detection
        self.vehicle_count = 0  # number of vehicles counted
        self.counting_line = None if cl_position == None else get_counting_line(
            self.cl_position, self.f_width, self.f_height)

        # create blobs from initial frame
        droi_frame = get_roi_frame(self.frame, self.droi)
        initial_bboxes = get_bounding_boxes(droi_frame, self.detector)
        for box in initial_bboxes:
            _blob = create_blob(box, frame, self.tracker)
            self.blobs[self.blob_id] = _blob
            self.blob_id += 1
Exemplo n.º 3
0
    def count(self, frame):
        self.frame = frame

        blobs_list = list(self.blobs.items())
        # update blob trackers
        blobs_list = Parallel(n_jobs=NUM_CORES, prefer='threads')(
            delayed(update_blob_tracker)(blob, blob_id, self.frame)
            for blob_id, blob in blobs_list)
        self.blobs = dict(blobs_list)

        for blob_id, blob in blobs_list:
            # count vehicle if it has crossed a counting line
            blob, self.counts = attempt_count(blob, blob_id,
                                              self.counting_lines, self.counts)
            self.blobs[blob_id] = blob

            # remove blob if it has reached the limit for tracking failures
            if blob.num_consecutive_tracking_failures >= self.mctf:
                del self.blobs[blob_id]

        if self.frame_count >= self.detection_interval:
            # rerun detection
            droi_frame = get_roi_frame(self.frame, self.droi)
            _bounding_boxes, _classes, _confidences = get_bounding_boxes(
                droi_frame, self.detector)

            self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences,
                                       self.blobs, self.frame, self.tracker,
                                       self.mcdf)
            self.blobs = remove_duplicates(self.blobs)
            self.frame_count = 0

        self.frame_count += 1
Exemplo n.º 4
0
    def __init__(self, initial_frame, detector, tracker, droi, show_droi, mcdf,
                 mctf, di, cl_position):
        self.frame = initial_frame  # current frame of video
        self.detector = detector
        self.tracker = tracker
        self.droi = droi  # detection region of interest
        self.show_droi = show_droi
        self.mcdf = mcdf  # maximum consecutive detection failures
        self.mctf = mctf  # maximum consecutive tracking failures
        self.di = di  # detection interval
        self.cl_position = cl_position  # counting line position

        self.blobs = OrderedDict()
        self.f_height, self.f_width, _ = self.frame.shape
        self.frame_count = 0  # number of frames since last detection
        self.vehicle_count = 0  # number of vehicles counted
        self.types_counts = OrderedDict()  # counts by vehicle type
        self.counting_line = None if cl_position == None else get_counting_line(
            self.cl_position, self.f_width, self.f_height)

        # create blobs from initial frame
        droi_frame = get_roi_frame(self.frame, self.droi)
        _bounding_boxes, _classes, _confidences = get_bounding_boxes(
            droi_frame, self.detector)
        #print (droi_frame)
        self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences,
                                   self.blobs, self.frame, self.tracker,
                                   self.counting_line, self.cl_position,
                                   self.mcdf)
Exemplo n.º 5
0
    def __init__(self, initial_frame, detector, tracker, droi, show_droi, mcdf,
                 mctf, di, counting_lines):
        self.frame = initial_frame
        self.detector = detector
        self.tracker = tracker
        self.droi = droi
        self.show_droi = show_droi
        self.mcdf = mcdf
        self.mctf = mctf
        self.di = di
        self.counting_lines = counting_lines

        self.blobs = {}
        self.f_height, self.f_width, _ = self.frame.shape
        self.frame_count = 0
        self.counts = {
            counting_line['label']: {}
            for counting_line in counting_lines
        }

        droi_frame = get_roi_frame(self.frame, self.droi)
        _bounding_boxes, _classes, _confidences = get_bounding_boxes(
            droi_frame)
        print(_bounding_boxes)
        self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences,
                                   self.blobs, self.frame, self.tracker,
                                   self.mcdf)
Exemplo n.º 6
0
    def count(self, frame):
        self.frame = frame
        counts_dict = {}
        blobs_list = list(self.blobs.items())
        blobs_list = Parallel(n_jobs=num_cores, prefer='threads')(
            delayed(update_blob_tracker)(blob, blob_id, self.frame)
            for blob_id, blob in blobs_list)
        self.blobs = dict(blobs_list)

        for blob_id, blob in blobs_list:
            blob, self.counts = attempt_count(blob, blob_id,
                                              self.counting_lines, self.counts)
            self.blobs[blob_id] = blob

            if blob.num_consecutive_tracking_failures >= self.mctf:
                del self.blobs[blob_id]

        if self.frame_count >= self.di:
            droi_frame = get_roi_frame(self.frame, self.droi)
            _bounding_boxes, _classes, _confidences = get_bounding_boxes(
                droi_frame)
            self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences,
                                       self.blobs, self.frame, self.tracker,
                                       self.mcdf)
            self.blobs = remove_duplicates(self.blobs)
            self.frame_count = 0

        self.frame_count += 1
    def count(self, frame):
        log = []
        self.frame = frame

        for _id, blob in list(self.blobs.items()):
            # update trackers
            success, box = blob.tracker.update(self.frame)
            if success:
                blob.num_consecutive_tracking_failures = 0
                blob.update(box)
            else:
                blob.num_consecutive_tracking_failures += 1

            # count vehicles that have left the frame if no counting line exists
            # or those that have passed the counting line if one exists
            if (self.counting_line == None and
                    (blob.num_consecutive_tracking_failures == self.mctf or blob.num_consecutive_detection_failures == self.mcdf) and
                    not blob.counted) \
                        or \
                    (self.counting_line != None and
                    is_passed_counting_line(blob.centroid, self.counting_line, self.cl_position) and
                    not blob.counted):
                blob.counted = True
                self.vehicle_count += 1
                # classes_of_interest = ['bicycle', 'car', 'motorcycle', 'bus', 'truck', 'person']
                if blob.vehicle_type == 'bicycle':
                    self.bicycle_count += 1
                elif blob.vehicle_type == 'car':
                    self.car_count += 1
                elif blob.vehicle_type == 'truck':
                    self.truck_count += 1
                elif blob.vehicle_type == 'bus':
                    self.bus_count += 1
                log.append({
                    'blob_id': _id,
                    'count': self.vehicle_count,
                    'datetime': datetime.now()
                })

            if blob.num_consecutive_tracking_failures >= self.mctf:
                # delete untracked blobs
                del self.blobs[_id]

        if self.frame_count >= self.di:
            # rerun detection
            droi_frame = get_roi_frame(self.frame, self.droi)
            boxes, classes_types = get_bounding_boxes(droi_frame,
                                                      self.detector)
            self.blobs, current_blob_id = add_new_blobs(
                boxes, classes_types, self.blobs, self.frame, self.tracker,
                self.blob_id, self.counting_line, self.cl_position, self.mcdf)
            self.blob_id = current_blob_id
            self.blobs = remove_duplicates(self.blobs)
            self.frame_count = 0

        self.frame_count += 1

        return log
Exemplo n.º 8
0
    def __init__(self, initial_frame, detector, tracker, droi, show_droi, mcdf, mctf, di, counting_lines, speed_estimation_lines , show_counts,frames_processed, processing_frame_rate, roads,veh_light, ped_light, camera_height, focal_length, pixel_length, resolution, vanishing_point):
        self.frame = initial_frame # current frame of video
        self.detector = detector
        self.tracker = tracker
        self.droi = droi # detection region of interest
        self.show_droi = show_droi
        self.mcdf = mcdf # maximum consecutive detection failures
        self.mctf = mctf # maximum consecutive tracking failures
        self.detection_interval = di

        self.counting_lines = counting_lines
        self.roads = roads
        self.speed_estimation_lines = speed_estimation_lines

        self.blobs = {}
        self.blobID = 0
        self.blobDistance = 0.0
        self.f_height, self.f_width, _ = self.frame.shape
        self.frame_count = 0 # number of frames since last detection
        self.counts = {counting_line['label']: {} for counting_line in counting_lines} # counts of vehicles by type for each counting line
        self.show_counts = show_counts
        self.processing_frame_rate = processing_frame_rate
        self.frames_processed = frames_processed
        self.waiting_zone = create_waiting_zone(self.counting_lines,self.droi)
        self.isWaiting = 0
        self.veh_light = veh_light
        self.ped_light = ped_light
        self.extend_flag = False
        self.reduce_flag = False
        self.extend_notification = 0
        self.reduce_notification = 0
        self.jaywalker_flag = False
        self.not_slowing_down_flag = False
        
        self.camera_height = camera_height
        self.focal_length = focal_length
        self.pixel_length= pixel_length
        self.resolution = resolution
        self.vanishing_point = vanishing_point
# =============================================================================
#         self.nearestBlobPosition = (0.0,0.0)
#         self.nearestBlobLastFramePosition = (0.0,0.0)
#         self.nearestBlobDistance = 0.0
#         self.nearestBlobLastFrameDistance=0.0
#         self.nearestBlob1FrameDistance=[]
#         self.NearestBlob = []
#         self.NearestBlob_remaining_time = 0.0
# =============================================================================
        
        # create blobs from initial frame((3) times)
        for x in range(6):
            self.blobID += 1
            droi_frame = get_roi_frame(self.frame, self.droi)
            _bounding_boxes, _classes, _confidences = get_bounding_boxes(droi_frame, self.detector)
            self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences, self.blobs, self.frame, self.tracker, self.mcdf, self.blobID)
Exemplo n.º 9
0
    def count(self, frame, dict1, frames_count, fps):
        self.frame = frame
        seconds = frames_count / fps

        video_time = str(datetime.timedelta(seconds=seconds))

        blobs_list = list(self.blobs.items())
        # update blob trackers
        blobs_list = Parallel(n_jobs=NUM_CORES, prefer='threads')(
            delayed(update_blob_tracker)(blob, blob_id, self.frame)
            for blob_id, blob in blobs_list)
        self.blobs = dict(blobs_list)

        for blob_id, blob in blobs_list:
            if (dict1.get(blob_id) is None):
                print('No value Present for Object Id : ' + blob_id)
                dict1[blob_id] = blob.type_confidence
                print('Updated Map')
                print("duration in seconds:", seconds)
                print("video time:", video_time)

            else:
                confidencePercentage = dict1.get(blob_id)
                if (blob.type_confidence > confidencePercentage):
                    dict1.update({blob_id: blob.type_confidence})
                    print('Confidence updated for Object' + blob_id +
                          'with value ' + str(blob.type_confidence))

            # count object if it has crossed a counting line

            blob, self.counts = attempt_count(blob, blob_id,
                                              self.counting_lines, self.counts)
            self.blobs[blob_id] = blob
            # remove blob if it has reached the limit for tracking failures
        if blob.num_consecutive_tracking_failures >= self.mctf:
            del self.blobs[blob_id]

        if self.frame_count >= self.detection_interval:
            # rerun detection
            droi_frame = get_roi_frame(self.frame, self.droi)
            _bounding_boxes, _classes, _confidences = get_bounding_boxes(
                droi_frame, self.detector)

            self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences,
                                       self.blobs, self.frame, self.tracker,
                                       self.mcdf)
            self.blobs = remove_duplicates(self.blobs)
            self.frame_count = 0

        self.frame_count += 1
Exemplo n.º 10
0
    def count(self, frame):
        self.frame = frame

        blobs_list = list(self.blobs.items())
        time0 = time.time()
        # update blob trackers
        blobs_list = [
            update_blob_tracker(blob, blob_id, self.frame)
            for blob_id, blob in blobs_list
        ]
        # blobs_list = Parallel(n_jobs=num_cores, prefer='threads')(
        #     delayed(update_blob_tracker)(blob, blob_id, self.frame) for blob_id, blob in blobs_list
        # )
        tracking_time_ms = (time.time() - time0) * 1000
        print("%d - %.3f" % (len(blobs_list), tracking_time_ms))
        self.running_average(tracking_time_ms, is_detection=False)
        self.blobs = dict(blobs_list)

        for blob_id, blob in blobs_list:
            # count vehicle if it has crossed a counting line
            blob, self.counts = attempt_count(blob, blob_id,
                                              self.counting_lines, self.counts)
            self.blobs[blob_id] = blob

            # remove blob if it has reached the limit for tracking failures
            if blob.num_consecutive_tracking_failures >= self.mctf:
                del self.blobs[blob_id]

        if self.frame_count >= self.di:
            time0 = time.time()
            # rerun detection
            if self.use_droi:
                droi_frame = get_roi_frame(self.frame, self.droi)
            else:
                droi_frame = self.frame

            _bounding_boxes, _classes, _confidences = get_bounding_boxes(
                droi_frame, self.detector)

            self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences,
                                       self.blobs, self.frame, self.tracker,
                                       self.mcdf)
            self.blobs = remove_duplicates(self.blobs)
            self.frame_count = 0
            det_time = float(time.time() - time0) * 1000
            print("\t", det_time)
            self.running_average(det_time, is_detection=True)

        self.frame_count += 1
Exemplo n.º 11
0
    def count(self, frame):
        log = []
        self.frame = frame

        if self.frame_count >= self.di:
            # rerun detection
            droi_frame = get_roi_frame(self.frame, self.droi)
            boxes = get_bounding_boxes(droi_frame, self.detector)
            self.blobs, current_blob_id = add_new_blobs(
                boxes, self.blobs, self.frame, self.tracker, self.blob_id,
                self.counting_line, self.cl_position, self.mcdf)
            self.blob_id = current_blob_id
            self.blobs = remove_duplicates(self.blobs)
            self.frame_count = 0

        for _id, blob in list(self.blobs.items()):
            # update trackers
            success, box = blob.tracker.update(self.frame)
            if success:
                blob.num_consecutive_tracking_failures = 0
                blob.update(box)
            else:
                blob.num_consecutive_tracking_failures += 1

            # delete untracked blobs
            if blob.num_consecutive_tracking_failures >= self.mctf:
                del self.blobs[_id]

            # count vehicles
            if is_passed_counting_line(blob.centroid, self.counting_line,
                                       self.cl_position) and not blob.counted:
                blob.counted = True
                self.vehicle_count += 1
                (x, y, w, h) = [int(v) for v in blob.bounding_box]
                roi = self.frame[y:y + h + 40, x:x + w + 40]
                #cv2.imwrite(f"./detected/{self.vehicle_count}.jpg", roi)
                cv2.imwrite("./roi.jpg", roi)
                log.append({
                    'blob_id': _id,
                    'count': self.vehicle_count,
                    'datetime': datetime.now()
                })

        self.frame_count += 1

        return log
Exemplo n.º 12
0
    def __init__(self, initial_frame, detector, tracker, droi, show_droi, mcdf,
                 mctf, di, counting_lines, draw_counts, use_droi):
        self.frame = initial_frame  # current frame of video
        self.detector = detector
        self.tracker = tracker
        self.droi = droi  # detection region of interest
        self.show_droi = show_droi
        self.use_droi = use_droi
        self.mcdf = mcdf  # maximum consecutive detection failures
        self.mctf = mctf  # maximum consecutive tracking failures
        self.di = di  # detection interval
        self.counting_lines = counting_lines

        self.blobs = {}
        self.f_height, self.f_width, _ = self.frame.shape
        self.frame_count = 0  # number of frames since last detection
        self.counts = {
            counting_line['label']: {}
            for counting_line in counting_lines
        }  # counts of vehicles by type for each counting line
        self.draw_counts = draw_counts

        # create blobs from initial frame
        droi_frame = get_roi_frame(self.frame, self.droi)
        _bounding_boxes, _classes, _confidences = get_bounding_boxes(
            droi_frame, self.detector)
        self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences,
                                   self.blobs, self.frame, self.tracker,
                                   self.mcdf)

        self.tracking_count = 0
        self.tracking_average = 0
        self.detection_count = 0
        self.detection_average = 0

        self.use_own_KCF_impl = False
        if self.use_own_KCF_impl:
            import multiprocessing as mp
            self.in_queue = mp.Queue()
            self.out_queue = mp.Queue()
            self.pool = mp.Pool(4, update_blob_tracker_queue,
                                (self.in_queue, self.out_queue))
Exemplo n.º 13
0
    def count_queue(self, frame):
        self.frame = frame

        for blob_id, blob in self.blobs.items():
            self.in_queue.put((blob, blob_id, self.frame))

        num_blobs = len(self.blobs)
        processed_blobs = 0
        while processed_blobs < num_blobs:
            blob_id, blob = self.out_queue.get(True)
            blob, self.counts = attempt_count(blob, blob_id,
                                              self.counting_lines, self.counts)
            self.blobs[blob_id] = blob

            # remove blob if it has reached the limit for tracking failures
            if blob.num_consecutive_tracking_failures >= self.mctf:
                del self.blobs[blob_id]

            processed_blobs += 1

        if self.frame_count >= self.di:
            # rerun detection
            if self.use_droi:
                droi_frame = get_roi_frame(self.frame, self.droi)
            else:
                droi_frame = self.frame

            _bounding_boxes, _classes, _confidences = get_bounding_boxes(
                droi_frame, self.detector)

            self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences,
                                       self.blobs, self.frame, self.tracker,
                                       self.mcdf)
            self.blobs = remove_duplicates(self.blobs)
            self.frame_count = 0

        self.frame_count += 1
Exemplo n.º 14
0
    def count(self, frame):
        log = []
        self.frame = frame

        for _id, blob in list(self.blobs.items()):
            # update trackers
            success, box = blob.tracker.update(self.frame)
            if success:
                blob.num_consecutive_tracking_failures = 0
                blob.update(box)
                log_info(
                    'Vehicle tracker updated.', {
                        'event': 'TRACKER_UPDATE',
                        'vehicle_id': _id,
                        'bounding_box': blob.bounding_box,
                        'centroid': blob.centroid,
                    })
                f = './ProcessRecords/Vehicletrackerupdated58.txt'
                with open(f, "a") as file:
                    file.write('TRACKER_UPDATE' + '-' + 'id' + str(_id) + '-' +
                               'bounding_box' + str(blob.bounding_box) + '-' +
                               'centroid' + str(blob.centroid) + "\n")

            else:
                blob.num_consecutive_tracking_failures += 1

            # count vehicles that have left the frame if no counting line exists
            # or those that have passed the counting line if one exists
            if (self.counting_line == None and \
                    (blob.num_consecutive_tracking_failures == self.mctf or blob.num_consecutive_detection_failures == self.mcdf) and \
                    not blob.counted) \
                        or \
                    (self.counting_line != None and \
                    # don't count a blob if it was first detected at a position past the counting line
                    # this enforces counting in only one direction


                    not is_passed_counting_line(blob.position_first_detected, self.counting_line, self.cl_position) and \
                    is_passed_counting_line(blob.centroid, self.counting_line, self.cl_position) and \
                    not blob.counted):
                blob.counted = True
                self.vehicle_count += 1
                # count by vehicle type
                if blob.type != None:
                    if blob.type in self.types_counts:
                        self.types_counts[blob.type] += 1
                    else:
                        self.types_counts[blob.type] = 1
                log_info(
                    'Vehicle counted.', {
                        'event': 'VEHICLE_COUNT',
                        'id': _id,
                        'type': blob.type,
                        'count': self.vehicle_count,
                        'position_first_detected':
                        blob.position_first_detected,
                        'position_counted': blob.centroid,
                        'counted_at': time.time(),
                    })
                f = './ProcessRecords/Vehiclecounted58.txt'
                with open(f, "a") as file:
                    file.write('VEHICLE_COUNT' + '-' + 'id' + str(_id) + '-' +
                               'type' + str(blob.type) + '-' + 'count' +
                               str(self.vehicle_count) + '-' +
                               'position_first_detected' +
                               str(blob.position_first_detected) + '-' +
                               'position_counted' + str(blob.centroid) + '-' +
                               'counted_at' + str(time.time()) + "\n")

            if blob.num_consecutive_tracking_failures >= self.mctf:
                # delete untracked blobs
                del self.blobs[_id]

        if self.frame_count >= self.di:
            # rerun detection
            droi_frame = get_roi_frame(self.frame, self.droi)
            _bounding_boxes, _classes, _confidences = get_bounding_boxes(
                droi_frame, self.detector)
            self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences,
                                       self.blobs, self.frame, self.tracker,
                                       self.counting_line, self.cl_position,
                                       self.mcdf)
            self.blobs = remove_duplicates(self.blobs)
            self.frame_count = 0

        self.frame_count += 1

        return log
Exemplo n.º 15
0
    def __init__(self, initial_frame, detector, tracker, droi, show_droi, mcdf, mctf, di, cl_position):
        ha = os.path.join('detect-and-count-object','detectors')
        path = os.path.join(ha,'yolo')
        labelsPath = os.path.join(path,'coco.names')
        LABELS = open(labelsPath).read().strip().split("\n")

        self.frame = initial_frame # current frame of video

        self.detector = detector

        self.tracker = tracker

        self.droi =  droi # detection region of interest

        self.show_droi = show_droi

        self.mcdf = mcdf # maximum consecutive detection failures

        self.mctf = mctf # maximum consecutive tracking failures

        self.di = di # detection interval

        self.cl_position = cl_position # counting line position


        self.countCar = 0
        self.countTruck = 0
        self.countBus = 0
        self.countPerson = 0
        self.countBicycle = 0
        self.countMotorcycle = 0

        self.blobs = OrderedDict()

        self.blob_id = 1

        self.f_height, self.f_width, _ = self.frame.shape

        self.frame_count = 0 # number of frames since last detection

        self.vehicle_count = 0 # number of vehicles counted

        self.counting_line = get_counting_line(self.cl_position, self.f_width, self.f_height)



        # create blobs from initial frame

        droi_frame = get_roi_frame(self.frame, self.droi)

        initial_bboxes = get_bounding_boxes(droi_frame, self.detector)
        
        for box in initial_bboxes:
            #print(box)
            if self.detector == 'yolo':
                _blob = create_blob(box[0], frame, self.tracker)
                style = LABELS[box[1][0]]
                self.blobs[self.blob_id] = _blob, style
                self.blob_id += 1
            else:
                _blob = create_blob(box, frame, self.tracker)
                self.blobs[self.blob_id] = _blob
                self.blob_id += 1
Exemplo n.º 16
0
    def visualize(self):
        
        ha = os.path.join('detect-and-count-object','detectors')
        path = os.path.join(ha,'yolo')
        labelsPath = os.path.join(path,'coco.names')
        LABELS = open(labelsPath).read().strip().split("\n")

            # initialize a list of colors to represent each possible class label
        np.random.seed(42)
        COLORS = np.random.randint(0, 255, size=(len(LABELS), 3),
                            dtype="uint8")
        

        frame = self.frame
        f_height, f_width, _ = frame.shape
        
        
        # draw and label blob bounding boxes
        droi_frame = get_roi_frame(self.frame, self.droi)
        boxes = get_bounding_boxes(droi_frame, self.detector)
        if self.detector == 'yolo':
            for box in boxes:
                boxs = box[0]
                for i in box[2].flatten():
                    (x, y) = (int(boxs[0]), int(boxs[1]))
                    (w, h) = (int(boxs[2]), int(boxs[3]))
                    color = [int(c) for c in COLORS[box[1][0]]]
                    cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
                    text = "{}: {:.4f}".format(LABELS[box[1][0]],box[1][1])
                    cv2.putText(frame, text, (x, y - 5),cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
        else:
            for _id, blob in self.blobs.items():

                (x, y, w, h) = [int(v) for v in blob.bounding_box]

                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        
                cv2.putText(frame, 'v_' + str(_id), (x, y - 2), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)

        # draw counting line

        cv2.line(frame, self.counting_line[0], self.counting_line[1], (0, 255, 0), 3)

        # display vehicle count
        cv2.rectangle(frame, (0, 0), (f_width, 75), (180, 132, 109), -1)
        textPosition = int(f_width/7)
        if f_width <= 1280:
            textSize = 0.5
        else:
            textSize = 1
        cv2.putText(frame, 'Total: ' + str(self.vehicle_count), (10, 60), cv2.FONT_HERSHEY_DUPLEX, textSize, (255, 0, 0,), 1, cv2.LINE_AA)
        cv2.putText(frame, 'Car: ' + str(self.countCar), (textPosition, 60), cv2.FONT_HERSHEY_DUPLEX, textSize, (255, 0, 0), 1, cv2.LINE_AA)
        cv2.putText(frame, 'Bus: ' + str(self.countBus), (textPosition*2, 60), cv2.FONT_HERSHEY_DUPLEX, textSize, (255, 0, 0), 1, cv2.LINE_AA)
        cv2.putText(frame, 'Truck: ' + str(self.countTruck), (textPosition*3, 60), cv2.FONT_HERSHEY_DUPLEX, textSize, (255, 0, 0), 1, cv2.LINE_AA)
        cv2.putText(frame, 'People: ' + str(self.countPerson), (textPosition*4, 60), cv2.FONT_HERSHEY_DUPLEX, textSize, (255, 0, 0), 1, cv2.LINE_AA)
        cv2.putText(frame, 'Bicycle: ' + str(self.countBicycle), (textPosition*5, 60), cv2.FONT_HERSHEY_DUPLEX, textSize, (255, 0, 0), 1, cv2.LINE_AA)
        cv2.putText(frame, 'Motorcycle: ' + str(self.countMotorcycle), (textPosition*6, 60), cv2.FONT_HERSHEY_DUPLEX, textSize, (255, 0, 0), 1, cv2.LINE_AA)
        

        # show detection roi

        if self.show_droi:

            frame = draw_roi(frame, self.droi)



        return frame
Exemplo n.º 17
0
    def count(self, frame):

        log = []

        self.frame = frame



        if self.frame_count >= self.di:

            # rerun detection

            droi_frame = get_roi_frame(self.frame, self.droi)
        
            boxes = get_bounding_boxes(droi_frame, self.detector)
            #print(self.blobs)
            self.blobs, current_blob_id = add_new_blobs(boxes, self.blobs, self.frame, self.tracker, self.blob_id, self.counting_line, self.cl_position, self.mcdf, self.detector)
            
            self.blob_id = current_blob_id

            self.blobs = remove_duplicates(self.blobs)

            self.frame_count = 0



        for _id, blob in list(self.blobs.items()):
            
            # update trackers
            #print(blob[0])
            success, box = blob[0].tracker.update(self.frame)
            
            if success:

                blob[0].num_consecutive_tracking_failures = 0

                blob[0].update(box)

            else:

                blob[0].num_consecutive_tracking_failures += 1



            # delete untracked blobs

            if blob[0].num_consecutive_tracking_failures >= self.mctf:

                del self.blobs[_id]



            # count vehicles

            if is_passed_counting_line(blob[0].centroid, self.counting_line, self.cl_position) and not blob[0].counted:
                
                blob[0].counted = True
                
                if blob[1] == 'car':
                    self.countCar +=1
                elif blob[1] == 'bus':
                    self.countBus +=1
                elif blob[1] == 'truck':
                    self.countTruck +=1
                elif blob[1] == 'person':
                    self.countPerson +=1
                elif blob[1] == 'bicycle':
                    self.countBicycle +=1
                elif blob[1] == 'motorcycle':
                    self.countMotorcycle +=1
                    
                self.vehicle_count += 1

                log.append({'blob_id': _id, 'count': self.vehicle_count, 'datetime': datetime.now()})

        self.frame_count += 1

        return log
import cv2
from detectors.detector import get_bounding_boxes
from config import CURRENT_DIR

IMAGE_PATH = f"{CURRENT_DIR}/data/screenshots/ff.jpg"
IMAGE_NAME = IMAGE_PATH.split("/")[-1].split(".")[0]
IMAGE_EXT = IMAGE_PATH.split("/")[-1].split(".")[1]

if __name__ == "__main__":

    image = cv2.imread(IMAGE_PATH)
    _bounding_boxes, _classes, _confidences = get_bounding_boxes(image)
    font = cv2.FONT_HERSHEY_DUPLEX
    line_type = cv2.LINE_8

    for i in range(0, len(_bounding_boxes)):
        (x, y, w, h) = [int(v) for v in _bounding_boxes[i]]
        cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
        cv2.putText(image, _classes[i], (x, y - 5), font, 1, (255, 0, 0), 2,
                    line_type)

    # cv2.imwrite(f"{IMAGE_NAME}_detected.{IMAGE_EXT}", image)
    cv2.imshow("fdf", image)
    cv2.waitKey(0)
Exemplo n.º 19
0
    def count(self, frame):
        _timer = cv2.getTickCount(
        )  # set timer to calculate processing frame rate

        self.frame = frame

        for _id, blob in list(self.blobs.items()):
            # update trackers
            success, box = blob.tracker.update(self.frame)
            if success:
                blob.num_consecutive_tracking_failures = 0
                blob.update(box)
                logger.debug('Vehicle tracker updated.',
                             extra={
                                 'meta': {
                                     'cat': 'TRACKER_UPDATE',
                                     'vehicle_id': _id,
                                     'bounding_box': blob.bounding_box,
                                     'centroid': blob.centroid,
                                 },
                             })
            else:
                blob.num_consecutive_tracking_failures += 1

            # count vehicles that have left the frame if no counting line exists
            # or those that have passed the counting line if one exists
            if (self.counting_line == None and \
                    (blob.num_consecutive_tracking_failures == self.mctf or blob.num_consecutive_detection_failures == self.mcdf) and \
                    not blob.counted) \
                        or \
                    (self.counting_line != None and \
                    # don't count a blob if it was first detected at a position past the counting line
                    # this enforces counting in only one direction


                    not is_passed_counting_line(blob.position_first_detected, self.counting_line, self.cl_position) and \
                    is_passed_counting_line(blob.centroid, self.counting_line, self.cl_position) and \
                    not blob.counted):
                blob.counted = True
                self.vehicle_count += 1
                # count by vehicle type
                if blob.type != None:
                    if blob.type in self.types_counts:
                        self.types_counts[blob.type] += 1
                    else:
                        self.types_counts[blob.type] = 1
                logger.info('Vehicle counted.',
                            extra={
                                'meta': {
                                    'cat': 'VEHICLE_COUNT',
                                    'id': _id,
                                    'type': blob.type,
                                    'count': self.vehicle_count,
                                    'position_first_detected':
                                    blob.position_first_detected,
                                    'position_counted': blob.centroid,
                                    'counted_at': time.time(),
                                },
                            })

            if blob.num_consecutive_tracking_failures >= self.mctf:
                # delete untracked blobs
                del self.blobs[_id]

        if self.frame_count >= self.di:
            # rerun detection
            droi_frame = get_roi_frame(self.frame, self.droi)
            _bounding_boxes, _classes, _confidences = get_bounding_boxes(
                droi_frame, self.detector)
            self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences,
                                       self.blobs, self.frame, self.tracker,
                                       self.counting_line, self.cl_position,
                                       self.mcdf)
            self.blobs = remove_duplicates(self.blobs)
            self.frame_count = 0

        self.frame_count += 1

        self.processing_frame_rate = round(
            cv2.getTickFrequency() / (cv2.getTickCount() - _timer), 2)
        logger.debug('Processing frame rate updated.',
                     extra={
                         'meta': {
                             'cat': 'PROCESSING_SPEED',
                             'frame_rate': self.processing_frame_rate
                         },
                     })
clposition = 'bottom' if args.clposition == None else args.clposition
counting_line = get_counting_line(clposition, f_width, f_height)
vehicle_count = 0

# create detection ROI
droi = [(0, 0), (f_width, 0), (f_width, f_height), (0, f_height)]
if args.droi:
    droi = []
    points = args.droi.replace(' ', '').split('|')
    for point_str in points:
        point = tuple(map(int, point_str.split(',')))
        droi.append(point)

# initialize trackers and create new blobs
droi_frame = get_roi_frame(frame, droi)
initial_bboxes = get_bounding_boxes(droi_frame, detector)
for box in initial_bboxes:
    _blob = create_blob(box, frame, tracker)
    blobs[blob_id] = _blob
    blob_id += 1

while True:
    k = cv2.waitKey(1)
    if args.iscam or cap.get(cv2.CAP_PROP_POS_FRAMES) + 1 < cap.get(
            cv2.CAP_PROP_FRAME_COUNT):
        _, frame = cap.read()

        nframes = cap.get(cv2.CAP_PROP_POS_FRAMES)
        frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
        if nframes % 10 == 0 or nframes == 1:
            print("Processing {} of {} frames".format(nframes, frame_count))
Exemplo n.º 21
0
    def count(self, frame,frames_processed, processing_frame_rate):
        self.frame = frame
        self.processing_frame_rate = processing_frame_rate
        self.frames_processed = frames_processed
        self.isWaiting = 0
        # rescan_requested = False
        # self.nearestBlobLastFrameDistance = self.nearestBlobDistance
        blobs_list = list(self.blobs.items())
        # update blob trackers
        blobs_list = Parallel(n_jobs=NUM_CORES, prefer='threads')(
            delayed(update_blob_tracker)(blob, blob_id, self.frame) for blob_id, blob in blobs_list
        )
        self.blobs = dict(blobs_list)

        for blob_id, blob in blobs_list:
            # count vehicle if it has crossed a counting line
            blob, self.counts = attempt_count(blob, blob_id, self.counting_lines,self.speed_estimation_lines , self.counts, self.frames_processed,self.processing_frame_rate)
            self.blobs[blob_id] = blob

            # remove blob if it has reached the limit for tracking failures
            if blob.num_consecutive_tracking_failures >= self.mctf:
                del self.blobs[blob_id]
            if self.detector == 'yolo':
                blob.onRoad =  str(_is_on_which_roads(self.roads, blob))
                blob.distance = distance_cal(blob.position,self.camera_height,self.focal_length,self.pixel_length,self.resolution,self.vanishing_point)
                # self.NearestBlob, self.nearestBlobPosition , rescan_requested = UpdateNearestBlobPosition(blob, blob_id, blobs_list, self.nearestBlobPosition, self.NearestBlob, rescan_requested)
                # self.nearestBlobDistance = distance_cal(self.nearestBlobPosition[1])           
            # check blob is waiting
            if self.detector == 'yolo_p':
                if self.veh_light == 0:
                    point = Point(blob.bottom_point)
                    polygon = Polygon(self.waiting_zone[0:4])
                    polygon2 = Polygon(self.waiting_zone[-4:])
                    if polygon.contains(point) == True or polygon2.contains(point) == True:
                        blob.isJaywalker = False
                        self.isWaiting += 1
                    else:
                        blob.isJaywalker = True
                        self.jaywalker_flag = True
                if self.ped_light != 0:
                     blob.isJaywalker = False
                     self.jaywalker_flag = False
                    
# =============================================================================
#         if rescan_requested:
#             self.nearestBlob1FrameDistance=[]
#             self.nearestBlobPosition = (0,0)
#             for blob_id, blob in blobs_list:
#                  self.NearestBlob, self.nearestBlobPosition , rescan_requested = UpdateNearestBlobPosition(blob, blob_id, blobs_list, self.nearestBlobPosition, self.NearestBlob,rescan_requested)
#                  self.nearestBlobDistance = distance_cal(self.nearestBlobPosition[1])
#             self.nearestBlobLastFrameDistance = self.nearestBlobDistance
#             rescan_requested = False
# =============================================================================

        # if self.processing_frame_rate > 0 and self.nearestBlobPosition[1] > 0 and self.nearestBlobDistance != self.nearestBlobLastFrameDistance and rescan_requested == False:
# =============================================================================
#             print('nor_list'+str(normalizing_1frame_distance(self.nearestBlobDistance,self.nearestBlobLastFrameDistance,self.nearestBlob1FrameDistance)))
#             print('list'+str(self.nearestBlob1FrameDistance))
#             print('1  '+str(self.nearestBlobDistance))
#             print('2  '+str(self.nearestBlobLastFrameDistance))
# =============================================================================
            # self.NearestBlob_remaining_time = cal_remaining_time( (self.nearestBlobDistance / normalizing_1frame_distance(self.nearestBlobDistance,self.nearestBlobLastFrameDistance,self.nearestBlob1FrameDistance)),1,self.processing_frame_rate)
        

            
        if self.frame_count >= self.detection_interval:
            # rerun detection
            droi_frame = get_roi_frame(self.frame, self.droi)
            _bounding_boxes, _classes, _confidences = get_bounding_boxes(droi_frame, self.detector)
            self.blobID += 1
            self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences, self.blobs, self.frame, self.tracker, self.mcdf, self.blobID)
            self.blobs = remove_duplicates(self.blobs)
            self.frame_count = 0

        self.frame_count += 1