Exemple #1
0
    def __init__(self, movidius_id=0, longrange=False):

        dir_path = os.path.dirname(os.path.realpath(__file__)) + "/../model"

        # Define constants
        self.NETWORK_INPUT_SIZE = 300
        self.NETWORK_OUTPUT_SIZE = 707

        # Get Movidius Devices
        devices = mvnc.enumerate_devices()
        if len(devices) < movidius_id+1:
            print('Not enough devices found')
            quit()

        # Load SSD Graph
        if longrange:
            graphfile = dir_path + "/ssd_longrange.graph"
        else:
            graphfile = dir_path + "/ssd.graph"
        with open(graphfile, mode='rb') as rf:
            graphfile = rf.read()

        self.SSDGraphDevice = mvnc.Device(devices[movidius_id])
        self.SSDGraphDevice.open()

        self.SSDGraph = mvnc.Graph("detection_graph")

        self.graph_fifo_in, self.graph_fifo_out = self.SSDGraph.allocate_with_fifos(self.SSDGraphDevice, graphfile)
Exemple #2
0
    def __init__(self,
                 network_graph_filename: str,
                 ncs_device: mvnc.Device,
                 name=None):
        self._device = ncs_device
        self._network_graph_filename = network_graph_filename
        try:
            with open(self._network_graph_filename, mode='rb') as graph_file:
                graph_in_memory = graph_file.read()
            self._graph = mvnc.Graph("AgeNet Graph")
            self._fifo_in, self._fifo_out = self._graph.allocate_with_fifos(
                self._device, graph_in_memory)

            self._input_fifo_capacity = self._fifo_in.get_option(
                mvnc.FifoOption.RO_CAPACITY)
            self._output_fifo_capacity = self._fifo_out.get_option(
                mvnc.FifoOption.RO_CAPACITY)

        except:
            print('\n\n')
            print('Error - could not load neural network graph file: ' +
                  network_graph_filename)
            print('\n\n')
            raise

        self._end_flag = True
        self._name = name
        if (self._name is None):
            self._name = "no name"

        self._async_count_lock = threading.Lock()
        self._async_inference_count = 0
    def __init__(self):
        #parameter
        self.publish_image = rospy.get_param("detecter/publish_image", False)
        self.dest_rate = rospy.get_param("detecter/dest_rate", 30)
        self.input_rate = rospy.get_param("detecter/input_rate", 30)
        self.threshold = rospy.get_param("detecter/threshold", 0.2)
        self.sim = rospy.get_param("detecter/sim", False)

        self.PREPROCESS_DIMS = (300, 300)

        # grab a list of all NCS devices plugged in to USB
        rospy.loginfo("finding NCS devices...")
        self.devices = mvnc.enumerate_devices()

        # if no devices found, exit the script
        if len(self.devices) == 0:
            rospy.loginfo("No devices found. Please plug in a NCS")
        # use the first device since this is a simple test script
        # (you'll want to modify this is using multiple NCS devices)
        rospy.loginfo("found {} devices. "
                      "opening device0...".format(len(self.devices)))
        self.device = mvnc.Device(self.devices[0])
        self.device.open()

        # open the CNN graph file
        rospy.loginfo("loading the graph file into memory...")
        my_dir = os.path.abspath(os.path.dirname(__file__))
        path = os.path.join(my_dir, "../graphs/mobilenetgraph")
        with open(path, mode="rb") as f:
            graph_in_memory = f.read()

        # load the graph into the NCS
        rospy.loginfo("allocating the graph on the NCS...")
        self.graph = mvnc.Graph("MobileNet-SSD")
        self.ssd_fifo_in, self.ssd_fifo_out = self.graph.allocate_with_fifos(
            self.device, graph_in_memory)

        self.node_name = rospy.get_name()
        if self.sim:
            self.subscriber = rospy.Subscriber(
                "camera/rgb/image_rect_color/compressed",
                CompressedImage,
                self.cbimage,
                queue_size=1,
                buff_size=2**24)
        else:
            self.subscriber = rospy.Subscriber("camera_node/image/compressed",
                                               CompressedImage,
                                               self.cbimage,
                                               queue_size=1,
                                               buff_size=2**24)
        self.pubImg = rospy.Publisher("detecter/image/compressed",
                                      CompressedImage,
                                      queue_size=1)
        self.pubBoxlist = rospy.Publisher("detecter/predictions",
                                          Boxlist,
                                          queue_size=1)
        rospy.loginfo("[%s] Initializing " % (self.node_name))
        self.bridge = CvBridge()
        self.frame_counter = 0
    def __init__(self, graphfile):
        select = 1
        self.detector = YoloDetector(select)
        for i in range(ObjectWrapper.devNum):
            ObjectWrapper.devHandle.append(
                mvnc.Device(ObjectWrapper.devices[i]))  ##------get devices
            ObjectWrapper.devHandle[i].open()  ##------ open device_i
            # load blob
            with open(graphfile, mode='rb') as f:
                blob = f.read()
            # create graph instance
            ObjectWrapper.graphHandle.append(mvnc.Graph('inst' + str(i)))
            # allocate resources
            fifoIn, fifoOut = ObjectWrapper.graphHandle[i].allocate_with_fifos(
                ObjectWrapper.devHandle[i], blob)
            ObjectWrapper.fifoInHandle.append(fifoIn)
            ObjectWrapper.fifoOutHandle.append(fifoOut)

        if (graphfile.endswith('416')):
            self.dim = (416, 416)
        elif (graphfile.endswith('288')):
            self.dim = (288, 288)
        elif (graphfile.endswith('352')):
            self.dim = (352, 352)
        else:
            self.dim = (416, 416)

        self.blockwd = int(self.dim[0] / 32)
        self.wh = self.blockwd * self.blockwd
        self.targetBlockwd = int(self.dim[0] / 32)
        self.classes = 6
        self.threshold = 0.35
        self.nms = 0.45
