Exemplo n.º 1
0
def do_inference(graph: mvnc.Graph,
                 image_filename: str,
                 number_results: int = 5) -> (List[str], List[numpy.float16]):
    """ executes one inference which will determine the top classifications for an image file.

    Parameters
    ----------
    graph : Graph
        The graph to use for the inference.  This should be initialize prior to calling
    image_filename : string
        The filename (full or relative path) to use as the input for the inference.
    number_results : int
        The number of results to return, defaults to 5

    Returns
    -------
    labels : List[str]
        The top labels for the inference.  labels[i] corresponds to probabilities[i]
    probabilities: List[numpy.float16]
        The top probabilities for the inference. probabilities[i] corresponds to labels[i]
    """

    # text labels for each of the possible classfications
    labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

    # Load image from disk and preprocess it to prepare it for the network
    # assuming we are reading a .jpg or .png color image so need to convert it
    # single channel gray scale image for mnist network.
    # Then resize the image to the size of image the network was trained with.
    # Next convert image to floating point format and normalize
    # so each pixel is a value between 0.0 and 1.0
    image_for_inference = cv2.imread(image_filename)
    image_for_inference = cv2.cvtColor(image_for_inference, cv2.COLOR_BGR2GRAY)
    image_for_inference = cv2.resize(image_for_inference,
                                     NETWORK_IMAGE_DIMENSIONS)
    image_for_inference = image_for_inference.astype(numpy.float32)
    image_for_inference[:] = ((image_for_inference[:]) * (1.0 / 255.0))

    # Start the inference by sending to the device/graph
    graph.LoadTensor(image_for_inference.astype(numpy.float16), None)

    # Get the result from the device/graph.  userobj should be the
    # same value that was passed in LoadTensor above.
    output, userobj = graph.GetResult()

    # sort indices in order of highest probabilities
    five_highest_indices = (-output).argsort()[:number_results]

    # get the labels and probabilities for the top results from the inference
    inference_labels = []
    inference_probabilities = []

    for index in range(0, number_results):
        inference_probabilities.append(str(
            output[five_highest_indices[index]]))
        inference_labels.append(labels[five_highest_indices[index]])

    return inference_labels, inference_probabilities
Exemplo n.º 2
0
    def do_inference(
            self,
            graph: mvnc.Graph,
            img,
            number_results: int = 5) -> (List[str], List[numpy.float16]):
        labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
        image_for_inference = img
        image_for_inference = cv2.cvtColor(image_for_inference,
                                           cv2.COLOR_BGR2GRAY)
        image_for_inference = cv2.resize(image_for_inference,
                                         NETWORK_IMAGE_DIMENSIONS)
        ret, image_for_inference = cv2.threshold(image_for_inference, 120, 255,
                                                 cv2.THRESH_BINARY_INV)
        cv2.imshow("image_for_inference:", image_for_inference)
        image_for_inference = image_for_inference.astype(numpy.float32)
        image_for_inference[:] = ((image_for_inference[:]) * (1.0 / 255.0))

        # Start the inference by sending to the device/graph
        graph.LoadTensor(image_for_inference.astype(numpy.float16), None)

        # Get the result from the device/graph.  userobj should be the
        # same value that was passed in LoadTensor above.
        output, userobj = graph.GetResult()

        # sort indices in order of highest probabilities
        five_highest_indices = (-output).argsort()[:number_results]

        # get the labels and probabilities for the top results from the inference
        inference_labels = []
        inference_probabilities = []

        for index in range(0, number_results):
            inference_probabilities.append(
                str(output[five_highest_indices[index]]))
            inference_labels.append(labels[five_highest_indices[index]])

        return inference_labels, inference_probabilities