def main() -> None:
    aspect_ratio = 16 / 9
    image_width = 720
    image_height = int(image_width / aspect_ratio)
    samples_per_pixel = 48
    max_depth = 5

    world = random_scene()

    lookfrom = Point3(13, 2, 3)
    lookat = Point3(0, 0, 0)
    vup = Vec3(0, 1, 0)
    vfov = 20
    dist_to_focus = 10
    aperture = 0.1
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus)

    print("Start rendering.")
    start_time = time.time()

    img_list = Parallel(n_jobs=2, verbose=20)(
        delayed(scan_frame)(world, cam, image_width, image_height, max_depth)
        for s in range(samples_per_pixel))

    end_time = time.time()
    print(f"\nDone. Total time: {round(end_time - start_time, 1)} s.")

    final_img = Img(image_width, image_height)
    for img in img_list:
        final_img.write_frame(img)
    final_img.average(samples_per_pixel).gamma(2)
    final_img.save("./output.png", True)
Ejemplo n.º 2
0
def main():
    args = parse_args()
    if args.category_num <= 0:
        raise SystemExit('Bad category_num: %d!' % args.category_num)

    cam = Camera(args)
    cam.open()
    if not cam.is_opened:
        sys.exit('Failed to open camera!')

    cls_dict = get_cls_dict(args.category_num)
    yolo_dim = int(args.model.split('-')[-1])
    if yolo_dim not in (288, 416, 608):
        raise SystemExit('Bad yolo_dim: %d!\nPlease make sure the model file name contains the correct dimension...' % yolo_dim)

    trt_yolov3 = TrtYOLOv3(args.model, (yolo_dim, yolo_dim), args.category_num)

    cam.start()
    open_window(WINDOW_NAME, args.image_width, args.image_height,
                'Camera TensorRT YOLOv3 Demo')
    vis = BBoxVisualization(cls_dict)
    loop_and_detect(cam, trt_yolov3, conf_th=0.3, vis=vis)

    cam.stop()
    cam.release()
    cv2.destroyAllWindows()
Ejemplo n.º 3
0
def main():
    args = parse_args()
    if args.category_num <= 0:
        raise SystemExit('ERROR: bad category_num (%d)!' % args.category_num)
    if not os.path.isfile('yolo/%s.trt' % args.model):
        raise SystemExit('ERROR: file (yolo/%s.trt) not found!' % args.model)

    cam = Camera(args)
    if not cam.isOpened():
        raise SystemExit('ERROR: failed to open camera!')

    cls_dict = get_cls_dict(args.category_num)
    yolo_dim = args.model.split('-')[-1]
    if 'x' in yolo_dim:
        dim_split = yolo_dim.split('x')
        if len(dim_split) != 2:
            raise SystemExit('ERROR: bad yolo_dim (%s)!' % yolo_dim)
        w, h = int(dim_split[0]), int(dim_split[1])
    else:
        h = w = int(yolo_dim)
    if h % 32 != 0 or w % 32 != 0:
        raise SystemExit('ERROR: bad yolo_dim (%s)!' % yolo_dim)

    trt_yolo = TrtYOLO(args.model, (h, w), args.category_num)

    open_window(WINDOW_NAME, 'Camera TensorRT YOLO Demo', 640, 480)

    vis = BBoxVisualization(cls_dict)
    loop_and_detect(cam, trt_yolo, conf_th=0.3, vis=vis)

    cam.release()
    cv2.destroyAllWindows()
Ejemplo n.º 4
0
def main():
    args = parse_args()
    if args.category_num <= 0:
        raise SystemExit('ERROR: bad category_num (%d)!' % args.category_num)
    if not os.path.isfile('yolo/%s.trt' % args.model):
        raise SystemExit('ERROR: file (yolo/%s.trt) not found!' % args.model)

    cam = Camera(args)
    if not cam.isOpened():
        raise SystemExit('ERROR: failed to open camera!')

    cls_dict = get_cls_dict(args.category_num)
    vis = BBoxVisualization(cls_dict)
    h, w = get_input_shape(args.model)
    trt_yolo = TrtYOLO(args.model, (h, w), args.category_num, args.letter_box)

    open_window(WINDOW_NAME, 'Camera TensorRT YOLO Demo', cam.img_width,
                cam.img_height)

    msg_queue = Queue(maxsize=100)

    # msg_queue.put("0,0,0,-1".encode())
    Thread(target=serArd, args=(msg_queue, )).start()
    loop_and_detect(cam, trt_yolo, msg_queue, conf_th=0.7, vis=vis)
    while True:
        pass

    cam.release()
    cv2.destroyAllWindows()
