Update app.py
Browse files
app.py
CHANGED
|
@@ -14,30 +14,49 @@ output_details = interpreter.get_output_details()
|
|
| 14 |
class_names = ['Dyskeratotic', 'Koilocytotic', 'Metaplastic', 'Parabasal', 'Superficial-Intermediat']
|
| 15 |
CONFIDENCE_THRESHOLD = 0.7
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def predict_image(image):
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
img_array =
|
| 21 |
-
img_array = np.expand_dims(img_array, axis=0)
|
| 22 |
|
|
|
|
| 23 |
interpreter.set_tensor(input_details[0]['index'], img_array)
|
| 24 |
interpreter.invoke()
|
| 25 |
output = interpreter.get_tensor(output_details[0]['index'])
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
if confidence < CONFIDENCE_THRESHOLD:
|
| 31 |
-
return f"⚠️ Low confidence ({confidence:.2f}).
|
| 32 |
else:
|
| 33 |
return f"✅ Prediction: {class_names[class_idx]} (Confidence: {confidence:.2f})"
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
-
return f"Error: {str(e)}"
|
| 36 |
|
|
|
|
| 37 |
gr.Interface(
|
| 38 |
fn=predict_image,
|
| 39 |
-
inputs=gr.Image(type="pil"),
|
| 40 |
outputs="text",
|
| 41 |
-
title="Cervical Cancer Classification",
|
| 42 |
-
description="Upload an image to
|
| 43 |
).launch()
|
|
|
|
| 14 |
class_names = ['Dyskeratotic', 'Koilocytotic', 'Metaplastic', 'Parabasal', 'Superficial-Intermediat']
|
| 15 |
CONFIDENCE_THRESHOLD = 0.7
|
| 16 |
|
| 17 |
+
def preprocess_image(image):
|
| 18 |
+
"""Preprocess input image for model"""
|
| 19 |
+
image = image.resize((224, 224)).convert("RGB")
|
| 20 |
+
img_array = np.array(image, dtype=np.float32)
|
| 21 |
+
|
| 22 |
+
# 🔑 IMPORTANT: Check model input scale
|
| 23 |
+
if input_details[0]['dtype'] == np.uint8:
|
| 24 |
+
img_array = np.expand_dims(img_array, axis=0).astype(np.uint8)
|
| 25 |
+
else:
|
| 26 |
+
img_array = img_array / 255.0
|
| 27 |
+
img_array = np.expand_dims(img_array, axis=0).astype(np.float32)
|
| 28 |
+
|
| 29 |
+
return img_array
|
| 30 |
+
|
| 31 |
def predict_image(image):
|
| 32 |
try:
|
| 33 |
+
# Preprocess
|
| 34 |
+
img_array = preprocess_image(image)
|
|
|
|
| 35 |
|
| 36 |
+
# Inference
|
| 37 |
interpreter.set_tensor(input_details[0]['index'], img_array)
|
| 38 |
interpreter.invoke()
|
| 39 |
output = interpreter.get_tensor(output_details[0]['index'])
|
| 40 |
|
| 41 |
+
# Ensure output is softmax
|
| 42 |
+
probabilities = tf.nn.softmax(output[0]).numpy()
|
| 43 |
+
|
| 44 |
+
class_idx = int(np.argmax(probabilities))
|
| 45 |
+
confidence = float(np.max(probabilities))
|
| 46 |
|
| 47 |
if confidence < CONFIDENCE_THRESHOLD:
|
| 48 |
+
return f"⚠️ Low confidence ({confidence:.2f}). Try another clearer image."
|
| 49 |
else:
|
| 50 |
return f"✅ Prediction: {class_names[class_idx]} (Confidence: {confidence:.2f})"
|
| 51 |
+
|
| 52 |
except Exception as e:
|
| 53 |
+
return f"❌ Error: {str(e)}"
|
| 54 |
|
| 55 |
+
# Gradio app
|
| 56 |
gr.Interface(
|
| 57 |
fn=predict_image,
|
| 58 |
+
inputs=gr.Image(type="pil", label="Upload Cervical Cell Image"),
|
| 59 |
outputs="text",
|
| 60 |
+
title="Cervical Cancer Cell Classification",
|
| 61 |
+
description="Upload an image to classify cervical cells using a TFLite model."
|
| 62 |
).launch()
|