I am trying to send angle values which gets from python script, and send this values to arduino for rotate servo motors. I used tutorials on youtube about mediapipe hand tracking. I am getting values of index finger’s angle from 0 degrees to 180. But how am i going to send these values to servo motor and rotate it? Here is my code below:
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
Sorry for my bad english. Thanks for advice.
These are the angles. You see maximum angle is 180 degrees and minimum is 0 degrees. I did not take 0 degrees because this is just print(int(angle))
output for you to understand what i am trying to do.
this is my hand btw. This photo show what code does. I drew purple line for calculate angle.
65
78
77
80
92
67
79
74
91
98
126
144
144
175
180
180
180
I am new here so if i opened this question in wrong topic sorry about that.