Exemplo n.º 1
0
    def __init__(self):
        self.image_sub = rospy.Subscriber("~ncs_image_rect",
                                          Image,
                                          self.img_cb,
                                          queue_size=5)
        self.mode_sub = rospy.Subscriber("~mode", FSMState, self.fsm_mode)
        self.image_pub = rospy.Publisher('image_with_box', Image, queue_size=5)
        self.veh_name = rospy.get_param('~veh_name')
        #for img_cb
        self.bridge = CvBridge()
        self.cv_image = 0
        self.cv_img_crop = []

        #caffe params
        self.model = model = 'street_en_harvest'

        self.device_work = False
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
        self.dim = (100, 32)

        #counter & flag params\

        self.ncs_set = 0
        self.start = 0
        self.time = 0
        self.n = 1
        self.stop_line = 0  ### 0
        self.deviceCheck()
Exemplo n.º 2
0
    def __init__(self, graph_file, img_shape, device_t):
        self.img_shape = img_shape


        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)

        print("[INFO] finding NCS devices...")
        devices = mvnc.EnumerateDevices()
        # if no devices found, exit the script
        if len(devices) == 0:
            print("[INFO] No devices found. Please plug in a NCS")
            quit()

        print("[INFO] found {} devices. device0 will be used. "
            "opening device0...".format(len(devices)))
        device = mvnc.Device(devices[0])
        device.OpenDevice()

        # open the CNN graph file
        print("[INFO] loading the graph file into memory...")
        with open(graph_file, mode="rb") as f:
            graph_in_memory = f.read()
         
        # load the graph into the NCS
        print("[INFO] allocating the graph on the NCS...")
        self.graph = device.AllocateGraph(graph_in_memory)
Exemplo n.º 3
0
    def __init__(self):
        self.initiated = False
        self.output = None

        # configure the NCS
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)

        # Get a list of ALL the sticks that are plugged in
        self.devices = mvnc.EnumerateDevices()
        if len(self.devices) == 0:
            print('No devices found')
            quit()

        # Pick the first stick to run the network
        self.device = mvnc.Device(self.devices[0])

        # Open the NCS
        self.device.OpenDevice()

        graph_filename = 'graph'

        # Load graph file to memory buffer
        self.graph_data = None
        with open(graph_filename, mode='rb') as f:
            self.graph_data = f.read()

        self.ssd_mobilenet_graph = self.device.AllocateGraph(self.graph_data)
Exemplo n.º 4
0
        def __init__(self, yolo_model='tiny-yolo-v2'):
            self.model = YOLO_MODELS[yolo_model]
            ncs_graph = os.path.join(YOLO_MODELS_DIR, yolo_model + '.graph')
            # configure the NCS
            mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 1)

            # Get a list of ALL the sticks that are plugged in
            devices = mvnc.EnumerateDevices()
            if len(devices) == 0:
                print('No devices found')
                quit()

            # Pick the first stick to run the network
            self.mvncs_dev = mvnc.Device(devices[0])

            # Open the NCS
            try:
                self.mvncs_dev.OpenDevice()
            except:
                print('Cannot Open NCS Device')

            #Load blob
            with open(ncs_graph, mode='rb') as f:
                blob = f.read()

            self.graph = self.mvncs_dev.AllocateGraph(blob)
            self.graph.SetGraphOption(mvnc.GraphOption.ITERATIONS, 1)
Exemplo n.º 5
0
    def __init__(self, graphfile):
        select = 1
        self.detector = YoloDetector(select)
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
        devices = mvnc.EnumerateDevices()
        if len(devices) == 0:
            print('No MVNC devices found')
            quit()
        self.device = mvnc.Device(devices[0])
        self.device.OpenDevice()
        opt = self.device.GetDeviceOption(mvnc.DeviceOption.OPTIMISATION_LIST)
        # load blob
        with open(graphfile, mode='rb') as f:
            blob = f.read()
        self.graph = self.device.AllocateGraph(blob)
        self.graph.SetGraphOption(mvnc.GraphOption.ITERATIONS, 1)
        iterations = self.graph.GetGraphOption(mvnc.GraphOption.ITERATIONS)

        self.dim = (416, 416)
        self.blockwd = 12
        self.wh = self.blockwd * self.blockwd
        self.targetBlockwd = 12
        self.classes = 20
        self.threshold = 0.2
        self.nms = 0.4
