[英]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.