sunil18p31a0101 commited on
Commit
35de6f0
·
verified ·
1 Parent(s): 7f5e6cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -15
app.py CHANGED
@@ -2,12 +2,22 @@ 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)
@@ -73,25 +83,23 @@ def extract_features(image_path):
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
81
  features['gender'] = 1 if gender.lower() == 'male' else 0
82
-
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('minmax_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}"
@@ -118,4 +126,4 @@ def gradio_interface():
118
 
119
  # Launch the app
120
  if __name__ == "__main__":
121
- gradio_interface().launch(share=True) # Set `share=True` to create a public link
 
2
  import joblib
3
  import numpy as np
4
  import pandas as pd
5
+ from PIL import Image
6
  import cv2
7
  from skimage.color import rgb2hsv
8
  from skimage.measure import shannon_entropy
9
  from scipy.ndimage import generic_filter
10
 
11
+ # Function to check image validity
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)
 
83
 
84
  # Function to make predictions
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
  features['age'] = age
94
  features['gender'] = 1 if gender.lower() == 'male' else 0
 
 
95
  features_df = pd.DataFrame([features])
 
 
 
 
 
96
 
97
+ # Load models
98
+ svr_model = joblib.load('svr_model.pkl')
99
+ scaler = joblib.load('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}"
 
126
 
127
  # Launch the app
128
  if __name__ == "__main__":
129
+ gradio_interface().launch(share=True)