Exemplo n.º 6
0
    def __init__(self):
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
        devices = mvnc.EnumerateDevices()
        if len(devices) == 0:
            print('No device found')
            quit()
        self.device = mvnc.Device(devices[0])
        self.graph = None

        # traffic light
        # read mean file
        self.traffic_dim = (227, 227)
        self.traffic_mean = (81.1, 88.5, 90.3)
        # read label file
        traffic_labelfile = 'traffic_label.txt'
        self.traffic_labels = numpy.loadtxt(traffic_labelfile, str, delimiter='\t')
        # Load graph
        self.traffic_light_model = 'traffic_light_graph'


        # object detection
        # read mean file
        self.detection_dim = (300, 300)
        self.detection_mean = (127.5, 127.5, 127.5)
        # read label file
        detection_labelfile = 'detection_label.txt'
        self.detection_labels = numpy.loadtxt(detection_labelfile, str, delimiter='\t')
        # Load graph
        self.object_detection_model = 'object_deteciton_graph'
Exemplo n.º 7
0
    def __init__(self, path_to_graph, categories):
        assert type(path_to_graph) is str, '文字列を指定してください'
        assert type(categories) is tuple or type(categories) is list, 'タプルかリストを指定してください'
        
        # グラフファイルまでのパスをなんとなく保存しておく
        self.path_to_graph = path_to_graph
        # カテゴリのリストを受け取る
        self.categories = categories

        # おまじない
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
        
        # Movidiusを読み込む
        devices = mvnc.EnumerateDevices()

        if len(devices) == 0: # 1本も見つからなかったら何もしない
            print('No devices found')
            return 
        
        # セットアップ
        self.device = mvnc.Device(devices[0])
        self.device.OpenDevice()

        # graphファイルを読み込む
        with open(self.path_to_graph, mode= 'rb') as f:
            graphfile = f.read()
        
        # デバイスにグラフファイルをセットする. 
        # self.graphオブジェクトを使って検出を行う
        self.graph = self.device.AllocateGraph(graphfile)
Exemplo n.º 8
0
    def initial(self):
        self.model = rospy.get_param("~model_name")
        self.ncs_pkg = rospy.get_param("~ncs_pkg")

        self.device_work = False
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
        self.deviceCheck()
        self.dim = (101, 101)  #(width, height)
    def run(self):

        # configure the NCS
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)

        # Get a list of ALL the sticks that are plugged in
        devices = mvnc.EnumerateDevices()
        if len(devices) == 0:
            print('No devices found')
            quit()

        # Pick the first stick to run the network
        device = mvnc.Device(devices[0])

        # Open the NCS
        device.OpenDevice()

        graph_filename = 'graph'

        # Load graph file to memory buffer
        with open(graph_filename, mode='rb') as f:
            graph_data = f.read()

        # allocate the Graph instance from NCAPI by passing the memory buffer
        ssd_mobilenet_graph = device.AllocateGraph(graph_data)

        # Create Video Capture object
        cap = cv2.VideoCapture(0)
        time.sleep(1)

        if ((cap == None or (not cap.isOpened()))):
            print('Could not open video device.  Make sure file exists:')
            #print ('file name:' + input_video_file)
            print('Also, if you installed python opencv via pip or pip3 you')
            print(
                'need to uninstall it and install from source with -D WITH_V4L=ON'
            )
            print('Use the provided script: install-opencv-from_source.sh')
            exit_app = True
            return
        while (not self.killSwitch):
            self.ret, self.display_image = cap.read()

            if (not self.ret):
                print("No image from the video device, exiting")
                break

            self.inferred_image = self.run_inference(self.display_image,
                                                     ssd_mobilenet_graph)

        # Clean up the graph and the device
        print("quitting")
        ssd_mobilenet_graph.DeallocateGraph()
        device.CloseDevice()
        cap.release()
Exemplo n.º 10
0
    def initial(self):
        self.model = model = 'street_en_harvest'
        self.start = 0
        self.time = 0
        self.n = 1

        self.camera_name = rospy.get_param('~camera_name')
        self.device_work = False
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
        self.deviceCheck()
        self.dim = (100, 32)  #(width, height)
Exemplo n.º 11
0
 def __init__(self):
     # configuration NCS
     mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
     self.devices = mvnc.EnumerateDevices()
     if len(self.devices) == 0:
         # No NCS devices found
         print('No NCS devices found')
         self.device = None
     else:
         # Try connecting to the first NCS device found
         self.device = mvnc.Device(self.devices[0])
         self.device.OpenDevice()
         self.opt = self.device.GetDeviceOption(mvnc.DeviceOption.OPTIMISATION_LIST)
Exemplo n.º 12
0
    def __init__(self, input_width, input_height, mean_proto, ncs_graph):
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
        devices = mvnc.EnumerateDevices()
        if len(devices) == 0:
            print('No NCS devices found')
            exit(1)
        self.device = mvnc.Device(devices[0])
        self.device.OpenDevice()
        with open(ncs_graph, mode='rb') as f:
            blob = f.read()
        self.graph = self.device.AllocateGraph(blob)

        super().__init__(input_width, input_height, mean_proto)
