File size: 30,057 Bytes
e2cffd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
"""

ISL Sign Language Translation - TechMatrix Solvers Initiative

Main Streamlit Application



Developed by: TechMatrix Solvers Team

- Abhay Gupta (Team Lead)

- Kripanshu Gupta (Backend Developer) 

- Dipanshu Patel (UI/UX Designer)

- Bhumika Patel (Deployment & Female Presenter)



Institution: Shri Ram Group of Institutions

"""

import streamlit as st
st.write("πŸš€ TechMatrix Solvers ISL Translator Loading...")

import os
os.environ["KERAS_BACKEND"] = "torch"
import keras

import cv2
import numpy as np
import tempfile
import time
from PIL import Image
from keras.models import Sequential
import pickle
from keras.layers import LSTM, Dense, Bidirectional, Dropout, Input, BatchNormalization
from pose_models import create_bodypose_model, create_handpose_model
from expression_mapping import expression_mapping
from isl_processor import ISLTranslationModel
import pandas as pd
import ffmpeg
import subprocess
from typing import NamedTuple
import json
import pose_utils as utils
from huggingface_hub import hf_hub_download
import shutil, platform
import uuid

# System information display
st.write("πŸ”§ **System Information:**")
st.write(f"Python Version: {platform.python_version()}")
st.write(f"FFmpeg: {shutil.which('ffmpeg')}, FFprobe: {shutil.which('ffprobe')}")

try:
    import cv2
    st.write(f"OpenCV Version: {cv2.__version__}")
except Exception as e:
    st.error(f"OpenCV import failed: {e}")

try:
    import torch
    st.write(f"PyTorch: {torch.__version__}, Keras: {keras.__version__}")
except Exception as e:
    st.error(f"PyTorch/Keras import failed: {e}")


class VideoProbeResult(NamedTuple):
    """Structure for video probe results"""
    return_code: int
    json: str
    error: str


def probe_video_info(file_path) -> VideoProbeResult:
    """

    Probe video file for metadata using FFprobe

    

    Args:

        file_path: Path to video file

        

    Returns:

        VideoProbeResult containing metadata

    """
    command_array = [
        "ffprobe",
        "-v", "quiet",
        "-print_format", "json",
        "-show_format",
        "-show_streams",
        file_path
    ]
    result = subprocess.run(
        command_array, 
        stdout=subprocess.PIPE, 
        stderr=subprocess.PIPE, 
        universal_newlines=True
    )
    return VideoProbeResult(
        return_code=result.returncode,
        json=result.stdout,
        error=result.stderr
    )


# Define feature columns for time series processing
body_features = [f'bodypeaks_x_{i}' for i in range(15)] + [f'bodypeaks_y_{i}' for i in range(15)]
hand0_features = [f'hand0peaks_x_{i}' for i in range(21)] + [f'hand0peaks_y_{i}' for i in range(21)] + [f'hand0peaks_peaktxt{i}' for i in range(21)]
hand1_features = [f'hand1peaks_x_{i}' for i in range(21)] + [f'hand1peaks_y_{i}' for i in range(21)] + [f'hand1peaks_peaktxt{i}' for i in range(21)]

feature_columns_processed = body_features + hand0_features + hand1_features
label_columns = ['Expression_encoded']


@st.cache_resource
def create_time_series_sequences(isl_data, feature_columns, label_columns, window_size=20):
    """

    Creates time series sequences from DataFrame with specified window size

    

    Args:

        isl_data: Input DataFrame with ISL data

        feature_columns: List of feature column names

        label_columns: List of label column names  

        window_size: Size of temporal window for sequence creation

        

    Returns:

        tuple: (X_sequences, y_sequences) for training/inference

    """
    if isl_data.empty:
        return [], []

    X_sequences = []
    y_sequences = []
    
    for group, file_df in isl_data.groupby(['Type', 'Expression_encoded', 'FileName']):
        expr_type, expression, filename = group
        
        # Create blank frame for padding
        blank_frame = np.zeros((1, 156))
        
        for idx, window_data in enumerate([file_df[i:i+window_size] for i in range(0, file_df.shape[0], 1)]):
            if window_data.shape[0] < window_size:
                # Pad sequence with blank frames at the beginning
                padding_needed = window_size - window_data.shape[0]
                padded_sequence = np.concatenate(
                    (np.repeat(blank_frame, padding_needed, axis=0), 
                     window_data[feature_columns].values), 
                    axis=0
                )
                X_sequences.append(padded_sequence)
                y_sequences.append(expression)
                continue
            
            X_sequences.append(window_data[feature_columns].values)
            y_sequences.append(expression)

    return X_sequences, y_sequences


