Code Boxes
# Python Code
import cv2
import mediapipe as mp
import serial
import time
mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
mp_drawing = mp.solutions.drawing_utils
ser = serial.Serial('COM4', 9600)
time.sleep(2)
cap = cv2.VideoCapture(0)
while True:
success, image = cap.read()
if not success:
break
image = cv2.flip(image, 1)
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(rgb_image)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
x = int(hand_landmarks.landmark[0].x * image.shape[1])
y = int(hand_landmarks.landmark[0].y * image.shape[0])
cv2.circle(image, (x, y), 10, (0, 255, 0), -1)
angle = int(x / image.shape[1] * 180)
ser.write(f"{angle}\n".encode())
cv2.imshow('Hand Gesture Detection', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
ser.close()
// Arduino Code
#include
Servo myServo;
void setup() {
Serial.begin(9600);
myServo.attach(9);
}
void loop() {
if (Serial.available() > 0) {
int angle = Serial.parseInt();
angle = constrain(angle, 0, 180);
myServo.write(angle);
}
}