-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot_control1.py
More file actions
150 lines (123 loc) · 3.95 KB
/
Copy pathrobot_control1.py
File metadata and controls
150 lines (123 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import cv2
import numpy as np
import mediapipe as mp
import tensorflow as tf
from tensorflow.keras.models import load_model
import RPi.GPIO as io
import time
# Initialize GPIO pins for motor control
io.setmode(io.BCM)
io.setwarnings(False)
# Define GPIO pins for motor control
leftMotor_DIR_pin = 22
rightMotor_DIR_pin = 23
leftMotor_PWM_pin = 17
rightMotor_PWM_pin = 18
io.setup(leftMotor_DIR_pin, io.OUT)
io.setup(rightMotor_DIR_pin, io.OUT)
io.setup(leftMotor_PWM_pin, io.OUT)
io.setup(rightMotor_PWM_pin, io.OUT)
leftMotorPWM = io.PWM(leftMotor_PWM_pin, 1000)
rightMotorPWM = io.PWM(rightMotor_PWM_pin, 1000)
leftMotorPWM.start(0)
rightMotorPWM.start(0)
# Constant values
PWM_MAX = 100
# Function to set motor speeds for the left motor
def setLeftMotorSpeed(speed):
# Ensure speed limit is respected
if speed > 1:
speed = 1
elif speed < -1:
speed = -1
# Set motor direction
if speed >= 0:
io.output(leftMotor_DIR_pin, False)
else:
io.output(leftMotor_DIR_pin, True)
# Set motor power
pwm = int(PWM_MAX * abs(speed))
leftMotorPWM.ChangeDutyCycle(pwm)
# Function to set motor speeds for the right motor
def setRightMotorSpeed(speed):
# Ensure speed limit is respected
if speed > 1:
speed = 1
elif speed < -1:
speed = -1
# Set motor direction
if speed >= 0:
io.output(rightMotor_DIR_pin, False)
else:
io.output(rightMotor_DIR_pin, True)
# Set motor power
pwm = int(PWM_MAX * abs(speed))
rightMotorPWM.ChangeDutyCycle(pwm)
# Mapping gestures to robot actions
def map_gesture_to_action(gesture):
if gesture == "stop":
# Move forward slowly to simulate a stop
setLeftMotorSpeed(0.2)
setRightMotorSpeed(0.2)
elif gesture == "fist":
setLeftMotorSpeed(-0.5)
setRightMotorSpeed(-0.5)
time.sleep(1) # Assuming a delay of 1 second to move backward
setLeftMotorSpeed(0)
setRightMotorSpeed(0)
elif gesture == "thumbs up":
setLeftMotorSpeed(0.5)
setRightMotorSpeed(-0.5)
elif gesture == "thumbs down":
setLeftMotorSpeed(-0.5)
setRightMotorSpeed(0.5)
elif gesture == "call me":
setLeftMotorSpeed(1)
setRightMotorSpeed(1)
elif gesture == "peace":
setLeftMotorSpeed(0.2)
setRightMotorSpeed(0.2)
# initialize mediapipe
mpHands = mp.solutions.hands
hands = mpHands.Hands(max_num_hands=1, min_detection_confidence=0.7)
mpDraw = mp.solutions.drawing_utils
# Load the gesture recognizer model
model = load_model('mp_hand_gesture')
# Load class names
f = open('gesture.names', 'r')
classNames = f.read().split('\n')
f.close()
# Initialize the webcam
cap = cv2.VideoCapture(0)
while True:
# Read each frame from the webcam
_, frame = cap.read()
x, y, c = frame.shape
# Flip the frame vertically
frame = cv2.flip(frame, 1)
framergb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Get hand landmark prediction
result = hands.process(framergb)
recognized_gesture = ''
# post process the result
if result.multi_hand_landmarks:
landmarks = []
for handslms in result.multi_hand_landmarks:
for lm in handslms.landmark:
lmx = int(lm.x * x)
lmy = int(lm.y * y)
landmarks.append([lmx, lmy])
# Predict gesture
prediction = model.predict([landmarks])
classID = np.argmax(prediction)
recognized_gesture = classNames[classID]
# Perform the corresponding action based on the recognized gesture
map_gesture_to_action(recognized_gesture)
if cv2.waitKey(1) == ord('q'):
break
# release the webcam and clean up GPIO
cap.release()
cv2.destroyAllWindows()
leftMotorPWM.stop()
rightMotorPWM.stop()
io.cleanup()