def test_psb_Builder_simple_obj():
    builder = Builder(VisionSubclass,
                      Args("path/to/images", keyword_argument=True),
                      ProcessorA,
                      Args(color='color'),
                      ProcessorB,
                      Args(camera=CustomObject('a var', 'b var')),
                      display_results=True)

    d = builder.todict()
    assert (d == truth_builder_simple_obj)

    processor = builder.build()

    assert (isinstance(processor, ProcessorB))
    assert (isinstance(processor.source, ProcessorA))
    assert (isinstance(processor.source.source, VisionSubclass))

    assert (processor.display_results)
    assert (processor.source.display_results)
    assert (processor.source.source.display_results)

    assert (isinstance(processor._camera, CustomObject))
    assert (processor._camera.a == 'a var')
    assert (processor._camera.b == 'b var')
    assert (processor.source._color == 'color')
    assert (processor.name == "ProcessorB <- ProcessorA <- path/to/images")

    with processor as vision:
        for frame in vision:
            break
def test_psb_Args_todict_kwargs_objects():
    arg = Args(1,
               2,
               3,
               custom=CustomObject('a var', 'b var'),
               custom1=CustomObject('A var', 'B var'))
    d = arg.todict()
    assert (d == args_truth)
def test_psb_Args_fromdict_kwargs_objects():
    arg = Args.fromdict(args_truth, (CustomObject, ))

    assert (arg.args == (1, 2, 3))
    assert (isinstance(arg.kwargs['custom'], CustomObject))
    assert (arg.kwargs['custom'].a == 'a var')
    assert (arg.kwargs['custom'].b == 'b var')
    assert (isinstance(arg.kwargs['custom1'], CustomObject))
    assert (arg.kwargs['custom1'].a == 'A var')
    assert (arg.kwargs['custom1'].b == 'B var')
def test_psb_Builder_Builder():
    builder = Builder(Builder(VisionSubclass,
                              Args("path/to/left_images",
                                   keyword_argument=True),
                              ProcessorA,
                              Args(color='color left'),
                              ProcessorB,
                              Args(camera=None),
                              display_results=1),
                      Builder(VisionSubclass,
                              Args("path/to/right_images",
                                   keyword_argument=True),
                              ProcessorA,
                              Args(color='color right'),
                              ProcessorB,
                              Args(camera=None),
                              display_results=2),
                      ProcessorC,
                      Args(camera='camera'),
                      display_results=True)

    d = builder.todict()
    assert (d == truth_builder_stereo)

    processor = builder.build()

    assert (isinstance(processor, ProcessorC))

    assert (isinstance(processor._a, ProcessorB))
    assert (isinstance(processor._a.source, ProcessorA))
    assert (isinstance(processor._a.source.source, VisionSubclass))

    assert (isinstance(processor._b, ProcessorB))
    assert (isinstance(processor._b.source, ProcessorA))
    assert (isinstance(processor._b.source.source, VisionSubclass))

    assert (processor.display_results)
    assert (processor._a.display_results == 1)
    assert (processor._a.source.display_results == 1)
    assert (processor._a.source.source.display_results == 1)
    assert (processor._b.display_results == 2)
    assert (processor._b.source.display_results == 2)
    assert (processor._b.source.source.display_results == 2)

    assert (processor._camera == 'camera')
    assert (
        processor.name ==
        "ProcessorB <- ProcessorA <- path/to/left_images : ProcessorB <- ProcessorA <- path/to/right_images"
    )

    with processor as vision:
        for frame in vision:
            break
def test_psb_Args_simple():
    arg = Args(1,
               2.5,
               3,
               None,
               True,
               False, (1, ), [2], {1, 2, 3, 1}, {'a': 'b'},
               custom='color',
               none_type=None,
               color=cv2.COLOR_BGR2GRAY)
    assert (arg.args == (1, 2.5, 3, None, True, False, (1, ), [2], {1, 2, 3}, {
        'a': 'b'
    }))
    assert (arg.kwargs == {
        'custom': 'color',
        'none_type': None,
        'color': cv2.COLOR_BGR2GRAY
    })
