def display(self):
        """ 
        display the processed images in a thread, for models expected output format see tf.image.combined_non_max_suppression
        https://www.tensorflow.org/api_docs/python/tf/image/combined_non_max_suppression
        """
        # init the starting variables to calculate FPS
        try:
            start = time.time()
            l = 0
            tick = 0
            while (self._cap.isOpened() and self._running):
                # if the display que is empty, nothing has been processed, just wait for an image to arrive
                # do not timeout ever as too long does not garuntee an error
                while self._display_que.empty() and self._running:
                    time.sleep(self._wait_time)
                    if not self._running:
                        return
                if not self._running:
                    return

                # get the images, the predictions placed on the que via the run function (the model)
                image, boxes, classes, conf = self._display_que.get()

                # there is potential for the images to be processed in batches, so for each image in the batch draw the boxes and the predictions and the confidence
                for i in range(image.shape[0]):
                    if self._print_conf:
                        self._obj_detected = draw_box(image[i], boxes[i], classes[i],
                                                    conf[i], self._colors,
                                                    self._labels)
                    else:
                        self._obj_detected = draw_box(image[i], boxes[i], classes[i],
                            None, self._colors,
                            self._labels)
                    #display the frame then wait in case something else needs to catch up
                    cv2.imshow("frame", image[i])
                    time.sleep(self._wait_time)

                    #compute the display fps
                    l += 1
                    if time.time() - start - tick >= 1:
                        tick += 1
                        # store the fps to diplayed to the user
                        self._display_fps = l
                        l = 0
                    if cv2.waitKey(1) & 0xFF == ord('q') or not self._running:
                        break
        except ValueError:
            self._running = False
            raise
        except Exception as e:
            #print ("display", e)
            self._running = False
            #time.sleep(10)
            raise e
        self._running = False
        return
Beispiel #2
0
def video_processor(model, version , vidpath, device="/CPU:0"):
    img_array = []

    i = 0
    t = 0
    start = time.time()
    tick = 0
    e, f, a, b, c, d = 0, 0, 0, 0, 0, 0
    if isinstance(model, str):
        with tf.device(device):
            model = build_model(name=model, model_version=version)
            model.make_predict_function()

    if hasattr(model, "predict"):
        predfunc = model.predict
        print("using pred function")
    else:
        predfunc = model
        print("using call function")

    colors = gen_colors(80)
    label_names = get_coco_names(
        path="yolo/dataloaders/dataset_specs/coco.names")
    print(label_names)

    # output_writer = cv2.VideoWriter('yolo_output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), frame_count, (480, 640))  # change output file name if needed
    pred = None
    cap = cv2.VideoCapture(vidpath)
    assert cap.isOpened()

    width = int(cap.get(3))
    height = int(cap.get(4))
    print('width, height, fps:', width, height, int(cap.get(5)))
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

    while cap.isOpened():
        success, image = cap.read()

        #with tf.device(device):
        e = datetime.datetime.now()
        image = tf.cast(image, dtype=tf.float32)
        image = image / 255
        f = datetime.datetime.now()

        if t % 1 == 0:
            a = datetime.datetime.now()
            #with tf.device(device):
            pimage = tf.expand_dims(image, axis=0)
            pimage = tf.image.resize(pimage, (416, 416))
            pred = predfunc(pimage)
            b = datetime.datetime.now()

        image = image.numpy()
        if pred != None:
            c = datetime.datetime.now()
            boxes, classes = int_scale_boxes(pred["bbox"], pred["classes"],
                                             width, height)
            draw = get_draw_fn(colors, label_names, 'YOLO')
            draw_box(image, boxes[0].numpy(), classes[0].numpy(),
                     pred["confidence"][0], draw)
            d = datetime.datetime.now()

        cv2.imshow('frame', image)
        i += 1
        t += 1

        if time.time() - start - tick >= 1:
            tick += 1
            print_opt((((f - e) + (b - a) + (d - c))), i)
            i = 0
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
    return