簡體   English   中英

如何在 Opencv Python 中查找特定地標點的坐標

[英]How to find co-ordinates of a specific Landmark point in Opencv Python

我想獲取物體移動的移動地標點的坐標。 我試着先檢測選擇一個點。 我是 OpenCV 和 python 的初學者。 不知道有沒有函數。

import cvzone
import numpy as np
from cvzone.FaceMeshModule import FaceMeshDetector
from cvzone.PlotModule import LivePlot



idList = [8]
 
cap = cv2.VideoCapture(0)
detector = FaceMeshDetector(maxFaces=1)


while True:
 
    if cap.get(cv2.CAP_PROP_POS_FRAMES) == cap.get(cv2.CAP_PROP_FRAME_COUNT):
        cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
 
    success, img = cap.read()
    img, faces = detector.findFaceMesh(img, draw = False)
    
    if faces:
        face = faces[0]
        leftUp = face[8]
        
        for id in idList:
            cv2.circle(img, face[id], 3,(80,200,120), cv2.FILLED)

    
    img = cv2.resize(img,(640,360))
    cv2.imshow("image", img)
    cv2.waitKey(25)
         
cap.release()
cv2.destroyAllWindows()

如果我對這個問題的理解是正確的,那么您想檢測地標中是否有特定點在移動。

為此,我建議首先確定地標的索引,我找不到具體的文檔,但您可以使用以下代碼來識別它:

import cv2
from cvzone.FaceMeshModule import FaceMeshDetector

cap = cv2.VideoCapture(0)
detector = FaceMeshDetector(maxFaces=1)

def click_event(event, x, y, faces, detector):
        if event == cv2.EVENT_LBUTTONDOWN and faces[0]:
            lstOfPoints = [detector.findDistance(faces[0][i], (x, y))[0] for i in range(0, 468)]
            print(lstOfPoints.index(min(lstOfPoints)))

while True:
        _, img = cap.read()
        img, faces = detector.findFaceMesh(img, draw=False)
        if faces:
            face = faces[0]
            for i in range(0, 468):
                    cv2.circle(img, (face[i][0], face[i][1]), 1, (0, 255, 0), cv2.FILLED)
            cv2.imshow("Image", img)
            cv2.setMouseCallback('Image', lambda event, x , y, flags, params :click_event(event, x, y, faces, detector))
            cv2.waitKey(1)
        else:
            cv2.imshow("Image", img)
            cv2.waitKey(1)

您可以單擊要打印其索引的要標識的點。

一旦找到要查找的點,就可以像這樣更改代碼:

import cv2
from cvzone.FaceMeshModule import FaceMeshDetector
import math
cap = cv2.VideoCapture(0)
detector = FaceMeshDetector(maxFaces=1)
idxOfPoint = 50
i = 10
while True:
        _, img = cap.read()
        img, faces = detector.findFaceMesh(img, draw=False)
        if faces:
            if i != 10 and i % 30 == 0:
                distanceChange = math.sqrt((faces[0][idxOfPoint][0] - pastCoord[0])**2 + (faces[0][idxOfPoint][1] - pastCoord[1])**2)
                print("point moved" if distanceChange > 20 else "point didn't move")
                pastCoord  = faces[0][idxOfPoint]
            if i == 10:
                pastCoord  = faces[0][idxOfPoint]
            cv2.circle(img, (faces[0][idxOfPoint][0], faces[0][idxOfPoint][1]), 1, (0, 255, 0), cv2.FILLED) 
            cv2.imshow("Image", img)
            cv2.waitKey(1)
            i += 1
        else:
            cv2.imshow("Image", img)
            cv2.waitKey(1)

其中 30(在i % 30中)表示每幀驗證的頻率,20(在distanceChange > 20中)表示觸發打印的最小距離變化。

暫無
暫無

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

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