資源簡介
增強現實,可直接用電腦攝像頭實時實現,無需另拍視頻。

代碼片段和文件信息
#?Useful?links
#?http://www.pygame.org/wiki/OBJFileLoader
#?https://rdmilligan.wordpress.com/2015/10/15/augmented-reality-using-opencv-opengl-and-blender/
#?https://clara.io/library
#?TODO?->?Implement?command?line?arguments?(scale?model?and?object?to?be?projected)
#??????->?Refactor?and?organize?code?(proper?funcition?definition?and?separation?classes?error?handling...)
import?argparse
import?cv2
import?numpy?as?np
import?math
import?os
from?objloader_simple?import?*
#?Minimum?number?of?matches?that?have?to?be?found
#?to?consider?the?recognition?valid
MIN_MATCHES?=?10??
def?main():
????“““
????This?functions?loads?the?target?surface?image
????“““
????homography?=?None?
????#?matrix?of?camera?parameters?(made?up?but?works?quite?well?for?me)?
????camera_parameters?=?np.array([[800?0?320]?[0?800?240]?[0?0?1]])
????#?create?ORB?keypoint?detector
????orb?=?cv2.ORB_create()
????#?create?BFMatcher?object?based?on?hamming?distance??
????bf?=?cv2.BFMatcher(cv2.NORM_HAMMING?crossCheck=True)
????#?load?the?reference?surface?that?will?be?searched?in?the?video?stream
????dir_name?=?‘D:/pycharmfile/augmented-reality-master/augmented-reality-master/‘
????model?=?cv2.imread(os.path.join(dir_name?‘reference/front.jpg‘)?0)
????#?Compute?model?keypoints?and?its?descriptors
????kp_model?des_model?=?orb.detectAndCompute(model?None)
????#?Load?3D?model?from?OBJ?file
????obj?=?OBJ(os.path.join(dir_name?‘models/rat.obj‘)?swapyz=True)
????#?init?video?capture
????cap?=?cv2.VideoCapture(0)
????while?True:
????????#?read?the?current?frame
????????ret?frame?=?cap.read()
????????if?not?ret:
????????????print?(“Unable?to?capture?video“)
????????????return?
????????#?find?and?draw?the?keypoints?of?the?frame
????????kp_frame?des_frame?=?orb.detectAndCompute(frame?None)
????????#?match?frame?descriptors?with?model?descriptors
????????matches?=?bf.match(des_model?des_frame)
????????#?sort?them?in?the?order?of?their?distance
????????#?the?lower?the?distance?the?better?the?match
????????matches?=?sorted(matches?key=lambda?x:?x.distance)
????????#?compute?Homography?if?enough?matches?are?found
????????if?len(matches)?>?MIN_MATCHES:
????????????#?differenciate?between?source?points?and?destination?points
????????????src_pts?=?np.float32([kp_model[m.queryIdx].pt?for?m?in?matches]).reshape(-1?1?2)
????????????dst_pts?=?np.float32([kp_frame[m.trainIdx].pt?for?m?in?matches]).reshape(-1?1?2)
????????????#?compute?Homography
????????????homography?mask?=?cv2.findHomography(src_pts?dst_pts?cv2.RANSAC?5.0)
????????????if?args.rectangle:
????????????????#?Draw?a?rectangle?that?marks?the?found?model?in?the?frame
????????????????h?w?=?model.shape
????????????????pts?=?np.float32([[0?0]?[0?h?-?1]?[w?-?1?h?-?1]?[w?-?1?0]]).reshape(-1?1?2)
????????????????#?project?corners?into?frame
????????????????dst?=?cv2.perspectiveTransform(pts?homography)
????????????????#?connect?them?with?lines??
????????????????frame?=?cv2.polylines(frame?[np.int32(d
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-04-06?22:45??augmented-reality-master\
?????文件????????1203??2019-01-05?09:36??augmented-reality-master\.gitignore
?????目錄???????????0??2019-04-06?23:08??augmented-reality-master\.idea\
?????文件?????????468??2019-04-06?22:52??augmented-reality-master\.idea\augmented-reality-master.iml
?????文件?????????138??2019-04-06?22:45??augmented-reality-master\.idea\encodings.xm
?????文件?????????188??2019-04-06?22:45??augmented-reality-master\.idea\misc.xm
?????文件?????????307??2019-04-06?22:45??augmented-reality-master\.idea\modules.xm
?????文件????????6996??2019-04-06?23:08??augmented-reality-master\.idea\workspace.xm
?????文件????????1077??2019-01-05?09:36??augmented-reality-master\LICENSE
?????目錄???????????0??2019-04-06?22:44??augmented-reality-master\models\
?????文件???????73379??2019-01-05?09:36??augmented-reality-master\models\cow.obj
?????文件???????53540??2019-01-05?09:36??augmented-reality-master\models\fox.obj
?????文件????????6186??2019-01-05?09:36??augmented-reality-master\models\pirate-ship-fat.obj
?????文件???????48596??2019-01-05?09:36??augmented-reality-master\models\rat.obj
?????文件??????201102??2019-01-05?09:36??augmented-reality-master\models\wolf.obj
?????文件????????2627??2019-01-05?09:36??augmented-reality-master\README.md
?????目錄???????????0??2019-04-06?22:59??augmented-reality-master\reference\
?????文件??????161116??2019-04-06?22:59??augmented-reality-master\reference\front.jpg
?????文件???????42588??2019-01-05?09:36??augmented-reality-master\reference\model.jpg
?????目錄???????????0??2019-04-06?23:03??augmented-reality-master\src\
?????目錄???????????0??2019-04-06?22:55??augmented-reality-master\src\__pycache__\
?????文件????????1230??2019-04-06?22:55??augmented-reality-master\src\__pycache__\objloader_simple.cpython-37.pyc
?????文件????????7199??2019-04-06?23:03??augmented-reality-master\src\ar_main.py
?????文件????????1757??2019-04-06?22:55??augmented-reality-master\src\objloader_simple.py
評論
共有 條評論