import gradio as gr from PIL import Image size = 128 def build_model(input_shape): model = build_model(input_shape=(size, size, 1)) return model model = build_model(input_shape=(size, size, 1)) model.load_weights('BreastCancerSegmentation.h5') def preprocess_image(image, size: int=128): image = cv2.resize(image, (size,size)) image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) image = image/255. return image def segment(image): image = preprocess_image(image, size=size) image = np.expand_dims(image, 0) output = model.predict(image, verbose=0) mask_image = output[0] mask_image = np.squeeze(mask_image, -1) mask_image *= 255 mask_image = mask_image.astype(np.uint8) mask_image = Image.fromarray(mask_image).convert("L") return mask_image if __name__ == "__main__": gr.Interface( fn=segment, inputs="image", outputs=gr.Image(type="pil", label="Breast Cancer Mask"), examples = [["/content/benign(10).png"], ["/content/benign(109).png"]], title = "Breast Cancer Ultrasound Image Segmentation", description = "Check out this exciting development in the field of breast cancer diagnosis and treatment! A demo of Breast Cancer Ultrasound Image Segmentation has been developed. Upload image file, or try out one of the examples below!" ).launch(share=True, debug=True)