piyushmadhukar commited on
Commit
02ad932
1 Parent(s): 4744c3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py CHANGED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import cv2 # Ensure you have opencv-python installed
4
+ from tensorflow.keras.models import load_model # Ensure you have TensorFlow installed
5
+
6
+ # Load your trained model
7
+ model = load_model(r"F:\pe udemy\PROJECTS\incomplete\cancer\breast_cancer_detection_model3.h5") # Update this path to your actual model file
8
+
9
+ # Define class names according to your model
10
+ class_names = ['benign', 'malignant', 'normal'] # Update this list if different
11
+
12
+ # Define the prediction function
13
+ def predict_cancer(images):
14
+ results = []
15
+ for img in images:
16
+ # Convert image to grayscale (if it's not already), resize, and normalize
17
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # Convert to grayscale if not already
18
+ img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) # Resize to match model input
19
+ img = np.expand_dims(img, axis=-1) # Add channel dimension
20
+ img = img / 255.0 # Normalize
21
+ img = np.expand_dims(img, axis=0) # Add batch dimension
22
+
23
+ # Make prediction
24
+ prediction = model.predict(img)
25
+ class_idx = np.argmax(prediction[0])
26
+ class_name = class_names[class_idx]
27
+ probability = np.max(prediction[0])
28
+ results.append(f"{class_name} (Probability: {probability:.2f})")
29
+
30
+ return results
31
+
32
+ # Define Gradio interface
33
+ def classify_images(images):
34
+ if not isinstance(images, list): # Ensure `images` is a list of images
35
+ images = [images]
36
+ return predict_cancer(images)
37
+
38
+ # Define the Gradio interface
39
+ input_images = gr.Image(type='numpy', label='Upload Ultrasound Images')
40
+ output_labels = gr.Textbox(label='Predictions')
41
+
42
+ gr_interface = gr.Interface(
43
+ fn=classify_images,
44
+ inputs=input_images,
45
+ outputs=output_labels,
46
+ title="Breast Cancer Detection from Ultrasound Images",
47
+ description="Upload multiple breast ultrasound images to get predictions on whether they show benign, malignant, or normal conditions."
48
+ )
49
+
50
+ # Launch the Gradio app
51
+ if __name__ == "__main__":
52
+ gr_interface.launch()