Spaces:
Sleeping
Sleeping
def predict_image(image): | |
try: | |
# Resize the image | |
img = image.resize((128, 128)) | |
# Convert the image to a NumPy array | |
img = np.array(img) | |
# Check if the image has 3 color channels | |
if img.shape == (128, 128): # If grayscale, convert to RGB | |
img = np.stack((img,) * 3, axis=-1) | |
# Add a batch dimension | |
img = np.expand_dims(img, axis=0) / 255.0 # Normalize the image | |
# Make the prediction | |
prediction = model.predict(img) | |
# Get the predicted class and confidence level | |
predicted_class = np.argmax(prediction) | |
confidence = np.max(prediction) * 100 # Convert to percentage | |
# Return the results | |
if predicted_class == 0: | |
return f'No tumor detected. Confidence: {confidence:.2f}%' | |
else: | |
return f'Tumor detected. Confidence: {confidence:.2f}%' | |
except Exception as e: | |
return f'Error processing image: {str(e)}' | |