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
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
if args.record: _row = '{0}, {1}, {2}\n'.format('v_' + str(_id), vehicle_count, datetime.now()) log_file.write(_row) log_file.flush() if frame_counter >= DETECTION_INTERVAL: # rerun detection droi_frame = get_roi_frame(frame, droi) boxes = get_bounding_boxes(droi_frame, detector) blobs, current_blob_id = add_new_blobs(boxes, blobs, frame, tracker, blob_id, counting_line, clposition) blob_id = current_blob_id blobs = remove_duplicates(blobs) frame_counter = 0 # draw and label blob bounding boxes for _id, blob in 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, counting_line[0], counting_line[1], (0, 255, 0), 3) # display vehicle count cv2.putText(frame, 'Count: ' + str(vehicle_count), (20, 60),
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
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