# Global translation model variable
translation_model = None


@st.cache_resource
def load_translation_model():
    """

    Load and configure the LSTM translation model

    

    Returns:

        Configured Keras Sequential model for ISL translation

    """
    model = Sequential()
    model.add(Input(shape=((20, 156))))
    model.add(keras.layers.Masking(mask_value=0.))
    model.add(BatchNormalization())
    model.add(Bidirectional(LSTM(32, recurrent_dropout=0.2, return_sequences=True)))
    
    model.add(Dropout(0.2))
    model.add(Bidirectional(LSTM(32, recurrent_dropout=0.2)))
    
    model.add(keras.layers.Activation('elu'))
    model.add(Dense(32, use_bias=False, kernel_initializer='he_normal'))
    
    model.add(BatchNormalization())
    model.add(Dropout(0.2))
    model.add(keras.layers.Activation('elu'))
    model.add(Dense(32, kernel_initializer='he_normal', use_bias=False))
    
    model.add(BatchNormalization())
    model.add(keras.layers.Activation('elu'))
    model.add(Dropout(0.2))
    model.add(Dense(len(list(expression_mapping.keys())), activation='softmax'))
    
    # Download pre-trained model weights
    model_file = hf_hub_download(
        repo_id="sunilsarolkar/isl-translation-model",
        filename="isl_model_final.keras"
    )
    model.load_weights(model_file)
    
    return model


# Load test data
@st.cache_data
def load_test_data():
    """Load test dataset and file information"""
    testing_cleaned_path = hf_hub_download(
        repo_id="sunilsarolkar/isl-test-data",
        filename="testing_cleaned.csv",
        repo_type="dataset"
    )
    
    test_files_path = hf_hub_download(
        repo_id="sunilsarolkar/isl-test-data", 
        filename="test_files.csv",
        repo_type="dataset"
    )
    
    testing_df = pd.read_csv(testing_cleaned_path)
    test_files_df = pd.read_csv(test_files_path)
    
    return testing_df, test_files_df


# Load test data
testing_df, test_files_df = load_test_data()


class VideoWriter:
    """Custom video writer using FFmpeg for better compatibility"""
    
    def __init__(self, output_file, input_fps, input_framesize, input_pix_fmt, input_vcodec):
        self.ff_process = (
            ffmpeg
            .input('pipe:',
                   format='rawvideo',
                   pix_fmt="bgr24",
                   s=f'{input_framesize[1]}x{input_framesize[0]}',
                   r=input_fps)
            .output(output_file, pix_fmt=input_pix_fmt, vcodec=input_vcodec)
            .overwrite_output()
            .run_async(pipe_stdin=True)
        )

    def write_frame(self, frame):
        """Write a single frame to the video"""
        self.ff_process.stdin.write(frame.tobytes())

    def close(self):
        """Close the video writer"""
        self.ff_process.stdin.close()
        self.ff_process.wait()


def calculate_weighted_average(numbers, weights):
    """

    Calculate weighted average of numbers

    

    Args:

        numbers: List of numbers

        weights: List of weights

        

    Returns:

        float: Weighted average

    """
    if sum(weights) == 0:
        return 0
    return sum(x * y for x, y in zip(numbers, weights)) / sum(weights)


@st.cache_data
def resize_image(image, width=None, height=None, interpolation=cv2.INTER_AREA):
    """

    Resize image maintaining aspect ratio

    

    Args:

        image: Input image

        width: Target width

        height: Target height

        interpolation: OpenCV interpolation method

        

    Returns:

        Resized image

    """
    dimensions = None
    (h, w) = image.shape[:2]

    if width is None and height is None:
        return image

    if width is None:
        ratio = height / float(h)
        dimensions = (int(w * ratio), height)
    else:
        ratio = width / float(w)
        dimensions = (width, int(h * ratio))

    resized = cv2.resize(image, dimensions, interpolation=interpolation)
    return resized


# Configure Streamlit page
st.set_page_config(
    page_title="ISL Translation - TechMatrix Solvers",
    page_icon="🀟",
    layout="wide"
)