def execute_graph(graph_file, img):
    try:
        with open(graph_file, mode='rb') as f:
            graph_from_disk = f.read()
        graph = mvnc.Graph(graph_file)
        input_fifo, output_fifo = graph.allocate_with_fifos(
            device, graph_from_disk)
    except:
        print('Error - could not load graph file')
        device.close()
        device.destroy()
        return 1

    graph.queue_inference_with_fifo_elem(input_fifo, output_fifo, img,
                                         'user object')
    output, userobj = output_fifo.read_elem()
    inference_time = graph.get_option(mvnc.GraphOption.RO_TIME_TAKEN)

    input_fifo.destroy()
    output_fifo.destroy()
    graph.destroy()
    device.close()
    device.destroy()

    return output, userobj, inference_time
Exemple #6
0
    def __init__(self, graphfile):
        select = 1
        self.detector = YoloDetector(select)
        for i in range(ObjectWrapper.devNum):
            ObjectWrapper.devHandle.append(
                mvnc.Device(ObjectWrapper.devices[i]))
            ObjectWrapper.devHandle[i].open()
            # load blob
            with open(graphfile, mode='rb') as f:
                blob = f.read()
            # create graph instance
            ObjectWrapper.graphHandle.append(mvnc.Graph('inst' + str(i)))
            # allocate resources
            fifoIn, fifoOut = ObjectWrapper.graphHandle[i].allocate_with_fifos(
                ObjectWrapper.devHandle[i], blob)
            ObjectWrapper.fifoInHandle.append(fifoIn)
            ObjectWrapper.fifoOutHandle.append(fifoOut)

        self.dim = (416, 416)
        self.blockwd = 12
        self.wh = self.blockwd * self.blockwd
        self.targetBlockwd = 13
        self.classes = 20
        self.threshold = 0.2
        self.nms = 0.4
    def __init__(self, graphfile):
        select = 1
        self.detector = YoloDetector(select)

        for i in range(
                ObjectWrapper.devNum):  # will loop for each device detected
            ObjectWrapper.devHandle.append(
                mvnc.Device(ObjectWrapper.devices[i])
            )  # pass in list of devices, append that device to device list.
            ObjectWrapper.devHandle[i].open()  # open that device.
            #opt = ObjectWrapper.devHandle[i].GetDeviceOption(mvnc.DeviceOption.OPTIMISATION_LIST)
            # load blob
            with open(graphfile, mode='rb') as f:
                blob = f.read()
            graph = mvnc.Graph('graph1')  # creates a graph instance

            # Allocate the graph and store to array
            #ObjectWrapper.graphHandle.append(graph.allocate(ObjectWrapper.devHandle[i], blob))
            input_fifo, output_fifo = graph.allocate_with_fifos(
                ObjectWrapper.devHandle[i], blob)

            ObjectWrapper.graphHandle.append(graph)
            ObjectWrapper.inputHandle.append(input_fifo)
            ObjectWrapper.outputHandle.append(output_fifo)

            self.dim = (416, 416)
            self.blockwd = 12
            self.wh = self.blockwd * self.blockwd
            self.targetBlockwd = 13
            self.classes = 1
            self.threshold = 0.2
            self.nms = 0.4
