Update app.py
Browse files
app.py
CHANGED
@@ -13,22 +13,27 @@ import os
|
|
13 |
st.set_page_config(page_title="Volume Estimator", layout="wide")
|
14 |
st.title("Volume Estimation using SAM Segmentation + MiDaS Depth")
|
15 |
|
16 |
-
# Load SAM and MiDaS models
|
17 |
@st.cache_resource
|
18 |
def load_models():
|
19 |
-
|
|
|
|
|
|
|
20 |
checkpoint_url = "https://huggingface.co/HCMUE-Research/SAM-vit-h/resolve/main/sam_vit_h_4b8939.pth"
|
21 |
checkpoint_path = "sam_vit_h_4b8939.pth"
|
|
|
|
|
22 |
if not os.path.exists(checkpoint_path):
|
|
|
|
|
23 |
with open(checkpoint_path, "wb") as f:
|
24 |
-
f.write(
|
25 |
|
26 |
-
# Load SAM
|
27 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
28 |
sam = sam_model_registry["vit_h"](checkpoint=checkpoint_path).to(device)
|
29 |
predictor = SamPredictor(sam)
|
30 |
|
31 |
-
# Load MiDaS
|
32 |
midas = torch.hub.load("intel-isl/MiDaS", "DPT_Large")
|
33 |
midas.eval()
|
34 |
midas_transform = Compose([
|
@@ -38,6 +43,7 @@ def load_models():
|
|
38 |
])
|
39 |
return predictor, midas, midas_transform
|
40 |
|
|
|
41 |
predictor, midas_model, midas_transform = load_models()
|
42 |
|
43 |
# Input source selection
|
|
|
13 |
st.set_page_config(page_title="Volume Estimator", layout="wide")
|
14 |
st.title("Volume Estimation using SAM Segmentation + MiDaS Depth")
|
15 |
|
|
|
16 |
@st.cache_resource
|
17 |
def load_models():
|
18 |
+
import requests
|
19 |
+
import os
|
20 |
+
|
21 |
+
# ✅ Use Hugging Face public model file URL
|
22 |
checkpoint_url = "https://huggingface.co/HCMUE-Research/SAM-vit-h/resolve/main/sam_vit_h_4b8939.pth"
|
23 |
checkpoint_path = "sam_vit_h_4b8939.pth"
|
24 |
+
|
25 |
+
# Download only if not already present
|
26 |
if not os.path.exists(checkpoint_path):
|
27 |
+
st.info("Downloading SAM model checkpoint...")
|
28 |
+
response = requests.get(checkpoint_url)
|
29 |
with open(checkpoint_path, "wb") as f:
|
30 |
+
f.write(response.content)
|
31 |
|
|
|
32 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
33 |
sam = sam_model_registry["vit_h"](checkpoint=checkpoint_path).to(device)
|
34 |
predictor = SamPredictor(sam)
|
35 |
|
36 |
+
# Load MiDaS model
|
37 |
midas = torch.hub.load("intel-isl/MiDaS", "DPT_Large")
|
38 |
midas.eval()
|
39 |
midas_transform = Compose([
|
|
|
43 |
])
|
44 |
return predictor, midas, midas_transform
|
45 |
|
46 |
+
|
47 |
predictor, midas_model, midas_transform = load_models()
|
48 |
|
49 |
# Input source selection
|