-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
293 lines (247 loc) · 11.2 KB
/
Copy pathapp.py
File metadata and controls
293 lines (247 loc) · 11.2 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
import sys
import os
import cv2
import torch
import numpy as np
import pathlib
import threading
import base64
import time
import uuid
import tempfile
import logging
from concurrent.futures import ThreadPoolExecutor
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
from gtts import gTTS
# --- PATCH FOR PYTHON 3.13 & NUMPY 2.0 ---
if not hasattr(np, 'int'):
np.int = int
if not hasattr(np, 'float'):
np.float = float
if not hasattr(np, 'bool'):
np.bool = bool
# --- FIX WINDOWS PATHS ---
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath
# --- SETUP YOLOv5 PATHS (CRITICAL FIX) ---
# This tells Python: "The code you need is inside the 'yolov5' folder"
yolov5_path = os.path.join(os.getcwd(), 'yolov5')
if yolov5_path not in sys.path:
sys.path.append(yolov5_path)
# --- IMPORT AI LIBRARIES ---
print(f"Checking for YOLOv5 at: {yolov5_path}")
try:
from ultralytics import YOLO
# Now we import directly from the 'models' folder inside yolov5
from models.experimental import attempt_load
from utils.general import non_max_suppression
from utils.torch_utils import select_device
print("✅ AI Libraries imported successfully!")
except ImportError as e:
print(f"\n❌ CRITICAL IMPORT ERROR: {e}")
print("----------------------------------------------------")
print(f"1. Is the folder named 'yolov5' inside '{os.getcwd()}'?")
print("2. Does 'yolov5/models/experimental.py' exist?")
print("3. Did you run 'pip install -r yolov5/requirements.txt'?")
print("----------------------------------------------------")
sys.exit(1)
# --- Configuration ---
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("SignLingo")
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, cors_allowed_origins="*", async_mode='threading')
# --- Main Logic Class ---
class SignLanguageSystem:
def __init__(self):
self.camera = None
self.is_running = False
self.mode = "asl" # 'asl' or 'isl'
# State tracking
self.detected_chars = []
self.current_sentence = ""
self.last_audio_time = 0
self.audio_cooldown = 4.0 # Seconds to wait before speaking again
# Background worker for audio (Prevents video freeze)
self.audio_executor = ThreadPoolExecutor(max_workers=1)
# Load Models
self.device = select_device('0' if torch.cuda.is_available() else 'cpu')
self.models = {}
self.load_models()
def load_models(self):
"""Loads models safely with error handling"""
try:
# UPDATE PATHS: Pointing to the 'models' folder
logger.info("Loading YOLOv8 (ASL)...")
asl_path = os.path.join("models", "yolov8_asl_best.pt")
if os.path.exists(asl_path):
self.models['asl'] = YOLO(asl_path)
else:
logger.error(f"❌ ASL Model missing: {asl_path}")
logger.info("Loading YOLOv5 (ISL)...")
isl_path = os.path.join("models", "yolov5_isl_best.pt")
if os.path.exists(isl_path):
self.models['isl'] = attempt_load(isl_path, device=self.device)
# Get class names for ISL
try:
self.isl_names = self.models['isl'].module.names if hasattr(self.models['isl'], 'module') else self.models['isl'].names
except:
self.isl_names = [f"Class_{i}" for i in range(100)]
else:
logger.error(f"❌ ISL Model missing: {isl_path}")
logger.info(f"✅ Models loaded. Device: {self.device}")
except Exception as e:
logger.error(f"❌ Critical Error loading models: {e}")
import traceback
traceback.print_exc()
def start_camera(self):
if self.camera is None or not self.camera.isOpened():
self.camera = cv2.VideoCapture(0)
if not self.camera.isOpened():
logger.error("Could not open webcam.")
return False
self.is_running = True
return True
def stop_camera(self):
self.is_running = False
if self.camera:
self.camera.release()
self.camera = None
def process_text_logic(self, raw_text):
"""Converts raw labels like 'HELLO' into sentences"""
mappings = {
"HELLO": "Hello, nice to meet you",
"THANK": "Thank you very much",
"HELP": "I need help please",
"YES": "Yes, that is correct",
"NO": "No, I don't think so"
}
return mappings.get(raw_text, raw_text)
def generate_and_send_audio(self, text):
"""Runs in background thread. Does NOT block video."""
try:
tts = gTTS(text=text, lang='en')
filename = f"audio_{uuid.uuid4()}.mp3"
filepath = os.path.join(tempfile.gettempdir(), filename)
tts.save(filepath)
with open(filepath, "rb") as audio_file:
audio_b64 = base64.b64encode(audio_file.read()).decode('utf-8')
socketio.emit('audio_update', {'audio_data': audio_b64})
os.remove(filepath) # Cleanup
logger.info(f"Audio sent for: {text}")
except Exception as e:
logger.error(f"TTS Error: {e}")
def detect_loop(self):
"""Main Loop: Reads Camera -> Predicts -> Emits Results"""
logger.info("Starting detection loop")
while self.is_running and self.camera:
success, frame = self.camera.read()
if not success:
break
annotated_frame = frame.copy()
detection_text = ""
conf_score = 0.0
# --- ASL LOGIC (YOLOv8) ---
if self.mode == 'asl' and 'asl' in self.models:
try:
results = self.models['asl'](frame, verbose=False, conf=0.5)
for r in results:
annotated_frame = r.plot()
if len(r.boxes) > 0:
box = r.boxes[0]
detection_text = self.models['asl'].names[int(box.cls)]
conf_score = float(box.conf)
except Exception as e:
logger.error(f"ASL Prediction Error: {e}")
# --- ISL LOGIC (YOLOv5) ---
elif self.mode == 'isl' and 'isl' in self.models:
try:
# Preprocess
img = cv2.resize(frame, (640, 640))
img = img.transpose((2, 0, 1))[::-1]
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(self.device).float() / 255.0
if img.ndimension() == 3: img = img.unsqueeze(0)
# Inference
pred = self.models['isl'](img)[0]
pred = non_max_suppression(pred, 0.25, 0.45)
for det in pred:
if len(det):
det[:, :4] = self.scale_coords(img.shape[2:], det[:, :4], frame.shape).round()
for *xyxy, conf, cls in det:
label = f'{self.isl_names[int(cls)]}'
# Draw Box
cv2.rectangle(annotated_frame, (int(xyxy[0]), int(xyxy[1])), (int(xyxy[2]), int(xyxy[3])), (0, 255, 0), 2)
cv2.putText(annotated_frame, label, (int(xyxy[0]), int(xyxy[1])-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
if conf > 0.5:
detection_text = self.isl_names[int(cls)]
conf_score = float(conf)
except Exception as e:
logger.error(f"ISL Prediction Error: {e}")
# --- LOGIC ---
processed_text = ""
if detection_text:
# Debounce: Only accept if different from last or 1 sec passed
if not self.detected_chars or self.detected_chars[-1] != detection_text:
self.detected_chars.append(detection_text)
if len(self.detected_chars) > 5: self.detected_chars.pop(0)
processed_text = self.process_text_logic(detection_text)
# Audio Trigger (Threaded)
if time.time() - self.last_audio_time > self.audio_cooldown:
self.last_audio_time = time.time()
self.audio_executor.submit(self.generate_and_send_audio, processed_text)
# --- STREAMING ---
try:
_, buffer = cv2.imencode('.jpg', annotated_frame, [int(cv2.IMWRITE_JPEG_QUALITY), 70])
frame_b64 = base64.b64encode(buffer).decode('utf-8')
socketio.emit('video_frame', {'frame': frame_b64})
if detection_text:
socketio.emit('detection_update', {
'raw_detection': detection_text,
'processed_text': processed_text if processed_text else "...",
'confidence': conf_score
})
except Exception as e:
logger.error(f"Streaming Error: {e}")
socketio.sleep(0.01) # Yield to other threads
def scale_coords(self, img1_shape, coords, img0_shape):
gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1])
pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2
coords[:, [0, 2]] -= pad[0]
coords[:, [1, 3]] -= pad[1]
coords[:, :4] /= gain
coords[:, :4] = coords[:, :4].clip(min=0)
return coords
# --- Initialize ---
system = SignLanguageSystem()
# --- Routes ---
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('connect')
def connect():
emit('status', {'message': 'Server Connected'})
@socketio.on('start_detection')
def start():
if system.start_camera():
emit('status', {'message': 'Camera Started'})
socketio.start_background_task(system.detect_loop)
else:
emit('status', {'message': 'Camera Failed'})
@socketio.on('stop_detection')
def stop():
system.stop_camera()
emit('status', {'message': 'Camera Stopped'})
@socketio.on('set_mode')
def set_mode(data):
system.mode = data.get('mode', 'asl')
system.detected_chars = []
emit('status', {'message': f'Switched to {system.mode.upper()}'})
@socketio.on('reset_detection')
def reset():
system.detected_chars = []
emit('detection_update', {'raw_detection': '', 'processed_text': '', 'confidence': 0})
if __name__ == '__main__':
os.makedirs('templates', exist_ok=True)
socketio.run(app, debug=True, port=5000)