Ejemplo n.º 5
0
def main():
    args = parse_args()
    cam = Camera(args)
    cam.open()
    if not cam.is_opened:
        sys.exit('Failed to open camera!')

    cls_dict = get_cls_dict('coco')
    yolo_dim = int(args.model.split('-')[-1])  # 416 or 608
    trt_yolov3 = TrtYOLOv3(args.model, (yolo_dim, yolo_dim))

    cam.start()
    # open_window(WINDOW_NAME, args.image_width, args.image_height,
                # 'Camera TensorRT YOLOv3 Demo')
    vis = BBoxVisualization(cls_dict)

    # for video
    # loop_and_detect(cam, trt_yolov3, conf_th=0.3, vis=vis)
    
    # for single file
    detect_demo(cam, trt_yolov3, conf_th=0.3, vis=vis)

    cam.stop()
    cam.release()
    cv2.destroyAllWindows()
Ejemplo n.º 6
0
def main():
    args = parse_args()

    #YOLO INIT
    #cls_dict = get_cls_dict('coco')
    cls_dict = get_cls_dict('deepfamily')
    print("classes count : ", len(cls_dict))
    yolo_dim = int(args.model.split('-')[-1])  # 416 or 608
    print("yolo_dim : ", yolo_dim)
    trt_yolov3 = TrtYOLOv3(args.model, (yolo_dim, yolo_dim))

    #CAMERA
    cam = Camera(args)
    cam.open()
    if not cam.is_opened:
        sys.exit('Failed to open camera!')
    cam.start()

    #CAM-WINDOW
    open_window(WINDOW_NAME, args.image_width, args.image_height,
                'DEEPFAMILY PROJECT - TensorRT YOLOv3')
    vis = BBoxVisualization(cls_dict)

    #DETECT-LOOP
    loop_and_detect(cam, trt_yolov3, conf_th=0.95, vis=vis)
    #loop_and_detect(cam, trt_yolov3, conf_th=0.95)

    cam.stop()
    cam.release()
    cv2.destroyAllWindows()
Ejemplo n.º 7
0
def main():
    args = parse_args()
    cam = Camera(args)
    cam.open()
    if not cam.is_opened:
        sys.exit('Failed to open camera!')

    trt_ssd = TrtSSD(args.model)

    cam.start()

    # initialize bot
    logger.info('initialize robot')
    robot = Robot()

    logger.info('starting to loop and detect')
    loop_and_detect(cam=cam,
                    trt_ssd=trt_ssd,
                    conf_th=0.3,
                    robot=robot,
                    model=args.model)

    logger.info('cleaning up')
    robot.stop()
    cam.stop()
    cam.release()
Ejemplo n.º 8
0
    def __init__(self,
                 width: int,
                 height: int,
                 title: str,
                 camera: bool = False):
        self.__Daytime = True
        self.__enableCamera = camera

        glut.glutInit()
        glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGBA)
        glut.glutInitWindowSize(width, height)
        glut.glutInitWindowPosition(0, 0)
        glut.glutCreateWindow(title)
        glut.glutReshapeFunc(self.__window_resize)
        glut.glutDisplayFunc(self.__display)
        glut.glutKeyboardFunc(self.__keyboardDown)
        glut.glutKeyboardUpFunc(self.__keyboardUp)
        glut.glutMotionFunc(self.__mouse_look_clb)

        self._create_shader()
        self.setSky(True)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        self.__ElementsList = []
        self.__camera = Camera()
        self.__lastX = width / 2
        self.__lastY = height / 2
        self.__first_mouse = True
        self.__left = False
        self.__right = False
        self.__forward = False
        self.__backward = False
