Beispiel #1
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)
Beispiel #2
0
def do_initialize():  # -> (mvnc.Device, mvnc.Graph):
    """Creates and opens the Neural Compute device and c
    reates a graph that can execute inferences on it.
    Returns
    -------
    device : mvnc.Device
        The opened device.  Will be None if couldn't open Device.
    graph : mvnc.Graph
        The allocated graph to use for inferences.  Will be None if couldn't allocate graph
    """

    devices = mvnc.EnumerateDevices()
    if len(devices) == 0:
        print('Error - No devices found')
        return (None, None)
    device = mvnc.Device(devices[0])
    device.OpenDevice()

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

    ##Change this accordingly
    ########################################################################################
    graph_filename = filefolder + '/modelpredict.graph'
    ########################################################################################
    try:
        with open(graph_filename, mode='rb') as f:
            in_memory_graph = f.read()
    except:
        print("Error reading graph file: " + graph_filename)

    graph = device.AllocateGraph(in_memory_graph)

    return device, graph
def main():
    processes = []
    try:
        frameBuffer = mp.Queue(10)
        results = mp.Queue()

        p = mp.Process(target=camThread,
                       args=(results, frameBuffer),
                       daemon=True)
        p.start()
        processes.append(p)

        n_devices = len(mvnc.EnumerateDevices())
        print("{} devices found".format(n_devices))
        for devnum in range(n_devices):
            p = mp.Process(target=inferenceThread,
                           args=(results, frameBuffer, devnum),
                           daemon=True)
            p.start()
            processes.append(p)

        while True:
            time.sleep(1)

    except KeyboardInterrupt:
        for p in processes:
            p.terminate()
        print("Finished")
Beispiel #4
0
def main():

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

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

    # Open the NCS
    device.OpenDevice()

    # The graph file that was created with the ncsdk compiler
    graph_file_name = GRAPH_FILENAME

    # 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 = device.AllocateGraph(graph_in_memory)

    # Open test images
    #tar0 = tarfile.open('mytar.tar')
    #np.asarray(bytearray(tar_extractfl.read()), dtype=np.uint8)

    # Detect face in training image (must convert to grayscale)
    validated_image = cv2.imread(validated_image_filename)
    gray = cv2.cvtColor(validated_image, cv2.COLOR_BGR2GRAY)

    face_location = faceCascade.detectMultiScale(
        gray,
        scaleFactor=FD_SCALE_FACTOR,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )
    print (face_location)
    
    # Run inference for face detected
    for (x, y, w, h) in face_location:
        print ("found face")
        # Run an inference on the training face
        face_image = validated_image[y:y+h, x:x+w]
        valid_output = run_inference(face_image, graph)
        cv2.rectangle(validated_image, (x,y), (x+w, y+h), (0, 255, 0), 2)

    cv2.namedWindow(CV_WINDOW_NAME, cv2.WINDOW_NORMAL)
    cv2.imshow(CV_WINDOW_NAME, validated_image)
    cv2.waitKey(0)
    #valid_output = run_inference(validated_image, graph)

    run_test(valid_output, graph)

    # Clean up the graph and the device
    graph.DeallocateGraph()
    device.CloseDevice()
Beispiel #5
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)
Beispiel #6
0
    def load(self,trained_model_location="",all_data=[]):
        
        if not os.path.isfile(trained_model_location+".movidius.graph"):
            return None

        # Get the device
        devices = mvnc.EnumerateDevices()
        if len(devices) == 0:
            print('No Movidus devices found. Aborting')
            quit()
        device = mvnc.Device(devices[0])
        device.OpenDevice()


        # Load the Graph
        
        with open(trained_model_location+".movidius.graph", 'rb') as f:
            unallocatedGraph = f.read()
        
        graph = device.AllocateGraph(unallocatedGraph)
        userdata = {}
        userdata["graph"] = graph
        userdata["device"] = device
        userdata["unloader"] = self.unloadDevice
        return userdata
