Spaces:
Runtime error
Runtime error
File size: 1,508 Bytes
5a8c585 |
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 |
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)
|