Ejemplo n.º 9
0
def infer():
    # Image source
    cam = Camera()
    stream = cam.get_stream()
    # Load edge model
    EDGE_MODEL = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                              '../models/tpu/ohmnilabs_floornet_224_quant_postprocess_edgetpu.tflite')
    inference = Inference(EDGE_MODEL)
    # Prediction
    while True:
        start = time.time()
        print("======================================")
        # Infer
        raw_img = stream.get()
        img, mask = inference.predict(raw_img)
        # Visualize
        mask = cv.cvtColor(mask, cv.COLOR_GRAY2BGR)
        cv.addWeighted(mask, 0.5, img, 0.5, 0, img)
        cv.imshow('Camera', img)

        # Calculate frames per second (FPS)
        end = time.time()
        print('Total estimated time: {:.4f}'.format(end-start))
        fps = 1/(end-start)
        print("FPS: {:.1f}".format(fps))

        if cv.waitKey(10) & 0xFF == ord('q'):
            break
    # Clear resources
    cv.destroyAllWindows()
    cam.terminate()
Ejemplo n.º 10
0
def main() -> None:
    aspect_ratio = 16 / 9
    image_width = 256
    image_height = int(image_width / aspect_ratio)
    samples_per_pixel = 20
    max_depth = 10

    world: HittableList = three_ball_scene()

    lookfrom = Point3(13, 2, 3)
    lookat = Point3(0, 0, 0)
    vup = Vec3(0, 1, 0)
    vfov = 20
    dist_to_focus: float = 10
    aperture: float = 0.1
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus)

    print("Start rendering.")
    start_time = time.time()

    n_processer = multiprocessing.cpu_count()
    img_list: List[Img] = Parallel(n_jobs=n_processer)(
        delayed(scan_line)(j, world, cam, image_width, image_height,
                           samples_per_pixel, max_depth)
        for j in range(image_height - 1, -1, -1))

    final_img = Img(image_width, image_height)
    final_img.set_array(np.concatenate([img.frame for img in img_list]))

    end_time = time.time()
    print(f"\nDone. Total time: {round(end_time - start_time, 1)} s.")
    final_img.save("./output.png", True)
Ejemplo n.º 11
0
def infer():
    # Image source
    cam = Camera()
    stream = cam.get_stream()
    floorNet = FloorNet()
    # Prediction
    while True:
        start = time.time()
        print("======================================")
        # Infer
        raw_img = stream.get()
        img, mask = floorNet.predict(raw_img)
        # Visualize
        mask = cv.cvtColor(mask, cv.COLOR_GRAY2BGR)
        cv.addWeighted(mask, 0.5, img, 0.5, 0, img)
        cv.imshow('Camera', img)

        # Calculate frames per second (FPS)
        end = time.time()
        print('Total estimated time: {:.4f}'.format(end-start))
        fps = 1/(end-start)
        print("FPS: {:.1f}".format(fps))

        if cv.waitKey(10) & 0xFF == ord('q'):
            break
    # Clear resources
    cv.destroyAllWindows()
    cam.terminate()
def main():
    args = parse_args()
    if args.category_num <= 0:
        raise SystemExit('ERROR: bad category_num (%d)!' % args.category_num)
    if not os.path.isfile('yolo/%s.trt' % args.model):
        raise SystemExit('ERROR: file (yolo/%s.trt) not found!' % args.model)

    cam = Camera(args)
    if not cam.isOpened():
        raise SystemExit('ERROR: failed to open camera!')

    cls_dict = get_cls_dict(args.category_num)
    vis = BBoxVisualization(cls_dict)
    h, w = get_input_shape(args.model)
    trt_yolo = TrtYOLO(args.model, (h, w), args.category_num, args.letter_box)

    mjpeg_server = MjpegServer(port=args.mjpeg_port)
    print('MJPEG server started...')
    try:
        loop_and_detect(cam,
                        trt_yolo,
                        conf_th=0.3,
                        vis=vis,
                        mjpeg_server=mjpeg_server)
    except Exception as e:
        print(e)
    finally:
        mjpeg_server.shutdown()
        cam.release()
Ejemplo n.º 13
0
def main():
    args = parse_args()
    cam = Camera(args)
    cam.open()

    # import pdb
    # pdb.set_trace()

    if not cam.is_opened:
        sys.exit('[INFO]  Failed to open camera!')

    cls_dict = get_cls_dict('coco')
    yolo_dim = int(args.model.split('-')[-1])  # 416 or 608
    trt_yolov3 = TrtYOLOv3(args.model, (yolo_dim, yolo_dim))

    print('[INFO]  Camera: starting')
    cam.start()
    open_window(WINDOW_NAME, args.image_width, args.image_height,
                'TensorRT YOLOv3 Detector')
    vis = BBoxVisualization(cls_dict)
    loop_and_detect(cam,
                    args.runtime,
                    trt_yolov3,
                    conf_th=0.3,
                    vis=vis,
                    window_name=WINDOW_NAME)

    print('[INFO]  Program: stopped')
    cam.stop()
    cam.release()
    cv2.destroyAllWindows()
