-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
310 lines (244 loc) Β· 11.3 KB
/
Copy pathmain.py
File metadata and controls
310 lines (244 loc) Β· 11.3 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python3
"""
Real-time Sign Language to Speech System
Main pipeline entry point that coordinates detection, tracking, NLP, and TTS
"""
import os
import cv2
import time
import threading
from collections import defaultdict, deque
from datetime import datetime
#from sentence_transformers import SentenceTransformer
import pyttsx3
from components.yolo_inference import YOLO12Detector
from components.deep_sort_tracker import DeepSORTTracker
from components.sentence_builder import SentenceBuilder
from components.tts_engine import TTSEngine
from components.yolo2voice_pipeline import generate_sentence_with_ollama, text_to_speech
from utils.system_logger import SystemLogger
from utils.draw import DrawingUtils
from utils.video_utils import VideoProcessor
from components.yolo2voice_pipeline import yolo_classes_to_voice
class SignLanguageToSpeechSystem:
def __init__(self, model_path="models/sign.pt", confidence_threshold=0.5):
"""
Initialize the real-time sign language to speech system
"""
# Core components
self.yolo_detector = YOLO12Detector(model_path, confidence_threshold)
self.tracker = DeepSORTTracker()
self.sentence_builder = SentenceBuilder()
self.tts_engine = TTSEngine()
self.logger = SystemLogger()
# Utilities
self.drawer = DrawingUtils()
self.video_processor = VideoProcessor()
# Tracking buffers
self.track_buffers = defaultdict(lambda: deque(maxlen=10)) # Store last 10 signs per track
self.track_timestamps = defaultdict(float)
self.processed_sentences = set() # Avoid duplicate processing
# Configuration
self.buffer_timeout = 3.0 # Process buffer after 3 seconds of inactivity
self.min_signs_for_sentence = 2 # Minimum signs needed to form sentence
# Threading for TTS (non-blocking)
self.tts_queue = deque()
self.tts_thread = threading.Thread(target=self._tts_worker, daemon=True)
self.tts_thread.start()
print("π Sign Language to Speech System initialized!")
def _tts_worker(self):
"""Background worker for TTS processing"""
while True:
if self.tts_queue:
sentence = self.tts_queue.popleft()
try:
# Generate and save audio
audio_path = self.tts_engine.synthesize_speech(sentence)
print(f"π Audio saved: {audio_path}")
# Log the sentence
self.logger.log_sentence(sentence)
except Exception as e:
print(f"β TTS Error: {e}")
time.sleep(0.1)
def process_frame(self, frame):
"""
Process a single frame through the entire pipeline
"""
current_time = time.time()
# Step 1: YOLO Detection
detections = self.yolo_detector.detect(frame)
# Step 2: DeepSORT Tracking
tracks = self.tracker.update(detections, frame)
# Step 3: Update tracking buffers
active_track_ids = set()
for track in tracks:
track_id = track.track_id
class_name = track.class_name
confidence = track.confidence
active_track_ids.add(track_id)
# Add to buffer if confidence is high enough
if confidence > 0.7:
self.track_buffers[track_id].append({
'class': class_name,
'confidence': confidence,
'timestamp': current_time
})
self.track_timestamps[track_id] = current_time
# Step 4: Process buffers for sentence generation
self._process_track_buffers(current_time)
# Step 5: Draw annotations
annotated_frame = self.drawer.draw_tracks(frame, tracks)
# Step 6: Display current sentence being built
for track_id, buffer in self.track_buffers.items():
if buffer:
current_sequence = [item['class'] for item in buffer]
partial_sentence = self.sentence_builder.build_partial_sentence(current_sequence)
annotated_frame = self.drawer.draw_text(
annotated_frame,
f"Track {track_id}: {partial_sentence}",
position=(10, 30 + track_id * 25)
)
return annotated_frame
def _process_track_buffers(self, current_time):
"""
Process tracking buffers to generate sentences using Ollama
"""
tracks_to_process = []
for track_id, buffer in self.track_buffers.items():
if not buffer:
continue
last_update = self.track_timestamps.get(track_id, 0)
time_since_update = current_time - last_update
# Process if buffer has enough signs and hasn't been updated recently
if (len(buffer) >= self.min_signs_for_sentence and
time_since_update > self.buffer_timeout):
tracks_to_process.append(track_id)
# Process eligible tracks with Ollama
for track_id in tracks_to_process:
buffer = self.track_buffers[track_id]
sign_sequence = [item['class'] for item in buffer]
unique_signs = list(set(sign_sequence)) # Remove duplicates
for sign_class in unique_signs:
try:
# Generate sentence using Ollama
sentence = generate_sentence_with_ollama(sign_class)
if sentence and sentence.strip():
print(f"π― Generated sentence for '{sign_class}': '{sentence}'")
# Create audio file with class name
audio_filename = f"logs/audio_outputs/{sign_class}_{int(current_time)}.wav"
os.makedirs("logs/audio_outputs", exist_ok=True)
# Generate and save audio
text_to_speech(sentence, audio_filename)
print(f"π Audio saved: {audio_filename}")
# Also add to TTS queue for immediate playback
self.tts_queue.append(sentence)
except Exception as e:
print(f"β Error processing class '{sign_class}': {e}")
# Clear the buffer after processing
self.track_buffers[track_id].clear()
def _generate_sentence_from_buffer(self, track_id):
"""
Generate sentence from a track's buffer
"""
buffer = self.track_buffers[track_id]
if not buffer:
return
# Extract sign sequence
sign_sequence = [item['class'] for item in buffer]
# Create unique identifier for this sequence
sequence_id = f"{track_id}_{hash(tuple(sign_sequence))}"
# Skip if already processed
if sequence_id in self.processed_sentences:
return
# Build sentence
sentence = self.sentence_builder.build_sentence(sign_sequence)
if sentence and len(sentence.strip()) > 0:
print(f"π Generated sentence from track {track_id}: '{sentence}'")
# Add to TTS queue
self.tts_queue.append(sentence)
# Mark as processed
self.processed_sentences.add(sequence_id)
# Clear the buffer
self.track_buffers[track_id].clear()
def run(self, source=0):
"""
Run the real-time system
"""
print(f"π₯ Starting video capture from source: {source}")
cap = cv2.VideoCapture(source)
if not cap.isOpened():
print("β Error: Could not open video source")
return
# Set camera properties
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
cap.set(cv2.CAP_PROP_FPS, 30)
print("π΄ System running! Press 'q' to quit, 's' to save current frame")
frame_count = 0
start_time = time.time()
try:
while True:
ret, frame = cap.read()
if not ret:
print("β Error: Could not read frame")
break
# Process frame
processed_frame = self.process_frame(frame)
# Calculate and display FPS
frame_count += 1
if frame_count % 30 == 0:
elapsed = time.time() - start_time
fps = frame_count / elapsed
processed_frame = self.drawer.draw_text(
processed_frame,
f"FPS: {fps:.1f}",
position=(10, processed_frame.shape[0] - 30)
)
# Display frame
cv2.imshow('Sign Language to Speech System', processed_frame)
# Handle key presses
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('s'):
# Save current frame
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"logs/frame_{timestamp}.jpg"
cv2.imwrite(filename, processed_frame)
print(f"πΎ Frame saved: {filename}")
except KeyboardInterrupt:
print("\nβΉοΈ System stopped by user")
finally:
cap.release()
cv2.destroyAllWindows()
print("π System shutdown complete")
def process_and_generate_audio(input_path):
# Load image (for video, adapt as needed)
frame = cv2.imread(input_path)
if frame is None:
print(f"Could not load image: {input_path}")
return "Error: Could not load image."
# 1. Detect classes
# Use absolute path for the YOLO model
detector = YOLO12Detector("F:/NLPPRO/sign2speech/models/sign.pt")
detections = detector.detect(frame)
detected_classes = list(set([det.class_name for det in detections]))
print("Detected classes:", detected_classes)
if not detected_classes:
return "No classes detected."
# 2. Generate sentences and audio using Ollama + pyttsx3
yolo_classes_to_voice(detected_classes)
return f"Processed {len(detected_classes)} classes. Audio saved in 'voices/'."
# Example usage:
if __name__ == "__main__":
result = process_and_generate_audio("F:/NLPPRO/sign2speech/dataset/IMG_6633.jpg")
print(result)
# Main entry point
print("π― Initializing Sign Language to Speech System...")
# Initialize system
system = SignLanguageToSpeechSystem(
model_path="models/sign.pt",
confidence_threshold=0.5
)
# Run the system
system.run(source=0) # Use webcam (0) or video file path