Beispiel #7
0
def run_inference(image_to_classify, args):

    scaled_image = cv2.resize(image_to_classify, (640, 480))
    
    image = scaled_image
    top,bottom,left,right = getPaddingSize(image)
    image = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=[0,0,0])
    frame = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    face_image = fd.detect_face(frame)
    if len(face_image) == 0 or numpy.any(face_image[0]) == None:
        return None
        
    resized_image = preprocess_image(face_image[0])
    # ***************************************************************
    # Send the image to the NCS
    # ***************************************************************
    devices = mvnc.EnumerateDevices()
    device = mvnc.Device(devices[0])
    device.OpenDevice()
    graph_file_name = args.facenetGraph
    with open(graph_file_name, mode='rb') as f:
        graph_in_memory = f.read()

    facenet_graph = device.AllocateGraph(graph_in_memory)

    facenet_graph.LoadTensor(resized_image.astype(numpy.float16), None)
    # ***************************************************************
    # Get the result from the NCS
    # ***************************************************************
    output, userobj = facenet_graph.GetResult()
    device.CloseDevice()

    return output
Beispiel #8
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'
Beispiel #9
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
Beispiel #10
0
def setup(gpu_memory_fraction=0.25):
    
    global graph, model, align

    print('Loading NCS graph')

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

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

    # Open the NCS
    device.OpenDevice()

    # The graph file that was created with the ncsdk compiler
    graph_file_name = GRAPH_FILENAME

    # 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 = device.AllocateGraph(graph_in_memory)

    with open('model.pkl', 'rb') as mod:
        model = pickle.load(mod)

    align = openface.AlignDlib(DLIB_FACE_PREDICTOR)
    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.EnumerateDevices()
        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.OpenDevice()

        self.SSDGraph = self.SSDGraphDevice.AllocateGraph(graphfile)
Beispiel #12
0
def setup():

    global graph, model, align

    print('Loading NCS graph')

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

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

    # Open the NCS
    device.OpenDevice()

    # The graph file that was created with the ncsdk compiler
    graph_file_name = GRAPH_FILENAME

    # 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 = device.AllocateGraph(graph_in_memory)
    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.EnumerateDevices()
        if 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.OpenDevice()

        self.PNet = self.PNetDevice.AllocateGraph(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()

        self.ONetDevice = mvnc.Device(devices[movidius_id_onet])
        self.ONetDevice.OpenDevice()

        self.ONet = self.ONetDevice.AllocateGraph(ographfile)
Beispiel #14
0
    def __init__(self):
        # name of the opencv window
        self.cv_window_name = "SSD MobileNet - hit any key to exit"

        # Get a list of ALL the sticks that are plugged in
        # we need at least one
        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()

        # The graph file that was created with the ncsdk compiler
        graph_file_name = 'graph'

        # 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.
        self.graph = self.device.AllocateGraph(graph_in_memory)
def classify(pic_name):
    t1 = time.time()
    pic = cv2.imread(pic_name)
    pic = cv2.resize(pic, (width, height), pic, interpolation=cv2.INTER_LINEAR)
    input_tensor = pic.astype(np.float16)
    print('x:', input_tensor.shape)
    # Open a device
    device_list = mvncapi.EnumerateDevices()
    device = mvncapi.Device(device_list[0])
    device.OpenDevice()

    # Load a graph from file at some GRAPH_FILEPATH
    GRAPH_FILEPATH = './ncsk_graph/inference.graph'
    with open(GRAPH_FILEPATH, mode='rb') as f:
        graph_buffer = f.read()

    # Allocate the graph to the device
    graph = device.AllocateGraph(graph_buffer)
    t2 = time.time()
    # Load the image to the device and trigger an inference
    graph.LoadTensor(input_tensor, 'user object')

    # Get the results from the device
    output, userobj = graph.GetResult()
    print('inference time:', time.time() - t2)
    # Do something with the results...
    print(output.shape)
    print(output)
    ki = np.argmax(output)
    # Clean up
    graph.DeallocateGraph()
    device.CloseDevice()
    print('total time:', time.time() - t1)
    return kinds[ki]
Beispiel #16
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)
Beispiel #17
0
def main():

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

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

    # Open the NCS
    device.OpenDevice()

    # The graph file that was created with the ncsdk compiler
    graph_file_name = GRAPH_FILENAME

    # 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 = device.AllocateGraph(graph_in_memory)

    validated_image = cv2.imread(validated_image_filename)
    valid_output = run_inference(validated_image, graph)

    run_camera(valid_output, validated_image_filename, graph)

    # Clean up the graph and the device
    graph.DeallocateGraph()
    device.CloseDevice()
