Spaces:
Sleeping
Sleeping
File size: 4,993 Bytes
b57665c 5e59210 a6d7c94 5e59210 a6d7c94 5e59210 1fb0975 5e59210 a6d7c94 1fb0975 0134e08 5e59210 0134e08 5e59210 0479ad8 1fb0975 5e59210 1b6c9ee 4fc155e 5e59210 a6d7c94 5e59210 54bf390 a6d7c94 5e59210 | 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 | # app.py
import gradio as gr
import tempfile
import os
import cv2
import numpy as np
from inference import predict_from_video, LipReadingModel
import logging
import time
# Configure Logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load the model once
def load_model():
logger.info("Loading Lip Reading Model...")
return LipReadingModel()
model = load_model()
# Prediction function with enhancements
def run_prediction(video_path):
"""
Takes a video file path, processes it, and returns the predicted text.
Includes error handling.
"""
MAX_SIZE_MB = 1000 # Maximum allowed video size in megabytes
if not video_path:
return "β No video provided. Please upload or record a video."
# Check video size
try:
video_size_mb = os.path.getsize(video_path) / (1024 * 1024)
logger.info(f"Uploaded video size: {video_size_mb:.2f} MB")
except Exception as e:
logger.error(f"Error accessing video file: {e}")
return f"β Error accessing video file: {e}"
if video_size_mb > MAX_SIZE_MB:
return f"β Video size exceeds {MAX_SIZE_MB} MB limit. Please upload a smaller video."
try:
# Run prediction
logger.info("Running prediction...")
start_time = time.time()
prediction = predict_from_video(video_path=video_path, model=model)
end_time = time.time()
total_time = end_time - start_time
logger.info(f"Prediction completed in {total_time:.2f} seconds.")
logger.info(f"Prediction result: {prediction}")
except Exception as e:
logger.error(f"Prediction error: {e}")
prediction = f"β An error occurred during prediction: {e}"
# Ensure prediction is a string
if not isinstance(prediction, str):
prediction = str(prediction)
return prediction
# Define Gradio interface
def create_interface():
with gr.Blocks(css="#title {font-size: 2em; color: #4CAF50}") as demo:
gr.Markdown("# π§ Lip Reading App")
gr.Markdown("""This application allows you to perform lip reading by either uploading a video or recording directly using your webcam.""")
with gr.TabItem("Upload Video"):
with gr.Column():
video_input = gr.Video(
label="π Upload Your Video",
sources="upload", # Ensure file path is returned
format="mp4" # Use mp4 format for compatibility
)
predict_button = gr.Button("π Run Prediction")
prediction_output = gr.Textbox(
label="π Predicted Text",
interactive=False,
lines=4,
placeholder="Prediction will appear here."
)
with gr.TabItem("Record Video"):
with gr.Column():
video_recorder = gr.Video(
label="π₯ Record Your Video",
sources="webcam", # Ensure file path is returned
format="mp4" # Use mp4 format for compatibility
# Removed 'recording_width' and 'recording_height'
)
predict_button_rec = gr.Button("π Run Prediction on Recorded Video")
prediction_output_rec = gr.Textbox(
label="π Predicted Text",
interactive=False,
lines=4,
placeholder="Prediction will appear here."
)
# Add user instructions and feedback
with gr.Accordion("βΉοΈ How to Use", open=False):
gr.Markdown("""
**Upload Video:**
- Click on the "Upload Your Video" button to select a video file from your device.
- Supported formats: MP4, AVI, MOV, MPG.
- After uploading, click "Run Prediction" to get the lip reading result.
**Record Video:**
- Click on the "Record Your Video" button to access your webcam.
- Grant the necessary permissions if prompted.
- Record your video and click "Stop Recording" once done.
- Wait until the recorded video appears on screen.
- Click "Run Prediction on Recorded Video" to get the lip reading result.
""")
# Define button actions
predict_button.click(fn=run_prediction, inputs=video_input, outputs=prediction_output)
predict_button_rec.click(fn=run_prediction, inputs=video_recorder, outputs=prediction_output_rec)
# Add footer or additional information if needed
gr.Markdown("""--- Β© 2024 Lip Reading App. All rights reserved.""")
return demo
# Launch the interface
if __name__ == "__main__":
demo = create_interface()
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=True # Set to False if not sharing publicly
)
|