|
|
import gradio as gr |
|
|
import tensorflow as tf |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
import requests |
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MODEL_PATH = "model1.h5" |
|
|
if not os.path.exists(MODEL_PATH): |
|
|
print("Downloading model from GitHub...") |
|
|
url = "https://media.githubusercontent.com/media/Sedtha-019/face-recognition-model/main/model1.h5" |
|
|
r = requests.get(url) |
|
|
r.raise_for_status() |
|
|
with open(MODEL_PATH, "wb") as f: |
|
|
f.write(r.content) |
|
|
print("β
Model downloaded successfully!") |
|
|
|
|
|
model = tf.keras.models.load_model(MODEL_PATH) |
|
|
print("β
Model loaded successfully!") |
|
|
|
|
|
class_names = ["Sed", "Rak"] |
|
|
|
|
|
def preprocess_image(image): |
|
|
image = image.resize((300, 300)) |
|
|
img_array = np.array(image) / 255.0 |
|
|
img_array = np.expand_dims(img_array, axis=0) |
|
|
return img_array |
|
|
|
|
|
def predict(image): |
|
|
if image is None: |
|
|
return {"Error": 1.0} |
|
|
|
|
|
img_array = preprocess_image(image) |
|
|
prediction = model.predict(img_array) |
|
|
|
|
|
return {class_names[i]: float(prediction[0][i]) for i in range(len(class_names))} |
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=predict, |
|
|
inputs=gr.Image(type="pil", label="Upload a Face Image"), |
|
|
outputs=gr.Label(num_top_classes=2, label="Prediction"), |
|
|
title="π§ Face Recognition (Sedtha Model)", |
|
|
description="Upload a face image β the model predicts who it is.", |
|
|
theme="default" |
|
|
) |
|
|
|
|
|
|
|
|
iface.launch() |