Exemple #8
0
    def __init__(self, movidius_id=0):

        dir_path = os.path.dirname(os.path.realpath(__file__))

        # Get a list of ALL the sticks that are plugged in
        # we need at least one
        devices = mvnc.enumerate_devices()
        if len(devices) < movidius_id + 1:
            print('Not enough devices found')
            quit()

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

        # Open the NCS
        self.device.open()

        # Read in the graph file to memory buffer
        graphfile = dir_path + "/../model/20170512-110547_mvds/facenet_movidius_ncs.graph"
        with open(graphfile, mode='rb') as f:
            graph_in_memory = f.read()

        # create the NCAPI graph instance from the memory buffer containing the graph file.
        self.graph = mvnc.Graph("embedding_graph")
        self.graph_fifo_in, self.graph_fifo_out = self.graph.allocate_with_fifos(
            self.device, graph_in_memory)
Exemple #9
0
def main():
    devices = mvnc.enumerate_devices()  #list all avilabile ncs.
    if (len(devices) == 0):
        print("please insert at least one NCS!")
    print(str(len(devices)) + "  ncs detected")  # print the number of ncs.

    device_0 = mvnc.Device(devices[0])
    #device_1 = mvnc.Device(devices[1])
    device_0.open()
    #device_1.open()
    graph_file_name_0 = 'test_0.graph'  #read the graph file respectively
    #graph_file_name_1 = 'test_0.graph'

    with open(graph_file_name_0, mode='rb') as f:
        graph_in_memory_0 = f.read()
    #with open(graph_file_name_1, mode='rb') as f:
    #graph_in_memory_1 = f.read()

    graph_0 = mvnc.Graph(graph_file_name_0)
    #graph_1 = mvnc.Graph(graph_file_name_1)
    fifoIn_0, fifoOut_0 = graph_0.allocate_with_fifos(device_0,
                                                      graph_in_memory_0)
    #fifoIn_1, fifoOut_1 = graph_1.allocate_with_fifos(device_1, graph_in_memory_1)
    infer_image_0 = cv2.imread(IMAGE_FULL_PATH_0)
    #infer_image_1 = cv2.imread(IMAGE_FULL_PATH_1)

    run_inference(infer_image_0, graph_0, fifoIn_0, fifoOut_0)
    # run_inference(infer_image_1, graph_1,fifoIn_1, fifoOut_1)

    graph_0.destroy()  #close the all ncs
    fifoIn_0.destroy()
    fifoOut_0.destroy()
    device_0.close()
    def __init__(self,
                 network_graph_filename: str,
                 ncs_device: mvnc.Device,
                 inital_box_prob_thresh: float,
                 classification_mask: list = None,
                 name=None):
        """Initializes an instance of the class

        :param network_graph_filename: is the path and filename to the graph
               file that was created by the ncsdk compiler
        :param ncs_device: is an open ncs device object to use for inferences for this graph file
        :param inital_box_prob_thresh: the initial box probablity threshold. between 0.0 and 1.0
        :param classification_mask: a list of 0 or 1 values, one for each classification label in the
        _classification_mask list.  if the value is 0 then the corresponding classification won't be reported.
        :param name: A name to use for the processor.  Nice to have when debugging multiple instances
        on multiple threads
        :return : None
        """
        self._device = ncs_device
        self._network_graph_filename = network_graph_filename
        # Load graph from disk and allocate graph.
        try:
            with open(self._network_graph_filename, mode='rb') as graph_file:
                graph_in_memory = graph_file.read()
            self._graph = mvnc.Graph("SSD MobileNet Graph")
            self._fifo_in, self._fifo_out = self._graph.allocate_with_fifos(
                self._device, graph_in_memory)

            self._input_fifo_capacity = self._fifo_in.get_option(
                mvnc.FifoOption.RO_CAPACITY)
            self._output_fifo_capacity = self._fifo_out.get_option(
                mvnc.FifoOption.RO_CAPACITY)

        except:
            print('\n\n')
            print('Error - could not load neural network graph file: ' +
                  network_graph_filename)
            print('\n\n')
            raise

        self._classification_labels = SsdMobileNetProcessor.get_classification_labels(
        )

        self._box_probability_threshold = inital_box_prob_thresh
        self._classification_mask = classification_mask
        if (self._classification_mask is None):
            # if no mask passed then create one to accept all classifications
            self._classification_mask = [
                1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
            ]

        self._end_flag = True
        self._name = name
        if (self._name is None):
            self._name = "no name"

        # lock to let us count calls to asynchronus inferences and results
        self._async_count_lock = threading.Lock()
        self._async_inference_count = 0
