-
Notifications
You must be signed in to change notification settings - Fork 0
/
ver2hitungfps.py
160 lines (125 loc) · 5.49 KB
/
ver2hitungfps.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
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
151
152
153
154
155
156
157
158
159
160
import cv2 as cv
import numpy as np
import os
import mediapipe as mp
import time
import math
import socket
from tensorflow.keras.models import load_model
host = "192.168.4.1"
port = 80
#host= 255.255.248.0
#port= 8080
class SocketCommunicator:
def __init__(self, host, port) -> None:
self.host = host
self.port = port
self.socket = None
self.connect()
pass
def connect(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((self.host, self.port))
print("Terkoneksi dengan kursi roda")
self.socket = s
except socket.error:
print("Mode Remote anjay")
def send(self, data):
if self.socket:
self.socket.send(data)
s = SocketCommunicator(host, port)
mp_holistic = mp.solutions.holistic
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
holistic_model = mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5)
actions = np.array(['Kanan', 'Maju', 'Stop', 'Mundur', 'Kiri'])
sequence = []
predictions = []
def media_pipe_detection(image, model):
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
image.flags.writeable = False
results = model.process(image)
image.flags.writeable = True
image = cv.cvtColor(image, cv.COLOR_RGB2BGR)
return image, results
def draw_land_marks(image, results):
custom_pose_connections = list(mp_pose.POSE_CONNECTIONS)
mp_drawing.draw_landmarks(image, results.pose_landmarks, connections=custom_pose_connections)
mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS)
mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS)
def extract_keypoints_normalize(results):
midpoint_shoulder_x, midpoint_shoulder_y = 0, 0
shoulder_length = 1
if results.pose_landmarks:
left_shoulder = results.pose_landmarks.landmark[11]
right_shoulder = results.pose_landmarks.landmark[12]
midpoint_shoulder_x = (left_shoulder.x + right_shoulder.x) / 2
midpoint_shoulder_y = (left_shoulder.y + right_shoulder.y) / 2
shoulder_length = math.sqrt((left_shoulder.x - right_shoulder.x) ** 2 + (left_shoulder.y - right_shoulder.y) ** 2)
selected_pose_landmarks = results.pose_landmarks.landmark[11:23]
pose = np.array([[(res.x - midpoint_shoulder_x) / shoulder_length,
(res.y - midpoint_shoulder_y) / shoulder_length] for res in selected_pose_landmarks]).flatten()
else:
pose = np.zeros(12 * 2)
if results.left_hand_landmarks:
left_hand = np.array([[(res.x - midpoint_shoulder_x) / shoulder_length,
(res.y - midpoint_shoulder_y) / shoulder_length] for res in results.left_hand_landmarks.landmark]).flatten()
else:
left_hand = np.zeros(21 * 2)
if results.right_hand_landmarks:
right_hand = np.array([[(res.x - midpoint_shoulder_x) / shoulder_length,
(res.y - midpoint_shoulder_y) / shoulder_length] for res in results.right_hand_landmarks.landmark]).flatten()
else:
right_hand = np.zeros(21 * 2)
return np.concatenate([pose, left_hand, right_hand])
def load_lstm_model():
model = load_model('elmantap.keras')
return model
lstm_model = load_lstm_model()
cap = cv.VideoCapture(0)
sequence = []
threshold = 0.4
prev_frame_time = 0
new_frame_time = 0
while cap.isOpened():
ret, frame = cap.read()
new_frame_time = time.time()
cv.putText(image, 'Kursi roda aktif', (10,200), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv.LINE_AA)
image, results = media_pipe_detection(frame, holistic_model)
draw_land_marks(image, results)
keypoints = extract_keypoints_normalize(results)
sequence.append(keypoints)
sequence = sequence[-30:]
if len(sequence) == 30:
res = lstm_model.predict(np.expand_dims(sequence, axis=0))[0]
if res[np.argmax(res)] > threshold:
predicted_class = actions[np.argmax(res)]
if predicted_class == 'Kanan':
cv.putText(image, 'Kanan', (10, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv.LINE_AA)
arah = 'A\n'
s.send(arah.encode('utf-8'))
elif predicted_class == 'Maju':
cv.putText(image, 'Maju', (10, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv.LINE_AA)
arah = 'B\n'
s.send(arah.encode('utf-8'))
elif predicted_class == 'Stop':
cv.putText(image, 'Stop', (10, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv.LINE_AA)
arah = 'C\n'
s.send(arah.encode('utf-8'))
elif predicted_class == 'Mundur':
cv.putText(image, 'Mundur', (10, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2, cv.LINE_AA)
arah = 'D\n'
s.send(arah.encode('utf-8'))
elif predicted_class == 'Kiri':
cv.putText(image, 'Kiri', (10, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2, cv.LINE_AA)
arah = 'E\n'
s.send(arah.encode('utf-8'))
fps = 1 / (new_frame_time - prev_frame_time)
prev_frame_time = new_frame_time
cv.putText(image, f'FPS: {int(fps)}', (10, 100), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv.LINE_AA)
cv.imshow('Smart Wheelchair Control', image)
if cv.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()