-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
72 lines (53 loc) · 1.96 KB
/
run.py
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
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
import os
import sys
import utils
import wrapper
import itertools
from config import *
# Load network and weights
network, class_names, colors = wrapper.load_network(config_file, data_file, weights_file)
# Load images and correstonding paths from image.txt file
images = utils.load_images(images_file)
#matches = utils.run_superglue(pairs_folder, network, images)
matches = utils.run_surf(images, network)
if not matches:
print("No common matches found, decrease the number of photos")
sys.exit(0)
common_kps = utils.retrieve_common_kps(matches)
for i in common_kps:
print(i)
axis = np.float32([[580,360,50], [680,360,50], [680,460,50], [580,460,50],
[580,360,-50], [680,360,-50], [680,460,-50], [580,460,-50]])
kps = []
for i, kp in enumerate(common_kps[0]):
kps.append(cv.KeyPoint(kp[0], kp[1], 0))
common_kps[0][i] = list(kp)
img = cv.imread(images[0][1], cv.IMREAD_GRAYSCALE)
last = cv.drawKeypoints(img, kps,None)
plt.imshow(last),plt.show()
# Create the 3D points from the common keypoints (Z=1 for planar objects)
imgp = np.array(common_kps[0], np.float32)
objp = np.ones((len(imgp), 3), np.float32)
for i,elem in enumerate(objp):
for k in range(0, 2):
if k!=2:
objp[i][k] = imgp[i][k]
for i, elem in enumerate(common_kps):
imgp = np.array(elem, np.float32)
img = cv.imread(images[i][1])
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
imgp = np.reshape(imgp, (len(imgp), 1, 2))
ret, rvecs, tvecs, _ = cv.solvePnPRansac(objp, imgp, K, d)
print(ret)
R = cv.Rodrigues(rvecs)[0]
C = -R.T.dot(tvecs)
print("Camera #" + str(i+1) + " position: " + str(C.ravel()))
# project 3D points to image plane
imgpts, jac = cv.projectPoints(axis, rvecs, tvecs, K, d)
img = utils.draw(img,imgp,imgpts)
plt.imshow(img), plt.show()
sys.exit(0)