Exemple #11
0
 def __init__(self, name, model_path, labels):
     self._device = None
     self._get_device()
     self._graph = mvnc.Graph(name)
     self._input = None
     self._output = None
     self._labels = labels
     self.allocate_model(model_path)
Exemple #12
0
 def AllocateGraph(self, graphfile):
     api2_graph = mvncapi2.Graph("mvnc_simple_api graph")
     api2_fifo_in, api2_fifo_out = api2_graph.allocate_with_fifos(
         self._api2_device,
         graphfile,
         input_fifo_data_type=mvncapi2.FifoDataType.FP16,
         output_fifo_data_type=mvncapi2.FifoDataType.FP16)
     return Graph(api2_graph, api2_fifo_in, api2_fifo_out)
Exemple #13
0
def setup(args):
    global facenet_graph, facenet_input_fifo, facenet_output_fifo, ssd_graph, ssd_input_fifo, ssd_output_fifo

    devices = mvncapi.enumerate_devices()
    device = mvncapi.Device(devices[0])
    device.open()
    with open(args.facenetGraph, mode="rb") as f:
        facenet_graph_data = f.read()
    facenet_graph = mvncapi.Graph('facenet_graph')
    facenet_input_fifo, facenet_output_fifo = facenet_graph.allocate_with_fifos(
        device, facenet_graph_data)

    with open(args.ssdGraph, mode="rb") as f:
        ssd_graph_data = f.read()
    ssd_graph = mvncapi.Graph('ssd_graph')
    ssd_input_fifo, ssd_output_fifo = ssd_graph.allocate_with_fifos(
        device, ssd_graph_data)
Exemple #14
0
 def __init__(self, device, h, w):
     with open('movidius/pnet-{}x{}.graph'.format(h, w), mode='rb') as f:
         graphFileBuff = f.read()
     self.pnetGraph = mvnc.Graph('PNet Graph {}x{}'.format(h, w))
     self.pnetIn, self.pnetOut = self.pnetGraph.allocate_with_fifos(
         device, graphFileBuff)
     self.h = h
     self.w = w
def load_graph(device, graph_file):
    global input_fifo, output_fifo

    with open(graph_file, mode='rb') as f:
        graph_buffer          = f.read()
    graph                     = mvnc.Graph(graph_file)
    input_fifo, output_fifo   = graph.allocate_with_fifos(device, graph_buffer)

    return graph,
