SkalskiP commited on
Commit
3bd34d6
β€’
1 Parent(s): 66be722

:tada: initial commit

Browse files
Files changed (4) hide show
  1. .gitignore +2 -0
  2. README.md +2 -2
  3. app.py +39 -0
  4. requirements.txt +9 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .idea/
2
+ venv/
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: SAM And MetaCLIP
3
  emoji: πŸ‘
4
- colorFrom: gray
5
- colorTo: blue
6
  sdk: gradio
7
  sdk_version: 3.50.2
8
  app_file: app.py
 
1
  ---
2
  title: SAM And MetaCLIP
3
  emoji: πŸ‘
4
+ colorFrom: pink
5
+ colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 3.50.2
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import supervision as sv
4
+ import torch
5
+ from PIL import Image
6
+ from transformers import pipeline
7
+
8
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
9
+ SAM_GENERATOR = pipeline(
10
+ task="mask-generation",
11
+ model="facebook/sam-vit-large",
12
+ device=DEVICE)
13
+
14
+
15
+ def run_segmentation(image_rgb_pil: Image.Image) -> sv.Detections:
16
+ outputs = SAM_GENERATOR(image_rgb_pil, points_per_batch=32)
17
+ mask = np.array(outputs['masks'])
18
+ return sv.Detections(xyxy=sv.mask_to_xyxy(masks=mask), mask=mask)
19
+
20
+
21
+ def inference(image_rgb_pil: Image.Image) -> Image.Image:
22
+ detections = run_segmentation(image_rgb_pil)
23
+ mask_annotator = sv.MaskAnnotator(color_lookup=sv.ColorLookup.INDEX)
24
+
25
+ img_bgr_numpy = np.array(image_rgb_pil)[:, :, ::-1]
26
+ annotated_bgr_image = mask_annotator.annotate(
27
+ scene=img_bgr_numpy, detections=detections)
28
+ return Image.fromarray(annotated_bgr_image[:, :, ::-1])
29
+
30
+
31
+ with gr.Blocks() as demo:
32
+ with gr.Row():
33
+ input_image = gr.Image(image_mode='RGB', type='pil')
34
+ result_image = gr.Image(image_mode='RGB', type='pil')
35
+ submit_button = gr.Button("Submit")
36
+
37
+ submit_button.click(inference, inputs=[input_image], outputs=result_image)
38
+
39
+ demo.launch(debug=False)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu118
2
+ torch
3
+ torchvision
4
+
5
+ numpy
6
+ pillow
7
+ gradio
8
+ transformers
9
+ supervision