Exemplo n.º 13
0
 def prepare(self):
     self.tinyyolo_status = True
     tiny_yolo_graph_file = './graph/tinyyolo_graph'
     mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 0)
     devices = mvnc.EnumerateDevices()
     if len(devices) == 0:
         print('No devices found')
         quit()
     self.device = mvnc.Device(devices[0])
     self.device.OpenDevice()
     with open(tiny_yolo_graph_file, mode='rb') as f:
         graph_from_disk = f.read()
     self.graph = self.device.AllocateGraph(graph_from_disk)
Exemplo n.º 14
0
def init_movidius():
    global for_close_graph, for_close_device
    mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
    devices = mvnc.EnumerateDevices()
    if len(devices) == 0:
        print('No devices found')
        quit()
    for_close_device = device = mvnc.Device(devices[0])
    device.OpenDevice()
    graph_filename = 'graph'
    with open(graph_filename, mode='rb') as f:
        graph_data = f.read()
    for_close_graph = device.AllocateGraph(graph_data)
    return for_close_graph
    def initial(self):

        self.device_work = False
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
        self.deviceCheck()
        shape_txt = ""
        shape = []
        with open(self.model_Base_Dir + 'input_shape.prototxt', 'r') as file:
            shape_txt = file.read().replace('\n', ' ')
        for s in shape_txt.split():
            if s.isdigit():
                shape.append(int(s))

        self.dim = (shape[2], shape[3])
Exemplo n.º 16
0
def open_ncs_device(verbose=False):
    if verbose:
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
    # Look for enumerated NCS device(s); quit program if none found.
    devices = mvnc.EnumerateDevices()
    if len(devices) == 0:
        print('No devices found')
        quit()

    # Get a handle to the first enumerated device and open it
    device = mvnc.Device(devices[0])
    device.OpenDevice()

    return device
Exemplo n.º 17
0
    def __init__(self):
        # Set logging level and initialize/open the first NCS we find
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 0)
        devices = mvnc.EnumerateDevices()
        if len(devices) == 0:
            print('No devices found')
            return 1
        self.device = mvnc.Device(devices[0])
        self.device.OpenDevice()

        #Load graph from disk and allocate graph via API
        with open(tiny_yolo_graph_file, mode='rb') as f:
            graph_from_disk = f.read()

        self.graph = self.device.AllocateGraph(graph_from_disk)
Exemplo n.º 18
0
def init_graph(blob):
    mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
    devices = mvnc.EnumerateDevices()
    if len(devices) == 0:
        print('No devices found')
        quit()
    device = mvnc.Device(devices[0])
    device.OpenDevice()
    opt = device.GetDeviceOption(mvnc.DeviceOption.OPTIMISATION_LIST)
    with open(blob, mode='rb') as f:
        blob = f.read()
    graph = device.AllocateGraph(blob)
    graph.SetGraphOption(mvnc.GraphOption.ITERATIONS, 1)
    iterations = graph.GetGraphOption(mvnc.GraphOption.ITERATIONS)
    return graph, device
Exemplo n.º 19
0
 def __init__(self, device_index):
     mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
     devices = mvnc.EnumerateDevices()
     if len(devices) <= 0:
         raise Exception('No Movidius device found.')
     if device_index > (len(devices) - 1):
         raise Exception('Movidius device %d does not exists' %
                         device_index)
     self.__device_index = device_index
     self.__device = mvnc.Device(devices[device_index])
     self.__graph = None
     self.__predict_queue = Queue.Queue(50)
     self.__predict_event = None
     self.__predict_thread = None
     self.__stop_flag = False
Exemplo n.º 20
0
def main():
    print('Running NCS Caffe TinyYolo example')

    # Set logging level and initialize/open the first NCS we find
    mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 0)
    devices = mvnc.EnumerateDevices()
    if len(devices) == 0:
        print('No devices found')
        return 1
    device = mvnc.Device(devices[0])
    device.OpenDevice()

    #Load graph from disk and allocate graph via API
    with open(tiny_yolo_graph_file, mode='rb') as f:
        graph_from_disk = f.read()

    graph = device.AllocateGraph(graph_from_disk)

    # Read image from file, resize it to network width and height
    # save a copy in display_image for display, then convert to float32, normalize (divide by 255),
    # and finally convert to convert to float16 to pass to LoadTensor as input for an inference
    input_image = cv2.imread(input_image_file)
    display_image = input_image
    input_image = cv2.resize(input_image,
                             (NETWORK_IMAGE_WIDTH, NETWORK_IMAGE_HEIGHT),
                             cv2.INTER_LINEAR)
    input_image = input_image.astype(np.float32)
    input_image = np.divide(input_image, 255.0)
    input_image = input_image[:, :, ::-1]  # convert to RGB

    # Load tensor and get result.  This executes the inference on the NCS
    graph.LoadTensor(input_image.astype(np.float16), 'user object')
    output, userobj = graph.GetResult()

    # filter out all the objects/boxes that don't meet thresholds
    filtered_objs = filter_objects(
        output.astype(np.float32), input_image.shape[1],
        input_image.shape[0])  # fc27 instead of fc12 for yolo_small

    print('Displaying image with objects detected in GUI')
    print('Click in the GUI window and hit any key to exit')
    #display the filtered objects/boxes in a GUI window
    display_objects_in_gui(display_image, filtered_objs)

    #Clean up
    graph.DeallocateGraph()
    device.CloseDevice()
    print('Finished')