Exemple #16
0
def main():
    vs=VideoStream(usePiCamera=True).start()
    time.sleep(1)
    print('Running NCS Caffe TinyYolo example')

    # Set logging level to only log errors
    mvnc.global_set_option(mvnc.GlobalOption.RW_LOG_LEVEL, 3)
    devices = #TODO mvnc.enumerate_devices()
    if len(devices) == 0:
        print('No devices found')
        return 1
    device = #TODO:mvnc.Device(devices[0])   use the mvnc API to assign the first device in devices to the device variable. 
    device.open()

    #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 = mvnc.Graph("Tiny Yolo Graph")
    fifo_in, fifo_out = #TODO: graph.allocate_with_fifos(device, graph_file_buffer) 
    #may need another line here 

    #Instantiate fifo_in and fifo_out using the graph file above. 

    # 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
    
    while True:
        
        frame=vs.read()# Get a frame from a video stream. 
        input_image=frame.copy()# copy frame to an input image. 
        display_image = cv2.resize(input_image, (NETWORK_IMAGE_WIDTH, NETWORK_IMAGE_HEIGHT), cv2.INTER_LINEAR)
        input_image = cv2.resize(input_image, (NETWORK_IMAGE_WIDTH, NETWORK_IMAGE_HEIGHT), cv2.INTER_LINEAR)
        input_image = input_image.astype(np.float32)
        input_image = # TODO: Scale values between 0 and 255   *= (255.0/image.max())
        input_image = input_image[:, :, ::-1]  # convert to RGB

        #TODO: graph.queue_inference_with_fifo_elem(input_fifo, output_fifo, input_image, 'user object') 
        #Use the queue_inference_with_fifo_elem to load the image and get the result from the NCS. This should be one line of code.
        
        output, userobj = fifo_out.read_elem()

        # 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])

        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)

    fifo_in.destroy()
    fifo_out.destroy()
    graph.destroy()
    device.close()
    device.destroy()
    print('Finished')
    def __init__(self, movidius_id_pnet=0, movidius_id_onet=1):

        dir_path = os.path.dirname(os.path.realpath(__file__)) + "/../model"

        #self.minsize = 20
        self.threshold = [0.6, 0.7, 0.7]
        self.factor = 0.709

        # Get Movidius Devices
        devices = mvnc.enumerate_devices()
        if movidius_id_pnet != movidius_id_onet and len(devices) < max(
                movidius_id_pnet, movidius_id_onet) + 1:
            print('Not enough devices found')
            quit()

        # Load PNet Graph
        with open(dir_path + "/pnet.graph", mode='rb') as rf:
            pgraphfile = rf.read()

        self.PNetDevice = mvnc.Device(devices[movidius_id_pnet])
        self.PNetDevice.open()

        self.PNet = mvnc.Graph("pnet_graph")
        self.PNet_fifo_in, self.PNet_fifo_out = self.PNet.allocate_with_fifos(
            self.PNetDevice, pgraphfile)

        # RNet se mantiene en Caffe
        #caffe.set_mode_cpu()
        #self.RNet = caffe.Net(caffe_model_path + "/rnet.prototxt", caffe_model_path + "/rnet.caffemodel", caffe.TEST)

        # Load ONet Graph
        with open(dir_path + "/onet.graph", mode='rb') as rf:
            ographfile = rf.read()

        if movidius_id_onet == movidius_id_pnet:
            self.ONetDevice = self.PNetDevice
        else:
            self.ONetDevice = mvnc.Device(devices[movidius_id_onet])
            self.ONetDevice.open()

        self.ONet = mvnc.Graph("onet_graph")
        self.ONet_fifo_in, self.ONet_fifo_out = self.ONet.allocate_with_fifos(
            self.ONetDevice, ographfile)
