Example #1
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()
Example #2
0
def main():
    global THREAD_RUNNING
    cuda.init()  # init pycuda driver

    args = parse_args()
    cap = open_cam_rtsp(args.cam_name, args.cam_password, args.cam_ip)
    if not cap.isOpened():
        sys.exit('Failed to open camera!')

    #抓取图像子进程
    THREAD_RUNNING = True
    th = threading.Thread(target=grab_img, args=(cap, ))
    th.start()

    #目标识别
    cls_dict = get_cls_dict(args.model.split('_')[-1])

    open_window(WINDOW_NAME, args.image_width, args.image_height,
                'Camera TensorRT SSD Demo for Jetson Nano')
    vis = BBoxVisualization(cls_dict)
    condition = threading.Condition()
    global IMG_HANDLE
    trt_thread = TrtThread(condition, IMG_HANDLE, args.model, conf_th=0.3)
    trt_thread.start()  # start the child thread
    loop_and_display(condition, vis)
    trt_thread.stop()  # stop the child thread

    #关闭图像子进程
    THREAD_RUNNING = False
    th.join()
    cap.release()
    cv2.destroyAllWindows()
Example #3
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()
Example #4
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()
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()
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()
Example #7
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()
Example #8
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()
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()
Example #10
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()
Example #11
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()
Example #12
0
def main():
    args = parse_args()
    cam = Camera(args)

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

    mtcnn = TrtMtcnn()
    cam.start()
    open_window(WINDOW_NAME, width=640, height=480, title='MTCNN Window')
    detect_faces(cam, mtcnn)

    cam.stop()
    cam.release()
    cv2.destroyAllWindows()
    del mtcnn
Example #13
0
def main():
    args = parse_args()
    labels = np.loadtxt('googlenet/synset_words.txt', str, delimiter='\t')
    cam = Camera(args)
    if not cam.isOpened():
        raise SystemExit('ERROR: failed to open camera!')

    open_window(WINDOW_NAME, 'Camera TensorRT GoogLeNet Demo', cam.img_width,
                cam.img_height)
    condition = threading.Condition()
    trt_thread = TrtGooglenetThread(condition, cam, labels, args.crop_center)
    trt_thread.start()  # start the child thread
    loop_and_display(condition)
    trt_thread.stop()  # stop the child thread

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

    mtcnn = TrtMtcnn()

    cam.start()
    open_window(WINDOW_NAME, args.image_width, args.image_height,
                'Camera TensorRT MTCNN Demo for Jetson TX2')
    loop_and_detect(cam, mtcnn, args.minsize)

    cam.stop()
    cam.release()
    cv2.destroyAllWindows()

    del (mtcnn)
Example #15
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)
    loop_and_detect(cam, trt_yolov3, conf_th=0.3, vis=vis)

    cam.stop()
    cam.release()
    cv2.destroyAllWindows()
def main():
    args = parse_args()
    labels = np.loadtxt('googlenet/synset_words.txt', str, delimiter='\t')
    cam = Camera(args)
    cam.open()
    if not cam.is_opened:
        sys.exit('Failed to open camera!')

    cam.start()  # let camera start grabbing frames
    open_window(WINDOW_NAME, args.image_width, args.image_height,
                'Camera TensorRT GoogLeNet Demo for Jetson Nano')
    condition = threading.Condition()
    trt_thread = TrtGooglenetThread(condition, cam, labels, args.crop_center)
    trt_thread.start()  # start the child thread
    loop_and_display(condition)
    trt_thread.stop()  # stop the child thread

    cam.stop()
    cam.release()
    cv2.destroyAllWindows()
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)

    client = init_mqtt(args.host, args.port)

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

    cls_dict = get_cls_dict(args.category_num)
    print("cls_dict:", cls_dict)
    #print(cls_dict[3])
    yolo_dim = args.model.split('-')[-1]
    print("yolo_dim:", yolo_dim)
    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)
        print('w:{0}, h:{1}'.format(w, h))
    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, args.letter_box)

    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, \
        cls_dict=cls_dict, client=client, topic=args.topic)

    cam.release()
    cv2.destroyAllWindows()

    client.disclose()
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)
    trt_yolo = TrtYOLO(args.model, args.category_num, args.letter_box)

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

    cam.release()
    cv2.destroyAllWindows()
Example #19
0
def main():
    args = parse_args()
    cam = Camera(args)
    if not cam.get_is_opened():
        raise SystemExit('ERROR: failed to open camera!')

    cuda.init()  # init pycuda driver

    cls_dict = get_cls_dict(args.model.split('_')[-1])

    open_window(WINDOW_NAME, 'Camera TensorRT SSD Demo', cam.img_width,
                cam.img_height)
    vis = BBoxVisualization(cls_dict)
    condition = threading.Condition()
    trt_thread = TrtThread(condition, cam, args.model, conf_th=0.3)
    trt_thread.start()  # start the child thread
    loop_and_display(condition, vis)
    trt_thread.stop()  # stop the child thread

    cam.release()
    cv2.destroyAllWindows()
