skavtech commited on
Commit
6e6f109
·
verified ·
1 Parent(s): 7e3a2f0

application code

Browse files
Files changed (1) hide show
  1. model.py +69 -0
model.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Code was Designed and Developed by 'SKAV TECH' Company
2
+ import os
3
+ import tensorflow as tf
4
+ from tensorflow.keras.preprocessing.image import load_img, img_to_array
5
+ import numpy as np
6
+ import gradio as gr
7
+
8
+ # Force TensorFlow to use CPU
9
+ os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
10
+
11
+ # Load the pre-trained model
12
+ model = tf.keras.models.load_model("maheshbabu.h5")
13
+
14
+ # Define class labels
15
+ classes = ["Normal", "Cancerous"]
16
+
17
+ # Prediction function
18
+ def predict(images):
19
+ results = []
20
+ cancerous_count = 0
21
+
22
+ for image in images:
23
+ try:
24
+ # Load and preprocess the image
25
+ img = load_img(image, target_size=(224, 224)) # Resize image
26
+ img_array = img_to_array(img) / 255.0 # Normalize pixel values
27
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
28
+
29
+ # Perform prediction
30
+ predictions = model.predict(img_array)
31
+ class_idx = np.argmax(predictions[0]) # Get index of highest probability
32
+ confidence = predictions[0][class_idx] # Get confidence score
33
+
34
+ result = f"{classes[class_idx]} ({confidence:.2f})"
35
+ results.append(result)
36
+
37
+ if classes[class_idx] == "Cancerous":
38
+ cancerous_count += 1
39
+ except Exception as e:
40
+ results.append(f"Error processing image: {str(e)}")
41
+
42
+ # Generate final summary
43
+ if cancerous_count > 0:
44
+ summary = f"Warning: {cancerous_count} out of {len(images)} samples are Cancerous. Please consult a doctor."
45
+ else:
46
+ summary = "All samples are Normal. No signs of cancer detected."
47
+
48
+ return results, summary
49
+
50
+ # Set up the Gradio interface
51
+ interface = gr.Interface(
52
+ fn=predict,
53
+ inputs=gr.Image(type="filepath", label="Upload Blood Cell Images", tool=None, shape=None, source="upload", multiple=True), # Allow multiple images
54
+ outputs=[
55
+ gr.JSON(label="Detailed Results"),
56
+ gr.Textbox(label="Final Summary")
57
+ ],
58
+ title="Enhanced Blood Cancer Detection",
59
+ description=(
60
+ "Upload 5-10 blood cell images to detect whether they are Normal or Cancerous. "
61
+ "The application uses a deep learning model to analyze each sample. "
62
+ "[Learn more about early cancer detection](https://www.cancer.org)."
63
+ ),
64
+ live=True,
65
+ theme="compact"
66
+ )
67
+
68
+ if __name__ == "__main__":
69
+ interface.launch(server_port=7860, server_name="0.0.0.0", share=True)