def load_graph(device):
    # Read the graph file into a buffer
    with open(ARGS.graph, mode='rb') as f:
        blob = f.read()
    # Load the grpah buffer into the Intel® Movidius™ NCS
    graph = mvnc.Graph(ARGS.graph)
    # Create and allocate a network graph to the device in FIFOs
    fifo_in, fifo_out = graph.allocate_with_fifos(device, blob)

    return graph, fifo_in, fifo_out
 def load_graph(self, graph_filename: str):
     logging.info("Load graph from `{}`.".format(graph_filename))
     with open(graph_filename, mode='rb') as graph_f:
         in_memory_graph = graph_f.read()
         api2_graph = mvncapi2.Graph(os.path.basename(graph_filename))
         api2_fifo_in, api2_fifo_out = api2_graph.allocate_with_fifos(self.device, in_memory_graph,
                                                                      input_fifo_data_type=mvncapi2.FifoDataType.FP16,
                                                                      output_fifo_data_type=mvncapi2.FifoDataType.FP16)
         self.graph = NCSGraph(api2_graph, api2_fifo_in, api2_fifo_out)
     if self.graph is None:
         raise SystemError("Cannot open NCS device {} with graph {}.".format(self.index, graph_filename))
Exemple #20
0
    def load_graph(self, device):
        # Load graph file data
        with open(self.graph_path, mode='rb') as f:
            blob = f.read()

        # Initialize a Graph object
        graph = mvnc.Graph('graph')

        # Allocate the graph to the device
        fifo_in, fifo_out = graph.allocate_with_fifos(device, blob)

        return graph, fifo_in, fifo_out
Exemple #21
0
def load_graph( device ):

    # Read the graph file into a buffer
    with open( ARGS.graph, mode='rb' ) as f:
        blob = f.read()

    # Load the graph buffer into the NCS
    graph = mvnc.Graph( ARGS.graph )
        # Set up fifos
    fifo_in, fifo_out = graph.allocate_with_fifos(device, blob)

    return graph, fifo_in, fifo_out
Exemple #22
0
def load_graph(device):
    # Read the graph file into a buffer
    with open(ARGS.graph, mode='rb') as f:
        graph_buffer = f.read()

    # Initialize Graph object
    graph = mvnc.Graph('graph')

    # Allocate the graph to the device and create input/output Fifos with default options in one call
    input_fifo, output_fifo = graph.allocate_with_fifos(device, graph_buffer)

    return graph, input_fifo, output_fifo
def load_graph(device):

    # Read the graph file into a buffer
    with open(ARGS.graph, mode='rb') as f:
        graph_buffer = f.read()

    # Load the graph buffer into the NCS
    # graph = device.AllocateGraph( blob )
    graph = mvncapi.Graph(ARGS.graph)
    # graph.allocate(device, graph_buffer)           # New way of allocating graph

    input_fifo, output_fifo = graph.allocate_with_fifos(device, graph_buffer)
    return graph, input_fifo, output_fifo
    def _setup(self, device):
        graph = mvncapi.Graph(self.graph_name)
        graph_buffer = read_graph_buffer(self.graph_path)

        fifo_in, fifo_out = graph.allocate_with_fifos(
            device,
            graph_buffer,
            input_fifo_data_type=mvncapi.FifoDataType.FP16,
            output_fifo_data_type=mvncapi.FifoDataType.FP16)

        self.graph = graph
        self.fifo_in = fifo_in
        self.fifo_out = fifo_out
Exemple #25
0
def embed(faces_gen, conf):
    """
    The same old embedding, but using NCS device for network forward pass
    :return: embeddings for a given list of faces
    """
    logging.debug('using conf: ' + str(conf))
    # The graph file that was created with the ncsdk compiler
    graph_file_name = conf['graph file']

    # read in the graph file to memory buffer
    with open(graph_file_name, mode='rb') as f:
        graph_in_memory = f.read()

    # create the NCAPI graph instance from the memory buffer containing the graph file.
    graph = mvncapi.Graph('graph1')

    devices = mvncapi.enumerate_devices()
    device_to_use = conf['device to use']
    if len(devices) < (device_to_use + 1):
        sys.exit("NCS device " + str(device_to_use) + " can't be found!")
    device = mvncapi.Device(devices[device_to_use])
    device.open()

    input_fifo, output_fifo = graph.allocate_with_fifos(
        device, graph_in_memory)
    for frame, aligned_list in faces_gen:
        faces = emb_helper.get_face_in_frame(frame, aligned_list,
                                             conf['image size'])
        emb_array = np.empty(shape=(len(aligned_list), 512))

        for i, face in enumerate(faces):
            # Write the image to the input queue and queue the inference in one call
            graph.queue_inference_with_fifo_elem(input_fifo, output_fifo,
                                                 face.astype(np.float32), None)

            # Get the results from the output queue
            output, user_obj = output_fifo.read_elem()
            emb_array[i] = output

        yield emb_array

    logging.debug('exiting NCS facenet embedding')

    logging.debug('destroying graph')
    graph.destroy()
    logging.debug('closing NSC queues')
    input_fifo.destroy()
    output_fifo.destroy()
    logging.debug('destroying device')
    device.close()
    device.destroy()
