File size: 1,621 Bytes
9df349c
3b35e83
 
 
 
 
9df349c
 
 
3b35e83
 
 
 
e7a1b6e
3b35e83
 
 
9df349c
 
 
 
 
3b35e83
9df349c
 
 
 
3b35e83
e7a1b6e
 
 
 
 
 
9df349c
 
3b35e83
 
 
 
9df349c
e7a1b6e
3b35e83
 
 
 
 
e7a1b6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import tensorflow as tf
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import numpy as np
import gradio as gr

# Force TensorFlow to use CPU
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

# Load the pre-trained model
model = tf.keras.models.load_model("maheshbabu.h5")

# Define class labels
classes = ["Normal", "Cancerous"]

# Prediction function
def predict(image):
    try:
        # Load and preprocess the image
        img = load_img(image, target_size=(224, 224))  # Resize image
        img_array = img_to_array(img) / 255.0         # Normalize pixel values
        img_array = np.expand_dims(img_array, axis=0) # Add batch dimension

        # Perform prediction
        predictions = model.predict(img_array)
        class_idx = np.argmax(predictions[0])         # Get index of highest probability
        confidence = predictions[0][class_idx]       # Get confidence score

        # Modify the output based on the prediction
        if classes[class_idx] == "Normal":
            return "No Cancer Found"
        else:
            return "Cancer Found!"

    except Exception as e:
        return f"Error processing image: {str(e)}"

# Set up the Gradio interface
interface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="filepath"),  # Use filepath to pass the image path
    outputs="text",  # Change output type to "text" for customized message
    title="Blood Cancer Detection",
    description="Upload an image to detect whether it is Normal or Cancerous."
)

if __name__ == "__main__":
    interface.launch(server_port=7860, server_name="0.0.0.0", share=True)