utkmst commited on
Commit
e4b1113
·
verified ·
1 Parent(s): 291093c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -1,26 +1,25 @@
1
  ___all___ = ['learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']
2
 
3
- from tensorflow.keras.models import model_from_json, load_model
 
 
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
- try:
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
- import numpy as np
23
- img = np.array(img).reshape((1, *img.size, 3)) # Reshape the image to match model input shape
 
 
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)))