[英]Optimizing the Bounding Box Drawing in OpenCV
def boxDrawing(layerOutput, frameWidth, frameHeight,class_ids,confidences,boxes,img):
for output in layerOutput:
for detection in output:
score = detection[5:]
class_id = np.argmax(score)
confidence = score[class_id]
if confidence > 0.5:
center_x = int(detection[0] * frameWidth)
center_y = int(detection[1] * frameHeight)
width = int(detection[2] * frameWidth)
height = int(detection[3] * frameHeight)
left = int(center_x - width / 2)
top = int(center_y - height / 2)
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([left, top, width, height])
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.8, 0.7)
font = cv2.FONT_HERSHEY_PLAIN
colors = np.random.uniform(0, 255, size = (len(boxes),3))
for i in range(len(boxes)):
if i in indexes:
x,y,w,h = boxes[i]
label = str(classes[class_ids[i]])
confi = str(round(confidences[i],2))
color = colors[i]
cv2.rectangle(img, (x,y), (x+w,y+h), color,1)
cv2.putText(img, label+" "+ confi, (x,y+20), font, 1, (255,255,255),1)
這是我將邊界框繪制到檢測的代碼。 但是,當我分析我的代碼時,似乎 np.argmax(score) 消耗了大量時間。 這是用於 boxDrawing function 的 cProfile output:
3375 60.327 0.018 132.575 0.039 2381106525.py:39(boxDrawing)
1 0.424 0.424 742.042 742.042 2381106525.py:68(algorythmYolo)
17010000 10.916 0.000 72.061 0.000 <__array_function__ internals>:177(argmax)
17010000 20.109 0.000 47.640 0.000 fromnumeric.py:1127(argmax)
17010000 7.243 0.000 27.531 0.000 fromnumeric.py:51(_wrapfunc)
17010000 11.303 0.000 58.942 0.000 {built-in method numpy.core._multiarray_umath.implement_array_function}
17010000 17.412 0.000 17.412 0.000 {method 'argmax' of 'numpy.ndarray' objects}
algorythmYolo 是我的主要 function 進行圖像處理; 因此,消耗了大部分時間,並且僅在 boxDrawing function 中調用了 argmax。 有什么方法可以優化 boxDrawing function (因此是 argmax),使其工作得更快? 提前致謝!
我將假設您的課程處於最后 5 個步驟,這就是為什么您收集最后 5 個索引以獲得 class id 的原因。
一種想法是使用完整的矩陣而不是單獨的一維向量,因為 numpy 已優化為使用矩陣運算:
# Get the matrix of all the output classes
classes = np.array(output)[:,5:]
# class_ids returns a 1d vector with corresponding class
class_ids = np.argmax(classes, axis=1)
這會將您的代碼更改為以下內容:
for output in layerOutput:
classes = np.array(output)[:,5:]
class_ids = np.argmax(classes, axis=1)
for index, detection in enumerate(output):
confidence = score[class_ids[index]]
if confidence > 0.5:
center_x = int(detection[0] * frameWidth)
center_y = int(detection[1] * frameHeight)
width = int(detection[2] * frameWidth)
height = int(detection[3] * frameHeight)
left = int(center_x - width / 2)
top = int(center_y - height / 2)
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([left, top, width, height])
切片和 maxarg 的示例:
import numpy as np
output = np.array([
[0,0,0,0,0,0,0,0,2,1,2,3,4],
[0,0,0,0,0,0,0,0,2,1,2,3,4],
[0,0,0,0,0,0,0,0,2,1,2,7,4],
[0,0,0,0,0,0,0,0,2,1,2,3,4],
[0,0,0,0,0,0,0,0,2,1,6,3,4],
])
print(output)
print(output[:,5:])
classes = output[:,5:]
#[[0 0 0 2 1 2 3 4]
# [0 0 0 2 1 2 3 4]
# [0 0 0 2 1 2 7 4]
# [0 0 0 2 1 2 3 4]
# [0 0 0 2 1 6 3 4]]
print(np.argmax(classes, axis=1))
# [7 7 6 7 5]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.