imagescientist commited on
Commit
ced9aa3
·
1 Parent(s): 04da5a8

Update app.py not just test file

Browse files
Files changed (1) hide show
  1. app.py +30 -20
app.py CHANGED
@@ -1,37 +1,47 @@
1
  from fastai.vision.all import *
2
-
3
  import gradio as gr
4
-
5
  from PIL import Image, ImageEnhance
6
  import torchvision.transforms as T
7
- import pathlib
8
- plt = platform.system()
9
- print(str(plt))
10
- if plt == 'linux': pathlib.WindowsPath = pathlib.PosixPath
11
 
12
  def name_to_hrs (r): return float(round(float(os.path.basename(r)[0:-4].split("_")[1][1:])*(minutes/60)+5,2))
13
  def validation_split (r): return os.path.basename(r)[0:-4].split("_")[3] == "R0003" or os.path.basename(r)[0:-4].split("_")[3] == "R0006"
14
  def get_label_filename(name): return path/'labels'/f'{name.stem}_annotationLabels.tif'
 
 
 
 
15
  zebrafish_age_predictor = load_learner('zebrafish_age_20220726.pkl')
16
  zebrafish_classifier = load_learner('fish_yolk_segmentation_20220726.pkl')
17
- examples = ["5hr.tif", "12hr.tif", "24hr.tif"]
 
 
 
 
18
  def process_zebrafish_image(img):
19
 
20
- # out_pl.clear_output()
21
- # enhancer = ImageEnhance.Brightness(img)
22
- # factor = 10
23
- # im_output = enhancer.enhance(factor)
24
- # with out_pl: display(im_output.to_thumb(256,256))
25
  age,tensor, tensor=zebrafish_age_predictor.predict(img)
26
 
27
  pred,pred_idx,probs=zebrafish_classifier.predict(img)
28
-
29
- # with out_pl_mask: pred.show(alpha=1, vmin=0, vmax=3, title='mask')
30
- # lbl_pred.value = f'Predicted age: {age[0]};'
31
- #return dict(zip(pred, map(float,age)))
32
- image_out = T.ToPILImage(pred)
33
- text_out = dict(zip("Age prediction", map(float,age)))
 
 
 
 
 
 
 
34
  return (image_out, text_out )
35
 
36
- intf = gr.Interface(fn=process_zebrafish_image, inputs=gr.inputs.Image(shape=(512, 512)), outputs=[gr.outputs.Image(), gr.outputs.Label()]).launch(share=True)
37
- intf.launch(inline=False)
 
1
  from fastai.vision.all import *
2
+ import numpy
3
  import gradio as gr
4
+ import PIL
5
  from PIL import Image, ImageEnhance
6
  import torchvision.transforms as T
7
+
8
+ # import pathlib
9
+ # plt = platform.system()
10
+ # if plt == 'Linux': pathlib.WindowsPath = pathlib.PosixPath
11
 
12
  def name_to_hrs (r): return float(round(float(os.path.basename(r)[0:-4].split("_")[1][1:])*(minutes/60)+5,2))
13
  def validation_split (r): return os.path.basename(r)[0:-4].split("_")[3] == "R0003" or os.path.basename(r)[0:-4].split("_")[3] == "R0006"
14
  def get_label_filename(name): return path/'labels'/f'{name.stem}_annotationLabels.tif'
15
+
16
+ #zebrafish_age_predictor= vision_learner().load('zebrafish_age_20220726.pkl')
17
+ #zebrafish_classifier = unet_learner().load('fish_yolk_segmentation_20220726.pkl')
18
+
19
  zebrafish_age_predictor = load_learner('zebrafish_age_20220726.pkl')
20
  zebrafish_classifier = load_learner('fish_yolk_segmentation_20220726.pkl')
21
+
22
+ title = "Zebrafish segmenter and age predictor"
23
+ description = "An rgb grayscale zebrafish fluorescence image segmenter and age predictor created with fastai. Created as a demo for Gradio and HuggingFace Spaces. Gradio does not display .tif files - those will only show up in the output. The input will be blank unless the file is .jpg or .png."
24
+
25
+ examples = ["early1.png","early2.png","early3.png", "late1.png", "late2.png", "late3.png", "mid1.png", "mid2.png","mid3.png"]
26
  def process_zebrafish_image(img):
27
 
 
 
 
 
 
28
  age,tensor, tensor=zebrafish_age_predictor.predict(img)
29
 
30
  pred,pred_idx,probs=zebrafish_classifier.predict(img)
31
+ img = img*10
32
+ img = PILImage.create(img)
33
+ #img = PILImage.create('24hr.tif')
34
+ _,axs = plt.subplots(1,3, figsize=(12,3))
35
+ img.show(ctx=axs[0], title='image')
36
+ pred.show(alpha=1, ctx=axs[1], vmin=0, vmax=3, title='mask')
37
+ img.show(ctx=axs[2], title='superimposed')
38
+ pred.show(ctx=axs[2], vmin=0, vmax=3);
39
+ fig = plt.gcf()
40
+ fig.canvas.draw()
41
+ image_out = PIL.Image.frombytes('RGB', fig.canvas.get_width_height(),fig.canvas.tostring_rgb())
42
+
43
+ text_out = "Age prediction "+ str(age[0])
44
  return (image_out, text_out )
45
 
46
+ intf = gr.Interface(fn=process_zebrafish_image, inputs=gr.inputs.Image(shape=(512, 512)), outputs=['image', 'text'], title = title, description=description, examples= examples).launch(debug=True, share=True)
47
+