Beispiel #18
0
def get_device(number=0):
    devices = mvnc.EnumerateDevices()
    devices_count = len(devices)
    if devices_count < number:
        raise Exception("Only have {} when requested numbered {}".format(
            devices_count, number))
    return mvnc.Device(devices[number])
Beispiel #19
0
def demo_on_stick():
    # this function predicts the 10 outputs by loading a graph to the compute stick. All calculations are on the stick.
    devices = mvnc.EnumerateDevices()
    if len(devices) == 0:
        print('No devices found')
        quit()
    device = mvnc.Device(devices[0])
    device.OpenDevice()
    with open(GRAPH_PATH, mode='rb') as f:
        blob = f.read()
    graph = device.AllocateGraph(blob)
    for i in range(n_image):
        image = cv2.imread(IMAGE_DIR + '%05d.jpg' % (i + 1))
        # image.shape = [480 x 640 x 3]
        image = cv2.resize(image, (160, 120), interpolation=cv2.INTER_NEAREST)
        # image.shape = [120 x 160 x 3]
        image = image[40:, :, :]
        # image.shape = [80 x 160 x 3]
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        image = cv2.normalize(image.astype('float'), None, 0.0, 1.0,
                              cv2.NORM_MINMAX)
        image = np.expand_dims(image, axis=2)
        cv2.imshow('image', image)
        cv2.waitKey(0)
        graph.LoadTensor(image.astype(np.float16), 'user object')
        output, userobj = graph.GetResult()
        print output[0]
    graph.DeallocateGraph()
    device.CloseDevice()
def main():

    use_camera = True
    devices = mvnc.EnumerateDevices()
    if len(devices) == 0:
        print('No NCS devices found')
        quit()

    device = mvnc.Device(devices[0])

    device.OpenDevice()
    graph_file_name = GRAPH_FILENAME
    with open(os.path.join(Config.model_dir, graph_file_name), mode='rb') as f:
        graph_in_memory = f.read()

    graph = device.AllocateGraph(graph_in_memory)

    print("load device")
    print(validated_image_list)

    valid_output = []
    for i in validated_image_list:
        validated_image = cv2.imread(
            os.path.join(Config.validated_image_dir, i))
        valid_output.append(run_inference(validated_image, graph))
    if (use_camera):
        run_camera(valid_output, validated_image_list, graph)

    graph.DeallocateGraph()
    device.CloseDevice()
    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)
Beispiel #22
0
def get_devices_list():
    devices = mvnc.EnumerateDevices()
    if len(devices) == 0:
        print('Not found any Intel Movidius NCS device!')
        return None
    else:
        return devices
Beispiel #23
0
def train():

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

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

    # Open the NCS
    device.OpenDevice()

    # The graph file that was created with the ncsdk compiler
    graph_file_name = GRAPH_FILENAME

    # 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 = device.AllocateGraph(graph_in_memory)

    # Dlib model
    align = openface.AlignDlib(DLIB_FACE_PREDICTOR)

    valid_data_directory_list = os.listdir(VALID_DATA_DIR)
    size = len(valid_data_directory_list)
    inference_output = {}
    for d in valid_data_directory_list:
        dir_name = VALID_DATA_DIR + d

        valid_image_filename_list = [
            dir_name + "/" + i for i in os.listdir(dir_name)
            if i.endswith(".jpg")
        ]

        for valid_image_filename in valid_image_filename_list:
            validated_image = cv2.imread(valid_image_filename)
            valid_output = run_inference(validated_image, graph, align)
            if numpy.any(valid_output) == None:
                print("No face detected in " + valid_image_filename +
                      " in dir: " + dir_name)
                continue
            if d in inference_output:
                inference_output[d].append(valid_output)
            else:
                inference_output[d] = [valid_output]

    with open('model.pkl', 'wb') as mod:
        pickle.dump(inference_output, mod)

    # Clean up the graph and the device
    graph.DeallocateGraph()
    device.CloseDevice()
