Commit
·
8a50c06
1
Parent(s):
7637f71
Replace shell commands with Python code
Browse files
app.py
CHANGED
@@ -1,21 +1,33 @@
|
|
1 |
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
import numpy as np
|
5 |
from torchvision.transforms import ToTensor
|
6 |
from PIL import Image
|
7 |
import cv2
|
8 |
-
import zipfile
|
9 |
|
10 |
# Ensure the necessary model files are available
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
!git clone https://github.com/yformer/EfficientSAM.git
|
16 |
-
import os
|
17 |
-
os.chdir("EfficientSAM")
|
18 |
-
!pip install git+https://github.com/facebookresearch/segment-anything.git
|
19 |
from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
|
20 |
from efficient_sam.build_efficient_sam import build_efficient_sam_vits
|
21 |
|
@@ -156,3 +168,4 @@ interface = gr.Interface(
|
|
156 |
)
|
157 |
|
158 |
interface.launch(debug=True)
|
|
|
|
1 |
|
2 |
+
import os
|
3 |
+
import subprocess
|
4 |
+
import requests
|
5 |
+
import zipfile
|
6 |
import gradio as gr
|
7 |
import torch
|
8 |
import numpy as np
|
9 |
from torchvision.transforms import ToTensor
|
10 |
from PIL import Image
|
11 |
import cv2
|
|
|
12 |
|
13 |
# Ensure the necessary model files are available
|
14 |
+
def download_file(url, destination):
|
15 |
+
response = requests.get(url, stream=True)
|
16 |
+
with open(destination, 'wb') as f:
|
17 |
+
f.write(response.content)
|
18 |
+
|
19 |
+
# Download SAM model
|
20 |
+
if not os.path.exists("weights/sam_vit_h_4b8939.pth"):
|
21 |
+
os.makedirs("weights", exist_ok=True)
|
22 |
+
download_file("https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth", "weights/sam_vit_h_4b8939.pth")
|
23 |
+
|
24 |
+
# Clone EfficientSAM repository if not already cloned
|
25 |
+
if not os.path.exists("EfficientSAM"):
|
26 |
+
subprocess.run(["git", "clone", "https://github.com/yformer/EfficientSAM.git"])
|
27 |
+
|
28 |
+
# Install dependencies
|
29 |
+
subprocess.run(["pip", "install", "git+https://github.com/facebookresearch/segment-anything.git"])
|
30 |
|
|
|
|
|
|
|
|
|
31 |
from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
|
32 |
from efficient_sam.build_efficient_sam import build_efficient_sam_vits
|
33 |
|
|
|
168 |
)
|
169 |
|
170 |
interface.launch(debug=True)
|
171 |
+
|