Exemple #26
0
    def __init__(self, graph_fpath, device_idx=0, fp16=True):
        # fx.global_set_option(fx.GlobalOption.RW_LOG_LEVEL, 0)
        self.dev = None
        self.graph = None
        self.fifoIn = None
        self.fifoOut = None

        devices = fx.enumerate_devices()
        if len(devices) < 1:
            print("Error - no NCS devices detected, verify an NCS device is connected.")
            return

        if device_idx > len(devices):
            print("Error - device NCS idx is out of range.")
            return

        self.fp16 = fp16
        self.dev = fx.Device(devices[device_idx])

        try:
            self.dev.open()
        except:
            print("Error - Could not open NCS device.")
            quit()

        print("Hello NCS! Device opened normally.")

        print("Opening graph: {}".format(graph_fpath))
        with open(graph_fpath, mode='rb') as f:
            graph_file_buff = f.read()

        self.graph = fx.Graph('graph')

        print("FIFO Allocation / FP16: {}".format(fp16))

        if self.fp16:
            self.fifoIn, self.fifoOut = self.graph.allocate_with_fifos(self.dev, graph_file_buff,
                                                                       input_fifo_data_type=fx.FifoDataType.FP16,
                                                                       output_fifo_data_type=fx.FifoDataType.FP16)
        else:
            self.fifoIn, self.fifoOut = self.graph.allocate_with_fifos(self.dev, graph_file_buff)

        output_tensor_list = self.graph.get_option(fx.GraphOption.RO_OUTPUT_TENSOR_DESCRIPTORS)
        self.output_shape = (output_tensor_list[0].h, output_tensor_list[0].w, output_tensor_list[0].c)

        input_tensor_list = self.graph.get_option(fx.GraphOption.RO_INPUT_TENSOR_DESCRIPTORS)
        self.input_shape = (input_tensor_list[0].h, input_tensor_list[0].w, input_tensor_list[0].c)
        self.input_cv_sz = (input_tensor_list[0].w, input_tensor_list[0].h)

        print('Input shape: {}'.format(self.input_shape))
        print('Output shape: {}'.format(self.output_shape))
    def __init__(self):
        self.node_name = rospy.get_name()
        rospy.loginfo("[%s] Initializing " % (self.node_name))

        # Param
        self.sim = rospy.get_param('~sim', True)
        self.model_path = rospy.get_param("~model")

        # Variables
        self.brdige = CvBridge()
        self.motor_cmd = None
        self.frame_counter = 0

        # NCS
        rospy.loginfo("finding NCS devices...")
        self.devices = mvnc.enumerate_devices()
        if len(self.devices) == 0:
            rospy.loginfo("No devices found. Please plug in a NCS")
        rospy.loginfo("found {} devices. "
                      "opening device0...".format(len(self.devices)))
        self.device = mvnc.Device(self.devices[0])
        self.device.open()

        with open(self.model_path, mode="rb") as f:
            self.graph_in_memory = f.read()

        rospy.loginfo("allocating the graph on the NCS...")
        self.graph = mvnc.Graph("Imitation-Learning")
        self.fifo_in, self.fifo_out = self.graph.allocate_with_fifos(
            self.device, self.graph_in_memory)

        # Timer
        rospy.Timer(rospy.Duration(0.2), self.send_motor_cmd)

        # Publisher
        if self.sim:
            from duckiepond_vehicle.msg import UsvDrive
            self.pub_motion = rospy.Publisher("~cmd_drive",
                                              UsvDrive,
                                              queue_size=1)
        else:
            self.pub_motion = rospy.Publisher("~cmd_drive",
                                              MotorCmd,
                                              queue_size=1)

        # Subscriber
        self.sub_image = rospy.Subscriber("~compressed_image",
                                          CompressedImage,
                                          self.cb_image,
                                          queue_size=1,
                                          buff_size=2**24)
