miyuki17 commited on
Commit
18320f2
·
verified ·
1 Parent(s): 8b881ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -12
app.py CHANGED
@@ -1,32 +1,44 @@
1
  import gradio as gr
2
  from PIL import Image
3
  from fastai.vision.all import *
4
-
5
- def predict_image(image):
6
- # learner = load_learner('model-epoch=119.ckpt')
7
- learner = load_learner('export_2.pkl')
8
- img = PILImage.create(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  pred = learner.predict(img)
10
  return pred[0]
11
 
12
 
13
  def create_interface():
14
-
15
- image_input = gr.Image()
16
-
 
17
  output = gr.Text()
18
 
19
  iface = gr.Interface(
20
  fn=predict_image,
21
  inputs=image_input,
22
  outputs=output,
23
- title="npy seg",
24
- description="Upload an image to seg."
25
  )
26
  return iface
27
 
28
  if __name__ == "__main__":
29
  iface = create_interface()
30
  iface.launch(share=True)
31
-
32
-
 
1
  import gradio as gr
2
  from PIL import Image
3
  from fastai.vision.all import *
4
+ import numpy as np
5
+
6
+ # Model Loading (Choose ONE)
7
+ # learner = load_learner('export_2.pkl') # If using exported pickle model
8
+ learner = load_learner('model-epoch=119.ckpt') # If using checkpoint
9
+
10
+ def predict_image(file):
11
+ if file.name.endswith('.npz'):
12
+ # Load NPZ data
13
+ with np.load(file.name) as data:
14
+ # Assuming your NPZ file contains an array named 'arr_0'
15
+ img_array = data['arr_0']
16
+ # Ensure correct dimensions (e.g., channels last)
17
+ # img_array = img_array.transpose((1, 2, 0)) # If needed
18
+ img = Image.fromarray(img_array)
19
+ else:
20
+ # Load regular image files (JPG, PNG, etc.)
21
+ img = PILImage.create(file.name)
22
  pred = learner.predict(img)
23
  return pred[0]
24
 
25
 
26
  def create_interface():
27
+ image_input = gr.File(
28
+ label="Upload Image or NPZ",
29
+ file_types=[".jpg", ".jpeg", ".png", ".npz"]
30
+ )
31
  output = gr.Text()
32
 
33
  iface = gr.Interface(
34
  fn=predict_image,
35
  inputs=image_input,
36
  outputs=output,
37
+ title="Medical Image Segmentation", # More informative title
38
+ description="Upload an image or NPZ file to segment." # Clear description
39
  )
40
  return iface
41
 
42
  if __name__ == "__main__":
43
  iface = create_interface()
44
  iface.launch(share=True)