Exemplo n.º 21
0
def execute_graph(blob, img):
    mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
    devices = mvnc.EnumerateDevices()
    if len(devices) == 0:
        print('No devices found')
        quit()
    device = mvnc.Device(devices[0])
    device.OpenDevice()
    opt = device.GetDeviceOption(mvnc.DeviceOption.OPTIMISATION_LIST)
    with open(blob, mode='rb') as f:
        blob = f.read()
    graph = device.AllocateGraph(blob)
    graph.SetGraphOption(mvnc.GraphOption.ITERATIONS, 1)
    iterations = graph.GetGraphOption(mvnc.GraphOption.ITERATIONS)
    graph.LoadTensor(img.astype(numpy.float16), 'user object')
    output, userobj = graph.GetResult()
    graph.DeallocateGraph()
    device.CloseDevice()
    return output, userobj
Exemplo n.º 22
0
    def prepare(self):
        self.googlenet_status = True
        self.dim = (224, 224)
        labels_file = './data/synset_words.txt'
        self.labels = numpy.loadtxt(labels_file, str, delimiter='\t')
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
        devices = mvnc.EnumerateDevices()
        if len(devices) == 0:
            print('No devices found')
            quit()

        self.device = mvnc.Device(devices[0])
        self.device.OpenDevice()
        network_blob = './graph/GoogleNet_graph'

        with open(network_blob, mode='rb') as f:
            blob = f.read()
        self.graph = self.device.AllocateGraph(blob)

        self.ilsvrc_mean = numpy.load('./data/ilsvrc_2012_mean.npy').mean(
            1).mean(1)
Exemplo n.º 23
0
 def prepare(self):
     self.gendernet_status = True
     blob = "./graph/gender_graph"
     self.gender_list = ['Male', 'Female']
     self.ilsvrc_mean = numpy.load('./data/age_gender_mean.npy').mean(
         1).mean(1)  #loading the mean file
     mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
     devices = mvnc.EnumerateDevices()
     if len(devices) == 0:
         print('No devices found')
     self.device = mvnc.Device(devices[0])
     self.device.OpenDevice()
     opt = self.device.GetDeviceOption(mvnc.DeviceOption.OPTIMISATION_LIST)
     with open(blob, mode='rb') as f:
         blob = f.read()
     self.graph = self.device.AllocateGraph(blob)
     self.graph.SetGraphOption(mvnc.GraphOption.ITERATIONS, 1)
     iterations = self.graph.GetGraphOption(mvnc.GraphOption.ITERATIONS)
     print(
         '-----------------------prepare gendernet successful-----------------------'
     )
def init_yolo():
    try:
        print('Running NCS Caffe TinyYolo example')

        # Set logging level and initialize/open the first NCS we find
        mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 0)
        devices = mvnc.EnumerateDevices()
        if len(devices) == 0:
            print('No devices found')
            return 1
        device = mvnc.Device(devices[0])
        device.OpenDevice()

        #Load graph from disk and allocate graph via API
        with open(tiny_yolo_graph_file, mode='rb') as f:
            graph_from_disk = f.read()

        graph = device.AllocateGraph(graph_from_disk)
        return 0,graph
        
    except:
        return -1,None
Exemplo n.º 25
0
 def prepare(self):
     self.agenet_status = True
     mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
     devices = mvnc.EnumerateDevices()
     if len(devices) == 0:
         print('No devices found')
     self.device = mvnc.Device(devices[0])
     self.device.OpenDevice()
     opt = self.device.GetDeviceOption(mvnc.DeviceOption.OPTIMISATION_LIST)
     blob = "./graph/age_graph"
     with open(blob, mode='rb') as f:
         blob = f.read()
     self.graph = self.device.AllocateGraph(blob)
     self.graph.SetGraphOption(mvnc.GraphOption.ITERATIONS, 1)
     iterations = self.graph.GetGraphOption(mvnc.GraphOption.ITERATIONS)
     self.age_list = [
         '0-2', '4-6', '8-12', '15-20', '25-32', '38-43', '48-53', '60-100'
     ]
     self.ilsvrc_mean = numpy.load('./data/age_gender_mean.npy').mean(
         1).mean(1)  #loading the mean file
     print(
         '-----------------------prepare agenet successful-----------------------'
     )
