計算機視覺模型輸出 blob 的標識如下:
[image_id, label, conf, x_min, y_min, x_max, y_max], where:
image_id - ID of the image in the batch
label - predicted class ID (0 - person)
conf - confidence for the predicted class
(x_min, y_min) - coordinates of the top left bounding box corner
(x_max, y_max) - coordinates of the bottom right bounding box corner
只需手動列印我正在使用的代碼中的陣列print(result_blob.tolist())
輸出如下所示:
[0.0, 0.0, 0.6192154288291931, 0.36988523602485657, 0.39582735300064087, 0.7380738258361816, 0.9911962151527405]
如果我想在視頻提要中以大于 0.5 的置信度在一個人周圍繪制一個帶有開放 CV 的邊界框,我該如何回圈遍歷 numpy 陣列中的這些元素來做到這一點?
該存盤庫還包含一些代碼和執行此操作的函式,但我不明白它是如何作業的,或者是否有更好的撰寫方法?有人可以幫我理解它下面detections = result.reshape(-1, 7)的Pythonenumerate是什么嗎?我得到的 cv2 繪圖矩形,但該函式令人困惑,為什么轉換浮點數為整數等...
def postprocess(result, image, fps=None):
"""
Define the postprocess function for output data
:param: result: the inference results
image: the orignal input frame
fps: average throughput calculated for each frame
:returns:
image: the image with bounding box and fps message
"""
detections = result.reshape(-1, 7)
for i, detection in enumerate(detections):
_, image_id, confidence, xmin, ymin, xmax, ymax = detection
if confidence > 0.5:
xmin = int(max((xmin * image.shape[1]), 10))
ymin = int(max((ymin * image.shape[0]), 10))
xmax = int(min((xmax * image.shape[1]), image.shape[1] - 10))
ymax = int(min((ymax * image.shape[0]), image.shape[0] - 10))
cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
cv2.putText(image, str(round(fps, 2)) " fps", (5, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 3)
return image
uj5u.com熱心網友回復:
該函式期望result是具有特定形狀的 numpy 陣列。如果有n個人檢測,則代碼的其余部分需要一個形狀為 的陣列(n, 7),因此是一個長度為 7 行的陣列。這是因為每次檢測有 7 條資訊。由于reshape該功能對傳遞的形狀很寬松,就像并且(n, 7)可以作業。(n * 7)(1, n, 7)
確保形狀(n, 7)允許對檢測進行簡單的回圈。因為一般來說,二維 numpy 陣列的行上的回圈可以寫成:
for row in array:
...
在此之上使用enumerate只會給出行的索引。但是,在這種情況下不使用這個索引,所以enumerate這里沒有用。可能這個回圈是從另一個后處理函式中復制的,并且是意外留下的。
檢測的解包image_id作為第二個元素,所以可能是另一個小錯誤。由于image_id也未使用,因此撰寫起來可能會更好:
confidence, xmin, ymin, xmax, ymax = detection[2:]
轉換xminetc 的 4 行做兩件事:
- 從相對坐標轉換為像素坐標(必須是整數)
- 確保矩形未在影像邊緣的 10 像素內繪制
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/516046.html
上一篇:如何使用argwhere函式訪問二進制numpy陣列column_wise的索引
下一篇:如何將公式應用于熊貓中的資料框
