Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,25 @@
|
|
1 |
___all___ = ['learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']
|
2 |
|
3 |
-
from tensorflow.keras.models import
|
|
|
|
|
4 |
import gradio as gr
|
5 |
-
import json
|
6 |
|
7 |
# Define the ETHNICITIES dictionary
|
8 |
ETHNICITIES = {0: "White", 1: "Black", 2: "Asian", 3: "Indian", 4: "Hispanic"}
|
9 |
|
10 |
# Load the trained model
|
11 |
-
|
12 |
-
# Load the entire model from the .h5 file
|
13 |
-
Model_L = load_model('model_L.h5')
|
14 |
-
except Exception as e:
|
15 |
-
print(f"Error loading model: {e}")
|
16 |
|
17 |
# Define the categories based on your model's output
|
18 |
categories = list(ETHNICITIES.values())
|
19 |
|
20 |
# Define the function to classify images
|
21 |
def classify_image(img):
|
22 |
-
|
23 |
-
img =
|
|
|
|
|
24 |
pred = Model_L.predict(img)
|
25 |
probs = pred[0]
|
26 |
return dict(zip(categories, map(float, probs)))
|
|
|
1 |
___all___ = ['learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']
|
2 |
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
import gradio as gr
|
|
|
7 |
|
8 |
# Define the ETHNICITIES dictionary
|
9 |
ETHNICITIES = {0: "White", 1: "Black", 2: "Asian", 3: "Indian", 4: "Hispanic"}
|
10 |
|
11 |
# Load the trained model
|
12 |
+
Model_L = load_model('model_L.h5')
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# Define the categories based on your model's output
|
15 |
categories = list(ETHNICITIES.values())
|
16 |
|
17 |
# Define the function to classify images
|
18 |
def classify_image(img):
|
19 |
+
img = img.resize((48, 48)) # Resize the image to match model input shape
|
20 |
+
img = img.convert('L') # Convert image to grayscale
|
21 |
+
img = np.array(img) / 255.0 # Normalize the image
|
22 |
+
img = img.reshape(-1, 48, 48, 1) # Reshape the image to match model input shape
|
23 |
pred = Model_L.predict(img)
|
24 |
probs = pred[0]
|
25 |
return dict(zip(categories, map(float, probs)))
|