• Apritag角点代码检测


    #import apriltag
    #import pupil_apriltags as apriltag     # for windows
    import cv2
    import numpy as np
    import sys
    import pandas as pd
    from pupil_apriltags import Detector
    import os
    print('ok')
    def init_detector():
    
        at_detector = Detector(families='tag36h11',
                               quad_decimate=1.0,
                               quad_sigma=0.0,
                               refine_edges=1,
                               decode_sharpening=0.25,
                               debug=0)
        return at_detector
    
    
    def extract_tags(filename, at_detector):
    
    
        # print(filter['marker_id'])
    
        img = cv2.imread('./images/' + filename)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
        tags = at_detector.detect(gray)
        # print(tags[0])
        # print("%d apriltags have been detected." % len(tags))
    
        font = cv2.FONT_HERSHEY_SIMPLEX
        df = pd.DataFrame()
        family = []
        tag_id = []
        tag_corners = []
        tag_centers = []
        image = []
    
        for tag in tags:
            if tag.tag_id not in [666, 777, 10]:
            # cv2.circle(img, tuple(tag.corners[0].astype(int)), 5, (0, 0, 255), -1)  # left-top
            # cv2.circle(img, tuple(tag.corners[1].astype(int)), 5, (0, 255, 0), -1)  # right-top
            # cv2.circle(img, tuple(tag.corners[2].astype(int)), 5, (255, 255, 0), -1)  # right-bottom
            # cv2.circle(img, tuple(tag.corners[3].astype(int)), 5, (255, 255, 255), -1)  # left-bottom
            # cv2.putText(img, str(tag.tag_id), tuple(tag.corners[0].astype(int)), font, 2, (0, 255, 255), 3)  # left-bottom
                #print('zzzzz')
                family.append(tag.tag_family)
                tag_id.append(tag.tag_id)
                tag_corners.append(tag.corners)
                tag_centers.append(tag.center)
                image.append(filename)
    
        df['family'] = family
        df['tag_id'] = tag_id
        df['tag_corners'] = tag_corners
        df['tag_centers'] = tag_centers
        df['image'] = image
    
        #print(df)
    
        return df
    
    def GetFileList(dir):
        file_list = os.listdir(dir)
        return file_list
    
    
    def write_info(df , filename):
        # if filename[:3] == 'CCD':
        #     ccd_id = filename.split('_')[0]
        #     #print(text)
        #     txt_name = ccd_id + '/' + filename + '.txt'
        # else:
        #     txt_name = 'SLR' + '/' + filename + '.txt'
    
        if not os.path.exists('./txt/'):
            os.makedirs('./txt/')
        txt_path = './txt' + filename + '.txt'
        #print(txt_path)
        if len(df) > 0:
            os.makedirs(os.path.dirname(txt_path), exist_ok=True)
            with open(txt_path, 'w') as f:
    
                f.write(str(len(df) * 4) + ' 128' + '
    ')
                for i in range(len(df)):
                    # print(df.iloc[i]['tag_id'])
                    # print(df.iloc[i]['tag_corners'])
                    for corner_id in range(len(df.iloc[i]['tag_corners'])):
    
                        D_value = ''
                        for z in range(64):
                            D_value = str(D_value) + ' ' + str(0)
                        for d in range(64):
                            D_value = str(D_value) + ' ' + str(0)
    
                        if df.iloc[i]['tag_corners'][corner_id][0] < 0:
                            df.iloc[i]['tag_corners'][corner_id][0] = 0
                        if df.iloc[i]['tag_corners'][corner_id][1] < 0:
                            df.iloc[i]['tag_corners'][corner_id][1] = 0
    
                        if i == len(df) -1 and  corner_id == 3:
                            line = str(df.iloc[i]['tag_corners'][corner_id][0]) + ' ' + str(
                                df.iloc[i]['tag_corners'][corner_id][1]) + ' ' + '0' + ' ' + '0' + D_value
                        else:
                            line = str(df.iloc[i]['tag_corners'][corner_id][0]) + ' ' + str(
                                df.iloc[i]['tag_corners'][corner_id][1]) + ' ' + '0' + ' ' + '0' + D_value + '
    '
    
                        # print(line)
                        f.write(line)
    
            f.close()
    
    
    def write_info_p(df , filename):
        # if filename[:3] == 'CCD':
        #     ccd_id = filename.split('_')[0]
        #     #print(text)
        #     txt_name = ccd_id + '/' + filename + '.txt'
        # else:
        #     txt_name = 'SLR' + '/' + filename + '.txt'
    
        if not os.path.exists('./pairs/'):
            os.makedirs('./pairs/')
        txt_path = './pairs' + filename + '.txt'
        if len(df) > 2:
            os.makedirs(os.path.dirname(txt_path), exist_ok=True)
            with open(txt_path, 'w') as f:
    
                    for i in range(len(df)):
                        # print(df.iloc[i]['tag_id'])
                        # print(df.iloc[i]['tag_corners'])
                        for corner_id in range(len(df.iloc[i]['tag_corners'])):
                            D_value = ''
                            for z in range(1):
                                D_value = str(D_value) + ' ' + str(df.iloc[i]['tag_id'])
                            for d in range(1):
                                D_value = str(D_value) + ' ' + str(corner_id)
    
                            if df.iloc[i]['tag_corners'][corner_id][0] < 0:
                                df.iloc[i]['tag_corners'][corner_id][0] = 0
                            if df.iloc[i]['tag_corners'][corner_id][1] < 0:
                                df.iloc[i]['tag_corners'][corner_id][1] = 0
    
                            if i == len(df) -1 and  corner_id == 3:
                                line = str(df.iloc[i]['tag_corners'][corner_id][0]) + ' ' + str(
                                    df.iloc[i]['tag_corners'][corner_id][1]) + ' ' + '0' + ' ' + '0' + D_value
                            else:
                                line = str(df.iloc[i]['tag_corners'][corner_id][0]) + ' ' + str(
                                    df.iloc[i]['tag_corners'][corner_id][1]) + ' ' + '0' + ' ' + '0' + D_value + '
    '
    
                            # print(line)
                            f.write(line)
    
            f.close()
    
    
    import os
    
    def file_name(file_dir):
        L = []
        for root, dirs, files in os.walk(file_dir):
            for file in files:
                if os.path.splitext(file)[1] == '.JPG' or os.path.splitext(file)[1] == '.bmp':
                    L.append(os.path.join(root, file))
    
        file_list = []
    
        for file in L:
            mod_file = file[8:]
            print(mod_file)
            file_list.append(mod_file)
        return file_list
    
    
    
    
    
    不疯魔不成活
  • 相关阅读:
    现代算法(一) 基因算法
    01-02周 学习总结
    Linux命令之touch详解
    Linux命令之umask详解
    Linux命令之wc详解
    Linux命令之stat详解
    Linux命令之tail详解
    Linux命令之head详解
    Linux命令之less详解
    Linux命令
  • 原文地址:https://www.cnblogs.com/gzr2018/p/14982345.html
Copyright © 2020-2023  润新知