sunil18p31a0101 commited on
Commit
3d80884
·
verified ·
1 Parent(s): fcb2284

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -2,17 +2,16 @@ 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
 
17
  # Extract RGB means
18
  meanr = np.mean(image[:, :, 0]) # Red channel
@@ -76,12 +75,12 @@ def extract_features(image_path):
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
@@ -96,14 +95,14 @@ def predict_hemoglobin(age, gender, 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() == 'M' 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 (1).pkl')
107
  scaler = joblib.load('minmax_scaler.pkl')
108
  label_encoder = joblib.load('label_encoder.pkl')
109
 
@@ -127,7 +126,7 @@ def predict_hemoglobin(age, gender, image):
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
 
 
2
  import pandas as pd
3
  import numpy as np
4
  import joblib
 
5
  from skimage.measure import shannon_entropy
6
  from skimage.color import rgb2hsv
7
  from scipy.ndimage import generic_filter
8
  import cv2
9
+ from PIL import Image
10
 
11
  # Function to extract features from the image
12
+ def extract_features(image):
13
+ # Convert PIL image to NumPy array
14
+ image = np.array(image)
 
15
 
16
  # Extract RGB means
17
  meanr = np.mean(image[:, :, 0]) # Red channel
 
75
  # Function to check if the image is a valid file format
76
  def check_image_format(image):
77
  try:
78
+ # Try opening the image using PIL
79
+ img = Image.open(image)
80
+ img.verify() # Verify if it's a valid image file
81
+ return True
 
82
  except Exception as e:
83
+ print(f"Error opening image: {e}")
84
  return False
85
 
86
  # Function to predict hemoglobin value
 
95
  features = extract_features(image)
96
 
97
  # Ensure gender is encoded correctly (0 for female, 1 for male)
98
+ features['Gender'] = 1 if gender.lower() == 'male' else 0
99
  features['Age'] = age
100
 
101
  # Create a DataFrame for features (do not include Hgb, as it's the predicted value)
102
  features_df = pd.DataFrame([features])
103
 
104
  # Load the trained model, scaler, and label encoder
105
+ svr_model = joblib.load('svr_model(1).pkl')
106
  scaler = joblib.load('minmax_scaler.pkl')
107
  label_encoder = joblib.load('label_encoder.pkl')
108
 
 
126
  # Gradio Interface setup
127
  def create_gradio_interface():
128
  # Define the inputs and outputs for the Gradio interface
129
+ image_input = gr.Image(type="pil", label="Image (Upload Image)", interactive=True)
130
  age_input = gr.Number(label="Age", value=25, precision=0)
131
  gender_input = gr.Radio(choices=["Male", "Female"], label="Gender", value="Male")
132