Nahiyan14 commited on
Commit
fd16596
·
verified ·
1 Parent(s): 6e30daf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -24
app.py CHANGED
@@ -9,25 +9,19 @@ model = load_model('best_automl_model')
9
 
10
  # Function to calculate RGB percentages
11
  def calculate_rgb_percentage(image):
12
- """
13
- Calculate the Red, Green, and Blue pixel percentages from the cropped conjunctiva portion.
14
- """
15
- # Convert image to numpy array
16
  image_array = np.array(image)
17
  red_channel = image_array[:, :, 0]
18
  green_channel = image_array[:, :, 1]
19
  blue_channel = image_array[:, :, 2]
20
 
21
- # Calculate totals
22
  total_red = np.sum(red_channel)
23
  total_green = np.sum(green_channel)
24
  total_blue = np.sum(blue_channel)
25
  total_rgb = total_red + total_green + total_blue
26
 
27
  if total_rgb == 0:
28
- return 0, 0, 0 # Avoid division by zero
29
 
30
- # Calculate percentages
31
  red_percentage = (total_red / total_rgb) * 100
32
  green_percentage = (total_green / total_rgb) * 100
33
  blue_percentage = (total_blue / total_rgb) * 100
@@ -36,20 +30,11 @@ def calculate_rgb_percentage(image):
36
 
37
  # Function to process image and predict anemia
38
  def process_image_and_predict(image, x1, y1, x2, y2, sex):
39
- """
40
- Crop the uploaded eye image based on user-provided coordinates, calculate RGB percentages, and predict anemia.
41
- """
42
  try:
43
- # Ensure the image is in RGB format
44
  image = image.convert("RGB")
45
-
46
- # Crop the image using user-provided coordinates
47
  cropped_image = image.crop((x1, y1, x2, y2))
48
-
49
- # Calculate RGB percentages
50
  red_percent, green_percent, blue_percent = calculate_rgb_percentage(cropped_image)
51
 
52
- # Prepare input for anemia prediction
53
  sex_encoded = 1 if sex == "Male" else 0
54
  input_data = pd.DataFrame({
55
  'Red Pixel': [red_percent],
@@ -58,7 +43,6 @@ def process_image_and_predict(image, x1, y1, x2, y2, sex):
58
  'Sex': [sex_encoded]
59
  })
60
 
61
- # Predict anemia and probability
62
  prediction = model.predict(input_data)[0]
63
  probability = model.predict_proba(input_data)[0][1]
64
  prediction_label = "Yes" if prediction == 1 else "No"
@@ -78,15 +62,15 @@ def process_image_and_predict(image, x1, y1, x2, y2, sex):
78
  interface = gr.Interface(
79
  fn=process_image_and_predict,
80
  inputs=[
81
- gr.Image(type="pil", label="Upload Eye Image"), # Upload image input
82
- gr.Slider(0, 500, step=1, label="X1 (Top-left corner)"), # X1 coordinate
83
- gr.Slider(0, 500, step=1, label="Y1 (Top-left corner)"), # Y1 coordinate
84
- gr.Slider(50, 500, step=1, label="X2 (Bottom-right corner)"), # X2 coordinate
85
- gr.Slider(50, 500, step=1, label="Y2 (Bottom-right corner)"), # Y2 coordinate
86
- gr.Radio(["Male", "Female"], label="Sex"), # Select Male/Female
87
  ],
88
  outputs=[
89
- gr.Image(type="pil", label="Cropped Conjunctiva"), # Cropped image output
90
  gr.Text(label="🔴 Red Pixel Percentage"),
91
  gr.Text(label="🟢 Green Pixel Percentage"),
92
  gr.Text(label="🔵 Blue Pixel Percentage"),
@@ -98,6 +82,7 @@ interface = gr.Interface(
98
  "Upload an eye image, adjust the cropping coordinates using sliders, "
99
  "and predict anemia based on the Red, Green, and Blue pixel percentages."
100
  ),
 
101
  )
102
 
103
  # Launch the app
 
9
 
10
  # Function to calculate RGB percentages
11
  def calculate_rgb_percentage(image):
 
 
 
 
12
  image_array = np.array(image)
13
  red_channel = image_array[:, :, 0]
14
  green_channel = image_array[:, :, 1]
15
  blue_channel = image_array[:, :, 2]
16
 
 
17
  total_red = np.sum(red_channel)
18
  total_green = np.sum(green_channel)
19
  total_blue = np.sum(blue_channel)
20
  total_rgb = total_red + total_green + total_blue
21
 
22
  if total_rgb == 0:
23
+ return 0, 0, 0
24
 
 
25
  red_percentage = (total_red / total_rgb) * 100
26
  green_percentage = (total_green / total_rgb) * 100
27
  blue_percentage = (total_blue / total_rgb) * 100
 
30
 
31
  # Function to process image and predict anemia
32
  def process_image_and_predict(image, x1, y1, x2, y2, sex):
 
 
 
33
  try:
 
34
  image = image.convert("RGB")
 
 
35
  cropped_image = image.crop((x1, y1, x2, y2))
 
 
36
  red_percent, green_percent, blue_percent = calculate_rgb_percentage(cropped_image)
37
 
 
38
  sex_encoded = 1 if sex == "Male" else 0
39
  input_data = pd.DataFrame({
40
  'Red Pixel': [red_percent],
 
43
  'Sex': [sex_encoded]
44
  })
45
 
 
46
  prediction = model.predict(input_data)[0]
47
  probability = model.predict_proba(input_data)[0][1]
48
  prediction_label = "Yes" if prediction == 1 else "No"
 
62
  interface = gr.Interface(
63
  fn=process_image_and_predict,
64
  inputs=[
65
+ gr.Image(type="pil", label="Upload Eye Image"),
66
+ gr.Slider(0, 500, step=1, label="X1 (Top-left corner)"),
67
+ gr.Slider(0, 500, step=1, label="Y1 (Top-left corner)"),
68
+ gr.Slider(50, 500, step=1, label="X2 (Bottom-right corner)"),
69
+ gr.Slider(50, 500, step=1, label="Y2 (Bottom-right corner)"),
70
+ gr.Radio(["Male", "Female"], label="Sex"),
71
  ],
72
  outputs=[
73
+ gr.Image(type="pil", label="Cropped Conjunctiva"),
74
  gr.Text(label="🔴 Red Pixel Percentage"),
75
  gr.Text(label="🟢 Green Pixel Percentage"),
76
  gr.Text(label="🔵 Blue Pixel Percentage"),
 
82
  "Upload an eye image, adjust the cropping coordinates using sliders, "
83
  "and predict anemia based on the Red, Green, and Blue pixel percentages."
84
  ),
85
+ css="style.css" # Link the CSS file
86
  )
87
 
88
  # Launch the app