Update app.py
Browse files
app.py
CHANGED
@@ -1,126 +1,137 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
from
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
predictor
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import subprocess
|
4 |
+
|
5 |
+
# โฌ๏ธ Install `segment_anything` if not already installed
|
6 |
+
try:
|
7 |
+
from segment_anything import SamPredictor, sam_model_registry
|
8 |
+
except ImportError:
|
9 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "git+https://github.com/facebookresearch/segment-anything.git"])
|
10 |
+
from segment_anything import SamPredictor, sam_model_registry
|
11 |
+
|
12 |
+
import streamlit as st
|
13 |
+
import cv2
|
14 |
+
import numpy as np
|
15 |
+
import pandas as pd
|
16 |
+
from PIL import Image
|
17 |
+
import torch
|
18 |
+
from torchvision.transforms import Compose, Resize, ToTensor, Normalize
|
19 |
+
from segment_anything import SamPredictor, sam_model_registry
|
20 |
+
|
21 |
+
# Set Streamlit configuration
|
22 |
+
st.set_page_config(page_title="Volume Estimator", layout="wide")
|
23 |
+
st.title("๐ฆ Volume Estimation using SAM Segmentation + MiDaS Depth")
|
24 |
+
|
25 |
+
# Load SAM and MiDaS models
|
26 |
+
@st.cache_resource
|
27 |
+
def load_models():
|
28 |
+
sam_checkpoint = "C:/Users/Administrator/Desktop/streamlit_tl/models/sam_vit_h_4b8939.pth"
|
29 |
+
sam = sam_model_registry["vit_h"](checkpoint=sam_checkpoint).to("cuda" if torch.cuda.is_available() else "cpu")
|
30 |
+
predictor = SamPredictor(sam)
|
31 |
+
|
32 |
+
midas = torch.hub.load("intel-isl/MiDaS", "DPT_Large")
|
33 |
+
midas.eval()
|
34 |
+
midas_transform = Compose([
|
35 |
+
Resize(384),
|
36 |
+
ToTensor(),
|
37 |
+
Normalize(mean=[0.5]*3, std=[0.5]*3)
|
38 |
+
])
|
39 |
+
return predictor, midas, midas_transform
|
40 |
+
|
41 |
+
predictor, midas_model, midas_transform = load_models()
|
42 |
+
|
43 |
+
# Input source selection
|
44 |
+
source_option = st.radio("Select input source", ("Upload Image", "Use Webcam"))
|
45 |
+
|
46 |
+
uploaded_file = None
|
47 |
+
image_pil = None
|
48 |
+
|
49 |
+
if source_option == "Upload Image":
|
50 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
51 |
+
if uploaded_file:
|
52 |
+
image_pil = Image.open(uploaded_file).convert("RGB")
|
53 |
+
|
54 |
+
elif source_option == "Use Webcam":
|
55 |
+
run_camera = st.checkbox("Start Camera")
|
56 |
+
|
57 |
+
if run_camera:
|
58 |
+
cap = cv2.VideoCapture(0)
|
59 |
+
stframe = st.empty()
|
60 |
+
capture = False
|
61 |
+
|
62 |
+
while run_camera and cap.isOpened():
|
63 |
+
ret, frame = cap.read()
|
64 |
+
if not ret:
|
65 |
+
break
|
66 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
67 |
+
stframe.image(frame_rgb, caption="Live Camera Feed", channels="RGB")
|
68 |
+
|
69 |
+
if st.button("๐ธ Capture Frame"):
|
70 |
+
image_pil = Image.fromarray(frame_rgb)
|
71 |
+
run_camera = False
|
72 |
+
cap.release()
|
73 |
+
break
|
74 |
+
|
75 |
+
# Continue processing if we have an image
|
76 |
+
if image_pil:
|
77 |
+
image_np = np.array(image_pil)
|
78 |
+
img_h, img_w = image_np.shape[:2]
|
79 |
+
st.image(image_pil, caption="Selected Image", use_container_width=True)
|
80 |
+
|
81 |
+
# Real-world reference dimensions
|
82 |
+
real_image_width_cm = 100
|
83 |
+
real_image_height_cm = 75
|
84 |
+
assumed_max_depth_cm = 100
|
85 |
+
|
86 |
+
pixel_to_cm_x = real_image_width_cm / img_w
|
87 |
+
pixel_to_cm_y = real_image_height_cm / img_h
|
88 |
+
|
89 |
+
# SAM Segmentation
|
90 |
+
predictor.set_image(image_np)
|
91 |
+
masks, _, _ = predictor.predict(multimask_output=False)
|
92 |
+
|
93 |
+
# MiDaS Depth Estimation
|
94 |
+
input_tensor = midas_transform(image_pil).unsqueeze(0)
|
95 |
+
with torch.no_grad():
|
96 |
+
depth_prediction = midas_model(input_tensor).squeeze().cpu().numpy()
|
97 |
+
depth_resized = cv2.resize(depth_prediction, (img_w, img_h))
|
98 |
+
|
99 |
+
# Object volume computation
|
100 |
+
volume_data = []
|
101 |
+
for i, mask in enumerate(masks):
|
102 |
+
mask_np = mask
|
103 |
+
x, y, w, h = cv2.boundingRect(mask_np.astype(np.uint8))
|
104 |
+
width_px = w
|
105 |
+
height_px = h
|
106 |
+
|
107 |
+
width_cm = width_px * pixel_to_cm_x
|
108 |
+
height_cm = height_px * pixel_to_cm_y
|
109 |
+
|
110 |
+
depth_masked = depth_resized[mask_np > 0.5]
|
111 |
+
|
112 |
+
if depth_masked.size == 0:
|
113 |
+
continue
|
114 |
+
|
115 |
+
normalized_depth = (depth_masked - np.min(depth_resized)) / (np.max(depth_resized) - np.min(depth_resized) + 1e-6)
|
116 |
+
depth_cm = np.mean(normalized_depth) * assumed_max_depth_cm
|
117 |
+
|
118 |
+
volume_cm3 = round(depth_cm * width_cm * height_cm, 2)
|
119 |
+
|
120 |
+
volume_data.append({
|
121 |
+
"Object": f"Object #{i+1}",
|
122 |
+
"Length (Depth)": f"{round(depth_cm, 2)} cm",
|
123 |
+
"Breadth (Width)": f"{round(width_cm, 2)} cm",
|
124 |
+
"Height": f"{round(height_cm, 2)} cm",
|
125 |
+
"Volume": f"{volume_cm3} cmยณ"
|
126 |
+
})
|
127 |
+
|
128 |
+
# Display volume table
|
129 |
+
if volume_data:
|
130 |
+
df = pd.DataFrame(volume_data)
|
131 |
+
st.markdown("### ๐ Object Dimensions and Volume")
|
132 |
+
st.dataframe(df)
|
133 |
+
|
134 |
+
csv = df.to_csv(index=False).encode('utf-8')
|
135 |
+
st.download_button("๐ Download Volume Table as CSV", csv, "object_volumes_with_units.csv", "text/csv")
|
136 |
+
else:
|
137 |
+
st.warning("๐ซ No objects were segmented.")
|