Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from gradio.outputs import Label
|
2 |
+
from icevision.all import *
|
3 |
+
from icevision.models.checkpoint import *
|
4 |
+
import PIL
|
5 |
+
import gradio as gr
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Load model
|
9 |
+
checkpoint_path = "model_checkpoint.pth"
|
10 |
+
checkpoint_and_model = model_from_checkpoint(checkpoint_path)
|
11 |
+
model = checkpoint_and_model["model"]
|
12 |
+
model_type = checkpoint_and_model["model_type"]
|
13 |
+
class_map = checkpoint_and_model["class_map"]
|
14 |
+
|
15 |
+
# Transforms
|
16 |
+
img_size = checkpoint_and_model["img_size"]
|
17 |
+
valid_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(img_size), tfms.A.Normalize()])
|
18 |
+
|
19 |
+
examples = [['1.jpg'],['2.jpg'],['3.jpg']]
|
20 |
+
|
21 |
+
def show_preds(input_image):
|
22 |
+
img = PIL.Image.fromarray(input_image, "RGB")
|
23 |
+
pred_dict = model_type.end2end_detect(img, valid_tfms, model, class_map=class_map, detection_threshold=0.5,
|
24 |
+
display_label=False, display_bbox=True, return_img=True,
|
25 |
+
font_size=16, label_color="#FF59D6")
|
26 |
+
|
27 |
+
return pred_dict["img"], len(pred_dict["detection"]["bboxes"])
|
28 |
+
|
29 |
+
|
30 |
+
gr_interface = gr.Interface(
|
31 |
+
fn=show_preds,
|
32 |
+
inputs=["image"],
|
33 |
+
outputs=[gr.outputs.Image(type="pil", label="RetinaNet Inference"), gr.outputs.Textbox(type="number", label="Microalgae Count")],
|
34 |
+
title="Microalgae Detector with RetinaNet",
|
35 |
+
description="This RetinaNet model counts microalgaes on a given image. Upload an image or click an example image below to use.",
|
36 |
+
article="<p style='text-align: center'><a href='https://dicksonneoh.com/portfolio/how_to_deploy_od_models_on_android_with_flutter/' target='_blank'>Blog post</a></p>",
|
37 |
+
examples=examples,
|
38 |
+
theme="dark-grass",
|
39 |
+
enable_queue=True
|
40 |
+
)
|
41 |
+
gr_interface.launch()
|