【问题标题】:How to Resize the Tensorflow Output Bounding Box to Original Image如何将 TensorFlow 输出边界框调整为原始图像的大小
【发布时间】: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


    【解决方案1】:

    解决方案

    此问题最简单的解决方案是继续找出缩小图像的h, w, c 与原始图像的h, w, c 进行比较的因子。

    用下面这段代码可以理解。

    h, w, c = image.shape # old image 512x512
    h1, w1, c1 = image2.shape # new image resize
    h = (h1/h) * h
    w = (w1/w) * w
    c = (c1/c) * c
    

    新形成的h, w, c 值属于图像2 或原始图像

    画矩形

    为了继续绘制一个矩形,我们将继续使用以下方法解决这个问题,以使其可行。

    def draw_rect(image,image2, box):
        h, w, c = image.shape # old image 512x512
        h1, w1, c1 = image2.shape # new image resize
        h = (h1/h) * h
        w = (w1/w) * w
        c = (c1/c) * c
        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(image2, (x_min, y_min), (x_max, y_max), (13, 13, 13), 2)
    

    【讨论】:

      猜你喜欢
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      • 2011-01-19
      • 1970-01-01
      相关资源
      最近更新 更多