-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathver0beta.py
130 lines (103 loc) · 4.87 KB
/
ver0beta.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
import cv2 as cv
import numpy as np
import os
import mediapipe as mp
import time
import math
from tensorflow.keras.models import load_model
# Inisialisasi model Mediapipe untuk mendeteksi pose dan landmark
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)
# Kelas yang digunakan: Kanan, Maju, Stop, Mundur, Kiri
actions = np.array(['Kanan', 'Maju', 'Stop', 'Mundur', 'Kiri'])
sequence = []
predictions = []
# Fungsi untuk melakukan deteksi dengan MediaPipe
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
# Fungsi menggambar landmark pada gambar
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)
# Fungsi untuk ekstraksi dan normalisasi keypoints
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])
# Fungsi untuk memuat model LSTM yang sudah dilatih
def load_lstm_model():
# Muat model lengkap dengan arsitektur dan bobot dari file .keras
model = load_model('agung.keras') # Ganti dengan path model LSTM kamu
return model
# Muat model
lstm_model = load_lstm_model()
# Proses video dari kamera
cap = cv.VideoCapture(0)
sequence = []
threshold = 0.4
while cap.isOpened():
ret, frame = cap.read()
# Deteksi pose dan landmark menggunakan MediaPipe
image, results = media_pipe_detection(frame, holistic_model)
draw_land_marks(image, results)
# Ekstraksi keypoints dan masukkan ke dalam sequence
keypoints = extract_keypoints_normalize(results)
sequence.append(keypoints)
sequence = sequence[-30:]
# Prediksi jika sequence sudah lengkap (30 frame)
if len(sequence) == 30:
res = lstm_model.predict(np.expand_dims(sequence, axis=0))[0]
# Jika confidence lebih dari threshold, tampilkan prediksi
if res[np.argmax(res)] > threshold:
predicted_class = actions[np.argmax(res)]
# Tampilkan teks berdasarkan prediksi
if predicted_class == 'Kanan':
cv.putText(image, 'Kanan', (10, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv.LINE_AA)
elif predicted_class == 'Maju':
cv.putText(image, 'Maju', (10, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv.LINE_AA)
elif predicted_class == 'Stop':
cv.putText(image, 'Stop', (10, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv.LINE_AA)
elif predicted_class == 'Mundur':
cv.putText(image, 'Mundur', (10, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2, cv.LINE_AA)
elif predicted_class == 'Kiri':
cv.putText(image, 'Kiri', (10, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2, cv.LINE_AA)
# Tampilkan frame
cv.imshow('Smart Wheelchair Control', image)
if cv.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()