Example #20
0
def main():
    args = parse_args()
    cam = Camera(args)
    is_open = up()
    #time.sleep(60)
    if is_open:
        cam.open()
        if not cam.is_opened:
            sys.exit('Failed to open camera!')

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

        cam.start()
        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.9, vis=vis)

        cam.stop()
        cam.release()
        cv2.destroyAllWindows()
Example #21
0
def main():
    args = parse_args()
    if not os.path.isfile('retinaface/%s.trt' % args.model):
        raise SystemExit('ERROR: file (retinaface/%s.trt) not found!' % args.model)

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

    cfg = cfg_mnet
    input_size = args.model.split('-')[-1]
    input_shape = (int(input_size), int(input_size))
    priorbox = PriorBox(cfg, input_shape)
    priors = priorbox.forward()
    trt_retinaface = TRT_RetinaFace(args.model, input_shape)

    open_window(
        WINDOW_NAME, 'Camera TensorRT Face Detection Demo',
        cam.img_width, cam.img_height)
    loop_and_detect(cam, trt_retinaface, priors, cfg)

    cam.release()
    cv2.destroyAllWindows()
Example #22
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)

    load_weight_start = time.time()
    trt_yolo = TrtYOLO(args.model, (h, w), args.category_num)
    load_weights_time = datetime.timedelta(seconds=time.time() -
                                           load_weight_start)
    print('Load weights Time: %s' % (load_weights_time))
    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)
    reporter = MemReporter()
    reporter.report()
    cam.release()
    cv2.destroyAllWindows()
Example #23
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.split('_')[-1])

    cuda.init()  # init pycuda driver

    cam.start()  # let camera start grabbing frames
    open_window(WINDOW_NAME, args.image_width, args.image_height,
                'Camera TensorRT SSD Demo for Jetson Nano')
    vis = BBoxVisualization(cls_dict)
    condition = threading.Condition()
    trt_thread = TrtThread(condition, cam, args.model, conf_th=0.3)
    trt_thread.start()  # start the child thread
    loop_and_display(condition, vis)
    trt_thread.stop()   # stop the child thread

    cam.stop()
    cam.release()
    cv2.destroyAllWindows()
Example #24
0
def main():
    # initialize camera class
    cam = Camera()
    if not cam.is_opened():
        raise SystemExit('EROR: failed to open camera!')
    cls_dict = alprClassNames()
    
    # Yolo dimensions (416x416)
    yolo_dim = 416
    h = w = int(yolo_dim)

    # Initialized model and tools
    cwd = os.getcwd()
    model_yolo = str(cwd) + '/weights/yolov4-tiny-416.trt'
    model_crnn = str(cwd) + '/weights/crnn.pth'
    trt_yolo = TrtYOLO(model_yolo, (h,w), category_num=1) # category number is number of classes
    crnn = alpr.AutoLPR(decoder='bestPath', normalise=True)
    crnn.load(crnn_path=model_crnn)
    open_window(WINDOW_NAME, TITLE, cam.img_width, cam.img_height)
    vis = BBoxVisualization(cls_dict)

    # Loop and detect
    full_scrn = False
    fps = 0.0 
    tic = time.time()
    while True:
        if cv2.getWindowProperty(WINDOW_NAME, 0) < 0:
            break
        img = cam.read()
        if img is None:
            break
        # Detect car plate
        boxes confs, clss = trt_yolo.detect(img, conf_th=0.5)
        # Crop and preprocess car plate 
        cropped = vis.crop_plate(img, boxes, confs, clss)
        # Recognize car plate
        lp_plate = ''
        fileLocate = str(cwd) + '/detections/detection1.jpg'
        if os.path.exists(fileLocate):
           lp_plate = lpr.predict(fileLocate)

        # Draw boxes and fps
        img = vis.draw_bboxes(img, boxes, confs, clss, lp=lp_plate)
        img = show_fps(img, fps)

        # Show image
        cv2.imshow(WINDOW_NAME, img)

        # Calculate fps
        toc = time.time()
        curr_fps = 1.0 / (toc - tic)
        fps = curr_fps if fps == 0.0 else (fps*0.95 + curr_fps*0.05)
        tic = toc

        # Exit key
        key = cv2.waitKey(1)
        if key == 27:  # ESC key: quit program
            break

    # Release capture and destroy all windows
    cam.release()
    cv2.destroyAllWindows()