File size: 982 Bytes
26360ef
8ecf583
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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)}'