Update app.py
Browse files
app.py
CHANGED
@@ -6,18 +6,29 @@ from PIL import Image
|
|
6 |
import torch
|
7 |
from torchvision.transforms import Compose, Resize, ToTensor, Normalize
|
8 |
from segment_anything import SamPredictor, sam_model_registry
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
st.set_page_config(page_title="Volume Estimator", layout="wide")
|
12 |
st.title("Volume Estimation using SAM Segmentation + MiDaS Depth")
|
13 |
|
14 |
# Load SAM and MiDaS models
|
15 |
@st.cache_resource
|
16 |
def load_models():
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
predictor = SamPredictor(sam)
|
20 |
|
|
|
21 |
midas = torch.hub.load("intel-isl/MiDaS", "DPT_Large")
|
22 |
midas.eval()
|
23 |
midas_transform = Compose([
|
@@ -46,7 +57,6 @@ elif source_option == "Use Webcam":
|
|
46 |
if run_camera:
|
47 |
cap = cv2.VideoCapture(0)
|
48 |
stframe = st.empty()
|
49 |
-
capture = False
|
50 |
|
51 |
while run_camera and cap.isOpened():
|
52 |
ret, frame = cap.read()
|
@@ -57,11 +67,10 @@ elif source_option == "Use Webcam":
|
|
57 |
|
58 |
if st.button("Capture Frame"):
|
59 |
image_pil = Image.fromarray(frame_rgb)
|
60 |
-
run_camera = False
|
61 |
cap.release()
|
62 |
break
|
63 |
|
64 |
-
# Continue
|
65 |
if image_pil:
|
66 |
image_np = np.array(image_pil)
|
67 |
img_h, img_w = image_np.shape[:2]
|
@@ -85,7 +94,7 @@ if image_pil:
|
|
85 |
depth_prediction = midas_model(input_tensor).squeeze().cpu().numpy()
|
86 |
depth_resized = cv2.resize(depth_prediction, (img_w, img_h))
|
87 |
|
88 |
-
#
|
89 |
volume_data = []
|
90 |
for i, mask in enumerate(masks):
|
91 |
mask_np = mask
|
@@ -97,7 +106,6 @@ if image_pil:
|
|
97 |
height_cm = height_px * pixel_to_cm_y
|
98 |
|
99 |
depth_masked = depth_resized[mask_np > 0.5]
|
100 |
-
|
101 |
if depth_masked.size == 0:
|
102 |
continue
|
103 |
|
@@ -114,7 +122,7 @@ if image_pil:
|
|
114 |
"Volume": f"{volume_cm3} cm³"
|
115 |
})
|
116 |
|
117 |
-
# Display volume
|
118 |
if volume_data:
|
119 |
df = pd.DataFrame(volume_data)
|
120 |
st.markdown("### Object Dimensions and Volume")
|
|
|
6 |
import torch
|
7 |
from torchvision.transforms import Compose, Resize, ToTensor, Normalize
|
8 |
from segment_anything import SamPredictor, sam_model_registry
|
9 |
+
import requests
|
10 |
+
import os
|
11 |
|
12 |
+
# Streamlit configuration
|
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 |
+
# Download SAM checkpoint from Hugging Face
|
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(requests.get(checkpoint_url).content)
|
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([
|
|
|
57 |
if run_camera:
|
58 |
cap = cv2.VideoCapture(0)
|
59 |
stframe = st.empty()
|
|
|
60 |
|
61 |
while run_camera and cap.isOpened():
|
62 |
ret, frame = cap.read()
|
|
|
67 |
|
68 |
if st.button("Capture Frame"):
|
69 |
image_pil = Image.fromarray(frame_rgb)
|
|
|
70 |
cap.release()
|
71 |
break
|
72 |
|
73 |
+
# Continue only if an image is available
|
74 |
if image_pil:
|
75 |
image_np = np.array(image_pil)
|
76 |
img_h, img_w = image_np.shape[:2]
|
|
|
94 |
depth_prediction = midas_model(input_tensor).squeeze().cpu().numpy()
|
95 |
depth_resized = cv2.resize(depth_prediction, (img_w, img_h))
|
96 |
|
97 |
+
# Compute object volumes
|
98 |
volume_data = []
|
99 |
for i, mask in enumerate(masks):
|
100 |
mask_np = mask
|
|
|
106 |
height_cm = height_px * pixel_to_cm_y
|
107 |
|
108 |
depth_masked = depth_resized[mask_np > 0.5]
|
|
|
109 |
if depth_masked.size == 0:
|
110 |
continue
|
111 |
|
|
|
122 |
"Volume": f"{volume_cm3} cm³"
|
123 |
})
|
124 |
|
125 |
+
# Display volume results
|
126 |
if volume_data:
|
127 |
df = pd.DataFrame(volume_data)
|
128 |
st.markdown("### Object Dimensions and Volume")
|