예제 #6
0
def main():
    parser = ArgumentParser(description="Stereo Camera calibration tool")
    parser.add_argument("left", help="Left Camera device ID/folder")
    parser.add_argument("right", help="Left Camera device ID/folder")
    parser.add_argument("-f",
                        "--file",
                        default="stereo_camera.json",
                        help="Output filename of the calibrated camera")
    parser.add_argument("-i",
                        "--size",
                        default="640,480",
                        help="Frame width and height")
    parser.add_argument("-p", "--fps", default="5", help="Frame rate")
    parser.add_argument("-g",
                        "--grid",
                        default="9,7",
                        help="Grid shape of the calibration target")
    parser.add_argument("-N",
                        type=int,
                        default=30,
                        help="Number of samples to gather")
    parser.add_argument("-t",
                        "--test",
                        const=True,
                        default=False,
                        action='store_const',
                        help="Test camera calibration file")
    parser.add_argument("-d",
                        "--disparity",
                        const=True,
                        default=False,
                        action='store_const',
                        help="Calculate Disparity Map")

    args = parser.parse_args()
    grid = tuple(int(i) for i in args.grid.split(','))
    size = tuple(int(i) for i in args.size.split(','))

    try:
        left = int(args.left)
        right = int(args.right)
    except:
        left = args.left
        right = args.right

    camera_model = None
    if args.test:
        with open(args.file) as f:
            camera_model = StereoCamera.fromdict(json.load(f))
            print("{} was loaded for evaluation".format(args.file))

    builder = Builder(
        Builder(VideoCapture,
                Args(left, width=size[0], height=size[1], fps=int(args.fps)),
                ImageTransform, Args(ocl=args.test), CalibratedCamera,
                Args(None if camera_model is None else camera_model.left)),
        Builder(VideoCapture,
                Args(right, width=size[0], height=size[1], fps=int(args.fps)),
                ImageTransform, Args(ocl=args.test), CalibratedCamera,
                Args(None if camera_model is None else camera_model.right)),
        CalibratedStereoCamera,
        Args(camera_model,
             max_samples=args.N,
             grid_shape=grid,
             calculate_disparity=args.disparity,
             display_results=True))

    if args.test:
        with builder.build() as vision:
            for frame in vision:
                if cv2.waitKey(1) == 27:
                    break
    else:
        with builder.build() as vision:
            cam = None
            while not cam:
                cam = vision.calibrate()
                if cv2.waitKey(1) == 27:
                    print("Calibration was aborted")
                    return

            with open(args.file, "w") as f:
                f.write(json.dumps(cam.todict(), indent=4))
                print("{} was written".format(args.file))
def test_psb_Args_fromdict_args_obj():
    arg = Args.fromdict(args_truth_simple_obj, (CustomObject, ))

    assert (isinstance(arg.args[2], CustomObject))
    assert (arg.kwargs == {'custom': 'color'})
def test_psb_Args_fromdict_simple():
    arg = Args.fromdict(args_truth_simple)

    assert (arg.args == (1, 2, 3))
    assert (arg.kwargs == {'custom': 'color'})
def test_psb_Args_todict_simple_obj():
    arg = Args(1, 2, CustomObject('a var', 'b var'), custom='color')
    d = arg.todict()
    assert (d == args_truth_simple_obj)
예제 #10
0
def test_psb_Args_todict_simple():
    arg = Args(1, 2, 3, custom='color')
    d = arg.todict()
    assert (d == args_truth_simple)
예제 #11
0
def test_psb_Args_object():
    arg = Args(1, 2, 3, custom=CustomObject('a var', 'b var'))
    assert (arg.args == (1, 2, 3))
    assert (isinstance(arg.kwargs['custom'], CustomObject))
    assert (arg.kwargs['custom'].a == 'a var')
    assert (arg.kwargs['custom'].b == 'b var')
