Spaces:
Runtime error
Runtime error
Update src/app.py
Browse files- src/app.py +56 -27
src/app.py
CHANGED
|
@@ -1,23 +1,16 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
import pickle
|
| 5 |
import plotly.express as px
|
| 6 |
|
| 7 |
st.set_page_config(layout="wide")
|
| 8 |
st.title("2D to 3D Reconstruction Demo")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
model = pickle.load(f)
|
| 13 |
-
|
| 14 |
-
# Eğer model dict olarak kaydedildiyse:
|
| 15 |
-
nfeatures = model.get("nfeatures", 2000)
|
| 16 |
-
ratio_thresh = model.get("ratio_test_threshold", 0.75)
|
| 17 |
-
|
| 18 |
-
sift = cv2.SIFT_create(nfeatures=nfeatures)
|
| 19 |
|
| 20 |
def match_and_reconstruct(img1, img2):
|
|
|
|
| 21 |
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
|
| 22 |
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
|
| 23 |
|
|
@@ -27,57 +20,93 @@ def match_and_reconstruct(img1, img2):
|
|
| 27 |
if des1 is None or des2 is None:
|
| 28 |
return None
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
matches = bf.knnMatch(des1, des2, k=2)
|
| 32 |
|
|
|
|
| 33 |
good = []
|
| 34 |
for m, n in matches:
|
| 35 |
-
if m.distance <
|
| 36 |
good.append(m)
|
| 37 |
|
| 38 |
-
if len(good) <
|
| 39 |
return None
|
| 40 |
|
| 41 |
pts1 = np.float32([kp1[m.queryIdx].pt for m in good])
|
| 42 |
pts2 = np.float32([kp2[m.trainIdx].pt for m in good])
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
if E is None:
|
| 47 |
return None
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
points_4d = cv2.triangulatePoints(P1, P2,
|
| 55 |
points_3d = points_4d[:3] / points_4d[3]
|
| 56 |
|
| 57 |
return points_3d.T
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if uploaded1 and uploaded2:
|
|
|
|
| 63 |
file_bytes1 = np.asarray(bytearray(uploaded1.read()), dtype=np.uint8)
|
| 64 |
file_bytes2 = np.asarray(bytearray(uploaded2.read()), dtype=np.uint8)
|
| 65 |
|
| 66 |
img1 = cv2.imdecode(file_bytes1, 1)
|
| 67 |
img2 = cv2.imdecode(file_bytes2, 1)
|
| 68 |
|
| 69 |
-
st.image(img1, caption="Image 1")
|
| 70 |
-
st.image(img2, caption="Image 2")
|
| 71 |
|
| 72 |
with st.spinner("Reconstructing 3D points..."):
|
| 73 |
points_3d = match_and_reconstruct(img1, img2)
|
| 74 |
|
| 75 |
if points_3d is not None:
|
|
|
|
| 76 |
fig = px.scatter_3d(
|
| 77 |
-
x=points_3d[:,0],
|
| 78 |
-
y=points_3d[:,1],
|
| 79 |
-
z=points_3d[:,2]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
)
|
|
|
|
| 81 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
| 82 |
else:
|
| 83 |
-
st.error("Not enough matches found.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import plotly.express as px
|
| 5 |
|
| 6 |
st.set_page_config(layout="wide")
|
| 7 |
st.title("2D to 3D Reconstruction Demo")
|
| 8 |
|
| 9 |
+
# SIFT detector
|
| 10 |
+
sift = cv2.SIFT_create(nfeatures=5000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def match_and_reconstruct(img1, img2):
|
| 13 |
+
|
| 14 |
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
|
| 15 |
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
|
| 16 |
|
|
|
|
| 20 |
if des1 is None or des2 is None:
|
| 21 |
return None
|
| 22 |
|
| 23 |
+
# Brute Force Matcher
|
| 24 |
+
bf = cv2.BFMatcher(cv2.NORM_L2)
|
| 25 |
matches = bf.knnMatch(des1, des2, k=2)
|
| 26 |
|
| 27 |
+
# Lowe Ratio Test
|
| 28 |
good = []
|
| 29 |
for m, n in matches:
|
| 30 |
+
if m.distance < 0.85 * n.distance:
|
| 31 |
good.append(m)
|
| 32 |
|
| 33 |
+
if len(good) < 12:
|
| 34 |
return None
|
| 35 |
|
| 36 |
pts1 = np.float32([kp1[m.queryIdx].pt for m in good])
|
| 37 |
pts2 = np.float32([kp2[m.trainIdx].pt for m in good])
|
| 38 |
|
| 39 |
+
# ---- Approximate Camera Intrinsics ----
|
| 40 |
+
h, w = gray1.shape
|
| 41 |
+
focal = 0.8 * w
|
| 42 |
+
|
| 43 |
+
K = np.array([
|
| 44 |
+
[focal, 0, w / 2],
|
| 45 |
+
[0, focal, h / 2],
|
| 46 |
+
[0, 0, 1]
|
| 47 |
+
])
|
| 48 |
+
|
| 49 |
+
# Essential Matrix
|
| 50 |
+
E, mask = cv2.findEssentialMat(
|
| 51 |
+
pts1,
|
| 52 |
+
pts2,
|
| 53 |
+
K,
|
| 54 |
+
method=cv2.RANSAC,
|
| 55 |
+
prob=0.999,
|
| 56 |
+
threshold=3.0
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
if E is None:
|
| 60 |
return None
|
| 61 |
|
| 62 |
+
# Recover Pose
|
| 63 |
+
_, R, t, mask_pose = cv2.recoverPose(E, pts1, pts2, K)
|
| 64 |
+
|
| 65 |
+
# Projection matrices
|
| 66 |
+
P1 = K @ np.hstack((np.eye(3), np.zeros((3, 1))))
|
| 67 |
+
P2 = K @ np.hstack((R, t))
|
| 68 |
|
| 69 |
+
# Triangulation
|
| 70 |
+
pts1_norm = pts1.T
|
| 71 |
+
pts2_norm = pts2.T
|
| 72 |
|
| 73 |
+
points_4d = cv2.triangulatePoints(P1, P2, pts1_norm, pts2_norm)
|
| 74 |
points_3d = points_4d[:3] / points_4d[3]
|
| 75 |
|
| 76 |
return points_3d.T
|
| 77 |
|
| 78 |
+
|
| 79 |
+
# Upload images
|
| 80 |
+
uploaded1 = st.file_uploader("Upload First Image", type=["jpg", "png"])
|
| 81 |
+
uploaded2 = st.file_uploader("Upload Second Image", type=["jpg", "png"])
|
| 82 |
|
| 83 |
if uploaded1 and uploaded2:
|
| 84 |
+
|
| 85 |
file_bytes1 = np.asarray(bytearray(uploaded1.read()), dtype=np.uint8)
|
| 86 |
file_bytes2 = np.asarray(bytearray(uploaded2.read()), dtype=np.uint8)
|
| 87 |
|
| 88 |
img1 = cv2.imdecode(file_bytes1, 1)
|
| 89 |
img2 = cv2.imdecode(file_bytes2, 1)
|
| 90 |
|
| 91 |
+
st.image(img1, caption="Image 1", use_column_width=True)
|
| 92 |
+
st.image(img2, caption="Image 2", use_column_width=True)
|
| 93 |
|
| 94 |
with st.spinner("Reconstructing 3D points..."):
|
| 95 |
points_3d = match_and_reconstruct(img1, img2)
|
| 96 |
|
| 97 |
if points_3d is not None:
|
| 98 |
+
|
| 99 |
fig = px.scatter_3d(
|
| 100 |
+
x=points_3d[:, 0],
|
| 101 |
+
y=points_3d[:, 1],
|
| 102 |
+
z=points_3d[:, 2],
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
fig.update_layout(
|
| 106 |
+
margin=dict(l=0, r=0, b=0, t=0)
|
| 107 |
)
|
| 108 |
+
|
| 109 |
st.plotly_chart(fig, use_container_width=True)
|
| 110 |
+
|
| 111 |
else:
|
| 112 |
+
st.error("Not enough good matches found. Try images with more texture and slight viewpoint difference.")
|