'''
1、读取视频,将视频分解为帧
2、读取二维码的信息,读取id为2的二维码信息
3、将得到的坐标信息,进行透视变换
'''
import json
import apriltag
import cv2 as cv
from myEncoder import MyEncoder
mp4 = cv.VideoCapture('./tag_video.mp4')
detector = apriltag.Detector()
i = 0
jsonpath = './jsonfile/'
if mp4.isOpened():
sucess, frame = mp4.read()
while sucess:
sucess, frame = mp4.read()
if sucess == True:
i += 1
gray = cv.cvtColor(frame, cv.COLOR_RGB2GRAY)
result = detector.detect(gray)
for j in range(len(result)):
homography1 = []
center1 = []
corner1 = []
# 解析tag信息
result_list = result[j]
tag_family = result_list.tag_family
tag_id = result_list.tag_id
hamming = result_list.hamming
goodness = result_list.goodness
decision_margin = result_list.decision_margin
homography = result_list.homography
center = result_list.center
corners = result_list.corners
# 将array 转换为 list
for m in range(3):
for n in range(3):
homography1.append(homography[m][n])
# 列表推导式
new_homography = [homography1[h: h + 3] for h in range(0, len(homography1), 3)]
for l in range(2):
center1.append(center[l])
for k in range(4):
for ind in range(2):
corner1.append(corners[k][ind])
new_corners = [corner1[q: q + 2] for q in range(0, len(corner1), 2)]
info = {
'tag_family': str(tag_family),
'tag_id': tag_id,
'hamming': hamming,
'goodness': goodness,
'decision_margin': decision_margin,
'homograph': new_homography,
'center': center1,
'corners': new_corners
}
save = jsonpath + str(i) + '.json'
with open(save, 'w') as f:
json.dump(info, f, sort_keys=False, indent=4, separators=(',', ':'))
将tag视频写入json文件中
最新推荐文章于 2024-05-26 10:01:15 发布