예제 #12
0
def main():
    parser = ArgumentParser(description="Camera calibration tool")
    parser.add_argument("camera", help="Camera device ID/folder")
    parser.add_argument("-f",
                        "--file",
                        default="camera.json",
                        help="Output filename of the calibrated camera")
    parser.add_argument("-g",
                        "--grid",
                        default="9,7",
                        help="Grid shape of the calibration target")
    parser.add_argument("-i",
                        "--size",
                        default="640,480",
                        help="Frame width and height")
    parser.add_argument("-p", "--fps", default="5", help="Frame rate")
    parser.add_argument("-N",
                        type=int,
                        default=30,
                        help="Number of samples to gather")
    parser.add_argument("-t",
                        "--test",
                        const=True,
                        default=False,
                        action='store_const',
                        help="Test camera calibration file")

    args = parser.parse_args()
    grid = tuple(int(i) for i in args.grid.split(','))
    size = tuple(int(i) for i in args.size.split(','))

    try:
        camera = int(args.camera)
    except:
        camera = args.camera

    camera_model = None
    if args.test:
        with open(args.file) as f:
            camera_model = PinholeCamera.fromdict(json.load(f))

    builder = Builder(
        VideoCapture,
        Args(camera, width=size[0], height=size[1], fps=int(args.fps)),
        CalibratedCamera,
        Args(camera_model,
             max_samples=args.N,
             grid_shape=grid,
             display_results=True))

    if args.test:
        with builder.build() as vision:
            for frame in vision:
                if cv2.waitKey(1) == 27:
                    break
    else:
        with builder.build() as vision:
            cam = None
            while not cam:
                cam = vision.calibrate()
                cv2.waitKey(1)

            with open(args.file, "w") as f:
                f.write(json.dumps(cam.todict(), indent=4))
예제 #13
0
def main():
    parser = ArgumentParser(description="Camera calibration tool")
    parser.add_argument("device", help="Camera device ID/folder")
    parser.add_argument("-f",
                        "--file",
                        default="model.json",
                        help="Output filename of the learned model")
    parser.add_argument("-c",
                        "--camera",
                        default="camera.json",
                        help="Calibrated PinholeCamera file")
    parser.add_argument("-H",
                        "--hand",
                        default="hand.json",
                        help="Color histogram of the hand")
    parser.add_argument("-e",
                        "--feature_type",
                        default="ORB",
                        help="Feature Type (e.g. ORB/FREAK/SIFT)")
    parser.add_argument("-i",
                        "--size",
                        default="640,480",
                        help="Frame width and height")
    parser.add_argument("-p", "--fps", default="5", help="Frame rate")
    parser.add_argument("-t",
                        "--test",
                        const=True,
                        default=False,
                        action='store_const',
                        help="Test learned model")

    args = parser.parse_args()
    size = tuple(int(i) for i in args.size.split(','))

    try:
        camera = int(args.device)
    except:
        camera = args.device

    camera_model = None
    object_model = None
    hand_model = HAND_MODEL
    if args.test:
        with open(args.file) as f:
            pass
            object_model = ObjectModel.fromdict(json.load(f))
    try:
        with open(args.camera) as f:
            camera_model = PinholeCamera.fromdict(json.load(f))
    except:
        pass

    try:
        with open(args.hand) as f:
            hand_model = np.float32(json.load(f))
    except:
        pass

    if args.test:
        builder = Builder(
            VideoCapture,
            Args(camera, width=size[0], height=size[1], fps=int(args.fps)),
            CalibratedCamera,
            Args(camera_model,
                 enabled=camera_model is not None,
                 display_results=True), FeatureExtraction,
            Args(feature_type=args.feature_type), ObjectRecognitionEngine,
            Args(feature_type=args.feature_type, display_results=True))
        object_model.display_results = True
        with builder.build() as engine:
            engine.models[object_model.name] = object_model
            for frame in engine:
                if cv2.waitKey(1) == 27:
                    break
    else:
        builder = Builder(
            VideoCapture,
            Args(camera, width=size[0], height=size[1],
                 fps=int(args.fps)), CalibratedCamera,
            Args(camera_model, enabled=camera_model
                 is not None), HistogramBackprojection,
            Args(hand_model,
                 invert=True,
                 combine_masks=True,
                 enabled=hand_model is not None,
                 display_results=True), BackgroundSeparation,
            Args(algorithm="MOG", display_results=True), FeatureExtraction,
            Args(feature_type=args.feature_type, display_results=True),
            ObjectRecognitionEngine,
            Args(feature_type=args.feature_type, display_results=True))
        print("'b' to relearn background")
        print("Spacebar to learn model view")
        with builder.build() as vision:
            last = 0
            cur = 0
            for frame, obj in vision:
                cur += 1
                ch = cv2.waitKey(1)
                if ch == 27:
                    break
                elif ch == ord(' ') and cur - last > 30:
                    model = vision.enroll("object",
                                          frame,
                                          add=True,
                                          display_results=True)
                    last = cur
                    if model is not None:
                        print("model updated: %i" % len(model))
                    if model:
                        object_model = model
                elif ch == ord('b'):
                    vision.vision.background_num = 0

            if object_model:
                with open(args.file, "w") as f:
                    f.write(json.dumps(object_model.todict(), indent=4))