Exemplo n.º 26
0
    parser.add_argument('-q-size',
                        '--queue-size',
                        dest='queue_size',
                        type=int,
                        default=5,
                        help='Size of the queue.')
    args = parser.parse_args()

    logger = multiprocessing.log_to_stderr()
    logger.setLevel(multiprocessing.SUBDEBUG)

    input_q = Queue(maxsize=args.queue_size)
    output_q = Queue(maxsize=args.queue_size)
    # configuration NCS
    network_blob = 'graph'
    mvnc.SetGlobalOption(mvnc.GlobalOption.LOGLEVEL, 2)
    devices = mvnc.EnumerateDevices()
    if len(devices) == 0:
        print('No devices found')
        quit()
    device = mvnc.Device(devices[0])
    device.OpenDevice()
    opt = device.GetDeviceOption(mvnc.DeviceOption.OPTIMISATIONLIST)
    # load blob
    with open(network_blob, mode='rb') as f:
        blob = f.read()
    graph = device.AllocateGraph(blob)
    graph.SetGraphOption(mvnc.GraphOption.ITERATIONS, 1)
    iterations = graph.GetGraphOption(mvnc.GraphOption.ITERATIONS)
    #
    pool = Pool(args.num_workers, worker, (graph, input_q, output_q))
Exemplo n.º 27
0
def main():
    global resize_output, resize_output_width, resize_output_height

    if (not handle_args()):
        print_usage()
        return 1

    # configure the NCS
    mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)

    # Get a list of ALL the sticks that are plugged in
    devices = mvnc.EnumerateDevices()
    if len(devices) == 0:
        print('No devices found')
        quit()

    # Pick the first stick to run the network
    device = mvnc.Device(devices[0])

    # Open the NCS
    device.OpenDevice()

    graph_filename = 'graph'

    # Load graph file to memory buffer
    with open(graph_filename, mode='rb') as f:
        graph_data = f.read()

    # allocate the Graph instance from NCAPI by passing the memory buffer
    ssd_mobilenet_graph = device.AllocateGraph(graph_data)

    # get list of all the .mp4 files in the image directory
    input_video_filename_list = os.listdir(input_video_path)
    input_video_filename_list = [
        i for i in input_video_filename_list if i.endswith('.mp90')
    ]

    if (len(input_video_filename_list) < 1):
        # no images to show
        print('No video (.mp4) files found')
        return 1

    cv2.namedWindow(cv_window_name)
    cv2.moveWindow(cv_window_name, 10, 10)

    exit_app = False
    while (True):
        for input_video_file in input_video_filename_list:

            cap = cv2.VideoCapture(input_video_path + input_video_file)

            actual_frame_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
            actual_frame_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
            print('actual video resolution: ' + str(actual_frame_width) +
                  ' x ' + str(actual_frame_height))

            if ((cap == None) or (not cap.isOpened())):
                print('Could not open video device.  Make sure file exists:')
                print('file name:' + input_video_file)
                print(
                    'Also, if you installed python opencv via pip or pip3 you')
                print(
                    'need to uninstall it and install from source with -D WITH_V4L=ON'
                )
                print('Use the provided script: install-opencv-from_source.sh')
                exit_app = True
                break

            frame_count = 0
            start_time = time.time()
            end_time = start_time

            ret, img = cap.read()
            size = img.shape[1], img.shape[0]

            # save results in a video file
            #fourcc = cv2.VideoWriter_fourcc(*'mp4v')
            fourcc = cv2.VideoWriter_fourcc(*'H264')
            video = cv2.VideoWriter(
                input_video_path + input_video_file + '_output.avi', fourcc,
                10, size)

            while (True):
                ret, source_image = cap.read()
                display_image = cropImage(source_image, 700, 350, 300, 300)

                if (not ret):
                    end_time = time.time()
                    print("No image from from video device, exiting")
                    break

                # check if the window is visible, this means the user hasn't closed
                # the window via the X button
                prop_val = cv2.getWindowProperty(cv_window_name,
                                                 cv2.WND_PROP_ASPECT_RATIO)
                if (prop_val < 0.0):
                    end_time = time.time()
                    exit_app = True
                    break

                #display_image = rotateImage(display_image, 90)

                run_inference(display_image, ssd_mobilenet_graph)

                if (resize_output):
                    display_image = cv2.resize(
                        display_image,
                        (resize_output_width, resize_output_height),
                        cv2.INTER_LINEAR)
                cv2.imshow(cv_window_name, display_image)

                video.write(display_image)

                raw_key = cv2.waitKey(1)
                if (raw_key != -1):
                    if (handle_keys(raw_key) == False):
                        end_time = time.time()
                        exit_app = True
                        break
                frame_count += 1

            frames_per_second = frame_count / (end_time - start_time)
            print('Frames per Second: ' + str(frames_per_second))

            cap.release()

            if (exit_app):
                break

        if (exit_app):
            break

    # Clean up the graph and the device
    ssd_mobilenet_graph.DeallocateGraph()
    device.CloseDevice()

    video.release()

    cv2.destroyAllWindows()
