Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/python/plane_ar.py
16337 views
1
#!/usr/bin/env python
2
3
'''
4
Planar augmented reality
5
==================
6
7
This sample shows an example of augmented reality overlay over a planar object
8
tracked by PlaneTracker from plane_tracker.py. solvePnP function is used to
9
estimate the tracked object location in 3d space.
10
11
video: http://www.youtube.com/watch?v=pzVbhxx6aog
12
13
Usage
14
-----
15
plane_ar.py [<video source>]
16
17
Keys:
18
SPACE - pause video
19
c - clear targets
20
21
Select a textured planar object to track by drawing a box with a mouse.
22
Use 'focal' slider to adjust to camera focal length for proper video augmentation.
23
'''
24
25
# Python 2/3 compatibility
26
from __future__ import print_function
27
28
import numpy as np
29
import cv2 as cv
30
import video
31
import common
32
from plane_tracker import PlaneTracker
33
from video import presets
34
35
# Simple model of a house - cube with a triangular prism "roof"
36
ar_verts = np.float32([[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0],
37
[0, 0, 1], [0, 1, 1], [1, 1, 1], [1, 0, 1],
38
[0, 0.5, 2], [1, 0.5, 2]])
39
ar_edges = [(0, 1), (1, 2), (2, 3), (3, 0),
40
(4, 5), (5, 6), (6, 7), (7, 4),
41
(0, 4), (1, 5), (2, 6), (3, 7),
42
(4, 8), (5, 8), (6, 9), (7, 9), (8, 9)]
43
44
class App:
45
def __init__(self, src):
46
self.cap = video.create_capture(src, presets['book'])
47
self.frame = None
48
self.paused = False
49
self.tracker = PlaneTracker()
50
51
cv.namedWindow('plane')
52
cv.createTrackbar('focal', 'plane', 25, 50, common.nothing)
53
self.rect_sel = common.RectSelector('plane', self.on_rect)
54
55
def on_rect(self, rect):
56
self.tracker.add_target(self.frame, rect)
57
58
def run(self):
59
while True:
60
playing = not self.paused and not self.rect_sel.dragging
61
if playing or self.frame is None:
62
ret, frame = self.cap.read()
63
if not ret:
64
break
65
self.frame = frame.copy()
66
67
vis = self.frame.copy()
68
if playing:
69
tracked = self.tracker.track(self.frame)
70
for tr in tracked:
71
cv.polylines(vis, [np.int32(tr.quad)], True, (255, 255, 255), 2)
72
for (x, y) in np.int32(tr.p1):
73
cv.circle(vis, (x, y), 2, (255, 255, 255))
74
self.draw_overlay(vis, tr)
75
76
self.rect_sel.draw(vis)
77
cv.imshow('plane', vis)
78
ch = cv.waitKey(1)
79
if ch == ord(' '):
80
self.paused = not self.paused
81
if ch == ord('c'):
82
self.tracker.clear()
83
if ch == 27:
84
break
85
86
def draw_overlay(self, vis, tracked):
87
x0, y0, x1, y1 = tracked.target.rect
88
quad_3d = np.float32([[x0, y0, 0], [x1, y0, 0], [x1, y1, 0], [x0, y1, 0]])
89
fx = 0.5 + cv.getTrackbarPos('focal', 'plane') / 50.0
90
h, w = vis.shape[:2]
91
K = np.float64([[fx*w, 0, 0.5*(w-1)],
92
[0, fx*w, 0.5*(h-1)],
93
[0.0,0.0, 1.0]])
94
dist_coef = np.zeros(4)
95
_ret, rvec, tvec = cv.solvePnP(quad_3d, tracked.quad, K, dist_coef)
96
verts = ar_verts * [(x1-x0), (y1-y0), -(x1-x0)*0.3] + (x0, y0, 0)
97
verts = cv.projectPoints(verts, rvec, tvec, K, dist_coef)[0].reshape(-1, 2)
98
for i, j in ar_edges:
99
(x0, y0), (x1, y1) = verts[i], verts[j]
100
cv.line(vis, (int(x0), int(y0)), (int(x1), int(y1)), (255, 255, 0), 2)
101
102
103
if __name__ == '__main__':
104
print(__doc__)
105
106
import sys
107
try:
108
video_src = sys.argv[1]
109
except:
110
video_src = 0
111
App(video_src).run()
112
113