sunil18p31a0101 commited on
Commit
1bd92fa
·
verified ·
1 Parent(s): fc38a48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -6
app.py CHANGED
@@ -11,7 +11,7 @@ from PIL import Image
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
@@ -73,11 +73,11 @@ def extract_features(image):
73
  }
74
 
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}")
@@ -87,14 +87,14 @@ def check_image_format(image):
87
  def predict_hemoglobin(age, gender, image):
88
  try:
89
  # Check if the image file is valid
90
- if not check_image_format(image):
91
  return "Error: The uploaded image file is not recognized or is corrupt. Please upload an image in JPG, PNG, BMP, or GIF format."
92
 
93
  # Extract features from the image
94
  features = extract_features(image)
95
 
96
  # Ensure gender is encoded correctly (0 for female, 1 for male)
97
- features['Gender'] = 1 if gender.lower() == 'M' else 0
98
  features['Age'] = age
99
 
100
  # Create a DataFrame for features (do not include Hgb, as it's the predicted value)
 
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
 
73
  }
74
 
75
  # Function to check if the image is a valid file format
76
+ def check_image_format(filepath):
77
  try:
78
  # Try opening the image using PIL
79
+ with Image.open(filepath) as img:
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}")
 
87
  def predict_hemoglobin(age, gender, image):
88
  try:
89
  # Check if the image file is valid
90
+ if not check_image_format(image.name):
91
  return "Error: The uploaded image file is not recognized or is corrupt. Please upload an image in JPG, PNG, BMP, or GIF format."
92
 
93
  # Extract features from the image
94
  features = extract_features(image)
95
 
96
  # Ensure gender is encoded correctly (0 for female, 1 for male)
97
+ features['Gender'] = 1 if gender.lower() == 'male' else 0
98
  features['Age'] = age
99
 
100
  # Create a DataFrame for features (do not include Hgb, as it's the predicted value)