-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfollow_me.py
86 lines (63 loc) · 2.58 KB
/
follow_me.py
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
import cv2
import mediapipe as mp
import dc_motors
from lcd import write_lcd, clear_lcd
write_lcd(first_line=' FOLLOW-ME MODE', second_line=' ATIVATED')
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_pose = mp.solutions.pose
cam = cv2.VideoCapture(0)
def look_left():
dc_motors.pl.ChangeDutyCycle(25)
dc_motors.pr.ChangeDutyCycle(25)
dc_motors.turn_left()
def look_right():
dc_motors.pl.ChangeDutyCycle(25)
dc_motors.pr.ChangeDutyCycle(25)
dc_motors.turn_right()
def follow():
dc_motors.pl.ChangeDutyCycle(80)
dc_motors.pr.ChangeDutyCycle(80)
dc_motors.forward()
with mp_pose.Pose(
min_detection_confidence=0.8,
min_tracking_confidence=0.8) as pose:
while cam.isOpened():
success, image = cam.read()
image_height, image_width, _ = image.shape
print(image_width)
border_left = int((image_width/2) - ((image_width/2)/2) + (((image_width/2)/2)/2))
border_right = int((image_width/2) + ((image_width/2)/2) - (((image_width/2)/2)/2))
if not success:
continue
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = pose.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.pose_landmarks:
x_coordinate = list()
y_coordinate = list()
for id, lm in enumerate(results.pose_landmarks.landmark):
h, w, c = image.shape
cx, cy = int(lm.x * w), int(lm.y * h)
x_coordinate.append(cx)
y_coordinate.append(cy)
left_shoulder = results.pose_landmarks.landmark[11].x * image_width
right_shoulder = results.pose_landmarks.landmark[12].x * image_width
center = (right_shoulder + left_shoulder) / 2
cv2.circle(img=image,
center=(int(center),320),
radius=3,
color=(0,0,255),
thickness=-1)
if center < border_right and center > border_left:
dc_motors.stop()
if results.pose_landmarks.landmark[15].y * image_height < results.pose_landmarks.landmark[13].y * image_height:
follow()
if center > border_right:
look_right()
if center < border_left:
look_left()
cv2.imshow("Camera", image)
cv2.waitKey(1)