Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,16 @@
|
|
1 |
import gradio as gr
|
2 |
-
import joblib
|
3 |
-
import numpy as np
|
4 |
import pandas as pd
|
5 |
-
|
6 |
-
import
|
7 |
-
|
8 |
from skimage.measure import shannon_entropy
|
|
|
9 |
from scipy.ndimage import generic_filter
|
|
|
10 |
|
11 |
-
# Function to
|
12 |
-
def check_image_format(image_path):
|
13 |
-
try:
|
14 |
-
with Image.open(image_path) as img:
|
15 |
-
img.verify() # Verify if it's a valid image
|
16 |
-
return True
|
17 |
-
except (IOError, SyntaxError):
|
18 |
-
return False
|
19 |
-
|
20 |
-
# Extract features from the image
|
21 |
def extract_features(image_path):
|
|
|
22 |
image = cv2.imread(image_path)
|
23 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
24 |
|
@@ -81,49 +73,70 @@ def extract_features(image_path):
|
|
81 |
"g5": g5,
|
82 |
}
|
83 |
|
84 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
def predict_hemoglobin(age, gender, image):
|
86 |
print(f"Image path: {image}") # Debugging line to check the image path
|
87 |
-
|
88 |
# Check if the image file is valid
|
89 |
if not check_image_format(image):
|
90 |
return "Error: The uploaded image file is not recognized or is corrupt."
|
91 |
|
|
|
92 |
features = extract_features(image)
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
95 |
features_df = pd.DataFrame([features])
|
96 |
|
97 |
-
# Load
|
98 |
svr_model = joblib.load('svr_model.pkl')
|
99 |
scaler = joblib.load('minmax_scaler.pkl')
|
100 |
label_encoder = joblib.load('label_encoder.pkl')
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
features_df_scaled = scaler.transform(features_df)
|
|
|
|
|
103 |
hemoglobin = svr_model.predict(features_df_scaled)[0]
|
104 |
|
105 |
return f"Predicted Hemoglobin Value: {hemoglobin:.2f}"
|
106 |
|
107 |
-
# Gradio Interface
|
108 |
-
def
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
age = gr.Number(label="Age", value=25)
|
114 |
-
gender = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male")
|
115 |
-
|
116 |
-
image_input = gr.Image(type="filepath", label="Upload Image (Path to Image)", interactive=True)
|
117 |
-
|
118 |
-
output = gr.Textbox(label="Predicted Hemoglobin Value")
|
119 |
-
|
120 |
-
# Prediction button
|
121 |
-
predict_btn = gr.Button("Predict Hemoglobin")
|
122 |
-
|
123 |
-
predict_btn.click(fn=predict_hemoglobin, inputs=[age, gender, image_input], outputs=output)
|
124 |
|
125 |
-
|
|
|
|
|
|
|
|
|
126 |
|
127 |
-
#
|
128 |
if __name__ == "__main__":
|
129 |
-
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import joblib
|
5 |
+
import os
|
6 |
from skimage.measure import shannon_entropy
|
7 |
+
from skimage.color import rgb2hsv
|
8 |
from scipy.ndimage import generic_filter
|
9 |
+
import cv2
|
10 |
|
11 |
+
# Function to extract features from the image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def extract_features(image_path):
|
13 |
+
# Load image and convert to RGB
|
14 |
image = cv2.imread(image_path)
|
15 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
16 |
|
|
|
73 |
"g5": g5,
|
74 |
}
|
75 |
|
76 |
+
# Function to check if the image is a valid file format
|
77 |
+
def check_image_format(image):
|
78 |
+
try:
|
79 |
+
img = cv2.imread(image)
|
80 |
+
if img is not None:
|
81 |
+
return True
|
82 |
+
else:
|
83 |
+
return False
|
84 |
+
except Exception as e:
|
85 |
+
return False
|
86 |
+
|
87 |
+
# Function to predict hemoglobin value
|
88 |
def predict_hemoglobin(age, gender, image):
|
89 |
print(f"Image path: {image}") # Debugging line to check the image path
|
90 |
+
|
91 |
# Check if the image file is valid
|
92 |
if not check_image_format(image):
|
93 |
return "Error: The uploaded image file is not recognized or is corrupt."
|
94 |
|
95 |
+
# Extract features from the image
|
96 |
features = extract_features(image)
|
97 |
+
|
98 |
+
# Ensure gender is encoded correctly (0 for female, 1 for male)
|
99 |
+
features['gender'] = 1 if gender.lower() == 'male' else 0
|
100 |
+
features['age'] = age
|
101 |
+
|
102 |
+
# Create a DataFrame for features (do not include Hgb, as it's the predicted value)
|
103 |
features_df = pd.DataFrame([features])
|
104 |
|
105 |
+
# Load the trained model, scaler, and label encoder
|
106 |
svr_model = joblib.load('svr_model.pkl')
|
107 |
scaler = joblib.load('minmax_scaler.pkl')
|
108 |
label_encoder = joblib.load('label_encoder.pkl')
|
109 |
|
110 |
+
# Ensure that features_df matches the expected training feature set (without 'Hgb')
|
111 |
+
expected_columns = ['meanr', 'meang', 'meanb', 'HHR', 'Ent', 'B', 'g1', 'g2', 'g3', 'g4', 'g5', 'Age', 'Gender']
|
112 |
+
|
113 |
+
for col in expected_columns:
|
114 |
+
if col not in features_df:
|
115 |
+
features_df[col] = 0 # Or some default value to match the expected columns.
|
116 |
+
|
117 |
+
features_df = features_df[expected_columns] # Ensure the correct order of columns
|
118 |
+
|
119 |
+
# Apply scaling (do not include 'Hgb' as it is the target)
|
120 |
features_df_scaled = scaler.transform(features_df)
|
121 |
+
|
122 |
+
# Predict hemoglobin using the trained SVR model
|
123 |
hemoglobin = svr_model.predict(features_df_scaled)[0]
|
124 |
|
125 |
return f"Predicted Hemoglobin Value: {hemoglobin:.2f}"
|
126 |
|
127 |
+
# Gradio Interface setup
|
128 |
+
def create_gradio_interface():
|
129 |
+
# Define the inputs and outputs for the Gradio interface
|
130 |
+
image_input = gr.Image(type="filepath", label="Image (Path to Image)", interactive=True)
|
131 |
+
age_input = gr.Number(label="Age", value=25, precision=0)
|
132 |
+
gender_input = gr.Radio(choices=["Male", "Female"], label="Gender", value="Male")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
+
# Set up the Gradio interface with the prediction function
|
135 |
+
gr.Interface(fn=predict_hemoglobin,
|
136 |
+
inputs=[age_input, gender_input, image_input],
|
137 |
+
outputs="text",
|
138 |
+
live=True).launch(share=True)
|
139 |
|
140 |
+
# Run the Gradio app
|
141 |
if __name__ == "__main__":
|
142 |
+
create_gradio_interface()
|