如何在android中将图像传递到tflite模型
我已经将Yolo模型转换为.tflite,以便在android中使用。这就是它在python中的使用方式-如何在android中将图像传递到tflite模型,android,computer-vision,tensorflow-lite,Android,Computer Vision,Tensorflow Lite,我已经将Yolo模型转换为.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()]
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)
我用netron将模型可视化。输入描述为名称:输入,
类型:float32[1416416,3],
量化:0≤ Q≤ 255,
地点:399
输出为
名称:输出框,
类型:float32[110647,8],
地点:400
我的问题是关于在android中使用这个模型。我已经在“解释器tflite”中加载了模型,我正在从相机获取字节[]格式的输入帧。如何将其转换为tflite.run(输入,输出)所需的输入?您需要调整输入图像的大小,以与
TensorFlow Lite
模型的输入大小相匹配,然后将其转换为RGB
格式以馈送到模型
通过使用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);
接下来要使用解释器运行推断,将预处理的图像提供给TensorFlow Lite
解释器:
tflite.run(inputImageBuffer.getBuffer(), outputProbabilityBuffer.getBuffer().rewind());
有关更多详细信息,请参考官方示例,此外,您还可以参考示例