st.title('🀟 ISL Sign Language Translation - TechMatrix Solvers Initiative')

# Add custom CSS for sidebar styling
st.markdown(
    """

    <style>

    [data-testid="stSidebar"][aria-expanded="true"] > div:first-child {

        width: 350px;

    }

    [data-testid="stSidebar"][aria-expanded="false"] > div:first-child {

        width: 350px;

        margin-left: -350px;

    }

    

    .team-info {

        background-color: #f0f2f6;

        padding: 1rem;

        border-radius: 0.5rem;

        margin: 1rem 0;

    }

    

    .tech-matrix-header {

        background: linear-gradient(90deg, #1e3a8a, #7c3aed);

        color: white;

        padding: 1rem;

        border-radius: 0.5rem;

        text-align: center;

        margin-bottom: 1rem;

    }

    </style>

    """,
    unsafe_allow_html=True,
)

# Add team branding header
st.markdown(
    """

    <div class="tech-matrix-header">

        <h2>πŸš€ TechMatrix Solvers</h2>

        <p>Innovating Accessible Technology Solutions</p>

    </div>

    """, 
    unsafe_allow_html=True
)

# Sidebar configuration
st.sidebar.title('🀟 ISL Translation System')
st.sidebar.subheader('Configuration')

# Team information in sidebar
st.sidebar.markdown(
    """

    <div class="team-info">

    <h3>πŸ‘¨β€πŸ’» Development Team</h3>

    <ul>

    <li><strong>Abhay Gupta</strong> - Team Lead</li>

    <li><strong>Kripanshu Gupta</strong> - Backend Dev</li>

    <li><strong>Dipanshu Patel</strong> - UI/UX Designer</li>

    <li><strong>Bhumika Patel</strong> - Deployment</li>

    </ul>

    <p><em>Shri Ram Group of Institutions</em></p>

    </div>

    """, 
    unsafe_allow_html=True
)

# Initialize frame-wise outputs storage
frame_predictions = {}

# Application mode selection
app_mode = st.sidebar.selectbox(
    'Choose Application Mode',
    ['About Project', 'Test Video Translation']
)

