Lip_Reading / app.py
thienphuc12339's picture
Update app.py
b57665c verified
Raw
History Blame Contribute Delete
4.99 kB
# 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
)