【发布时间】:2021-09-12 19:37:49
【问题描述】:
所以,我正在开发一个我已经成功训练的模型,它是一个.tflite 模型并使用 Tensorflow Lite。我使用 Python 3 作为与 tensorflow 的接口,但无法从返回的图像中添加边界框。
我的问题是:
如何将 TensorFlow 输出边界框调整为原始图像的大小
我能够获得512x512 输入文件的输出,但如果我是正确的,我无法继续获得原始文件的输出。如何继续并重新调整模型输出的大小,以便继续从原始图像中裁剪该部分,然后保存。
代码
import tensorflow as tf
import numpy as np
import cv2
import pathlib
import os
from silence_tensorflow import silence_tensorflow
from PIL import Image
silence_tensorflow()
interpreter = tf.lite.Interpreter(model_path="model.tflite")
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
#print(input_details)
#print(output_details)
interpreter.allocate_tensors()
def draw_rect(image, box):
h, w, c = image.shape
y_min = int(max(1, (box[0] * h)))
x_min = int(max(1, (box[1] * w)))
y_max = int(min(h, (box[2] * h)))
x_max = int(min(w, (box[3] * w)))
# draw a rectangle on the image
cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (13, 13, 13), 2)
for file in pathlib.Path('./').iterdir():
if file.suffix != '.jpeg' and file.suffix != '.png':
continue
img = cv2.imread(r"{}".format(file.resolve()))
print(f'[Converting] {file.resolve()}')
new_img = cv2.resize(img, (512, 512))
interpreter.set_tensor(input_details[0]['index'], [new_img])
interpreter.invoke()
rects = interpreter.get_tensor(
output_details[0]['index'])
scores = interpreter.get_tensor(
output_details[2]['index'])
for index, score in enumerate(scores[0]):
if index == 0:
print('[Quantity]')
print(index,score)
if index == 1:
print('[Barcode]')
print(index,score)
if score > 0.2:
draw_rect(new_img,rects[0][index])
print(rects[0][index])
cv2.imshow("image", new_img)
cv2.waitKey(0)
【问题讨论】:
标签: python tensorflow opencv tensorflow-lite