def inferencer(results, frameBuffer):

    graph = None
    graphHandle0 = None
    graphHandle1 = None

    mvnc.global_set_option(mvnc.GlobalOption.RW_LOG_LEVEL, 4)
    devices = mvnc.enumerate_devices()
    if len(devices) == 0:
        print("No NCS devices found")
        sys.exit(1)
    print(len(devices))

    with open(join(graph_folder, "graph"), mode="rb") as f:
        graph_buffer = f.read()
    graph = mvnc.Graph('MobileNet-SSD')

    devopen = False
    for devnum in range(len(devices)):
        try:
            device = mvnc.Device(devices[devnum])
            device.open()
            graphHandle0, graphHandle1 = graph.allocate_with_fifos(
                device, graph_buffer)
            devopen = True
            break
        except:
            continue

    if devopen == False:
        print("NCS Devices open Error!!!")
        sys.exit(1)

    print("Loaded Graphs!!! " + str(devnum))

    while True:
        try:
            if frameBuffer.empty():
                continue

            color_image = frameBuffer.get()
            prepimg = preprocess_image(color_image)
            graph.queue_inference_with_fifo_elem(graphHandle0, graphHandle1,
                                                 prepimg.astype(np.float32),
                                                 color_image)
            out, _ = graphHandle1.read_elem()
            results.put(out)
        except:
            import traceback
            traceback.print_exc()
def load_multidevice_graph(device, graph_file):
    """
    Function to work with 2 NCS devices.

    :param device: NCS device to use
    :param graph_file: graph file for the particular device
    :return: opened graph object, input_fifo, and output_fifo for the device
    """
    with open(graph_file, mode='rb') as f:
        graph_buffer          = f.read()
    graph                     = mvnc.Graph(graph_file)
    external_input_fifo, external_output_fifo   = graph.allocate_with_fifos(device, graph_buffer)

    return graph, external_input_fifo, external_output_fifo
    def __init__(self, resolution=(480, 640), framerate=20):
        from picamera.array import PiRGBArray
        from picamera import PiCamera
        resolution = (resolution[1], resolution[0])
        # initialize the camera and stream
        self.camera = PiCamera()  # PiCamera gets resolution (height, width)
        self.camera.resolution = resolution
        self.height, self.width = (resolution[1], resolution[0])
        self.camera.framerate = framerate
        self.rawCapture = PiRGBArray(self.camera, size=resolution)
        self.stream = self.camera.capture_continuous(self.rawCapture,
                                                     format="rgb",
                                                     use_video_port=True)

        #Initialize and import MobileNet SSD graph and activate NCS device
        self.GRAPH = 'graph/graph'
        self.CLASSES = ('background',
                   'aeroplane', 'bicycle', 'bird', 'boat',
                   'bottle', 'bus', 'car', 'cat', 'chair',
                   'cow', 'diningtable', 'dog', 'horse',
                   'motorbike', 'person', 'pottedplant',
                   'sheep', 'sofa', 'train', 'tvmonitor')
        self.input_size = (300, 300)
        np.random.seed(3)
        self.colors = 255 * np.random.rand(len(self.CLASSES), 3)

        # discover our device
        devices = mvnc.enumerate_devices()
        device = mvnc.Device(devices[0])
        device.open()

        # load graph onto device
        with open(self.GRAPH, 'rb') as f:
            graph_file = f.read()

        self.graph = mvnc.Graph('graph1')
        # graph.allocate(device, graph_file)
        self.input_fifo, self.output_fifo = self.graph.allocate_with_fifos(device, graph_file)



        # initialize the frame and the variable used to indicate
        # if the thread should be stopped
        self.frame = None
        self.on = True

        print('PiCamera loaded.. .warming camera')
        time.sleep(2)