Ejemplo n.º 14
0
def three_ball_scene(aspect_ratio: float, time0: float, time1: float) \
        -> Tuple[BVHNode, Camera]:
    world = HittableList()
    world.add(
        Sphere(Point3(0, 0, -1), 0.5, Lambertian(SolidColor(0.1, 0.2, 0.5))))
    world.add(
        Sphere(Point3(0, -100.5, -1), 100, Lambertian(SolidColor(0.8, 0.8,
                                                                 0))))
    world.add(Sphere(Point3(1, 0, -1), 0.5, Metal(Color(0.8, 0.6, 0.2), 0.3)))
    world.add(Sphere(Point3(-1, 0, -1), 0.5, Dielectric(1.5)))
    world.add(Sphere(Point3(-1, 0, -1), -0.45, Dielectric(1.5)))
    world_bvh = BVHNode(world.objects, time0, time1)

    lookfrom = Point3(3, 3, 2)
    lookat = Point3(0, 0, -1)
    vup = Vec3(0, 1, 0)
    vfov = 20
    dist_to_focus: float = (lookfrom - lookat).length()
    aperture: float = 0
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus, time0, time1)

    # lookfrom = Point3(13, 2, 3)
    # lookat = Point3(0, 0, 0)
    # vup = Vec3(0, 1, 0)
    # vfov = 20
    # dist_to_focus: float = 10
    # aperture: float = 0.1
    # cam = Camera(
    #     lookfrom, lookat, vup, vfov, aspect_ratio, aperture, dist_to_focus,
    #     time0, time1
    # )

    return world_bvh, cam
Ejemplo n.º 15
0
def main():
    print(f"{datetime.datetime.now().isoformat()} start!", flush=True)
    args = parse_args()
    if args.category_num <= 0:
        raise SystemExit('ERROR: bad category_num (%d)!' % args.category_num)
    if not os.path.isfile('yolo/%s.trt' % args.model):
        raise SystemExit('ERROR: file (yolo/%s.trt) not found!' % args.model)

    cam = Camera(args)
    if not cam.isOpened():
        raise SystemExit('ERROR: failed to open camera!')

    cls_dict = get_cls_dict(args.category_num)
    yolo_dim = args.model.split('-')[-1]
    if 'x' in yolo_dim:
        dim_split = yolo_dim.split('x')
        if len(dim_split) != 2:
            raise SystemExit('ERROR: bad yolo_dim (%s)!' % yolo_dim)
        w, h = int(dim_split[0]), int(dim_split[1])
    else:
        h = w = int(yolo_dim)
    if h % 32 != 0 or w % 32 != 0:
        raise SystemExit('ERROR: bad yolo_dim (%s)!' % yolo_dim)

    trt_yolo = TrtYOLO(args.model, (h, w), args.category_num)

    # open_window(
    #     WINDOW_NAME, 'Camera TensorRT YOLO Demo',
    #     cam.img_width, cam.img_height)
    vis = BBoxVisualization(cls_dict)
    loop_and_detect(cam, trt_yolo, conf_th=0.3, vis=vis)

    cam.release()
Ejemplo n.º 16
0
def main():
    args = parse_args()
    cam = Camera(args)
    cam.open()
    if not cam.is_opened:
        sys.exit('Failed to open camera!')

    cls_dict = get_cls_dict(args.model)
    trt_ssd = TrtSSD(args.model, INPUT_HW)

    cam.start()
    if args.use_console:
        loop_and_detect_console(cam,
                                trt_ssd,
                                conf_th=0.3,
                                loop=args.loop,
                                cls_dict=cls_dict)
    else:
        open_window(WINDOW_NAME, args.image_width, args.image_height,
                    'Camera TensorRT SSD Demo for Jetson Nano')
        vis = BBoxVisualization(cls_dict)
        loop_and_detect(cam, trt_ssd, conf_th=0.3, vis=vis)

    cam.stop()
    cam.release()
    cv2.destroyAllWindows()