if app_mode == 'About Project':
    st.markdown(
        """

        ## 🎯 Project Overview

        

        Welcome to the **ISL Sign Language Translation System** developed by **TechMatrix Solvers**. 

        This cutting-edge application demonstrates real-time Indian Sign Language recognition and 

        translation using advanced deep learning techniques.

        

        ### πŸ—οΈ Technical Architecture

        

        Our system combines multiple state-of-the-art technologies:

        

        1. **Body Pose Estimation**: 25-point skeletal tracking using OpenPose

        2. **Hand Landmark Detection**: 21-point hand keypoint identification  

        3. **Temporal Modeling**: Bidirectional LSTM networks for sequence analysis

        4. **Real-time Processing**: Optimized inference pipeline for live translation

        """
    )
    
    st.markdown(
        """

        ### πŸ“Š Dataset Information

        

        Our model is trained on the comprehensive [INCLUDE dataset](https://zenodo.org/records/4010759):

        """
    )
    
    # Dataset statistics table
    dataset_stats = {
        "Metric": [
            "Categories", "Total Words", "Training Videos", 
            "Avg Videos/Class", "Avg Video Length", "Resolution", "Frame Rate"
        ],
        "Value": [
            "15", "263", "4,257", "16.3", "2.57s", "1920x1080", "25fps"
        ]
    }
    st.table(pd.DataFrame(dataset_stats))
    
    # Display dataset processing visualization
    try:
        categories_image = np.array(Image.open('original_project/categories_processed.png'))
        st.image(categories_image, caption="πŸ“ˆ Processed Categories Distribution")
    except:
        st.info("πŸ“Š Dataset visualization images will be displayed when available")
    
    # Model architecture information
    st.markdown(
        """

        ### 🧠 Neural Network Architecture

        

        ```python

        # TechMatrix Solvers LSTM Translation Model

        model = Sequential([

            Input(shape=(20, 156)),  # 20-frame temporal window

            Masking(mask_value=0.),

            BatchNormalization(),

            Bidirectional(LSTM(32, recurrent_dropout=0.2, return_sequences=True)),

            Dropout(0.2),

            Bidirectional(LSTM(32, recurrent_dropout=0.2)),

            Dense(32, activation='elu'),

            BatchNormalization(), 

            Dropout(0.2),

            Dense(len(expression_mapping), activation='softmax')

        ])

        ```

        

        **Model Statistics:**

        - Total Parameters: 82,679 (322.96 KB)

        - Trainable Parameters: 82,239 (321.25 KB) 

        - Input Features: 156-dimensional vectors

        - Temporal Window: 20 frames

        """
    )
    
    # Technology stack
    col1, col2 = st.columns(2)
    
    with col1:
        st.markdown(
            """

            ### πŸ› οΈ Technology Stack

            

            **Frontend & UI:**

            - Streamlit (Interactive Web App)

            - Custom CSS Styling

            - Responsive Design

            

            **Deep Learning:**

            - Keras/TensorFlow Backend

            - PyTorch Integration

            - LSTM Networks

            - OpenPose Models

            """
        )
    
    with col2:
        st.markdown(
            """

            ### πŸ“± Key Features

            

            **Real-time Processing:**

            - Live video analysis

            - Pose keypoint extraction

            - Temporal sequence modeling

            - Confidence scoring

            

            **User Experience:**

            - Intuitive interface

            - Visual feedback

            - Progress tracking

            - Result visualization

            """
        )
    
    # Team contact information
    st.markdown(
        """

        ### πŸ“ž Contact Information

        

        **TechMatrix Solvers Team:**

        

        | Name | Role | Email | Phone |

        |------|------|-------|--------|

        | **Abhay Gupta** | Team Lead | contact2abhaygupta6187@gmail.com | 8115814535 |

        | **Kripanshu Gupta** | Backend Developer | guptakripanshu83@gmail.com | 7067058400 |

        | **Dipanshu Patel** | UI/UX Designer | dipanshupatel43@gmail.com | 9294526404 |

        | **Bhumika Patel** | Deployment & Presenter | bp7249951@gmail.com | 9302271422 |

        

        **Institution:** Shri Ram Group of Institutions

        

        ### πŸ“š Documentation

        

        For detailed technical documentation and implementation details, please refer to our 

        [comprehensive documentation](https://docs.google.com/document/d/1mzr2KGHRJT5heUjFF20NQ3Gb89urpjZJ/edit?usp=sharing).

        

        ---

        

        **Β© 2024 TechMatrix Solvers - Innovating Accessible Technology Solutions**

        """
    )

