Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import tensorflow as tf | |
| from tensorflow.keras.preprocessing import image | |
| import numpy as np | |
| # Load your trained TensorFlow face recognition model | |
| model = tf.keras.models.load_model(r"C:\Users\tiruv\Downloads\1.h5") | |
| # Map the predicted label to a class name | |
| class_names = { | |
| 0: "akilesh", | |
| 1: "aswath", | |
| 2: "bhuvan", | |
| 3: "karthikeyan", | |
| 4: "lalpradhap", | |
| 5: "muhilan", | |
| 6: "ragavan", | |
| 7: "sanjay", | |
| 8: "seenivas", | |
| 9: "sharvesh" | |
| } | |
| def predict_image(img): | |
| if img is None: | |
| return "No image provided" | |
| try: | |
| # Preprocess the image | |
| img = img.resize((224, 224)) # Ensure the size matches your training data | |
| img_array = image.img_to_array(img) | |
| img_array = tf.expand_dims(img_array, 0) # Create a batch of size 1 | |
| # Predict the class | |
| predictions = model.predict(img_array) | |
| predicted_class = np.argmax(predictions[0]) | |
| # Map prediction to class name | |
| predicted_class_name = class_names.get(predicted_class, "Unknown class") | |
| return predicted_class_name | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Create Gradio interface | |
| gr.Interface(fn=predict_image, | |
| inputs=gr.Image(type="pil"), # Default configuration | |
| outputs="text", | |
| title="Image Classifier", | |
| description="Upload an image to classify it").launch(share=True) | |