Ejemplo n.º 17
0
def main() -> None:
    aspect_ratio = 16 / 9
    image_width = 256
    image_height = int(image_width / aspect_ratio)
    samples_per_pixel = 20
    max_depth = 10

    world: HittableList = three_ball_scene()

    lookfrom = Point3(13, 2, 3)
    lookat = Point3(0, 0, 0)
    vup = Vec3(0, 1, 0)
    vfov = 20
    dist_to_focus: float = 10
    aperture: float = 0.1
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus)

    print("Start rendering.")
    start_time = time.time()

    n_processer = multiprocessing.cpu_count()
    img_list: List[Img] = Parallel(n_jobs=n_processer, verbose=10)(
        delayed(scan_line)(j, world, cam, image_width, image_height,
                           samples_per_pixel, max_depth)
        for j in range(image_height - 1, -1, -1))

    # # Profile prologue
    # import cProfile
    # import pstats
    # import io
    # from pstats import SortKey
    # pr = cProfile.Profile()
    # pr.enable()

    # img_list: List[Img] = list()
    # for j in range(image_height-1, -1, -1):
    #     img_list.append(
    #         scan_line(
    #             j, world, cam,
    #             image_width, image_height,
    #             samples_per_pixel, max_depth
    #         )
    #     )

    # # Profile epilogue
    # pr.disable()
    # s = io.StringIO()
    # sortby = SortKey.CUMULATIVE
    # ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
    # ps.print_stats()
    # print(s.getvalue())

    final_img = Img(image_width, image_height)
    final_img.set_array(np.concatenate([img.frame for img in img_list]))

    end_time = time.time()
    print(f"\nDone. Total time: {round(end_time - start_time, 1)} s.")
    final_img.save("./output.png", True)
def main():
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    # Ask tensorflow logger not to propagate logs to parent (which causes
    # duplicated logging)
    logging.getLogger('tensorflow').propagate = False
    global args

    args = parse_args()
    logger.info('called with args: %s' % args)

    # build the class (index/name) dictionary from labelmap file
    logger.info('reading label map')
    cls_dict = read_label_map(args.labelmap_file)

    pb_path = './data/{}_trt.pb'.format(args.model)
    log_path = './logs/{}_trt'.format(args.model)
    if args.do_build:
        logger.info('building TRT graph and saving to pb: %s' % pb_path)
        build_trt_pb(args.model, pb_path)

    logger.info('opening camera device/file')
    cam = Camera(args)
    cam.open()
    if not cam.is_opened:
        sys.exit('Failed to open camera!')

    logger.info('loading TRT graph from pb: %s' % pb_path)
    trt_graph = load_trt_pb(pb_path)

    logger.info('starting up TensorFlow session')
    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True
    #tf_sess = tf.Session(config=tf_config, graph=trt_graph) -- Vincent
    #Solve : "unable to satfisfy explicit device /dev/CPU:0 -- Vincent
    tf_sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                               log_device_placement=True),
                         graph=trt_graph)
    if args.do_tensorboard:
        logger.info('writing graph summary to TensorBoard')
        write_graph_tensorboard(tf_sess, log_path)

    logger.info('warming up the TRT graph with a dummy image')
    od_type = 'faster_rcnn' if 'faster_rcnn' in args.model else 'ssd'
    dummy_img = np.zeros((720, 1280, 3), dtype=np.uint8)
    _, _, _ = detect(dummy_img, tf_sess, conf_th=.3, od_type=od_type)

    cam.start()  # ask the camera to start grabbing images
    # grab image and do object detection (until stopped by user)
    logger.info('starting to loop and detect')
    vis = BBoxVisualization(cls_dict)
    open_display_window(cam.img_height, cam.img_width)
    result = loop_and_detect(cam, tf_sess, args.conf_th, vis, od_type=od_type)
    logger.info('cleaning up')
    cam.stop()  # terminate the sub-thread in camera
    tf_sess.close()
    cam.release()
    cv2.destroyAllWindows()
