Exemplo n.º 1
0
def run_inference(model, category_index, cap, labels, roi_position=0.6, threshold=0.5, x_axis=True, skip_frames=20, save_path='', show=True):
    counter = [0, 0, 0, 0]  # left, right, up, down
    total_frames = 0

    ct = CentroidTracker(maxDisappeared=40, maxDistance=50)
    trackers = []
    trackableObjects = {}

    # Check if results should be saved
    if save_path:
        width = int(cap.get(3))
        height = int(cap.get(4))
        fps = cap.get(cv2.CAP_PROP_FPS)
        out = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc('M','J','P','G'), fps, (width, height))

    while cap.isOpened():
        ret, image_np = cap.read()
        if not ret:
            break

        height, width, _ = image_np.shape
        rgb = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
        
        status = "Waiting"
        rects = []

        if total_frames % skip_frames == 0:
            status = "Detecting"
            trackers = []

            # Actual detection.
            output_dict = run_inference_for_single_image(model, image_np)

            for i, (y_min, x_min, y_max, x_max) in enumerate(output_dict['detection_boxes']):
                if output_dict['detection_scores'][i] > threshold and (labels == None or category_index[output_dict['detection_classes'][i]]['name'] in labels):
                    tracker = dlib.correlation_tracker()
                    rect = dlib.rectangle(int(x_min * width), int(y_min * height), int(x_max * width), int(y_max * height))
                    tracker.start_track(rgb, rect)
                    trackers.append(tracker)
        else:
            status = "Tracking"
            for tracker in trackers:
                # update the tracker and grab the updated position
                tracker.update(rgb)
                pos = tracker.get_position()

				# unpack the position object
                x_min, y_min, x_max, y_max = int(pos.left()), int(pos.top()), int(pos.right()), int(pos.bottom())
                
                # add the bounding box coordinates to the rectangles list
                rects.append((x_min, y_min, x_max, y_max))

        objects = ct.update(rects)

        for (objectID, centroid) in objects.items():
            to = trackableObjects.get(objectID, None)

            if to is None:
                to = TrackableObject(objectID, centroid)
            else:
                if x_axis and not to.counted:
                    x = [c[0] for c in to.centroids]
                    direction = centroid[0] - np.mean(x)

                    if centroid[0] > roi_position*width and direction > 0:
                        counter[1] += 1
                        to.counted = True
                    elif centroid[0] < roi_position*width and direction < 0:
                        counter[0] += 1
                        to.counted = True
                    
                elif not x_axis and not to.counted:
                    y = [c[1] for c in to.centroids]
                    direction = centroid[1] - np.mean(y)

                    if centroid[1] > roi_position*height and direction > 0:
                        counter[3] += 1
                        to.counted = True
                    elif centroid[1] < roi_position*height and direction < 0:
                        counter[2] += 1
                        to.counted = True

                to.centroids.append(centroid)

            trackableObjects[objectID] = to

            text = "ID {}".format(objectID)
            cv2.putText(image_np, text, (centroid[0] - 10, centroid[1] - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
            cv2.circle(image_np, (centroid[0], centroid[1]), 4, (255, 255, 255), -1)

        # Draw ROI line
        if x_axis:
            cv2.line(image_np, (int(roi_position*width), 0), (int(roi_position*width), height), (0xFF, 0, 0), 5)
        else:
            cv2.line(image_np, (0, int(roi_position*height)), (width, int(roi_position*height)), (0xFF, 0, 0), 5)

        # display count and status
        font = cv2.FONT_HERSHEY_SIMPLEX
        if x_axis:
            cv2.putText(image_np, f'Left: {counter[0]}; Right: {counter[1]}', (10, 35), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX)
        else:
            cv2.putText(image_np, f'Up: {counter[2]}; Down: {counter[3]}', (10, 35), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX)
        cv2.putText(image_np, 'Status: ' + status, (10, 70), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX)

        if show:
            cv2.imshow('cumulative_object_counting', image_np)
            if cv2.waitKey(25) & 0xFF == ord('q'):
                break

        if save_path:
            out.write(image_np)
        
        total_frames += 1
        
    cap.release()
    if save_path:
        out.release()
    cv2.destroyAllWindows()
Exemplo n.º 2
0
    def run_camera(self):
        totalFrames = 0
        trackers = []
        trackableObjects = {}
        W = None
        H = None
        class_list = []

        self.read_config()

        self.s.enter(1, 1, self.publish_online, (self.s,))

        # Video Source
        # vid_capture = cv2.VideoCapture(0)
        vid_capture = cv2.VideoCapture("../../img/in/campus4-c1.avi")

        # Load Model
        print("[INFO] loading model...")
        net = cv2.dnn.readNetFromCaffe("mobilenet_ssd/MobileNetSSD_deploy.prototxt",
                                       "mobilenet_ssd/MobileNetSSD_deploy.caffemodel")
        print("Done")
        # Start FPS counter
        self.fps = FPS().start()

        print("Running...")
        error = False
        while True:
            _, frame = vid_capture.read()

            # if end of video
            if frame is None:
                print("no Frame")
                vid_capture = cv2.VideoCapture("../../img/in/campus4-c1.avi")
                _, frame = vid_capture.read()
                # error = True
                # break

            # resize and convert to rgb for dlib
            frame = cv2.resize(frame, self.resolution, interpolation=cv2.INTER_AREA)
            rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            # set frame dimensions
            if W is None or H is None:
                (H, W) = frame.shape[:2]

            # snapshot
            if self.take_snap:
                self.publish_snap(frame)
                self.take_snap = False
                continue

            # init bounding box rectangles
            rects = []

            # Only search for objects every 5 frames
            if totalFrames % self.skip_frame == 0:
                totalFrames = 0
                # init new set of trackers
                trackers = []
                class_list = []

                # convert the frame to a blob and pass the blob through the
                # network and obtain the detections
                blob = cv2.dnn.blobFromImage(frame, 0.007843, (W, H), 127.5)
                net.setInput(blob)
                detections = net.forward()

                # loop over the detections
                for i in np.arange(0, detections.shape[2]):
                    # extract the confidence (i.e., probability) associated
                    # with the prediction
                    confidence = detections[0, 0, i, 2]

                    if confidence > self.min_confidence:
                        # if the class label is not a person, ignore it
                        idx = int(detections[0, 0, i, 1])
                        target = self.CLASSES[idx]
                        if not self.targets.__contains__(target):
                            continue

                        # compute the (x, y)-coordinates of the bounding box
                        box = detections[0, 0, i, 3:7] * np.array([W, H, W, H])
                        (start_x, start_y, end_x, end_y) = box.astype("int")

                        # make a dlib rectangle object and start the dlib tracker
                        tracker = dlib.correlation_tracker()
                        rect = dlib.rectangle(start_x, start_y, end_x, end_y)
                        tracker.start_track(rgb, rect)

                        trackers.append(tracker)
                        class_list.append(target)

            # use tracker during skipped frames, not object recognition
            else:
                for tracker in trackers:
                    # update the tracker and grab the updated position
                    tracker.update(rgb)
                    pos = tracker.get_position()

                    # unpack position object
                    start_x = int(pos.left())
                    start_y = int(pos.top())
                    end_x = int(pos.right())
                    end_y = int(pos.bottom())

                    # add the box coordinates to the rectangles list
                    rects.append((start_x, start_y, end_x, end_y))

            # use centroids to match old and new centroids and then loop over them
            (objects, class_dict) = self.ct.update(rects, class_list)

            for (object_id, centroid) in objects.items():
                # check if object is in the object list
                t_object = trackableObjects.get(object_id, None)

                # if there is no existing trackable object, create one
                if t_object is None:
                    t_object = TrackableObject(object_id, centroid, class_dict[object_id])

                else:
                    t_object.centroids.append(centroid)

                # store the trackable object in our dictionary
                trackableObjects[object_id] = t_object

                # draw Centroid
                text = "ID {}".format(object_id)
                cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
                cv2.circle(frame, (centroid[0], centroid[1]), 4, (0, 255, 0), -1)

            for roi in self.rois:
                draw_roi(frame, roi, self.rois[roi])

            # Debugging
            if self.debug:
                cv2.imshow("Tracking", frame)

            totalFrames += 1
            self.fps.update()

            k = cv2.waitKey(5) & 0xFF
            if k == 27:  # Esc
                break

        self.publish_fps()
        self.fps.stop()
        cv2.destroyAllWindows()
        vid_capture.release()
        print("Camera stopped")
        return True, error
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-m',
                        '--model',
                        type=str,
                        required=True,
                        help='File path of .tflite file.')
    parser.add_argument('-l',
                        '--labelmap',
                        type=str,
                        required=True,
                        help='File path of labels file.')
    parser.add_argument('-v',
                        '--video_path',
                        type=str,
                        default='',
                        help='Path to video. If None camera will be used')
    parser.add_argument('-t',
                        '--threshold',
                        type=float,
                        default=0.5,
                        help='Detection threshold')
    parser.add_argument('-roi',
                        '--roi_position',
                        type=float,
                        default=0.6,
                        help='ROI Position (0-1)')
    parser.add_argument('-la',
                        '--labels',
                        nargs='+',
                        type=str,
                        help='Label names to detect (default="all-labels")')
    parser.add_argument('-a',
                        '--axis',
                        default=True,
                        action="store_false",
                        help='Axis for cumulative counting (default=x axis)')
    parser.add_argument('-e',
                        '--use_edgetpu',
                        action='store_true',
                        default=False,
                        help='Use EdgeTPU')
    parser.add_argument(
        '-s',
        '--skip_frames',
        type=int,
        default=20,
        help='Number of frames to skip between using object detection model')
    parser.add_argument('-sh',
                        '--show',
                        default=True,
                        action="store_false",
                        help='Show output')
    parser.add_argument(
        '-sp',
        '--save_path',
        type=str,
        default='',
        help='Path to save the output. If None output won\'t be saved')
    parser.add_argument(
        '--type',
        choices=['tensorflow', 'yolo', 'yolov3-tiny'],
        default='tensorflow',
        help='Whether the original model was a Tensorflow or YOLO model')
    args = parser.parse_args()

    labelmap = load_labels(args.labelmap)
    interpreter = make_interpreter(args.model, args.use_edgetpu)
    interpreter.allocate_tensors()
    _, input_height, input_width, _ = interpreter.get_input_details(
    )[0]['shape']

    if args.video_path != '':
        cap = cv2.VideoCapture(args.video_path)
    else:
        cap = cv2.VideoCapture(0)

    if not cap.isOpened():
        print("Error opening video stream or file")

    if args.save_path:
        width = int(cap.get(3))
        height = int(cap.get(4))
        fps = cap.get(cv2.CAP_PROP_FPS)
        out = cv2.VideoWriter(args.save_path,
                              cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps,
                              (width, height))

    counter = [0, 0, 0, 0]  # left, right, up, down
    total_frames = 0

    ct = CentroidTracker(maxDisappeared=40, maxDistance=50)
    trackers = []
    trackableObjects = {}

    while cap.isOpened():
        ret, image_np = cap.read()
        if not ret:
            break

        height, width, _ = image_np.shape
        rgb = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)

        status = "Waiting"
        rects = []

        if total_frames % args.skip_frames == 0:
            status = "Detecting"
            trackers = []

            image_pred = cv2.resize(image_np, (input_width, input_height))

            # Perform inference
            results = detect_objects(interpreter, image_pred, args.threshold,
                                     args.type)

            for obj in results:
                y_min, x_min, y_max, x_max = obj['bounding_box']
                if obj['score'] > args.threshold and (
                        args.labels == None
                        or labelmap[obj['class_id']] in args.labels):
                    tracker = dlib.correlation_tracker()
                    rect = dlib.rectangle(int(x_min * width),
                                          int(y_min * height),
                                          int(x_max * width),
                                          int(y_max * height))
                    tracker.start_track(rgb, rect)
                    trackers.append(tracker)
        else:
            status = "Tracking"
            for tracker in trackers:
                # update the tracker and grab the updated position
                tracker.update(rgb)
                pos = tracker.get_position()

                # unpack the position object
                x_min, y_min, x_max, y_max = int(pos.left()), int(
                    pos.top()), int(pos.right()), int(pos.bottom())

                if x_min < width and x_max < width and y_min < height and y_max < height and x_min > 0 and x_max > 0 and y_min > 0 and y_max > 0:
                    # add the bounding box coordinates to the rectangles list
                    rects.append((x_min, y_min, x_max, y_max))

        objects = ct.update(rects)

        for (objectID, centroid) in objects.items():
            to = trackableObjects.get(objectID, None)

            if to is None:
                to = TrackableObject(objectID, centroid)
            else:
                if args.axis and not to.counted:
                    x = [c[0] for c in to.centroids]
                    direction = centroid[0] - np.mean(x)

                    if centroid[
                            0] > args.roi_position * width and direction > 0:
                        counter[1] += 1
                        to.counted = True
                    elif centroid[
                            0] < args.roi_position * width and direction < 0:
                        counter[0] += 1
                        to.counted = True

                elif not args.axis and not to.counted:
                    y = [c[1] for c in to.centroids]
                    direction = centroid[1] - np.mean(y)

                    if centroid[
                            1] > args.roi_position * height and direction > 0:
                        counter[3] += 1
                        to.counted = True
                    elif centroid[
                            1] < args.roi_position * height and direction < 0:
                        counter[2] += 1
                        to.counted = True

                to.centroids.append(centroid)

            trackableObjects[objectID] = to

            text = "ID {}".format(objectID)
            cv2.putText(image_np, text, (centroid[0] - 10, centroid[1] - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
            cv2.circle(image_np, (centroid[0], centroid[1]), 4,
                       (255, 255, 255), -1)

        # Draw ROI line
        if args.axis:
            cv2.line(image_np, (int(args.roi_position * width), 0),
                     (int(args.roi_position * width), height), (0xFF, 0, 0), 5)
        else:
            cv2.line(image_np, (0, int(args.roi_position * height)),
                     (width, int(args.roi_position * height)), (0xFF, 0, 0), 5)

        # display count and status
        font = cv2.FONT_HERSHEY_SIMPLEX
        if args.axis:
            cv2.putText(image_np, f'Left: {counter[0]}; Right: {counter[1]}',
                        (10, 35), font, 0.8, (0, 0xFF, 0xFF), 2,
                        cv2.FONT_HERSHEY_SIMPLEX)
        else:
            cv2.putText(image_np, f'Up: {counter[2]}; Down: {counter[3]}',
                        (10, 35), font, 0.8, (0, 0xFF, 0xFF), 2,
                        cv2.FONT_HERSHEY_SIMPLEX)
        cv2.putText(image_np, 'Status: ' + status, (10, 70), font, 0.8,
                    (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX)

        if args.show:
            cv2.imshow('cumulative_object_counting', image_np)
            if cv2.waitKey(25) & 0xFF == ord('q'):
                break

        if args.save_path:
            out.write(image_np)

        total_frames += 1

    cap.release()
    if args.save_path:
        out.release()
    cv2.destroyAllWindows()
        if frame is not None:
            height = frame.shape[0]
            width = frame.shape[1]

            objects = ct.update([
                (int(detection.xmin * width), int(detection.ymin * height),
                 int(detection.xmax * width), int(detection.ymax * height))
                for detection in detections
            ])

            for (objectID, centroid) in objects.items():
                to = trackableObjects.get(objectID, None)

                if to is None:
                    to = TrackableObject(objectID, centroid)
                else:
                    if args.axis and not to.counted:
                        x = [c[0] for c in to.centroids]
                        direction = centroid[0] - np.mean(x)

                        if centroid[
                                0] > args.roi_position * width and direction > 0 and np.mean(
                                    x) < args.roi_position * width:
                            counter[1] += 1
                            to.counted = True
                        elif centroid[
                                0] < args.roi_position * width and direction < 0 and np.mean(
                                    x) > args.roi_position * width:
                            counter[0] += 1
                            to.counted = True
Exemplo n.º 5
0
def gen():
    """Video streaming generator function."""
    print('Inicio de contador de personas.')
    print('Iniciando captura de video de la cámara ...')
    #cap = cv2.VideoCapture(LINK_CAMARA, cv2.CAP_FFMPEG)
    #cap = cv2.VideoCapture(LINK_CAMARA)
    cap = cv2.VideoCapture('768x576.avi')

    category_index = label_map_util.create_category_index_from_labelmap(
        LABEL_MAP, use_display_name=True)

    print('Iniciando detección de personas ...')
    model = detection_model
    labels = LABEL_MAP
    roi_position = 0.6
    threshold = 0.1  # Valor original 0.5
    x_axis = True
    skip_frames = 20
    show = True

    counter = [0, 0, 0, 0]  # left, right, up, down
    total_frames = 0

    #ct = CentroidTracker(maxDisappeared=40, maxDistance=50)
    #ct = CentroidTracker(maxDisappeared=20, maxDistance=20)
    ct = CentroidTracker(maxDisappeared=5, maxDistance=10)
    trackers = []
    trackableObjects = {}

    while cap.isOpened():
        ret, image_np = cap.read()
        if not ret:
            print('Error. No se pudo recuperar captura de video de la cámara.')
            break

        height, width, _ = image_np.shape
        rgb = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)

        status = "Waiting"
        rects = []

        if total_frames % skip_frames == 0:
            status = "Detecting"
            trackers = []

            # Actual detection.
            output_dict = run_inference_for_single_image(model, image_np)

            for i, (y_min, x_min, y_max,
                    x_max) in enumerate(output_dict['detection_boxes']):
                if output_dict['detection_scores'][i] > threshold and (
                        labels == None
                        or category_index[output_dict['detection_classes']
                                          [i]]['name'] in labels):
                    tracker = dlib.correlation_tracker()
                    rect = dlib.rectangle(int(x_min * width),
                                          int(y_min * height),
                                          int(x_max * width),
                                          int(y_max * height))
                    tracker.start_track(rgb, rect)
                    trackers.append(tracker)
        else:
            status = "Tracking"
            for tracker in trackers:
                # update the tracker and grab the updated position
                tracker.update(rgb)
                pos = tracker.get_position()

                # unpack the position object
                x_min, y_min, x_max, y_max = int(pos.left()), int(
                    pos.top()), int(pos.right()), int(pos.bottom())

                # add the bounding box coordinates to the rectangles list
                rects.append((x_min, y_min, x_max, y_max))

        objects = ct.update(rects)

        for (objectID, centroid) in objects.items():
            to = trackableObjects.get(objectID, None)

            if to is None:
                to = TrackableObject(objectID, centroid)
            else:
                if x_axis and not to.counted:
                    x = [c[0] for c in to.centroids]
                    direction = centroid[0] - np.mean(x)

                    if centroid[0] > roi_position * width and direction > 0:
                        counter[1] += 1
                        to.counted = True
                    elif centroid[0] < roi_position * width and direction < 0:
                        counter[0] += 1
                        to.counted = True

                elif not x_axis and not to.counted:
                    y = [c[1] for c in to.centroids]
                    direction = centroid[1] - np.mean(y)

                    if centroid[1] > roi_position * height and direction > 0:
                        counter[3] += 1
                        to.counted = True
                    elif centroid[1] < roi_position * height and direction < 0:
                        counter[2] += 1
                        to.counted = True

                to.centroids.append(centroid)

            trackableObjects[objectID] = to

            text = "ID {}".format(objectID)
            cv2.putText(image_np, text, (centroid[0] - 10, centroid[1] - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
            cv2.circle(image_np, (centroid[0], centroid[1]), 4,
                       (255, 255, 255), -1)

        # Original
        # Draw ROI line
        if x_axis:
            cv2.line(image_np, (int(roi_position * width), 0),
                     (int(roi_position * width), height), (0xFF, 0, 0), 5)
        else:
            cv2.line(image_np, (0, int(roi_position * height)),
                     (width, int(roi_position * height)), (0xFF, 0, 0), 5)

        # display count and status
        font = cv2.FONT_HERSHEY_SIMPLEX

        # Original
        if x_axis:
            cv2.putText(image_np, f'Left: {counter[0]}; Right: {counter[1]}',
                        (10, 35), font, 0.8, (0, 0xFF, 0xFF), 2,
                        cv2.FONT_HERSHEY_SIMPLEX)
        else:
            cv2.putText(image_np, f'Up: {counter[2]}; Down: {counter[3]}',
                        (10, 35), font, 0.8, (0, 0xFF, 0xFF), 2,
                        cv2.FONT_HERSHEY_SIMPLEX)

        cv2.putText(image_np, 'Status: ' + status, (10, 70), font, 0.8,
                    (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX)

        if show:
            img = cv2.resize(image_np, (0, 0), fx=0.5, fy=0.5)
            frame = cv2.imencode('.jpg', img)[1].tobytes()
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
            time.sleep(0.1)

        total_frames += 1
Exemplo n.º 6
0
            start_x = int(pos.left())
            start_y = int(pos.top())
            end_x = int(pos.right())
            end_y = int(pos.bottom())

            rects.append((start_x, start_y, end_x, end_y))

    cv2.line(frame, (0, H // 2), (W, H // 2), (0, 255, 255), 2)

    objs = cent_tracker.update_centroid(rects)

    for (obj_id, centroid) in objs.items():
        tra_objs = trackable_objs_dict.get(obj_id, None)
        if tra_objs is None:
            tra_objs = TrackableObject(obj_id, centroid)
        else:
            y = [c[1] for c in tra_objs.centroids]
            direction = centroid[1] - np.mean(y)
            tra_objs.centroids.append(centroid)

            if not tra_objs.counted:
                if direction < 0 and centroid[1] < H // 2:
                    total_ups += 1
                    tra_objs.counted = True
                elif direction > 0 and centroid[1] > H // 2:
                    total_downs += 1
                    tra_objs.counted = True

        trackable_objs_dict[obj_id] = tra_objs