def evaluate(_):
    win_name = 'Detector'
    cv2.namedWindow(win_name)

    video = FLAGS.video
    print(video)

    if is_url(video):
        videoPafy = pafy.new(video)
        video = videoPafy.getbest(preftype="mp4").url

    if video == '0':
        cam = cv2.VideoCapture(0)
    else:
        cam = cv2.VideoCapture(video)
    if not cam.isOpened():
        raise IOError('Can\'t open "{}"'.format(FLAGS.video))

    source_h = cam.get(cv2.CAP_PROP_FRAME_HEIGHT)
    source_w = cam.get(cv2.CAP_PROP_FRAME_WIDTH)

    model_cls = find_class_by_name(FLAGS.model_name, [yolo])
    print(model_cls)
    model = model_cls(input_shape=(source_h, source_w, 3))
    print(model)
    model.init()

    frame_num = 0
    start_time = time.time()
    fps = 0
    try:
        while True:
            ret, frame = cam.read()

            if not ret:
                logger.info('Can\'t read video data. Potential end of stream')
                return

            predictions = model.evaluate(frame)

            for o in predictions:
                x1 = o['box']['left']
                x2 = o['box']['right']

                y1 = o['box']['top']
                y2 = o['box']['bottom']

                color = o['color']
                class_name = o['class_name']

                # Draw box
                cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)

                # Draw label
                (test_width, text_height), baseline = cv2.getTextSize(
                    class_name, cv2.FONT_HERSHEY_SIMPLEX, 0.75, 1)
                cv2.rectangle(frame, (x1, y1),
                              (x1 + test_width, y1 - text_height - baseline),
                              color,
                              thickness=cv2.FILLED)
                cv2.putText(frame, class_name, (x1, y1 - baseline),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)
                objectt = Object(frameno=frame_num,
                                 objectname=class_name,
                                 objectconfidence=0.0)
                db.session.add(objectt)
                db.session.commit()

            end_time = time.time()
            fps = fps * 0.9 + 1 / (end_time - start_time) * 0.1
            start_time = end_time

            # Draw additional info
            frame_info = 'Frame: {0}, FPS: {1:.2f}'.format(frame_num, fps)
            cv2.putText(frame, frame_info, (10, frame.shape[0] - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
            logger.info(frame_info)

            cv2.imshow(win_name, frame)

            if predictions:
                logger.info('Predictions: {}'.format(
                    format_predictions(predictions)))
                print(predictions[0]["class_name"])
                print(predictions[0]["score"])

            key = cv2.waitKey(1) & 0xFF

            # Exit
            if key == ord('q'):
                break

            # Take screenshot
            if key == ord('s'):
                cv2.imwrite('frame_{}.jpg'.format(time.time()), frame)

            frame_num += 1

    finally:
        cv2.destroyAllWindows()
        cam.release()
        model.close()
Пример #2
0
    def _cam_loop(self):

        # DEBUG:PiCam Stream
        # cam = cv2.VideoCapture('http://192.168.1.6:8080/?action=stream')

        # Multi Threaded PiCam stream
        cam = VideoStreamThread('http://192.168.1.7:8080/?action=stream')
        cam.start()

        # DEBUG: Youtube stream test
        # url = 'https://youtu.be/W1yKqFZ34y4'
        # vPafy = pafy.new(url)
        # play = vPafy.getbest(preftype="webm")
        # cam = cv2.VideoCapture(play.url)

        source_h = cam.stream.get(cv2.CAP_PROP_FRAME_HEIGHT)
        source_w = cam.stream.get(cv2.CAP_PROP_FRAME_WIDTH)

        # Init New Model
        NewModel = YoloNewModel(input_shape=(source_h, source_w, 3))
        NewModel.init()

        # Init COCO model
        model = Yolo2Model(input_shape=(source_h, source_w, 3))
        model.init()

        start_time = time.time()
        frame_num = 0
        fps = 0
        try:
            while self.is_running:
                frame = cam.read().copy()

                # COCO Predictions
                predictions = model.evaluate(frame)

                # New Predictions
                NewPredictions = NewModel.evaluate(frame)

                for o in NewPredictions:
                    x1 = o['box']['left']
                    x2 = o['box']['right']

                    y1 = o['box']['top']
                    y2 = o['box']['bottom']

                    color = o['color']
                    class_name = o['class_name']

                    # Draw box
                    cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)

                    # Draw label
                    (test_width, text_height), baseline = cv2.getTextSize(
                        class_name, cv2.FONT_HERSHEY_SIMPLEX, 0.75, 1)
                    cv2.rectangle(
                        frame, (x1, y1),
                        (x1 + test_width, y1 - text_height - baseline),
                        color,
                        thickness=cv2.FILLED)
                    cv2.putText(frame, class_name, (x1, y1 - baseline),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)

                end_time = time.time()
                fps = fps * 0.9 + 1 / (end_time - start_time) * 0.1
                start_time = end_time

                # Draw additional info
                frame_info = 'Frame: {0}, FPS: {1:.2f}'.format(frame_num, fps)
                cv2.putText(frame, frame_info, (10, frame.shape[0] - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
                logger.info(frame_info)

                self._detect_frame_data_id = frame_num
                _, img = cv2.imencode('.jpg', frame, self.encode_params)
                self._detect_frame_data = img

                if NewPredictions:
                    formatted = format_predictions(NewPredictions)
                    logger.info('Predictions: {}'.format(formatted))
                    self._send_dh(format_notification(NewPredictions))

                frame_num += 1

        finally:
            # cam.release()
            cam.stop()
            model.close()
            NewModel.close()
Пример #3
0
def evaluate(frame, model):
    # win_name = 'Detector'
    # cv2.namedWindow(win_name)

    # video = FLAGS.video

    # if is_url(video):
    #     videoPafy = pafy.new(video)
    #     video = videoPafy.getbest(preftype="mp4").url

    # cam = cv2.VideoCapture(0)
    # if not cam.isOpened():
    #     raise IOError("'Can\'t open")

    # source_h = cam.get(cv2.CAP_PROP_FRAME_HEIGHT)
    # source_w = cam.get(cv2.CAP_PROP_FRAME_WIDTH)

    # model_cls = find_class_by_name('Yolo2Model', [yolo])
    # model = model_cls(input_shape=(source_h, source_w, 3))
    # model.init()

    frame_num = 0
    start_time = time.time()
    fps = 0

    # ret, frame = cam.read()

    # if not ret:
    #     logger.info('Can\'t read video data. Potential end of stream')
    #     return

    predictions = model.evaluate(frame)

    for o in predictions:
        x1 = o['box']['left']
        x2 = o['box']['right']

        y1 = o['box']['top']
        y2 = o['box']['bottom']

        color = o['color']
        class_name = o['class_name']

        # Draw box
        cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)

        # Draw label
        (test_width,
         text_height), baseline = cv2.getTextSize(class_name,
                                                  cv2.FONT_HERSHEY_SIMPLEX,
                                                  0.75, 1)
        cv2.rectangle(frame, (x1, y1),
                      (x1 + test_width, y1 - text_height - baseline),
                      color,
                      thickness=cv2.FILLED)
        cv2.putText(frame, class_name, (x1, y1 - baseline),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)

    end_time = time.time()
    fps = fps * 0.9 + 1 / (end_time - start_time) * 0.1
    start_time = end_time

    # Draw additional info
    frame_info = 'Frame: {0}, FPS: {1:.2f}'.format(frame_num, fps)
    cv2.putText(frame, frame_info, (10, frame.shape[0] - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
    logger.info(frame_info)

    # cv2.imshow("detector", frame)

    if predictions:
        logger.info('Predictions: {}'.format(format_predictions(predictions)))

    return frame
Пример #4
0
    def _cam_loop(self):
        logger.info('Start camera loop')
        ###

        # PiCam Stream
        # cam = cv2.VideoCapture('http://192.168.1.6:8080/?action=stream')

        # Multi Threaded PiCam stream
        # cam = VideoStreamThread('http://192.168.1.7:8080/?action=stream')
        # cam.start()

        # Multi Threaded Amcrest Stream
        cam = VideoStreamThread(
            'rtsp://*****:*****@192.168.1.5:80/cam/realmonitor?channel=1&subtype=0'
        )
        cam.start()

        source_h = cam.stream.get(cv2.CAP_PROP_FRAME_HEIGHT)
        source_w = cam.stream.get(cv2.CAP_PROP_FRAME_WIDTH)

        model = Yolo2Model(input_shape=(source_h, source_w, 3))
        model.init()

        start_time = time.time()
        frame_num = 0
        fps = 0
        try:
            while self.is_running:
                frame = cam.read().copy()
                predictions = model.evaluate(frame)

                for o in predictions:
                    x1 = o['box']['left']
                    x2 = o['box']['right']

                    y1 = o['box']['top']
                    y2 = o['box']['bottom']

                    color = o['color']
                    class_name = o['class_name']

                    # Draw box
                    cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)

                    # Draw label
                    (test_width, text_height), baseline = cv2.getTextSize(
                        class_name, cv2.FONT_HERSHEY_SIMPLEX, 0.75, 1)
                    cv2.rectangle(
                        frame, (x1, y1),
                        (x1 + test_width, y1 - text_height - baseline),
                        color,
                        thickness=cv2.FILLED)
                    cv2.putText(frame, class_name, (x1, y1 - baseline),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)

                end_time = time.time()
                fps = fps * 0.9 + 1 / (end_time - start_time) * 0.1
                start_time = end_time

                # Draw additional info
                frame_info = 'Frame: {0}, FPS: {1:.2f}'.format(frame_num, fps)
                cv2.putText(frame, frame_info, (10, frame.shape[0] - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
                logger.info(frame_info)

                self._detect_frame_data_id = frame_num
                _, img = cv2.imencode('.jpg', frame, self.encode_params)
                self._detect_frame_data = img

                if predictions:
                    formatted = format_predictions(predictions)
                    logger.info('Predictions: {}'.format(formatted))
                    self._send_dh(format_notification(predictions))

                frame_num += 1

        finally:
            cam.stop()
            model.close()
Пример #5
0
def evaluate(_):
    win_name = 'Detector'
    cv2.namedWindow(win_name)

    cam = cv2.VideoCapture('http://10.104.10.84:8090/')
    #cam = cv2.VideoCapture('http://10.104.10.23:8080/video')
    source_h = cam.get(cv2.CAP_PROP_FRAME_HEIGHT)
    source_w = cam.get(cv2.CAP_PROP_FRAME_WIDTH)

    model_cls = find_class_by_name(FLAGS.model_name, [yolo])
    model = model_cls(input_shape=(source_h, source_w, 3))
    model.init()

    frame_num = 0
    start_time = time.time()
    fps = 0
    try:
        while True:
            ret, frame = cam.read()

            if not ret:
                logger.info('Can\'t read video data. Potential end of stream')
                return

            predictions = model.evaluate(frame)

            for o in predictions:
                x1 = o['box']['left']
                x2 = o['box']['right']

                y1 = o['box']['top']
                y2 = o['box']['bottom']

                color = o['color']
                class_name = o['class_name']

                # Draw box
                cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)

                # Draw label
                (test_width, text_height), baseline = cv2.getTextSize(
                    class_name, cv2.FONT_HERSHEY_SIMPLEX, 0.75, 1)
                cv2.rectangle(frame, (x1, y1),
                              (x1 + test_width, y1 - text_height - baseline),
                              color,
                              thickness=cv2.FILLED)
                cv2.putText(frame, class_name, (x1, y1 - baseline),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)

            end_time = time.time()
            fps = fps * 0.9 + 1 / (end_time - start_time) * 0.1
            start_time = end_time

            # Draw additional info
            frame_info = 'Frame: {0}, FPS: {1:.2f}'.format(frame_num, fps)
            cv2.putText(frame, frame_info, (10, frame.shape[0] - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
            logger.info(frame_info)

            cv2.imshow(win_name, frame)

            if predictions:
                logger.info('Predictions: {}'.format(
                    format_predictions(predictions)))

            key = cv2.waitKey(1) & 0xFF

            # Exit
            if key == ord('q'):
                break

            # Take screenshot
            if key == ord('s'):
                cv2.imwrite('frame_{}.jpg'.format(time.time()), frame)

            frame_num += 1

    finally:
        cv2.destroyAllWindows()
        cam.release()
        model.close()
Пример #6
0
    def _cam_loop(self):
        logger.info('Start camera loop')
        cam = cv2.VideoCapture('http://10.104.10.84:8090')
        #cam = cv2.VideoCapture('http://10.104.10.23:8080/video')
        if not cam.isOpened():
            raise IOError('Can\'t open "{}"'.format(0))

        source_h = cam.get(cv2.CAP_PROP_FRAME_HEIGHT)
        source_w = cam.get(cv2.CAP_PROP_FRAME_WIDTH)

        model = Yolo2Model(input_shape=(source_h, source_w, 3))
        model.init()

        start_time = time.time()
        frame_num = 0
        fps = 0
        try:
            while self.is_running:
                ret, frame = cam.read()

                if not ret:
                    logger.warning('Can\'t read video data')
                    continue

                predictions = model.evaluate(frame)

                for o in predictions:
                    x1 = o['box']['left']
                    x2 = o['box']['right']

                    y1 = o['box']['top']
                    y2 = o['box']['bottom']

                    color = o['color']
                    class_name = o['class_name']

                    # Draw box
                    cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)

                    # Draw label
                    (test_width, text_height), baseline = cv2.getTextSize(
                        class_name, cv2.FONT_HERSHEY_SIMPLEX, 0.75, 1)
                    cv2.rectangle(
                        frame, (x1, y1),
                        (x1 + test_width, y1 - text_height - baseline),
                        color,
                        thickness=cv2.FILLED)
                    cv2.putText(frame, class_name, (x1, y1 - baseline),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)

                end_time = time.time()
                fps = fps * 0.9 + 1 / (end_time - start_time) * 0.1
                start_time = end_time

                # Draw additional info
                frame_info = 'Frame: {0}, FPS: {1:.2f}'.format(frame_num, fps)
                cv2.putText(frame, frame_info, (10, frame.shape[0] - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
                logger.info(frame_info)

                self._detect_frame_data_id = frame_num
                _, img = cv2.imencode('.jpg', frame, self.encode_params)
                self._detect_frame_data = img

                if predictions:
                    formatted = format_predictions(predictions)
                    logger.info('Predictions: {}'.format(formatted))
                    self._send_dh(format_notification(predictions))

                frame_num += 1

        finally:
            cam.release()
            model.close()
Пример #7
0
    def predict(self):
        ret, frame = self.cam.read()
        predictions = self.model.evaluate(frame)
        num_persons = 0
        for o in predictions:
            x1 = o['box']['left']
            x2 = o['box']['right']

            y1 = o['box']['top']
            y2 = o['box']['bottom']

            color = o['color']
            class_name = o['class_name']
            if class_name == 'person':
                if (abs(x1 - x2) * abs(y1 - y2)) > 1000:
                    num_persons = num_persons + 1
                    # Draw box
                    cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)

                    # Draw label
                    (test_width, text_height), baseline = cv2.getTextSize(
                        class_name, cv2.FONT_HERSHEY_SIMPLEX, 0.75, 1)
                    cv2.rectangle(
                        frame, (x1, y1),
                        (x1 + test_width, y1 - text_height - baseline),
                        color,
                        thickness=cv2.FILLED)
                    cv2.putText(frame, class_name, (x1, y1 - baseline),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)

        end_time = time.time()
        self.fps = self.fps * 0.9 + 1 / (end_time - self.start_time) * 0.1
        self.start_time = end_time
        # Draw additional info
        frame_info = '{0}, FPS: {1:.2f}'.format(
            datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y"), self.fps)
        cv2.putText(frame, frame_info, (10, frame.shape[0] - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
        logger.info(frame_info)

        if predictions:
            logger.info('Predictions: {}'.format(
                format_predictions(predictions)))
        if num_persons > 0 and time.time() - self.last_time > 20:
            name = datetime.datetime.now().strftime("%I:%M%p-%B-%d-%Y")
            cv2.imwrite("/home/nvidia/footage/" + name + ".png", frame)
            self.last_time = time.time()
            cv2.imwrite('yo.png', frame)
            img_f = open('yo.png', 'r')
            out = base64.b64encode(img_f.read())
            pusher_image.push(str(name) + " " + str(out))
            os.remove('yo.png')
            new_notification = onesignal_sdk.Notification(
                contents={
                    "en": "A person may be at your house at " + str(name),
                    "tr": "Person!"
                })
            new_notification.set_included_segments(["All"])
            onesignal_response = onesignal_client.send_notification(
                new_notification)

        return frame
Пример #8
0
def video(_):
    win_name = 'Detector'
    cv2.namedWindow(win_name)

    video = FLAGS.video

    # Define the codec and create VideoWriter object
    # fourcc = cv2.VideoWriter_fourcc(*'XVID')
    # out = cv2.VideoWriter('videos/YOLO_output.avi',fourcc, 30.0, (640,480))
    # fourcc = cv2.VideoWriter_fourcc(*'0X00000021')
    # out = cv2.VideoWriter('videos/YOLO_output.mp4',fourcc, 30.0, (640,480))
    # out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (640,480))

    cam = cv2.VideoCapture(video)
    if not cam.isOpened():
        raise IOError('Can\'t open "{}"'.format(FLAGS.video))

    frame_width = int(cam.get(3))
    frame_height = int(cam.get(4))

    # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
    out = cv2.VideoWriter('videos/YOLO_output.avi',
                          cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10,
                          (frame_width, frame_height))

    source_h = cam.get(cv2.CAP_PROP_FRAME_HEIGHT)
    source_w = cam.get(cv2.CAP_PROP_FRAME_WIDTH)

    model_cls = find_class_by_name(FLAGS.model_name, [yolo])
    model = model_cls(input_shape=(source_h, source_w, 3))
    model.init()

    frame_num = 0
    start_time = time.time()
    fps = 0

    try:
        while True:
            # ret, frame = cam.read()

            for i in range(int(FLAGS.skip_n_frames)):
                ret, frame = cam.read()

            if not ret:
                logger.info('Can\'t read video data. Potential end of stream')
                return

            predictions = model.evaluate(frame)

            p = []
            for i in predictions:
                if i['class_name'] == 'person':
                    box_1 = i['box']
                    r1 = (box_1['top'], box_1['left'], box_1['bottom'],
                          box_1['right'])
                    if frame_size(r1) <= 100000:
                        if i['score'] > float(FLAGS.threshold):
                            p.append(i)

            predictions = p
            num_persons = len(predictions)

            # trajectory computation
            if frame_num != 0:
                cost_matrix = [[] for a in predictions]
                for i in range(len(predictions)):
                    box_1 = predictions[i]['box']
                    r1 = (box_1['top'], box_1['left'], box_1['bottom'],
                          box_1['right'])
                    for j in range(len(previous_frame_pred)):
                        box_2 = previous_frame_pred[j]['box']
                        r2 = (box_2['top'], box_2['left'], box_2['bottom'],
                              box_2['right'])

                        cost_matrix[i].append(intersection_over_union(r1, r2))

                cost = np.array(cost_matrix)
                # print(np.max(cost, axis=1))
                max_indices = np.argmax(cost, axis=1)

                for index in range(len(max_indices)):
                    if index <= num_previous_pred:
                        # if previous_frame_pred[max_indices[index]]['index'] and cost[index][max_indices[index]] >= 0.5:
                        #     predictions[index]['index'] = previous_frame_pred[max_indices[index]]['index']
                        if previous_frame_pred[
                                max_indices[index]]['color'] and cost[index][
                                    max_indices[index]] >= 0.5:
                            # if previous_frame_pred[max_indices[index]]['color']:
                            predictions[index]['color'] = previous_frame_pred[
                                max_indices[index]]['color']
                            predictions[index]['index'] = previous_frame_pred[
                                max_indices[index]]['index']
                        else:
                            predictions[index]['color'] = (randint(0, 255),
                                                           randint(0, 255),
                                                           randint(0, 255))
                            predictions[index]['index'] = randint(12, 100)

            previous_frame_pred = predictions
            num_previous_pred = len(predictions)

            for o in predictions:
                x1 = o['box']['left']
                x2 = o['box']['right']

                y1 = o['box']['top']
                y2 = o['box']['bottom']

                color = o['color']
                class_name = o['class_name'] + str(o['index'])
                # index = str(o['index'])

                # Draw box
                cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)

                # Draw label
                (test_width, text_height), baseline = cv2.getTextSize(
                    class_name, cv2.FONT_HERSHEY_SIMPLEX, 0.75, 1)
                cv2.rectangle(frame, (x1, y1),
                              (x1 + test_width, y1 - text_height - baseline),
                              color,
                              thickness=cv2.FILLED)
                cv2.putText(frame, class_name, (x1, y1 - baseline),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)

            end_time = time.time()
            # fps = fps * 0.9 + 1/(end_time - start_time) * 0.1
            fps = fps * 0.9 + 1 / (end_time - start_time) * 0.1
            start_time = end_time

            if predictions:
                logger.info('Predictions: {}'.format(
                    format_predictions(predictions)))

            # Draw additional info
            frame_info = 'Frame: {0}, FPS: {1:.2f}, Persons: {2}'.format(
                frame_num, fps, num_persons)
            cv2.putText(frame, frame_info, (10, frame.shape[0] - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
            logger.info(frame_info)

            out.write(frame)
            cv2.imshow(win_name, frame)

            if FLAGS.save:
                image_name = 'videos/YOLOFrames/frame_' + str(
                    frame_num) + '.png'
                cv2.imwrite(image_name, frame)

            # if predictions:
            #     logger.info('Predictions: {}'.format(
            #         format_predictions(predictions)))

            key = cv2.waitKey(1) & 0xFF

            # Exit
            if key == ord('q'):
                break

            # Take screenshot
            if key == ord('s'):
                cv2.imwrite('frame_{}.jpg'.format(time.time()), frame)

            frame_num += 1

    finally:
        if FLAGS.image:
            cv2.imshow('image', frame)
        else:
            cv2.destroyAllWindows()
            cam.release()
            out.release()
            model.close()