abuhanzala commited on
Commit
011b561
·
verified ·
1 Parent(s): 465e140

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -8
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import numpy as np
3
- from PIL import Image
4
  import tensorflow as tf
5
 
6
  # Load TFLite model
@@ -13,13 +13,39 @@ output_details = interpreter.get_output_details()
13
 
14
  # Class labels
15
  class_names = ['Dyskeratotic', 'Koilocytotic', 'Metaplastic', 'Parabasal', 'Superficial-Intermediat']
16
- CONFIDENCE_THRESHOLD = 0.7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  def predict_image(image):
19
  try:
20
  # Validate input
21
- if image.mode != "RGB":
22
- return {"Error": 1.0}, "⚠️ Please upload a valid cervical cell image (RGB image required)."
 
23
 
24
  # Preprocess
25
  image = image.resize((224, 224)).convert("RGB")
@@ -29,14 +55,14 @@ def predict_image(image):
29
  # Run inference
30
  interpreter.set_tensor(input_details[0]['index'], img_array)
31
  interpreter.invoke()
32
- output = interpreter.get_tensor(output_details[0]['index'])[0] # shape (num_classes,)
33
 
34
  # Normalize
35
  probs = tf.nn.softmax(output).numpy()
36
 
37
- # Check if prediction is below confidence threshold
38
  if np.max(probs) < CONFIDENCE_THRESHOLD:
39
- return {"Error": 1.0}, "⚠️ The model is unsure. Please upload a clearer/correct image of cervical cells."
40
 
41
  # Convert to dict for Gradio Label
42
  probs_dict = {class_names[i]: float(probs[i]) for i in range(len(class_names))}
@@ -51,5 +77,5 @@ gr.Interface(
51
  inputs=gr.Image(type="pil"),
52
  outputs=[gr.Label(num_top_classes=len(class_names)), gr.Textbox()],
53
  title="Cervical Cancer Classification",
54
- description="Upload an image. The model shows probabilities for each class and warns if the image is incorrect."
55
  ).launch()
 
1
  import gradio as gr
2
  import numpy as np
3
+ from PIL import Image, ImageFilter, ImageStat
4
  import tensorflow as tf
5
 
6
  # Load TFLite model
 
13
 
14
  # Class labels
15
  class_names = ['Dyskeratotic', 'Koilocytotic', 'Metaplastic', 'Parabasal', 'Superficial-Intermediat']
16
+ CONFIDENCE_THRESHOLD = 0.5
17
+
18
+ def is_valid_cervical_image(image):
19
+ """Basic smart validation: checks variance, edges, brightness"""
20
+ # Convert to grayscale
21
+ gray = image.convert("L")
22
+ stat = ImageStat.Stat(gray)
23
+
24
+ # Variance check (texture)
25
+ variance = stat.var[0]
26
+ if variance < 500: # threshold, tweak as needed
27
+ return False, "Image lacks texture. Upload a proper cervical cell image."
28
+
29
+ # Edge detection check
30
+ edges = gray.filter(ImageFilter.FIND_EDGES)
31
+ edge_data = np.array(edges)
32
+ edge_pixels = np.sum(edge_data > 50)
33
+ if edge_pixels < 1000: # too few edges
34
+ return False, "Image has too few edges. Upload a clear cervical cell image."
35
+
36
+ # Brightness check
37
+ brightness = stat.mean[0]
38
+ if brightness < 30 or brightness > 220:
39
+ return False, "Image brightness/contrast is not suitable. Adjust and upload again."
40
+
41
+ return True, ""
42
 
43
  def predict_image(image):
44
  try:
45
  # Validate input
46
+ valid, message = is_valid_cervical_image(image)
47
+ if not valid:
48
+ return {"Error": 1.0}, f"⚠️ {message}"
49
 
50
  # Preprocess
51
  image = image.resize((224, 224)).convert("RGB")
 
55
  # Run inference
56
  interpreter.set_tensor(input_details[0]['index'], img_array)
57
  interpreter.invoke()
58
+ output = interpreter.get_tensor(output_details[0]['index'])[0]
59
 
60
  # Normalize
61
  probs = tf.nn.softmax(output).numpy()
62
 
63
+ # Confidence check
64
  if np.max(probs) < CONFIDENCE_THRESHOLD:
65
+ return {"Error": 1.0}, "⚠️ The model is unsure. Please upload a clearer cervical cell image."
66
 
67
  # Convert to dict for Gradio Label
68
  probs_dict = {class_names[i]: float(probs[i]) for i in range(len(class_names))}
 
77
  inputs=gr.Image(type="pil"),
78
  outputs=[gr.Label(num_top_classes=len(class_names)), gr.Textbox()],
79
  title="Cervical Cancer Classification",
80
+ description="Upload a cervical cell image. The model shows probabilities for each class and warns if the image is incorrect."
81
  ).launch()