sunil18p31a0101 commited on
Commit
57b4d6e
·
verified ·
1 Parent(s): 7660b5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -28
app.py CHANGED
@@ -1,16 +1,14 @@
1
- import os
2
- import cv2
3
  import joblib
4
  import numpy as np
5
  import pandas as pd
6
- import gradio as gr
7
  from skimage.color import rgb2hsv
8
  from skimage.measure import shannon_entropy
9
  from scipy.ndimage import generic_filter
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
 
@@ -58,7 +56,7 @@ def extract_features(image_path):
58
  g4 = generic_filter(gray_image, g4_filter, size=3).mean()
59
  g5 = generic_filter(gray_image, g5_filter, size=3).mean()
60
 
61
- # Return extracted features
62
  return {
63
  "meanr": meanr,
64
  "meang": meang,
@@ -73,10 +71,10 @@ def extract_features(image_path):
73
  "g5": g5,
74
  }
75
 
76
- # Function to predict hemoglobin
77
  def predict_hemoglobin(age, gender, image):
78
  # Extract features from the image
79
- features = extract_features(image.name) # Get the image file path
80
 
81
  # Add age and gender to the features
82
  features['age'] = age
@@ -85,37 +83,39 @@ def predict_hemoglobin(age, gender, image):
85
  # Convert features to DataFrame
86
  features_df = pd.DataFrame([features])
87
 
88
- # Load pre-trained models
89
  svr_model = joblib.load('svr_model.pkl') # SVR model
90
- scaler = joblib.load('minmax_scaler.pkl') # MinMaxScaler
91
  label_encoder = joblib.load('label_encoder.pkl') # LabelEncoder
92
 
93
  # Apply MinMaxScaler transformation
94
  features_df_scaled = scaler.transform(features_df)
95
 
96
- # Predict hemoglobin value
97
  hemoglobin = svr_model.predict(features_df_scaled)[0]
98
 
99
  return f"Predicted Hemoglobin Value: {hemoglobin:.2f}"
100
 
101
  # Gradio Interface
102
- def create_gradio_interface():
103
- # Define the input components (age, gender, and image)
104
- age_input = gr.Number(label="Age", value=25, interactive=True)
105
- gender_input = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male", interactive=True)
106
- image_input = gr.Image(type="file", label="Image (Path to Image)", interactive=True)
107
-
108
- # Define the output component (prediction result)
109
- output = gr.Textbox(label="Hemoglobin Prediction")
 
 
 
 
 
 
 
 
110
 
111
- # Create the Gradio interface
112
- gr.Interface(
113
- fn=predict_hemoglobin,
114
- inputs=[age_input, gender_input, image_input],
115
- outputs=output,
116
- live=True
117
- ).launch()
118
 
119
- # Start the Gradio app
120
  if __name__ == "__main__":
121
- create_gradio_interface()
 
1
+ import gradio as gr
 
2
  import joblib
3
  import numpy as np
4
  import pandas as pd
5
+ import cv2
6
  from skimage.color import rgb2hsv
7
  from skimage.measure import shannon_entropy
8
  from scipy.ndimage import generic_filter
9
 
10
+ # Extract features from the image (same as your previous code)
11
  def extract_features(image_path):
 
12
  image = cv2.imread(image_path)
13
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
14
 
 
56
  g4 = generic_filter(gray_image, g4_filter, size=3).mean()
57
  g5 = generic_filter(gray_image, g5_filter, size=3).mean()
58
 
59
+ # Return features
60
  return {
61
  "meanr": meanr,
62
  "meang": meang,
 
71
  "g5": g5,
72
  }
73
 
74
+ # Function to make predictions
75
  def predict_hemoglobin(age, gender, image):
76
  # Extract features from the image
77
+ features = extract_features(image) # Use the file path directly
78
 
79
  # Add age and gender to the features
80
  features['age'] = age
 
83
  # Convert features to DataFrame
84
  features_df = pd.DataFrame([features])
85
 
86
+ # Load the pre-trained models
87
  svr_model = joblib.load('svr_model.pkl') # SVR model
88
+ scaler = joblib.load('scaler.pkl') # MinMaxScaler
89
  label_encoder = joblib.load('label_encoder.pkl') # LabelEncoder
90
 
91
  # Apply MinMaxScaler transformation
92
  features_df_scaled = scaler.transform(features_df)
93
 
94
+ # Make the prediction
95
  hemoglobin = svr_model.predict(features_df_scaled)[0]
96
 
97
  return f"Predicted Hemoglobin Value: {hemoglobin:.2f}"
98
 
99
  # Gradio Interface
100
+ def gradio_interface():
101
+ with gr.Blocks() as demo:
102
+ gr.Markdown("## Hemoglobin Prediction from Image Features")
103
+
104
+ with gr.Row():
105
+ age = gr.Number(label="Age", value=25)
106
+ gender = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male")
107
+
108
+ image_input = gr.Image(type="filepath", label="Upload Image (Path to Image)", interactive=True)
109
+
110
+ output = gr.Textbox(label="Predicted Hemoglobin Value")
111
+
112
+ # Prediction button
113
+ predict_btn = gr.Button("Predict Hemoglobin")
114
+
115
+ predict_btn.click(fn=predict_hemoglobin, inputs=[age, gender, image_input], outputs=output)
116
 
117
+ return demo
 
 
 
 
 
 
118
 
119
+ # Launch the app
120
  if __name__ == "__main__":
121
+ gradio_interface().launch(share=True) # Set `share=True` to create a public link