简体   繁体   English

如何将图像传递给android中的tflite model

[英]How to pass image to tflite model in android

I have converted a Yolo model to.tflite for use in android. This is how it was used in python -我已经将 Yolo model 转换为 .tflite 以用于 android。这就是它在 python 中的使用方式 -

net = cv2.dnn.readNet("yolov2.weights", "yolov2.cfg")
classes = []
with open("yolov3.txt", "r") as f:
    classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))

cap= cv2.VideoCapture(0)


while True:
    _,frame= cap.read()
    height,width,channel= frame.shape
    blob = cv2.dnn.blobFromImage(frame, 0.00392, (320, 320), (0, 0, 0), True, crop=False)
    net.setInput(blob)
    outs = net.forward(output_layers)
    for out in outs:
        for detection in out:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            if confidence > 0.2:
            # Object detected
                center_x = int(detection[0] * width)
                center_y = int(detection[1] * height)
                w = int(detection[2] * width)
                h = int(detection[3] * height)
                # Rectangle coordinates
                x = int(center_x - w / 2)
                y = int(center_y - h / 2)

I used.netron https://github.com/lutzroeder.netron to visualize the model. The input is described as name: inputs, type: float32[1,416,416,3], quantization: 0 ≤ q ≤ 255, location: 399 and the output as name: output_boxes, type: float32[1,10647,8], location: 400.我使用了.netron https://github.com/lutzroeder.netron来可视化 model。输入描述为名称:输入,类型:float32[1,416,416,3],量化:0 ≤ q ≤ 255,位置:399 和output 作为名称:output_boxes,类型:float32[1,10647,8],位置:400。

My problem is regarding using this model in android. I have loaded the model in "Interpreter tflite", I am getting the input frames from the camera in byte[] format.我的问题是关于在 android 中使用这个 model。我已经在“Interpreter tflite”中加载了 model,我正在以 byte[] 格式从相机获取输入帧。 How can I convert it into the required input for tflite.run(input, output)?如何将其转换为 tflite.run(input, output) 所需的输入?

You need to resize the input image to match with the input size of TensorFlow-Lite model, and then convert it to RGB format to feed to the model.您需要调整输入图像的大小以匹配TensorFlow-Lite model 的输入大小,然后将其转换为RGB格式以提供给 model。

By using the ImageProcessor from TensorFlow-Lite Support Library, you can easily do image resizing and conversion.通过使用TensorFlow-Lite支持库中的ImageProcessor ,您可以轻松地进行图像大小调整和转换。

ImageProcessor imageProcessor =
        new ImageProcessor.Builder()
            .add(new ResizeWithCropOrPadOp(cropSize, cropSize))
            .add(new ResizeOp(imageSizeX, imageSizeY, ResizeMethod.NEAREST_NEIGHBOR))
            .add(new Rot90Op(numRoration))
            .add(getPreprocessNormalizeOp())
            .build();
return imageProcessor.process(inputImageBuffer);

Next to run inference with the interpreter, you feed the preprocessed image to the TensorFlow-Lite interpreter:接下来要使用解释器运行推理,将预处理后的图像提供给TensorFlow-Lite解释器:

tflite.run(inputImageBuffer.getBuffer(), outputProbabilityBuffer.getBuffer().rewind());

Refer this official example for more details, additionally you can refer this example as well.参考这个官方例子了解更多细节,另外你也可以参考这个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM