-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAugmentedRality.py
More file actions
113 lines (97 loc) · 4.57 KB
/
AugmentedRality.py
File metadata and controls
113 lines (97 loc) · 4.57 KB
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 cv2
import numpy as np
cap = cv2.VideoCapture(0)
imgTarget = cv2.imread('TargetImage.jpg')
myVid = cv2.VideoCapture('video.mp4')
detection = False
frameCounter = 0
success, imgVideo = myVid.read()
hT,wT,cT = imgTarget.shape
imgVideo = cv2.resize(imgVideo,(wT,hT))
orb = cv2.ORB_create(nfeatures=1000)
kp1, des1 = orb.detectAndCompute(imgTarget,None)
# imgTarget = cv2.drawKeypoints(imgTarget,kp1,None)
def stackImages(imgArray,scale,lables=[]):
sizeW= imgArray[0][0].shape[1]
sizeH = imgArray[0][0].shape[0]
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0], list)
if rowsAvailable:
for x in range ( 0, rows):
for y in range(0, cols):
imgArray[x][y] = cv2.resize(imgArray[x][y], (sizeW,sizeH), None, scale, scale)
if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((sizeH, sizeW, 3), np.uint8)
hor = [imageBlank]*rows
hor_con = [imageBlank]*rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
hor_con[x] = np.concatenate(imgArray[x])
ver = np.vstack(hor)
ver_con = np.concatenate(hor)
else:
for x in range(0, rows):
imgArray[x] = cv2.resize(imgArray[x], (sizeW, sizeH), None, scale, scale)
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
hor= np.hstack(imgArray)
hor_con= np.concatenate(imgArray)
ver = hor
if len(lables) != 0:
eachImgWidth= int(ver.shape[1] / cols)
eachImgHeight = int(ver.shape[0] / rows)
print(eachImgHeight)
for d in range(0, rows):
for c in range (0,cols):
cv2.rectangle(ver,(c*eachImgWidth,eachImgHeight*d),(c*eachImgWidth+len(lables[d])*13+27,30+eachImgHeight*d),(255,255,255),cv2.FILLED)
cv2.putText(ver,lables[d],(eachImgWidth*c+10,eachImgHeight*d+20),cv2.FONT_HERSHEY_COMPLEX,0.7,(255,0,255),2)
return ver
while True:
sucess,imgWebcam = cap.read()
imgAug = imgWebcam.copy()
kp2, des2 = orb.detectAndCompute(imgWebcam, None)
# imgWebcam = cv2.drawKeypoints(imgWebcam, kp2, None)
if detection == False:
myVid.set(cv2.CAP_PROP_POS_FRAMES,0)
frameCounter = 0
else:
if frameCounter == myVid.get(cv2.CAP_PROP_FRAME_COUNT):
myVid.set(cv2.CAP_PROP_POS_FRAMES, 0)
frameCounter = 0
success, imgVideo = myVid.read()
imgVideo = cv2.resize(imgVideo, (wT, hT))
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2,k=2)
good =[]
for m,n in matches:
if m.distance < 0.75 *n.distance:
good.append(m)
print(len(good))
imgFeatures = cv2.drawMatches(imgTarget,kp1,imgWebcam,kp2,good,None,flags=2) # shows how coordinates are connected between image and similar image on camera
# Homography : source and destination point, relation between them, we'll get matrix, from which we can find what ever transformation we want between them
if len(good) > 10:
detection = True
srcPts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2) # source point
dstPts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2) # destination point
matrix, mask = cv2.findHomography(srcPts,dstPts,cv2.RANSAC,5) # homography matrix
print(matrix)
pts = np.float32([[0,0],[0,hT],[wT,hT],[wT,0]]).reshape(-1,1,2)
dst = cv2.perspectiveTransform(pts,matrix)
img2 = cv2.polylines(imgWebcam,[np.int32(dst)],True,(255,0,255),3)
imgWarp = cv2.warpPerspective(imgVideo,matrix, (imgWebcam.shape[1],imgWebcam.shape[0]))
maskNew = np.zeros((imgWebcam.shape[0],imgWebcam.shape[1]),np.uint8)
cv2.fillPoly(maskNew,[np.int32(dst)],(255,255,255))
maskInv = cv2.bitwise_not(maskNew)
imgAug = cv2.bitwise_and(imgAug,imgAug,mask = maskInv)
imgAug = cv2.bitwise_or(imgWarp,imgAug)
imgStacked = stackImages(([imgWebcam,imgVideo,imgTarget],[imgFeatures,imgWarp,imgAug]),0.5)
# cv2.imshow(‘maskNew’, imgAug)
# cv2.imshow(‘imgWarp’, imgWarp)
# cv2.imshow(‘img2’, img2)
# cv2.imshow(‘imgFeatures’, imgFeatures)
# cv2.imshow(‘ImgTarget’,imgTarget)
# cv2.imshow(‘myVid’,imgVideo)
# cv2.imshow(‘Webcam’, imgWebcam)
cv2.imshow('imgStacked', imgStacked)
cv2.waitKey(1)
frameCounter +=1