Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,48 @@
|
|
| 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 |
-
# # Bbox default shape [0,0,0,0] -> [x1,y1,x2,y2]
|
| 31 |
-
# ann = prompt_process.point_prompt(points=[[200,200]],pointlabel=[1])
|
| 32 |
-
# prompt_process.plot(annotations=ann,output='./')
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
|
| 46 |
|
| 47 |
|
|
|
|
| 1 |
+
'''
|
| 2 |
+
contains the infernce code for fastsam.py
|
| 3 |
+
'''
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from ultralytics import FastSAM
|
| 8 |
+
from ultralytics.models.fastsam import FastSAMPrompt
|
| 9 |
|
| 10 |
+
def inference(image):
|
| 11 |
|
| 12 |
+
model = FastSAM('FastSAM.pt')
|
| 13 |
|
| 14 |
+
## inference results
|
| 15 |
+
# Run inference on an image
|
| 16 |
+
results = model(source, device='cpu', retina_masks=True, imgsz=1024, conf=0.4, iou=0.9)
|
| 17 |
|
| 18 |
+
# Prepare a Prompt Process object
|
| 19 |
+
prompt_process = FastSAMPrompt(source, results, device='cpu')
|
| 20 |
|
| 21 |
+
# Everything prompt
|
| 22 |
+
ann = prompt_process.everything_prompt()
|
| 23 |
|
| 24 |
+
# Bbox default shape [0,0,0,0] -> [x1,y1,x2,y2]
|
| 25 |
+
ann = prompt_process.box_prompt(bbox=[200, 200, 300, 300])
|
| 26 |
|
| 27 |
+
# Text prompt
|
| 28 |
+
ann = prompt_process.text_prompt(text='a photo of a dog')
|
| 29 |
+
|
| 30 |
+
# Bbox default shape [0,0,0,0] -> [x1,y1,x2,y2]
|
| 31 |
+
ann = prompt_process.point_prompt(points=[[200,200]],pointlabel=[1])
|
| 32 |
+
return prompt_process.plot(annotations=ann,output='./')
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
+
title = "Usage of FastSAM"
|
| 38 |
+
description = "Implementation of pre-trained fast-sam model for spaces."
|
| 39 |
|
| 40 |
+
demo = gr.Interface(inference,inputs=[gr.Image(shape=(32,32),labels="Input Image"),"checkbox"],
|
| 41 |
+
outputs = [gr.Image(shape=(32,32),label='Output').style(width=128,height=128)],
|
| 42 |
+
title = title,
|
| 43 |
+
description = description)
|
| 44 |
|
| 45 |
+
demo.launch(debug = True)
|
| 46 |
|
| 47 |
|
| 48 |
|