Ejemplo n.º 19
0
def random_scene(aspect_ratio: float, time0: float, time1: float) \
        -> Tuple[BVHNode, Camera]:
    world = HittableList()

    # ground_material = Lambertian(SolidColor(0.5, 0.5, 0.5))
    ground_material = Lambertian(
        CheckerTexture(SolidColor(0.2, 0.3, 0.1), SolidColor(0.9, 0.9, 0.9)))
    world.add(Sphere(Point3(0, -1000, 0), 1000, ground_material))

    for a in range(-11, 11):
        for b in range(-11, 11):
            choose_mat = random_float()
            center = Point3(a + 0.9 * random_float(), 0.2,
                            b + 0.9 * random_float())

            if (center - Vec3(4, 0.2, 0)).length() > 0.9:
                if choose_mat < 0.6:
                    # Diffuse
                    albedo = Color.random() * Color.random()
                    sphere_material_diffuse = Lambertian(SolidColor(albedo))
                    center2 = center + Vec3(0, random_float(0, 0.5), 0)
                    world.add(
                        MovingSphere(center, center2, 0, 1, 0.2,
                                     sphere_material_diffuse))
                elif choose_mat < 0.8:
                    # Metal
                    albedo = Color.random(0.5, 1)
                    fuzz = random_float(0, 0.5)
                    sphere_material_metal = Metal(albedo, fuzz)
                    world.add(Sphere(center, 0.2, sphere_material_metal))
                else:
                    # Glass
                    sphere_material_glass = Dielectric(1.5)
                    world.add(Sphere(center, 0.2, sphere_material_glass))

    material_1 = Dielectric(1.5)
    world.add(Sphere(Point3(0, 1, 0), 1, material_1))

    material_2 = Lambertian(SolidColor(0.4, 0.2, 0.1))
    world.add(Sphere(Point3(-4, 1, 0), 1, material_2))

    material_3 = Metal(Color(0.7, 0.6, 0.5), 0)
    world.add(Sphere(Point3(4, 1, 0), 1, material_3))

    world_bvh = BVHNode(world.objects, time0, time1)

    lookfrom = Point3(13, 2, 3)
    lookat = Point3(0, 0, 0)
    vup = Vec3(0, 1, 0)
    vfov = 20
    dist_to_focus: float = 10
    aperture: float = 0.1
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus, time0, time1)

    return world_bvh, cam
Ejemplo n.º 20
0
def shot(w=224, h=224):
    cam = Camera()
    cam.resolution = (w, h)
    cam.vflip, cam.hflip = True, True
    try:
        cam.capture('./test.png', resize=(w, h))
        # cam.capture('./test.png')
    finally:
        cam.close()
    return send_file('./test.png')
Ejemplo n.º 21
0
def main() -> None:
    aspect_ratio = 16 / 9
    image_width = 1920
    image_height = int(image_width / aspect_ratio)
    samples_per_pixel = 48
    max_depth = 10

    world: HittableList = random_scene()

    lookfrom = Point3(13, 2, 3)
    lookat = Point3(0, 0, 0)
    vup = Vec3(0, 1, 0)
    vfov = 20
    dist_to_focus: float = 10
    aperture: float = 0.1
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus)

    print("Start rendering.")
    start_time = time.time()

    img_list: List[Vec3List] = Parallel(n_jobs=4, verbose=20)(
        delayed(scan_frame)(world, cam, image_width, image_height, max_depth)
        for s in range(samples_per_pixel))

    # # Profile prologue
    # import cProfile
    # import pstats
    # import io
    # from pstats import SortKey
    # pr = cProfile.Profile()
    # pr.enable()

    # img_list: List[Vec3List] = list()
    # for sample_num in range(samples_per_pixel):
    #     img_list.append(
    #         scan_frame(world, cam, image_width, image_height, max_depth)
    #     )

    # # Profile epilogue
    # pr.disable()
    # s = io.StringIO()
    # sortby = SortKey.CUMULATIVE
    # ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
    # ps.print_stats()
    # print(s.getvalue())

    end_time = time.time()
    print(f"\nDone. Total time: {round(end_time - start_time, 1)} s.")

    final_img = Img(image_width, image_height)
    for img in img_list:
        final_img.write_frame(img)
    final_img.average(samples_per_pixel).gamma(2).up_side_down()
    final_img.save("./output.png", True)
