def __init__(self, initial_frame, droi_scale, red_line_scale, detection_type): self.frame = initial_frame # current frame of video self.droi = [] # detection region of interest f_height, f_width, _ = initial_frame.shape for p in droi_scale: self.droi.append((int(p[0] * f_width), int(p[1] * f_height))) 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.detection_type = detection_type self.tracker = Sort() self.memory = {} droi_frame = get_roi_frame(self.frame, self.droi) initial_bboxes = get_bounding_boxes(droi_frame, detection_type) # print(initial_bboxes) np.set_printoptions( formatter={'float': lambda x: "{0:0.3f}".format(x)}) initial_bboxes = np.asarray(initial_bboxes) self.tracks = self.tracker.update(initial_bboxes) self.red_line = [] f_height, f_width, _ = frame.shape for p in red_line_scale: self.red_line.append((int(p[0] * f_width), int(p[1] * f_height)))
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
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
# set counting line 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:
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
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
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
def count(self, frame): log = [] self.frame = frame # initialize a list of colors to represent each possible class label np.random.seed(42) COLORS = np.random.randint(0, 255, size=(200, 3), dtype="uint8") droi_frame = get_roi_frame(self.frame, self.droi) dets = get_bounding_boxes(droi_frame, detection_type) np.set_printoptions( formatter={'float': lambda x: "{0:0.3f}".format(x)}) dets = np.asarray(dets) tracks = self.tracker.update(dets) boxes = [] indexIDs = [] classIDs = [] c = [] previous = self.memory.copy() self.memory = {} for track in tracks: boxes.append([track[0], track[1], track[2], track[3]]) indexIDs.append(int(track[4])) classIDs.append(int(track[5])) self.memory[indexIDs[-1]] = boxes[-1] if len(boxes) > 0: i = int(0) for box in boxes: # extract the bounding box coordinates (x, y) = (int(box[0]), int(box[1])) (w, h) = (int(box[2]), int(box[3])) color = [int(c) for c in COLORS[indexIDs[i] % len(COLORS)]] cv2.rectangle(self.frame, (x, y), (w, h), color, 2) if indexIDs[i] in previous: previous_box = previous[indexIDs[i]] (x2, y2) = (int(previous_box[0]), int(previous_box[1])) (w2, h2) = (int(previous_box[2]), int(previous_box[3])) p0 = (int(x + (w - x) / 2), int(y + (h - y) / 2)) p1 = (int(x2 + (w2 - x2) / 2), int(y2 + (h2 - y2) / 2)) cv2.line(self.frame, p0, p1, color, 3) if intersect(p0, p1, self.red_line[0], self.red_line[1]): self.vehicle_count += 1 log.append({ 'blob_id': indexIDs[i], 'type': get_class_name(classIDs[i]), 'count': self.vehicle_count, 'datetime': datetime.now() }) # text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i]) text = "{}-{}".format(get_class_name(classIDs[i]), indexIDs[i]) cv2.putText(self.frame, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2) i += 1 # draw line cv2.line(self.frame, self.red_line[0], self.red_line[1], (0, 0, 255), 5) # draw counter cv2.putText(self.frame, str(self.vehicle_count), (100, 200), cv2.FONT_HERSHEY_DUPLEX, 5.0, (0, 255, 0), 10) self.frame = draw_roi(self.frame, self.droi) return log, self.frame
cap = cv2.VideoCapture(video) if not cap.isOpened(): sys.exit('Error capturing video.') output_video = None # write video _, frame = cap.read() resized_frame = cv2.resize(frame, output_size) droi_scale = setting_roi(resized_frame) h, w, _ = resized_frame.shape droi = [] for p in droi_scale: droi.append((int(p[0] * w), int(p[1] * h))) # droi_frame = get_roi_frame(resized_frame, droi) red_line_scale = setting_red_line(droi_frame) detection_type = 1 if args.person else 0 vehicle_counter = VehicleCounter(frame, droi_scale, red_line_scale, detection_type) if args.record: # initialize video object and log file to record counting f_height, f_width, _ = frame.shape output_video_path = './output/output.avi' output_video = cv2.VideoWriter( output_video_path, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 30, (f_width, f_height)) log_file_name = 'log.txt'