我试图发送从python脚本中获取的角度值,并将这些值发送给arduino,用于旋转伺服电机。我使用了youtube上关于mediapipe手部跟踪的教程。我得到了食指角度的值,从0度到180度。但我如何将这些值发送到伺服电机并旋转它呢?下面是我的代码:
import cv2
import numpy as np
import HandTrackingModule as htm
import math
import Utilities
from Utilities import connectToRobot
portNo = "COM5"
wCam, hCam = 640, 480
cap = cv2.VideoCapture(1)
cap.set(3, wCam)
cap.set(4, hCam)
Utilities.connectToRobot(portNo)
detector = htm.handDetector(detectionCon=0.75)
while True:
success, img = cap.read()
img = detector.findHands(img)
lmList = detector.findPosition(img, draw=False)
if len(lmList) != 0:
#print(lmList[8], lmList[0])
x1, y1 = lmList[8][1], lmList[8][2]
x2, y2 = lmList[0][1], lmList[0][2]
cv2.circle(img, (x1, y1), 10, (255, 0, 255), cv2.FILLED)
cv2.circle(img, (x2, y2), 10, (255, 0, 255), cv2.FILLED)
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
length = math.hypot(x2 - x1, y2 - y1)
#print(length)
# Index finger range 330 - 140
# servo motor angle range 180 - 0
angle = np.interp(length, [140, 300], [0, 180])
print(int(angle))
cv2.imshow("Image", img)
key = cv2.waitKey(1)
if key & 0xff == ord('q'):
break
对不起,我的英语不好。谢谢你的建议
这些是角度。您可以看到最大角度为180度,最小角度为0度。我没有拿0度,因为这只是print(int(angle))
输出,你可以理解我在做什么。enter image description here顺便说一句,这是我的手。这张照片显示了代码的作用。我画了一条紫色的线来计算角度
65
78
77
80
92
67
79
74
91
98
126
144
144
175
180
180
180
目前没有回答
相关问题 更多 >
编程相关推荐