Exemplo n.º 28
0
def MultiStick():
    mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)

    devices = mvnc.EnumerateDevices()
    if len(devices) == 0:
        print("No devices found")
        quit()
    print(len(devices))

    devHandle = []
    graphHandle = []

    with open(join(graph_folder, "graph"), mode="rb") as f:
        graph = f.read()

    for devnum in range(len(devices)):
        devHandle.append(mvnc.Device(devices[devnum]))
        devHandle[devnum].OpenDevice()
        graphHandle.append(devHandle[devnum].AllocateGraph(graph))
        graphHandle[devnum].SetGraphOption(mvnc.GraphOption.ITERATIONS, 1)
        iterations = graphHandle[devnum].GetGraphOption(
            mvnc.GraphOption.ITERATIONS)

    print("\nLoaded Graphs!!!")

    cam = cv2.VideoCapture(0)
    #cam = cv2.VideoCapture('/home/pi/SSD_MobileNet/xxxx.mp4')

    if cam.isOpened() != True:
        print("Camera/Movie Open Error!!!")
        quit()

    windowWidth = 1280
    windowHeight = 640
    cam.set(cv2.CAP_PROP_FRAME_WIDTH, windowWidth)
    cam.set(cv2.CAP_PROP_FRAME_HEIGHT, windowHeight)

    lock = Lock()
    frameBuffer = []
    results = Queue()
    lastresults = None

    LABELS = ('background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle',
              'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog',
              'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa',
              'train', 'tvmonitor')

    def init():
        glClearColor(0.7, 0.7, 0.7, 0.7)

    def idle():
        glutPostRedisplay()

    def resizeview(w, h):
        glViewport(0, 0, w, h)
        glLoadIdentity()
        glOrtho(-w / 1920, w / 1920, -h / 1080, h / 1080, -1.0, 1.0)

    def keyboard(key, x, y):
        key = key.decode('utf-8')
        if key == 'q':
            lock.acquire()
            while len(frameBuffer) > 0:
                frameBuffer.pop()
            lock.release()
            for devnum in range(len(devices)):
                graphHandle[devnum].DeallocateGraph()
                devHandle[devnum].CloseDevice()
            print("\n\nFinished\n\n")
            sys.exit()

    def camThread():
        lastresults = None

        s, img = cam.read()

        if not s:
            print("Could not get frame")
            return 0

        lock.acquire()
        if len(frameBuffer) > 10:
            for i in range(10):
                del frameBuffer[0]
        frameBuffer.append(img)
        lock.release()
        res = None

        if not results.empty():
            res = results.get(False)
            flag, img = overlay_on_image(img, res)
            if flag != 0:
                #glutLeaveMainLoop()
                print(img)
                return img
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            h, w = img.shape[:2]
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
                         GL_UNSIGNED_BYTE, img)
            lastresults = res
        else:
            imdraw = overlay_on_image(img, lastresults)
            imdraw = cv2.cvtColor(imdraw, cv2.COLOR_BGR2RGB)
            h, w = imdraw.shape[:2]
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
                         GL_UNSIGNED_BYTE, imdraw)

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glColor3f(1.0, 1.0, 1.0)
        glEnable(GL_TEXTURE_2D)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glBegin(GL_QUADS)
        glTexCoord2d(0.0, 1.0)
        glVertex3d(-1.0, -1.0, 0.0)
        glTexCoord2d(1.0, 1.0)
        glVertex3d(1.0, -1.0, 0.0)
        glTexCoord2d(1.0, 0.0)
        glVertex3d(1.0, 1.0, 0.0)
        glTexCoord2d(0.0, 0.0)
        glVertex3d(-1.0, 1.0, 0.0)
        glEnd()
        glFlush()
        glutSwapBuffers()

    def inferencer(results, lock, frameBuffer, handle):
        failure = 0
        sleep(5)
        while failure < 100:

            lock.acquire()
            if len(frameBuffer) == 0:
                lock.release()
                failure += 1
                continue

            img = frameBuffer[-1].copy()
            del frameBuffer[-1]
            failure = 0
            lock.release()

            now = time.time()
            im = preprocess_image(img)
            handle.LoadTensor(im.astype(np.float16), None)
            out, userobj = handle.GetResult()

            results.put(out)
            #print("elapsedtime = ", time.time() - now)

    def preprocess_image(src):

        img = cv2.resize(src, (300, 300))
        img = img - 127.5
        img = img * 0.007843

        return img

    def overlay_on_image(display_image, object_info):

        sumbox = [0, 0]
        flag = 0
        if isinstance(object_info, type(None)):
            return display_image

        num_valid_boxes = int(object_info[0])
        img_cp = display_image.copy()

        if num_valid_boxes > 0:
            for box_index in range(num_valid_boxes):
                base_index = 7 + box_index * 7
                if (not np.isfinite(object_info[base_index])
                        or not np.isfinite(object_info[base_index + 1])
                        or not np.isfinite(object_info[base_index + 2])
                        or not np.isfinite(object_info[base_index + 3])
                        or not np.isfinite(object_info[base_index + 4])
                        or not np.isfinite(object_info[base_index + 5])
                        or not np.isfinite(object_info[base_index + 6])):
                    continue

                x1 = max(0, int(object_info[base_index + 3] * img_cp.shape[0]))
                y1 = max(0, int(object_info[base_index + 4] * img_cp.shape[1]))
                x2 = min(img_cp.shape[0],
                         int(object_info[base_index + 5] * img_cp.shape[0]))
                y2 = min(img_cp.shape[1],
                         int(object_info[base_index + 6] * img_cp.shape[1]))

                x1_ = str(x1)
                y1_ = str(y1)
                x2_ = str(x2)
                y2_ = str(y2)
                if (object_info[base_index + 2]) > flag:
                    flag = int(object_info[base_index + 2])
                    sumbox = [(int(x1_) + int(x2_)) // 2,
                              (int(y1_) + int(y2_)) // 2]
            # print('box at index: ' + str(box_index) + ' : ClassID: ' + LABELS[int(object_info[base_index + 1])] + '  '
            #   'Confidence: ' + str(object_info[base_index + 2]*100) + '%  ' +
            #     'Top Left: (' + x1_ + ', ' + y1_ + ')  Bottom Right: (' + x2_ + ', ' + y2_ + ')')

                object_info_overlay = object_info[base_index:base_index + 7]

                min_score_percent = 10
                source_image_width = img_cp.shape[1]
                source_image_height = img_cp.shape[0]

                base_index = 0
                class_id = object_info_overlay[base_index + 1]
                percentage = int(object_info_overlay[base_index + 2] * 100)
                if (percentage <= min_score_percent):
                    continue

                label_text = LABELS[int(class_id)] + " (" + str(
                    percentage) + "%)"
                box_left = int(object_info_overlay[base_index + 3] *
                               source_image_width)
                box_top = int(object_info_overlay[base_index + 4] *
                              source_image_height)
                box_right = int(object_info_overlay[base_index + 5] *
                                source_image_width)
                box_bottom = int(object_info_overlay[base_index + 6] *
                                 source_image_height)

                box_color = (255, 128, 0)
                box_thickness = 1
                cv2.rectangle(img_cp, (box_left, box_top),
                              (box_right, box_bottom), box_color,
                              box_thickness)

                label_background_color = (125, 175, 75)
                label_text_color = (255, 255, 255)

                label_size = cv2.getTextSize(label_text,
                                             cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                                             1)[0]
                label_left = box_left
                label_top = box_top - label_size[1]
                if (label_top < 1):
                    label_top = 1
                label_right = label_left + label_size[0]
                label_bottom = label_top + label_size[1]
                cv2.rectangle(img_cp, (label_left - 1, label_top - 1),
                              (label_right + 1, label_bottom + 1),
                              label_background_color, -1)
                cv2.putText(img_cp, label_text, (label_left, label_bottom),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, label_text_color, 1)
        if sumbox == [0, 0]:
            return 0, img_cp
        else:
            return 1, sumbox

    glutInitWindowPosition(0, 0)
    glutInitWindowSize(1280, 640)
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE)
    glutCreateWindow("DEMO")
    #glutFullScreen()
    glutDisplayFunc(camThread)
    glutReshapeFunc(resizeview)
    glutKeyboardFunc(keyboard)
    init()
    glutIdleFunc(idle)

    print("press 'q' to quit!\n")

    threads = []

    for devnum in range(len(devices)):
        t = Thread(target=inferencer,
                   args=(results, lock, frameBuffer, graphHandle[devnum]))
        t.start()
        threads.append(t)

    glutMainLoop()
