ASL / app.py
Just161's picture
Add application file
02d0a21
import gradio as gr
from API.prediction1 import predict_asl_letter, extract_hand
import os
import numpy as np
from PIL import Image
import cv2
def adjust_brightness(image, brightness=40):
"""
Adjust the brightness of an image.
"""
img = np.array(image, dtype=np.float32)
img = img + brightness
img = np.clip(img, 0, 255).astype(np.uint8)
return Image.fromarray(img)
def denoise_image(image, strength=10):
"""
Denoise the image with a given strength.
"""
img = np.array(image)
return Image.fromarray(cv2.fastNlMeansDenoisingColored(img, None, strength, strength, 7, 21))
def sharpen_image(image, intensity=0):
"""
Sharpen the image based on the given intensity.
"""
img = np.array(image)
if intensity <= 0:
return Image.fromarray(img)
alpha = intensity / 100.0
blurred = cv2.GaussianBlur(img, (0, 0), sigmaX=3)
sharpened = cv2.addWeighted(img, 1 + alpha, blurred, -alpha, 0)
return Image.fromarray(sharpened)
def reset_fields():
"""
Reset all input and output fields.
"""
return None, None, "", "", 10, 0, 10
MODEL_PATH = "MODEL/asl_sign_language_model.keras"
LABEL_PATH = "MODEL/label_classes_large.npy"
def predict_image(image, brightness, denoise, sharpen):
"""
Predict the ASL letter from an image and process hand region.
"""
processed_image = adjust_brightness(image, brightness)
processed_image = denoise_image(processed_image, denoise)
processed_image = sharpen_image(processed_image, sharpen)
temp_path = "temp_image.png"
processed_image.save(temp_path)
label, confidence = predict_asl_letter(temp_path, model_path=MODEL_PATH, label_path=LABEL_PATH)
hand_image = extract_hand(temp_path)
return (
hand_image,
f"{label}",
f"{confidence:.2f}%"
)
def create_app():
with gr.Blocks() as demo:
gr.Markdown("# ASL Recognition")
gr.Markdown("This project is designed to help users learn the American Sign Language (ASL) alphabet by recognizing and mimicking gestures for each letter. This project was developed as part of the Le Wagon #1705 Data Sciecne & AI course by the following contributors: Diana, Robert, Jean-Michel, Gabriel & Boris.")
gr.Markdown("1. Upload an image or take one using your webcam<br>2. Hit the green 'Make Prediction' button<br>3. Wait for a prediction<br>4. Fine tune your input to adjust prediction and confidence")
with gr.Row():
# Neue Spalte ganz links
with gr.Column(scale=1):
gr.Image(
value="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Asl_alphabet_gallaudet.png/351px-Asl_alphabet_gallaudet.png",
label="ASL Alphabet Chart",
elem_id="asl-chart",
)
gr.Markdown("American Sign Language alphabet, laid out by Darren Stone, derived from the Gallaudet-TT font. This work has been released into the public domain by its author, Ds13 at English Wikipedia.")
# Mittlere Spalte
with gr.Column(scale=2):
input_image = gr.Image(label="Upload Image", type="pil", elem_id="input-image")
output_hand_region = gr.Image(label="Hand Region", type="pil", elem_id="hand-region")
# Rechte Spalte
with gr.Column(scale=2):
with gr.Row():
prediction_button = gr.Button("Make Prediction", elem_id="predict-btn")
reset_button = gr.Button("Reset", elem_id="reset-btn")
# gr.Markdown("Here you'll find the letter our model found based on calculated landmarks. Fine-tune the prediction and confidence by adjusting the following parameters: Brightness, Denoising, and Sharpening. The output in 'Hand Region' might assist in fine-tuning. Hit the green 'Make Prediction' button again.")
output_prediction = gr.Textbox(label="Prediction", interactive=False)
output_confidence = gr.Textbox(label="Confidence", interactive=False)
with gr.Group():
brightness = gr.Slider(0, 100, step=10, value=10, label="Brightness")
sharpen = gr.Slider(0, 100, step=10, value=10, label="Sharpening")
denoise = gr.Slider(0, 20, step=1, value=0, label="Denoise")
# Button-Aktionen
prediction_button.click(
predict_image,
inputs=[input_image, brightness, denoise, sharpen],
outputs=[
output_hand_region,
output_prediction,
output_confidence
]
)
reset_button.click(
reset_fields,
inputs=[],
outputs=[
input_image, output_hand_region, output_prediction,
output_confidence, brightness, denoise, sharpen
]
)
# CSS zur Anpassung der Buttons
demo.css = """
#predict-btn {
background-color: green;
color: white;
font-weight: bold;
}
#reset-btn {
background-color: blue;
color: white;
font-weight: bold;
}
"""
return demo
if __name__ == "__main__":
app = create_app()
app.launch()