amtsal commited on
Commit
822c28c
·
1 Parent(s): 74a4c92

feat: add fp

Browse files
Files changed (2) hide show
  1. app.py +135 -0
  2. label.txt +1 -0
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import mediapipe as mp
4
+ import tensorflow as tf
5
+ from tensorflow.keras.models import Model
6
+ import gradio as gr
7
+
8
+ # Load model architecture and weights (replace with your actual model setup)
9
+ # def build_bilstm_model(num_vocabs=13, num_frames=19, num_landmarks=42):
10
+ # # Your model architecture here
11
+ # pass
12
+
13
+ # model = build_bilstm_model()
14
+ # model.load_weights('final_model.keras')
15
+
16
+
17
+ model = tf.keras.models.load_model('final_model.keras', compile=False)
18
+
19
+ # Load labels
20
+ with open('label.txt', 'r') as f:
21
+ label = f.readline().split()
22
+
23
+ # MediaPipe setup
24
+ mp_holistic = mp.solutions.holistic
25
+ mp_drawing = mp.solutions.drawing_utils
26
+ holistic = mp_holistic.Holistic(
27
+ min_detection_confidence=0.5,
28
+ min_tracking_confidence=0.5
29
+ )
30
+
31
+ # Global state
32
+ is_recording = False
33
+ sequence = []
34
+ sentence = []
35
+ sequence_length = 19
36
+ threshold = 0.5
37
+
38
+ # Landmark drawing styles
39
+ STYLES = {
40
+ "left_hand": ((121, 22, 76), (121, 44, 250), 2, 4),
41
+ "right_hand": ((245, 117, 66), (245, 66, 230), 2, 4)
42
+ }
43
+
44
+ def mediapipe_detection(image, model):
45
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
46
+ image.flags.writeable = False
47
+ results = model.process(image)
48
+ image.flags.writeable = True
49
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
50
+ return image, results
51
+
52
+ def draw_styled_landmarks(image, results):
53
+ for landmark_type in ["left_hand", "right_hand"]:
54
+ landmarks, connections = (
55
+ (results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS) if landmark_type == "left_hand"
56
+ else (results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS)
57
+ )
58
+ if landmarks:
59
+ color, connection_color, thickness, radius = STYLES[landmark_type]
60
+ mp_drawing.draw_landmarks(
61
+ image,
62
+ landmarks,
63
+ connections,
64
+ mp_drawing.DrawingSpec(color=color, thickness=thickness, circle_radius=radius),
65
+ mp_drawing.DrawingSpec(color=connection_color, thickness=thickness, circle_radius=radius//2)
66
+ )
67
+
68
+ def extract_landmarks(results):
69
+ lh = np.array([[res.x, res.y, res.z] for res in results.left_hand_landmarks.landmark]).flatten() if results.left_hand_landmarks else np.zeros(21*3)
70
+ rh = np.array([[res.x, res.y, res.z] for res in results.right_hand_landmarks.landmark]).flatten() if results.right_hand_landmarks else np.zeros(21*3)
71
+ return np.concatenate([lh, rh])
72
+
73
+ def process_frame(image):
74
+ global is_recording, sequence, sentence
75
+
76
+ if is_recording:
77
+ # Process frame with MediaPipe
78
+ image, results = mediapipe_detection(image, holistic)
79
+ draw_styled_landmarks(image, results)
80
+
81
+ # Extract landmarks and update sequence
82
+ landmarks = extract_landmarks(results)
83
+ sequence.append(landmarks)
84
+ sequence = sequence[-30:] # Keep last 30 frames
85
+
86
+ # Make prediction when sequence is complete
87
+ if len(sequence) == sequence_length:
88
+ res = model.predict(np.expand_dims(sequence, axis=0))[0]
89
+ predicted_label = label[np.argmax(res)]
90
+ confidence = np.max(res)
91
+
92
+ if confidence > threshold:
93
+ if not sentence or sentence[-1] != predicted_label:
94
+ sentence.append(predicted_label)
95
+ sentence = sentence[-5:] # Keep last 5 predictions
96
+
97
+ sequence = [] # Reset sequence
98
+
99
+ # Draw prediction text
100
+ cv2.rectangle(image, (0,0), (640, 40), (245, 117, 16), -1)
101
+ cv2.putText(image, ' '.join(sentence), (3,30),
102
+ cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
103
+ else:
104
+ # Draw instruction text
105
+ cv2.rectangle(image, (50, 50), (380, 100), (0, 255, 0), -1)
106
+ cv2.putText(image, "Press START to begin", (60,85),
107
+ cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255), 2)
108
+
109
+ return image
110
+
111
+ def toggle_recording():
112
+ global is_recording
113
+ is_recording = not is_recording
114
+ return is_recording
115
+
116
+ with gr.Blocks() as demo:
117
+ gr.Markdown("# Sign Language Detection 👐")
118
+ gr.Markdown("Aplikasi deteksi bahasa isyarat menggunakan MediaPipe dan TensorFlow")
119
+
120
+ with gr.Row():
121
+ webcam = gr.Image(label="Webcam Input", source="webcam", streaming=True)
122
+ output = gr.Image(label="Processed Output")
123
+
124
+ btn = gr.Button("Start/Stop Recording")
125
+ btn.click(toggle_recording)
126
+
127
+ webcam.stream(
128
+ fn=process_frame,
129
+ inputs=webcam,
130
+ outputs=output,
131
+ every=0.1
132
+ )
133
+
134
+ if __name__ == "__main__":
135
+ demo.launch()
label.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ hai nama kamu pagi siang malam siapa sudah belum makan suka selamat aku