Ejemplo n.º 22
0
    def warp_ref_image(self, depth, ref_image, K, ref_K, pose):
        """
        Warps a reference image to produce a reconstruction of the original one.
        Parameters
        ----------
        inv_depths : torch.Tensor [B,1,DH,DW]
            Inverse depth map of the original image
        ref_image : torch.Tensor [B,3,H,W]
            Reference RGB image
        K : torch.Tensor [B,3,3]
            Original camera intrinsics
        ref_K : torch.Tensor [B,3,3]
            Reference camera intrinsics
        pose : Pose
            Original -> Reference camera transformation
        Returns
        -------
        ref_warped : torch.Tensor [B,3,H,W]
            Warped reference image (reconstructing the original one)
        """
        B, _, H, W = ref_image.shape
        device = ref_image.get_device()

        # Generate cameras
        _, _, DH, DW = depth.shape
        scale_factor = DW / float(W)
        cam = Camera(K=K.float()).scaled(scale_factor).to(device)
        ref_cam = Camera(K=ref_K.float(),
                         Tcw=pose).scaled(scale_factor).to(device)

        ref_warped = view_synthesis(ref_image,
                                    depth,
                                    ref_cam,
                                    cam,
                                    padding_mode=self.padding_mode)

        # Return warped reference image
        return ref_warped
Ejemplo n.º 23
0
def yolo_detection():
    # dev = cuda.Device(0)
    # ctx = dev.make_context()
    args = parse_args()
    print(args)
    """
    config  assert
    """
    if args.category_num <= 0:
        raise SystemExit('ERROR: bad category_num (%d)!' % args.category_num)
    if not os.path.isfile('yolo/darknet/%s.trt' % args.model):
        raise SystemExit('ERROR: file (yolo/darknet/%s.trt) not found!' %
                         args.model)
    cls_dict = get_cls_dict(args.category_num)
    yolo_dim = args.model.split('-')[-1]

    if 'x' in yolo_dim:
        dim_split = yolo_dim.split('x')
        if len(dim_split) != 2:
            raise SystemExit('ERROR: bad yolo_dim (%s)!' % yolo_dim)
        w, h = int(dim_split[0]), int(dim_split[1])
    else:
        h = w = int(yolo_dim)
    if h % 32 != 0 or w % 32 != 0:
        raise SystemExit('ERROR: bad yolo_dim (%s)!' % yolo_dim)
    """
    capture the image
    """
    cam = Camera(args)
    if not cam.isOpened():
        raise SystemExit('ERROR: failed to open camera!')
    """
    deploy the yolo model
    """
    trt_yolo = TrtYOLO(args.model, (h, w), args.category_num)

    # open_window(
    #     WINDOW_NAME, 'Camera TensorRT YOLO Demo',
    #     cam.img_width, cam.img_height)
    """
    detect the insulator using model
    """
    vis = BBoxVisualization(cls_dict)
    loop_and_detect(cam, trt_yolo, conf_th=0.3, vis=vis)
    """
    release the image
    """

    cam.release()
Ejemplo n.º 24
0
def main():
    args = parse_args()
    if args.category_num <= 0:
        raise SystemExit(f'ERROR: bad category_num ({args.category_num})!')
    if not os.path.isfile(args.model):
        raise SystemExit(f'ERROR: file {args.model} not found!')

    # Process valid coco json file
    process_valid_json(args.valid_coco)

    if args.write_images:
        if not os.path.exists(args.image_output): os.mkdir(args.image_output)

    # Create camera for video/image input
    cam = Camera(args)
    if not cam.get_is_opened():
        raise SystemExit('ERROR: failed to open camera!')

    class_dict = get_cls_dict(args.category_num)
    yolo_dim = (args.model.replace(".trt", "")).split('-')[-1]
    if 'x' in yolo_dim:
        dim_split = yolo_dim.split('x')
        if len(dim_split) != 2:
            raise SystemExit(f'ERROR: bad yolo_dim ({yolo_dim})!')
        w, h = int(dim_split[0]), int(dim_split[1])
    else:
        h = w = int(yolo_dim)
    if h % 32 != 0 or w % 32 != 0:
        raise SystemExit(f'ERROR: bad yolo_dim ({yolo_dim})!')

    # Create yolo
    trt_yolo = TrtYOLO(args.model, (h, w), args.category_num)

    if args.activate_display:
        open_window(WINDOW_NAME, 'Camera TensorRT YOLO Demo', cam.img_width,
                    cam.img_height)
    visual = BBoxVisualization(class_dict)

    # Run detection
    loop_and_detect(cam,
                    trt_yolo,
                    args,
                    confidence_thresh=args.confidence_threshold,
                    visual=visual)

    # Clean up
    cam.release()
    if args.activate_display:
        cv2.destroyAllWindows()
