[英]Select a bounding box on an image and annotate
我正在做一個項目,我想在一個主題上畫一個邊界框和 select 它(通過鼠標單擊),所以我可以在圖像上方有一個文本對話框 hover 之類的東西,然后我可以輸入一個 label。 I'm already using OpenCV to detect the object and draw an initial bounding box on it using the Haar Cascade classifier, but so far I can't find the right combination of OpenCV directives to be able to select that bounding box and then annotate it . 相關代碼如下。
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
將不勝感激一些好的指針。 謝謝。
您可以將鼠標的 x/y position 與邊界框進行比較。 下面的代碼描述了如何做到這一點。
首先,為了能夠處理鼠標輸入,您必須創建一個 namedWindow。 然后,您可以將 mouseCallback 附加到該 window:
# create window
cv2.namedWindow("Frame")
# attach a callback to the window, that calls 'getFace'
cv2.setMouseCallback("Frame", getFace)
在 getFace 方法中,您檢查按鈕是否按下,然后循環遍歷面並檢查鼠標的 x/y 是否在面的邊界框的范圍內。 如果是,則返回人臉的索引。
def getFace(event, x,y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
# if mousepressed
for i in range(len(faces)):
# loop through faces
(face_x,face_y,w,h) = faces[i]
# unpack variables
if x > face_x and x < face_x + w:
# if x is within x-range of face
if y > face_y and y < face_y + h:
# if y is also in y-range of face
return i
# then return the index of the face
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.