簡體   English   中英

如何在 OpenCV 中使用 yolov5 tflite 導出

[英]How to use yolov5 tflite export with OpenCV

我從 Yolov5 導出了一個 tflite 文件,並使用以下代碼獲得了輸出數據:

import numpy as np
import tensorflow as tf
from PIL import Image
import os

img = Image.open(os.path.join('dataset', 'images','val','IMG_6099.JPG'))
img = img.resize((256,256),Image.ANTIALIAS)
numpydata = np.asarray(img)
interpreter = tf.lite.Interpreter(model_path="yolov5s-fp16.tflite")
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

input_shape = input_details[0]['shape']
input_data = np.array(img,dtype=np.float32)
input_data = tf.expand_dims(input_data, 0)
interpreter.set_tensor(input_details[0]['index'], input_data)

interpreter.invoke()

output_data = interpreter.get_tensor(output_details[0]['index'])

打印輸出output_data

[[[1.6754180e-02 3.2771632e-02 8.4546164e-02 ... 2.2025524e-05
   3.0189141e-05 6.1972853e-05]
  [1.5505254e-02 3.5847023e-02 9.6953809e-02 ... 1.9333076e-05
   1.5587271e-05 3.6931968e-05]
  [1.6107641e-02 3.6390714e-02 8.2990780e-02 ... 1.6197217e-05
   1.4623029e-05 3.6216315e-05]
  ...
  [8.6931992e-01 8.8494051e-01 2.4040593e-01 ... 3.1457843e-05
   2.4052188e-05 2.2471884e-05]
  [8.6244017e-01 9.0521729e-01 4.4481179e-01 ... 5.1936011e-05
   3.9207229e-05 3.5609013e-05]
  [8.6841702e-01 9.0255147e-01 7.0057535e-01 ... 1.0812500e-04
   1.0073676e-04 7.7818921e-05]]]

這些數字是什么? 更重要的是如何在圖像上顯示結果? 我也已經看到這個帖子了。

這是我試圖實時捕獲對象的代碼:

cap = cv2.VideoCapture(0)
ret, frame = cap.read()
print(ret)
frame = cv2.resize(frame, (256 , 256))
    

for i in range(len(scores)):
    if ((scores[i] > 0.1) and (scores[i] <= 1.0)):
        H = frame.shape[0]
        W = frame.shape[1]
        xmin = int(max(1,(xyxy[0][i] * W)))
        ymin = int(max(1,(xyxy[1][i] * H)))
        xmax = int(min(H,(xyxy[2][i] * W)))
        ymax = int(min(W,(xyxy[3][i] * H)))

        # cv2.rectangle(frame, (xmin,ymin), (xmax,ymax), (10, 255, 0), 2)
plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

您附加的帖子很清楚: [x ,y ,w ,h , conf, class0, class1, ...] 總共 85 列和許多對象(行)檢測到。 col 0-3是boxes,col 4是conf,另外80是class

您需要使用 conf 過濾行。 否則你會得到很多錯誤的檢測對象。

[x ,y ,w ,h] 也不是輸入圖像的真實比例。

要獲得真實的框,您可能需要進行一些處理,例如重新縮放、xywh 到 xyxy、NMS(非最大抑制)等。

您可以查看 Yolov5 源代碼中的 detect.py 和 utils/general.py。

得到真正的盒子后,在圖像上繪制盒子。

下面的文檔向您展示了如何使用打開的 cv https://docs.opencv.org/4.x/dc/da5/tutorial_py_drawing_functions.html在圖像上繪制框

如果你google,有很多例子教你如何使用open cv繪圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM