File size: 2,440 Bytes
886e412
 
 
 
 
 
86d32c9
5e62771
886e412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd7f531
886e412
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
import numpy as np
import cv2
from keras.models import load_model

# Load the trained models
model1 = load_model('./isatron_v3.h5')
# model2 = load_model('./Isatron_v2.h5')

# Print the loaded models
print(model1)
# print(model2)

# Define image size and labels
img_size1 = 150
# img_size2 = 224
labels = ['PNEUMONIA', 'NORMAL']

def load_and_preprocess_image1(img):
    # Convert Gradio image (PIL Image) to numpy array
    img = np.array(img)
    # Convert RGB to grayscale
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Resize image to the required input size
    img = cv2.resize(img, (img_size1, img_size1))
    # Reshape image for model input
    img = img.reshape(-1, img_size1, img_size1, 1)
    # Normalize image
    img = img / 255.0
    return img

# def load_and_preprocess_image2(img):
#     # Convert Gradio image (PIL Image) to numpy array
#     img = np.array(img)
#     # Convert RGB to grayscale
#     img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
#     # Resize image to the required input size
#     img = cv2.resize(img, (img_size2, img_size2))
#     # Reshape image for model input
#     img = img.reshape(-1, img_size2, img_size2, 1)
#     # Normalize image
#     img = img / 255.0
#     return img

def image_classifier1(img):
    # Preprocess the image
    img = load_and_preprocess_image1(img)
    # Perform prediction using model1
    prediction = model1.predict(img)[0][0]  # Assuming your model outputs a single probability
    # Return the prediction
    return {'PNEUMONIA': prediction, 'NORMAL': 1-prediction}  # Invert the prediction for Gradio

# def image_classifier2(img):
#     # Preprocess the image
#     img = load_and_preprocess_image2(img)
#     # Perform prediction using model2
#     prediction = model2.predict(img)[0][0]  # Assuming your model outputs a single probability
#     # Return the prediction
#     return {'PNEUMONIA': 1-prediction, 'NORMAL': prediction}  # Invert the prediction for Gradio

# Create Gradio interfaces for each model
demo_model1 = gr.Interface(fn=image_classifier1, inputs="image", outputs="label", title="IsaTron V2")
# demo_model2 = gr.Interface(fn=image_classifier2, inputs="image", outputs="label", title="Pneumonia Detection - Model 2")

# gr.Parallel(demo_model1, demo_model2).launch(share=True)
# Launch the interfaces simultaneously
if __name__ == "__main__":
    # demo_model1.launch(share=True)
    demo_model1.launch(share=True)