Пример #1
0
def detect(target: str, language: str, dir: str, camera: str, path: str):
    """
    Take feed from camera and detect vehicle (using TVM opt model),
    draw bounding box, and read license plate (based on the language)

    Parameters
    ----------
    target: str
        The device (CPU or GPU) that TVM will inference on
    language: str
        Region of the license plate that OpenALPR will detect
    dir: str
        Directory that has shared library, graphs, and params
    camera: str
        Specified camera input to use
    path: str
        Path to OpenALPR folder
    """

    alpr = get_alpr(language, path)
    ctx = tvm.context(target, 0)
    if ctx.exist:
        graph, lib, params = build(dir)
    else:
        print("Target does not exist")
        sys.exit(1)

    print("Starting video stream...")

    cap = VideoCaptureThreading("/dev/video" + camera)
    cap.start()
    module = graph_runtime.create(graph, lib, ctx)
    module.load_params(params)
    fps = FPS()
    fps = fps.start()
    while True:

        _, frame, oframe, x, img = cap.read()

        class_IDs, scores, bounding_boxs = run(x, module, ctx)

        class_IDs, bounding_boxs, scores = convertAsNumpy(
            class_IDs, bounding_boxs, scores)

        oframe = draw_plates(class_IDs, scores, bounding_boxs, oframe, img,
                             alpr)
        cv2.imshow("frame", oframe)

        if cv2.waitKey(1) & 0xFF == ord("q"):
            cap.stop()
            break

        fps.update()
    fps.stop()
    print("Approx. FPS: {:.2f}".format(fps.fps()))
    cv2.destroyAllWindows()
Пример #2
0
 def __init__(self, module, ctx, alpr, x=None, img=None, oframe=None):
     self.x = x
     self.img = img
     self.alpr = alpr
     self.oframe = oframe
     self.stopped = False
     self.module = module
     self.ctx = ctx
     class_IDs, scores, bounding_boxs = run(x, module, ctx)
     self.class_IDs, self.scores, self.bounding_boxs = convertAsNumpy(
         class_IDs, bounding_boxs, scores
     )
Пример #3
0
def numpy_convert_test():
    """
    Check that convertAsNumpy is able to convert nd.array to np.array
    """

    a = nd.array([1, 2, 3])
    b = nd.array([1, 2, 3])
    c = nd.array([1, 2, 3])
    a_new, b_new, c_new = convertAsNumpy(a, b, c)

    assert isinstance(a_new, np.ndarray)
    assert isinstance(b_new, np.ndarray)
    assert isinstance(c_new, np.ndarray)
    print("[3/3] Test numpy_convert Pass")
Пример #4
0
    def inf(self):
        while not self.stopped:

            class_IDs, scores, bounding_boxs = run(self.x, self.module, self.ctx)

            class_IDs, bounding_boxs, scores = convertAsNumpy(
                class_IDs, bounding_boxs, scores
            )

            oframe = draw_plates(
                class_IDs, scores, bounding_boxs, self.oframe, self.img, self.alpr
            )

            cv2.imshow("frame", oframe)

            if cv2.waitKey(1) & 0xFF == ord("q"):
                self.stopped = True
Пример #5
0
def mxnet_inf_test(frame):
    """
    Test getting alpr object, fetching input image, transorming imgae, making inference using mxnet, and converting result to numpy
    """

    alpr = get_alpr("eu", "/srv/openalpr")
    ctx = mx.cpu()
    model_name = "ssd_512_mobilenet1.0_voc"
    net = model_zoo.get_model(model_name, pretrained=True, ctx=ctx)
    net.hybridize()

    x, img = data.transforms.presets.ssd.load_test(frame, short=480)
    x = x.as_in_context(ctx)

    class_IDs, scores, bounding_boxs = net(x)

    class_IDs, bounding_boxs, scores = convertAsNumpy(class_IDs, bounding_boxs,
                                                      scores)
Пример #6
0
def detect(target: str, language: str, camera: str, path: str):
    """
    Take feed from camera and detect vehicle (using mxnet),
    draw bounding box, and read license plate (based on the language)

    Parameters
    ----------
    target: str
        The device (CPU or GPU) that mxnet will inference on
    language: str
        Region of the license plate that OpenALPR will detect
    camera: str
        Specified camera input to use
    path: str
        Specified path to OpenALPR folder
    """

    if target == "llvm":
        ctx = mx.cpu()
    elif target == "cuda":
        ctx = mx.gpu()
    else:
        print("Target does not exist")
        sys.exit(1)

    alpr = get_alpr(language, path)
    model_name = "ssd_512_mobilenet1.0_voc"
    net = model_zoo.get_model(model_name, pretrained=True, ctx=ctx)
    net.hybridize()

    print("Starting video stream...")
    cap = cv2.VideoCapture("/dev/video" + camera)
    if not cap.isOpened():
        print("Could not open video device (change video_camera)")
        sys.exit(1)

    fps = FPS().start()
    while True:
        ret, frame = cap.read()
        oframe = frame

        frame = mx.nd.array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)).astype("uint8")
        x, img = data.transforms.presets.ssd.transform_test(frame, short=480)
        x = x.as_in_context(ctx)

        class_IDs, scores, bounding_boxs = net(x)

        class_IDs, bounding_boxs, scores = convertAsNumpy(
            class_IDs, bounding_boxs, scores
        )

        oframe = draw_plates(class_IDs, scores, bounding_boxs, oframe, img, alpr)

        cv2.imshow("frame", oframe)

        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
        fps.update()
    fps.stop()
    print("Elapsed time: {:.2f}".format(fps.elapsed()))
    print("Approx. FPS: {:.2f}".format(fps.fps()))

    # clean up capture window
    cap.release()
    cv2.destroyAllWindows()