Spaces:
Runtime error
Runtime error
File size: 2,792 Bytes
8f07d0e 20b7049 1b24fa6 20b7049 afe9383 20b7049 afe9383 20b7049 38255a1 afe9383 20b7049 afe9383 20b7049 afe9383 20b7049 afe9383 20b7049 afe9383 38255a1 afe9383 20b7049 afe9383 20b7049 afe9383 20b7049 afe9383 20b7049 afe9383 20b7049 afe9383 20b7049 afe9383 20b7049 afe9383 20b7049 afe9383 20b7049 afe9383 20b7049 afe9383 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 | import streamlit as st
import cv2
import numpy as np
import plotly.express as px
st.set_page_config(layout="wide")
st.title("2D to 3D Reconstruction Demo")
# SIFT detector
sift = cv2.SIFT_create(nfeatures=5000)
def match_and_reconstruct(img1, img2):
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
kp1, des1 = sift.detectAndCompute(gray1, None)
kp2, des2 = sift.detectAndCompute(gray2, None)
if des1 is None or des2 is None:
return None
# Brute Force Matcher
bf = cv2.BFMatcher(cv2.NORM_L2)
matches = bf.knnMatch(des1, des2, k=2)
# Lowe Ratio Test
good = []
for m, n in matches:
if m.distance < 0.85 * n.distance:
good.append(m)
if len(good) < 12:
return None
pts1 = np.float32([kp1[m.queryIdx].pt for m in good])
pts2 = np.float32([kp2[m.trainIdx].pt for m in good])
# ---- Approximate Camera Intrinsics ----
h, w = gray1.shape
focal = 0.8 * w
K = np.array([
[focal, 0, w / 2],
[0, focal, h / 2],
[0, 0, 1]
])
# Essential Matrix
E, mask = cv2.findEssentialMat(
pts1,
pts2,
K,
method=cv2.RANSAC,
prob=0.999,
threshold=3.0
)
if E is None:
return None
# Recover Pose
_, R, t, mask_pose = cv2.recoverPose(E, pts1, pts2, K)
# Projection matrices
P1 = K @ np.hstack((np.eye(3), np.zeros((3, 1))))
P2 = K @ np.hstack((R, t))
# Triangulation
pts1_norm = pts1.T
pts2_norm = pts2.T
points_4d = cv2.triangulatePoints(P1, P2, pts1_norm, pts2_norm)
points_3d = points_4d[:3] / points_4d[3]
return points_3d.T
# Upload images
uploaded1 = st.file_uploader("Upload First Image", type=["jpg", "png"])
uploaded2 = st.file_uploader("Upload Second Image", type=["jpg", "png"])
if uploaded1 and uploaded2:
file_bytes1 = np.asarray(bytearray(uploaded1.read()), dtype=np.uint8)
file_bytes2 = np.asarray(bytearray(uploaded2.read()), dtype=np.uint8)
img1 = cv2.imdecode(file_bytes1, 1)
img2 = cv2.imdecode(file_bytes2, 1)
st.image(img1, caption="Image 1", use_column_width=True)
st.image(img2, caption="Image 2", use_column_width=True)
with st.spinner("Reconstructing 3D points..."):
points_3d = match_and_reconstruct(img1, img2)
if points_3d is not None:
fig = px.scatter_3d(
x=points_3d[:, 0],
y=points_3d[:, 1],
z=points_3d[:, 2],
)
fig.update_layout(
margin=dict(l=0, r=0, b=0, t=0)
)
st.plotly_chart(fig, use_container_width=True)
else:
st.error("Not enough good matches found. Try images with more texture and slight viewpoint difference.")
|