Beispiel #24
0
def main():

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

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

    #Pick the second stick to run the Person Detector
    device2 = mvnc.Device(devices[1])

    # Open the first NCS
    device.OpenDevice()

    # Open the second NCS
    device2.OpenDevice()

    # The graph file that was created with the ncsdk compiler
    graph_file_name = GRAPH_FILENAME
    graph2 = 'graph'

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

    with open(graph2, mode='rb') as f:
        graph_data = f.read()

    # create the NCAPI graph instance from the memory buffer containing the graph file.
    graph = device.AllocateGraph(graph_in_memory)

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

    valid_output = {}

    for root, dirs, files in os.walk(VALIDATED_IMAGES_DIR):
        for file in files:
            if file[-4:] == ".jpg" or file[-4:] == ".png":
                validated_image_filename = VALIDATED_IMAGES_DIR + file
                validated_image = cv2.imread(validated_image_filename)
                valid_output[file[:-4]] = run_inference(validated_image, graph)

    #http_thread = ThreadedHTTP(('', 8686), ReedHandler)

    run_camera(valid_output, graph, ssd_mobilenet_graph)

    # Clean up the graph and the device
    graph.DeallocateGraph()
    ssd_mobilenet_graph.DeallocateGraph()
    device.CloseDevice()
    device2.CloseDevice()
Beispiel #25
0
 def deviceCheck(self):
     #check device is plugged in
     self.devices = mvnc.EnumerateDevices()
     if len(self.devices) == 0:
         self.device_work = False
         rospy.loginfo('NCS device not found')
     else:
         self.device_work = True
         rospy.loginfo('NCS device found')
         self.initialDevice()
Beispiel #26
0
def setupMovidius():
    try:
        devices = mvnc.EnumerateDevices()
        if len(devices) != 0:
            device = mvnc.Device(devices[0])
            device.OpenDevice()
            movidius_graph = device.AllocateGraph(
                'ssd_mobilenet_v1_coco_2017_11_17/frozen_inference_graph.pb')
    except ImportError:
        print("Movidius not setup")
    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()
Beispiel #28
0
def main():
    # name of the opencv window
    cv_window_name = "SSD MobileNet - hit any key to exit"

    # Get a list of ALL the sticks that are plugged in
    # we need at least one
    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()

    # The graph file that was created with the ncsdk compiler
    graph_file_name = 'detect.graph'

    # 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 = device.AllocateGraph(graph_in_memory)

    if __MODE__ == 'VID':
        vid = cv2.VideoCapture(VIDEO_FULL_PATH)
        while (vid.isOpened()):
            ret, frame = vid.read()
            # read the image to run an inference on from the disk
            infer_image = cv2.resize(frame, (512, 512))

            # run a single inference on the image and overwrite the
            # boxes and labels
            run_inference(infer_image, graph)

            # display the results and wait for user to hit a key
            cv2.imshow(cv_window_name, infer_image)
            cv2.waitKey(1)
    elif __MODE__ == 'IMG':
        infer_image = cv2.imread(IMAGE_FULL_PATH)
        infer_image = cv2.resize(infer_image, (512, 512))
        run_inference(infer_image, graph)
        # display the results and wait for user to hit a key
        cv2.imshow(cv_window_name, infer_image)
        cv2.waitKey(0)
    else:
        print("Please specify a valid option for processing ('VID' or 'IMG')")

    # Clean up the graph and the device
    graph.DeallocateGraph()
    device.CloseDevice()
Beispiel #29
0
def open_ncs_device():

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

    device = mvnc.Device(devices[0])
    device.OpenDevice()

    return device
Beispiel #30
0
def main():
    # name of the opencv window
    cv_window_name = "SSD MobileNet - hit any key to exit"

    # Get a list of ALL the sticks that are plugged in
    # we need at least one
    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()

    # The graph file that was created with the ncsdk compiler
    graph_file_name = 'graph'

    # point to the camera
    video_capture = cv2.VideoCapture(
        "http://*****:*****@192.168.208.200/mjpg/1/video.mjpg")

    # 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 = device.AllocateGraph(graph_in_memory)

    while True:  # fps._numFrames < 120
        _, frame_camera = video_capture.read()
        # read the image to run an inference on from the disk
        #infer_image = cv2.imread(IMAGE_FULL_PATH)

        # run a single inference on the image and overwrite the
        # boxes and labels
        run_inference(frame_camera, graph)

        # resize and display the results and wait for user to hit a key
        resized_image = cv2.resize(
            frame_camera,
            (int(frame_camera.shape[1] / 2), int(frame_camera.shape[0] / 2)))
        cv2.imshow("CAMERA IMAGE", resized_image)

        # wait if 'q' has been pressed to stop the system
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

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