Spaces:
Sleeping
Sleeping
Commit
·
d422727
1
Parent(s):
e4bc999
Update app.py
Browse files
app.py
CHANGED
@@ -2,27 +2,37 @@ import gradio as gr
|
|
2 |
import tensorflow as tf
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
|
|
5 |
|
6 |
# Loading saved model
|
7 |
model = tf.keras.models.load_model('gender_recognition.h5')
|
8 |
|
9 |
def predict(input_image):
|
10 |
try:
|
|
|
|
|
11 |
# Resizing and preprocessing input image
|
12 |
-
input_image =
|
13 |
-
input_image = np.
|
14 |
input_image = np.expand_dims(input_image, axis=0) # Add a dimension for the batch size
|
15 |
|
16 |
# Making prediction
|
17 |
prediction = model.predict(input_image)
|
18 |
|
19 |
# Postprocess prediction
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
return output
|
23 |
except Exception as e:
|
24 |
return str(e)
|
25 |
|
|
|
26 |
# Creating Gradio interface
|
27 |
iface = gr.Interface(
|
28 |
fn=predict,
|
|
|
2 |
import tensorflow as tf
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
+
import cv2
|
6 |
|
7 |
# Loading saved model
|
8 |
model = tf.keras.models.load_model('gender_recognition.h5')
|
9 |
|
10 |
def predict(input_image):
|
11 |
try:
|
12 |
+
# Convert PIL Image to OpenCV format (numpy array)
|
13 |
+
input_image = cv2.cvtColor(np.array(input_image), cv2.COLOR_RGB2BGR)
|
14 |
# Resizing and preprocessing input image
|
15 |
+
input_image = cv2.resize(input_image, (178, 218))
|
16 |
+
input_image = np.array(input_image).astype(np.float32) / 255.0
|
17 |
input_image = np.expand_dims(input_image, axis=0) # Add a dimension for the batch size
|
18 |
|
19 |
# Making prediction
|
20 |
prediction = model.predict(input_image)
|
21 |
|
22 |
# Postprocess prediction
|
23 |
+
labels = ['Female', 'Male']
|
24 |
+
threshold = 0.5 # threshold for classifying as 'Male'
|
25 |
+
predicted_gender = 'Male' if prediction[0][1] > threshold else 'Female'
|
26 |
+
prediction_probability = prediction[0][1] if predicted_gender == 'Male' else prediction[0][0]
|
27 |
+
|
28 |
+
# Combine the predicted gender and the probability into a single string
|
29 |
+
output = f"{predicted_gender}. {prediction_probability * 100:.2f}% probability."
|
30 |
|
31 |
return output
|
32 |
except Exception as e:
|
33 |
return str(e)
|
34 |
|
35 |
+
|
36 |
# Creating Gradio interface
|
37 |
iface = gr.Interface(
|
38 |
fn=predict,
|