1import cv2
2import serial
3import mediapipe as mp
4import math
5import numpy as np
6import time
7import pyfirmata
8
9
10
11board = pyfirmata.Arduino('COM3')
12servo = board.get_pin('d:9:s')
13
14
15mp_drawing = mp.solutions.drawing_utils
16mp_drawing_styles = mp.solutions.drawing_styles
17mp_hands = mp.solutions.hands
18
19
20
21
22wCam, hCam = 640, 480
23cam = cv2.VideoCapture(0)
24cam.set(3,wCam)
25cam.set(4,hCam)
26
27
28with mp_hands.Hands(
29 model_complexity=0,
30 min_detection_confidence=0.5,
31 min_tracking_confidence=0.5) as hands:
32
33 while cam.isOpened():
34 success, image = cam.read()
35
36 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
37 results = hands.process(image)
38 image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
39 if results.multi_hand_landmarks:
40 for hand_landmarks in results.multi_hand_landmarks:
41 mp_drawing.draw_landmarks(
42 image,
43 hand_landmarks,
44 mp_hands.HAND_CONNECTIONS,
45 mp_drawing_styles.get_default_hand_landmarks_style(),
46 mp_drawing_styles.get_default_hand_connections_style()
47 )
48
49
50 lmList = []
51 if results.multi_hand_landmarks:
52 myHand = results.multi_hand_landmarks[0]
53 for id, lm in enumerate(myHand.landmark):
54 h, w, c = image.shape
55 cx, cy = int(lm.x * w), int(lm.y * h)
56 lmList.append([id, cx, cy])
57
58
59 if len(lmList) != 0:
60 x1, y1 = lmList[4][1], lmList[4][2]
61 x2, y2 = lmList[8][1], lmList[8][2]
62
63
64 cv2.circle(image, (x1,y1),15,(255,255,255))
65 cv2.circle(image, (x2,y2),15,(255,255,255))
66 cv2.line(image,(x1,y1),(x2,y2),(255,0,0),3)
67 length = math.hypot(x2-x1,y2-y1)
68 if length < 50:
69 cv2.line(image,(x1,y1),(x2,y2),(0,0,0),3)
70
71
72 Pos = np.interp(length, [50, 220], [0, 100])
73 Posgripper= (round(Pos))
74
75 converted_Posgripper = str(Posgripper)
76 cv2.putText(image, str(Posgripper), (50, 60), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 0))
77
78 Servopos=(100-Posgripper)
79 print (Servopos)
80 servo.write(Servopos)
81
82
83 cv2.imshow('handDetector', image)
84 if cv2.waitKey(1) & 0xFF == ord('q'):
85 break
86cam.release()
Anonymous user
2 years ago
How can I control more servos? can you help me?