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
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 __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.f_height, self.f_width, _ = self.frame.shape self.frame_count = 0 # number of frames since last detection self.processing_frame_rate = 0 # number of frames processed per second 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) self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences, self.blobs, self.frame, self.tracker, self.counting_line, self.cl_position, self.mcdf)
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)
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)
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
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
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))
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
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
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 }, })