Ejemplo n.º 25
0
def main():
    camera = Camera(1, True)
    while True:
        blocks, frame = camera.get_block_coords()
        position, angle, frame2 = camera.get_robot_position()
        nav = Navigate()
        if blocks and position:
            block_data = nav.calculate_distances_angles(
                blocks, position, angle)
            print(nav.add_block_to_rejects(block_data, position, angle))
        cv2.imshow('frame', frame)
        cv2.imshow('frame2', frame2)
        k = cv2.waitKey(5) & 0xFF
        if k == 27:
            break
Ejemplo n.º 26
0
def main():
    args = parse_args()
    cam = Camera(args)
    if not cam.get_is_opened():
        raise SystemExit('ERROR: failed to open camera!')

    mtcnn = TrtMtcnn()

    open_window(
        WINDOW_NAME, 'Camera TensorRT MTCNN Demo for Jetson Nano',
        cam.img_width, cam.img_height)
    loop_and_detect(cam, mtcnn, args.minsize)

    cam.release()
    cv2.destroyAllWindows()
Ejemplo n.º 27
0
    def __init__(self):
        self.camera = Camera(camera_suffixes=Config.camera_suffixes)
        self.history = EpisodeHistory()
        self.gripper = Gripper('172.16.0.2', Config.gripper_speed,
                               Config.gripper_force)
        self.robot = Robot('panda_arm', Config.general_dynamics_rel)
        self.saver = Saver(Config.database_url, Config.collection)

        self.current_bin = Config.start_bin

        self.md = MotionData().with_dynamics(1.0)

        self.overall_start = 0

        self.last_after_images: Optional[List[OrthographicImage]] = None
Ejemplo n.º 28
0
def main():
    # Parse arguments and get input
    args = parse_args()
    cam = Camera(args)
    if not cam.isOpened():
        raise SystemExit('ERROR: failed to open camera!')

    # Create NN1 and NN2 models and load into memory
    cls_dict = get_cls_dict(args.model.split('_')[-1])
    trt_ssd = TrtSSD(args.model, INPUT_HW)
    mtcnn = TrtMtcnn()

    # Create Preview Window
    open_window(WINDOW_NAME, 'Camera Preview', cam.img_width, cam.img_height)
    vis = BBoxVisualization(cls_dict)

    # Enter Detection Mode
    while True:
        # Get Image
        img = cam.read()
        out.write(img)
        nn1_results = []
        # Run Neural Networks
        img, nn1_results, nn2_results, nn3_results = loop_and_detect(
            img, mtcnn, args.minsize, trt_ssd, conf_th=0.3, vis=vis)

        # Communicate to Arduino
        if (nn1_results != []):
            img = robot_drive(img, nn1_results)
        else:
            serial_port.write("N".encode())
            print("N")

        # Display and save output
        cv2.imshow(WINDOW_NAME, img)
        outNN.write(img)

        # User/Keyboard Input
        key = cv2.waitKey(1)
        if key == ord('q'):
            out.release()
            outNN.release()
            break

    # Clean up and exit
    cam.release()
    cv2.destroyAllWindows()
    serial_port.close()
Ejemplo n.º 29
0
def main():
    args = parse_args()
    labels = np.loadtxt('googlenet/synset_words.txt', str, delimiter='\t')
    cam = Camera(args)
    if not cam.get_is_opened():
        raise SystemExit('ERROR: failed to open camera!')

    # initialize the tensorrt googlenet engine
    net = PyTrtGooglenet(DEPLOY_ENGINE, ENGINE_SHAPE0, ENGINE_SHAPE1)

    open_window(WINDOW_NAME, 'Camera TensorRT GoogLeNet Demo', cam.img_width,
                cam.img_height)
    loop_and_classify(cam, net, labels, args.crop_center)

    cam.release()
    cv2.destroyAllWindows()
Ejemplo n.º 30
0
def main():
    args = parse_args()
    cam = Camera(args)
    if not cam.get_is_opened():
        raise SystemExit('ERROR: failed to open camera!')

    cls_dict = get_cls_dict(args.model.split('_')[-1])
    trt_ssd = TrtSSD(args.model, INPUT_HW)

    open_window(WINDOW_NAME, 'Camera TensorRT SSD Demo', cam.img_width,
                cam.img_height)
    vis = BBoxVisualization(cls_dict)
    loop_and_detect(cam, trt_ssd, conf_th=0.3, vis=vis)

    cam.release()
    cv2.destroyAllWindows()