def visualize(self):
        frame = self.frame

        # draw and label blob bounding boxes
        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) + '(' + str(blob.vehicle_type) + ')',
                        (x, y - 2), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 255, 0), 2,
                        cv2.LINE_AA)
        # draw counting line
        if self.counting_line != None:
            cv2.line(frame, self.counting_line[0], self.counting_line[1],
                     (255, 215, 0), 3)
        # display vehicle count
        text_org_y = 60
        cv2.putText(frame, 'Count: ' + str(self.vehicle_count),
                    (20, text_org_y), cv2.FONT_HERSHEY_DUPLEX, 1.5,
                    (0, 0, 255), 2, cv2.LINE_AA)
        cv2.putText(frame, 'Cars_count: ' + str(self.car_count),
                    (20, text_org_y * 2), cv2.FONT_HERSHEY_DUPLEX, 1.5,
                    (0, 0, 255), 2, cv2.LINE_AA)
        cv2.putText(frame, 'Trucks_count: ' + str(self.truck_count),
                    (20, text_org_y * 3), cv2.FONT_HERSHEY_DUPLEX, 1.5,
                    (0, 0, 255), 2, cv2.LINE_AA)
        cv2.putText(frame, 'Bicycles_count: ' + str(self.bicycle_count),
                    (20, text_org_y * 4), cv2.FONT_HERSHEY_DUPLEX, 1.5,
                    (0, 0, 255), 2, cv2.LINE_AA)
        # show detection roi
        if self.show_droi:
            frame = draw_roi(frame, self.droi)

        return frame
    def visualize(self):
        _frame = self.frame

        # draw and label blob bounding boxes
        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
        if self.counting_line is not None:
            cv2.line(_frame, self.counting_line[0], self.counting_line[1], (0, 255, 0), 3)
        # display vehicle count
        cv2.putText(_frame, 'Count: ' + str(self.vehicle_count), (20, 60), cv2.FONT_HERSHEY_DUPLEX, 2, (255, 0, 0), 2,
                    cv2.LINE_AA)
        # show detection roi
        if self.show_droi:
            _frame = draw_roi(_frame, self.droi)

        return _frame
            (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),
                    cv2.FONT_HERSHEY_DUPLEX, 2, (255, 0, 0), 2, cv2.LINE_AA)

        # show detection roi
        if args.showdroi:
            frame = draw_roi(frame, droi)

        # save frame in video output
        if args.record:
            output_video.write(frame)

        # visualize vehicle counting
        if not args.hideimage:
            resized_frame = cv2.resize(frame, (858, 480))
            cv2.imshow('tracking', resized_frame)

        frame_counter += 1

        # save frame if 's' key is pressed
        if k & 0xFF == ord('s'):
            cv2.imwrite(
Example #4
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
Example #5
0
    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