elif app_mode == 'Test Video Translation':
    # Video selection interface
    st.markdown("## πŸŽ₯ Test Video Translation")
    
    category = st.sidebar.selectbox(
        'Choose Category',
        np.sort(test_files_df['Category'].unique(), axis=-1, kind='mergesort')
    )
    
    # Filter by category
    category_mask = (test_files_df['Category'] == category)
    test_files_category = test_files_df[category_mask]
    
    class_name = st.sidebar.selectbox(
        'Choose Class',
        np.sort(test_files_category['Class'].unique(), axis=-1, kind='mergesort')
    )
    
    # Filter by class
    class_mask = (test_files_df['Class'] == class_name)
    filename = st.sidebar.selectbox(
        'Choose File',
        np.sort(test_files_category[class_mask]['Filename'].unique(), axis=-1, kind='mergesort')
    )
    
    # Display selection info
    st.info(f"πŸ“‚ Selected: {category} β†’ {class_name} β†’ {filename}")
    
    if st.sidebar.button("πŸš€ Start Translation", type="primary"):
        # Filter test data for selected video
        data_mask = ((testing_df['FileName'] == filename) & 
                    (testing_df['Type'] == category) & 
                    (testing_df['Expression'] == class_name))
        
        window_size = 20
        current_test_data = testing_df[data_mask]

        if current_test_data.empty:
            st.error(f"⚠️ No matching data found for: {filename} | {category} | {class_name}")
            st.stop()
        else:
            st.success(f"βœ… Loaded {current_test_data.shape[0]} frames for processing")
        
        # Create time series data
        X_test_processed, y_test_processed = create_time_series_sequences(
            current_test_data, feature_columns_processed, label_columns, window_size=window_size
        )
        X_test_processed = np.array(X_test_processed)

        # Configure Streamlit display options
        st.set_option('deprecation.showfileUploaderEncoding', False)

        st.sidebar.markdown('---')
        st.markdown(
            """

            <style>

            [data-testid="stSidebar"][aria-expanded="true"] > div:first-child {

                width: 400px;

            }

            [data-testid="stSidebar"][aria-expanded="false"] > div:first-child {

                width: 400px;

                margin-left: -400px;

            }

            </style>

            """,
            unsafe_allow_html=True,
        )

        st.sidebar.markdown('---')
        st.markdown('## πŸ“Š Translation Results')

        # Progress tracking container
        progress_container = st.empty()

        with progress_container.container():
            progress_df = pd.DataFrame([['--', '--']], 
                                     columns=['Frames Processed', 'Detected Sign'])
            progress_table = st.table(progress_df)
            
        # Video display container
        video_display = st.empty()
        st.markdown("<hr/>", unsafe_allow_html=True)
        frame_display = st.empty()

        # Download test video
        video_file_path = hf_hub_download(
            repo_id="sunilsarolkar/isl-test-data",
            filename=f'test/{category}/{class_name}/{filename}',
            repo_type="dataset"
        )

        if not os.path.exists(video_file_path):
            st.error(f"⚠️ Video file not found: {video_file_path}")
            st.stop()

        # Initialize video capture
        video_capture = cv2.VideoCapture(video_file_path)

        # Get video metadata
        probe_result = probe_video_info(video_file_path)
        video_info = json.loads(probe_result.json)
        video_stream = [stream for stream in video_info["streams"] if stream["codec_type"] == "video"][0]
        
        input_fps = video_stream["avg_frame_rate"]
        input_pix_fmt = video_stream["pix_fmt"]
        input_vcodec = video_stream["codec_name"]
        format_name = video_info["format"]["format_name"].split(",")[0]

        # Video properties
        width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps_input = int(video_capture.get(cv2.CAP_PROP_FPS))
        
        # Processing variables
        total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
        frame_buffer = []
        
        # Output video configuration
        output_file = f"/tmp/techmatrix_output_{uuid.uuid4().hex}.{format_name}"
        video_writer = None
        weighted_predictions = {}
        
        frame_idx = 0
        
        try:
            # Process each frame
            for _, frame_data in current_test_data.iterrows():
                if not video_capture.isOpened():
                    st.error(f"❌ Could not open video: {video_file_path}")
                    break
                
                if video_capture.isOpened():
                    ret, frame = video_capture.read()
                    
                    if len(frame_buffer) < window_size:
                        # Initial frames - build up buffer
                        visualization_canvas = utils.render_stick_model(
                            frame,
                            eval(frame_data['bodypose_circles']),
                            eval(frame_data['bodypose_sticks']),
                            eval(frame_data['handpose_edges']),
                            eval(frame_data['handpose_peaks'])
                        )
                        
                        # Add prediction plots
                        canvas_with_predictions = utils.create_bar_plot_visualization(
                            visualization_canvas, {}, 
                            f'Building Buffer - Frame {frame_idx + 1} [No Predictions Yet]',
                            visualization_canvas
                        )
                        canvas_with_predictions = utils.create_bar_plot_visualization(
                            canvas_with_predictions, weighted_predictions,
                            f'Weighted Average - Frame {frame_idx + 1} [No Predictions Yet]',
                            visualization_canvas
                        )
                        canvas_with_predictions = utils.add_bottom_padding(
                            canvas_with_predictions, (255, 255, 255), 100
                        )
                        
                        # Initialize video writer
                        if video_writer is None:
                            input_framesize = canvas_with_predictions.shape[:2]
                            video_writer = VideoWriter(output_file, input_fps, input_framesize, 
                                                     input_pix_fmt, input_vcodec)

                        video_writer.write_frame(canvas_with_predictions)
                        
                        # Update progress display
                        with progress_container.container():
                            progress_df = pd.DataFrame(
                                [[f'{frame_idx + 1}/{current_test_data.shape[0]}', 
                                  '<Building 20-frame buffer>']],
                                columns=['Frames Processed', 'Detected Sign']
                            )
                            progress_table = st.table(progress_df)
                            
                        frame_buffer.append(frame)
                        
                        # Display current frame
                        with video_display.container():
                            st.image(canvas_with_predictions, channels='BGR', use_column_width=True)
                    else:
                        # Process with full buffer - make predictions
                        frame_buffer[:-1] = frame_buffer[1:]
                        frame_buffer[-1] = frame
                        
                        # Load translation model
                        translation_model = load_translation_model()
                        
                        # Make prediction on current window
                        sequence_idx = frame_idx - 20
                        prediction_output = translation_model(
                            X_test_processed[sequence_idx].reshape(
                                1, X_test_processed[sequence_idx].shape[0], 
                                X_test_processed[sequence_idx].shape[1]
                            )
                        )
                        prediction_output = prediction_output[0].cpu().detach().numpy()
                        
                        # Get top predictions
                        top_prediction_idx = np.argmax(prediction_output)
                        top_3_indices = prediction_output.argsort()[-3:][::-1]
                        top_3_signs = [expression_mapping[i] for i in top_3_indices]
                        top_3_probabilities = prediction_output[top_3_indices]
                        
                        # Update frame-wise predictions for weighted average
                        for sign, prob in zip(top_3_signs, top_3_probabilities):
                            if sign not in frame_predictions:
                                frame_predictions[sign] = []
                            frame_predictions[sign].append(prob)
                        
                        # Current frame predictions
                        current_predictions = {}
                        for sign, prob in zip(top_3_signs, top_3_probabilities):
                            current_predictions[sign] = prob

                        # Calculate weighted averages
                        for sign in frame_predictions:
                            sign_predictions = frame_predictions[sign]
                            sign_weights = [len(sign_predictions) for _ in range(len(sign_predictions))]
                            weighted_predictions[sign] = calculate_weighted_average(
                                sign_predictions, sign_weights
                            )

                        # Sort predictions by confidence
                        sorted_predictions = dict(
                            sorted(weighted_predictions.items(), key=lambda item: item[1], reverse=True)
                        )
                        
                        # Create visualization
                        visualization_canvas = utils.render_stick_model(
                            frame,
                            eval(frame_data['bodypose_circles']),
                            eval(frame_data['bodypose_sticks']),
                            eval(frame_data['handpose_edges']),
                            eval(frame_data['handpose_peaks'])
                        )
                        
                        # Add prediction visualizations
                        canvas_with_predictions = utils.create_bar_plot_visualization(
                            visualization_canvas, current_predictions,
                            f'Current Window Prediction (Frames {sequence_idx + 1}-{frame_idx + 1})',
                            visualization_canvas
                        )
                        canvas_with_predictions = utils.create_bar_plot_visualization(
                            canvas_with_predictions, weighted_predictions,
                            f'Cumulative Weighted Average - Frame {frame_idx + 1}',
                            visualization_canvas
                        )
                        canvas_with_predictions = utils.add_bottom_padding(
                            canvas_with_predictions, (255, 255, 255), 100
                        )
                        
                        video_writer.write_frame(canvas_with_predictions)
                        
                        # Get best prediction for display
                        best_sign = max(weighted_predictions, key=weighted_predictions.get)
                        best_confidence = weighted_predictions[best_sign]
                        
                        # Update progress display
                        with progress_container.container():
                            progress_df = pd.DataFrame(
                                [[f'{frame_idx + 1}/{current_test_data.shape[0]}',
                                  f'{best_sign} ({best_confidence * 100:.2f}%)']],
                                columns=['Frames Processed', 'Detected Sign']
                            )
                            progress_table = st.table(progress_df)
                            
                        # Display current frame
                        with video_display.container():
                            st.image(canvas_with_predictions, channels='BGR', use_column_width=True)

                    frame_idx += 1

            # Finalize video processing
            st.success("βœ… Video processing completed!")
            
            with video_display.container():
                if video_writer is not None:
                    video_writer.close()
                    with open(output_file, 'rb') as video_file:
                        output_video_bytes = video_file.read()
                    st.video(output_video_bytes)
                    st.info(f"πŸ’Ύ Processed video saved: {output_file}")
                else:
                    st.warning("⚠️ No video output generated")

        finally:
            # Clean up resources
            video_capture.release()
            if video_writer is not None:
                video_writer.close()
            cv2.destroyAllWindows()

# Footer
st.markdown(
    """

    ---

    <div style="text-align: center; color: #666;">

    <p><strong>TechMatrix Solvers</strong> | Shri Ram Group of Institutions</p>

    <p>Innovating Accessible Technology Solutions for Everyone πŸš€</p>

    </div>

    """, 
    unsafe_allow_html=True
)