Exemplo n.º 29
0
class MvDetector():
    mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 1)
    devices = mvnc.EnumerateDevices()
    devNum = len(devices)
    if len(devices) == 0:
        print('No MVNC devices found')
        quit()
    devHandle = []
    graphHandle = []

    def __init__(self, graphfile, cfgfile=None):
        if cfgfile == None:
            cfgfile = graphfile.replace(".graph", ".cfg")

        if not os.path.exists(graphfile):
            print("Error: Could not find graph file {}".format(graphfile))
            return
        if not os.path.exists(cfgfile):
            print(
                "Error: Could not find darknet config file {}".format(cfgfile))
            return

        for i in range(MvDetector.devNum):
            MvDetector.devHandle.append(mvnc.Device(MvDetector.devices[i]))
            MvDetector.devHandle[i].OpenDevice()
            opt = MvDetector.devHandle[i].GetDeviceOption(
                mvnc.DeviceOption.OPTIMISATION_LIST)
            # load blob
            with open(graphfile, mode='rb') as f:
                blob = f.read()
            MvDetector.graphHandle.append(
                MvDetector.devHandle[i].AllocateGraph(blob))
            MvDetector.graphHandle[i].SetGraphOption(
                mvnc.GraphOption.ITERATIONS, 1)
            #MvDetector.graphHandle[i].SetGraphOption(mvnc.GraphOption.DONTBLOCK, 1)
            iterations = MvDetector.graphHandle[i].GetGraphOption(
                mvnc.GraphOption.ITERATIONS)

        imgWidth = cfgGetVal(cfgfile, "net", "width")
        imgHeight = cfgGetVal(cfgfile, "net", "height")
        numClasses = cfgGetVal(cfgfile, "region", "classes")
        anchors = cfgGetVal(cfgfile, "region", "anchors")

        self.detector = createYoloDetector(numClasses, anchors)

        self.dim = (imgWidth, imgHeight)
        self.blockwd = BLOCK_WD
        self.wh = BLOCK_WD * BLOCK_WD
        self.targetBlockwd = TARGET_BLOCK_WD
        self.classes = numClasses
        self.nms = NMS

    def __del__(self):
        for i in range(MvDetector.devNum):
            MvDetector.graphHandle[i].DeallocateGraph()
            MvDetector.devHandle[i].CloseDevice()

    def PrepareImage(self, img, dim):
        tPrep0 = time.time()

        imgw = img.shape[1]
        imgh = img.shape[0]
        imgb = np.empty((dim[0], dim[1], 3))
        imgb.fill(0.5)

        if imgh / imgw > dim[1] / dim[0]:
            neww = int(imgw * dim[1] / imgh)
            newh = dim[1]
        else:
            newh = int(imgh * dim[0] / imgw)
            neww = dim[0]
        offx = int((dim[0] - neww) / 2)
        offy = int((dim[1] - newh) / 2)

        tDiff0 = 1000. * (time.time() - tPrep0)
        print("      prep0 = {:.2f}".format(tDiff0))

        tRes = time.time()

        #img01 = img.copy()/255.0
        #imgb[offy:offy+newh,offx:offx+neww,:] = resize(img01,(newh,neww),1)
        imgNet = cv2.resize(img, (neww, newh), cv2.INTER_LINEAR)
        imgNet = np.divide(imgNet, 255.)
        imgb[offy:offy + newh, offx:offx + neww, :] = imgNet
        im = imgb[:, :, (2, 1, 0)]

        tResDiff = 1000. * (time.time() - tRes)
        print("      resize = {:.2f}".format(tResDiff))

        return im, int(offx * imgw / neww), int(
            offy * imgh / newh), neww / dim[0], newh / dim[1]

    def _reshape(self, out):
        shape = out.shape
        out = np.transpose(out.reshape(self.wh, int(shape[0] / self.wh)))
        out = out.reshape(shape)
        return out

    def GetDetector(self):
        return self.detector

    def Detect(self, img, thresh):
        #imgw = img.shape[1]
        #imgh = img.shape[0]

        #im,offx,offy,xscale,yscale = self.PrepareImage(img, self.dim)
        #print('xscale = {}, yscale = {}'.format(xscale, yscale))
        #print("in shape = {}".format(img.shape))
        MvDetector.graphHandle[0].LoadTensor(img, 'user object')
        out, userobj = MvDetector.graphHandle[0].GetResult()
        #print("out shape = {}".format(out.shape))
        out = self._reshape(out)
        out = out.astype(np.float32)

        #with open("mvout.txt", "w") as fh:
        #    outStr = [str(x) for x in out]
        #    fh.writelines("\n".join(outStr))

        #print("out shape after reshape = {}".format(out.shape))

        #out = self.detector.Detect(out, thresh) # 4ms
        #pyresults = [BBox(x,xscale,yscale, offx, offy) for x in internalresults]

        return out
Exemplo n.º 30
0
    # print("window closed")
    widget.hide()
    pipeline.set_state(Gst.State.NULL)
    Gtk.main_quit()


def sigint_handler(signal, frame):
    Gtk.main_quit()


if __name__ == "__main__":
    Gdk.init([])
    Gtk.init([])
    Gst.init([])

    fx.SetGlobalOption(fx.GlobalOption.LOGLEVEL, 0)

    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    sourcegroup = parser.add_mutually_exclusive_group()

    # VideoSource subclasses as choices for source
    sourcegroup.add_argument('--v4l2-src',
                             help="v4l2 source device name, e.g. /dev/video0",
                             default="/dev/video1")
    sourcegroup.add_argument(
        '--src',
        choices=[
            cls.__name__ for cls in vars()['VideoSource'].__subclasses__()
        ],