def __setstate__(self, state): ( self.__detector_params, self.__marker_min_perimeter, self.__marker_detector_modes, ) = state params = self.__detector_params.to_dict() self._detector = pupil_apriltags.Detector(**params)
def __init__(self): """Конструктор класса""" self.tag_detector = apriltag.Detector( families="tagStandard41h12", nthreads=1, quad_decimate=1.0, quad_sigma=0.0, refine_edges=1, decode_sharpening=0.25, debug=0, ) self.detect_dict = {1: 0, 2: 0, 3: 0}
def __init__(self, cameras_dir, intrinsics, extrinsics, cameras=[ "020122061233", "821312060044", "020122061651", "821312062243" ], tag_size=0.0585): self.intrinsics = intrinsics self.extrinsics = extrinsics self.cameras_dir = cameras_dir self.cameras = cameras self.detector = pupil_apriltags.Detector(families="tag36h11") self.tag_size = tag_size
def is_april(gray): results = [] # execute the detector to find the tags (more options exist) detector = pupil_apriltags.Detector(families="tag36h11") results = detector.detect(gray, estimate_tag_pose=False, camera_params=None, tag_size=0.17) #print(results) print("{} AprilTag(s) detected".format(len(results))) if results != []: go_time = 1 else: go_time = 0 return go_time
def get_xy(gray, image): # execute the detector to find the tags (more options exist) detector = pupil_apriltags.Detector(families="tag36h11") results = detector.detect(gray, estimate_tag_pose=False, camera_params=None, tag_size=0.17) #print("{} AprilTag(s) detected".format(len(results))) gold = (0, 215, 255) cv2.circle(image, (577, 204), 10, gold, 3) #577, 324 (204 for dots) # loop over the AprilTag detection resultsimage for r in results: # extract the bounding box (x, y)-coordinates for the AprilTag # and convert each of the (x, y)-coordinate pairs to integers (ptA, ptB, ptC, ptD) = r.corners ptB = (int(ptB[0]), int(ptB[1])) ptC = (int(ptC[0]), int(ptC[1])) ptD = (int(ptD[0]), int(ptD[1])) ptA = (int(ptA[0]), int(ptA[1])) # draw the bounding box of the AprilTag detection cv2.line(image, ptA, ptB, (0, 0, 255), 2) cv2.line(image, ptB, ptC, (0, 0, 255), 2) cv2.line(image, ptC, ptD, (0, 0, 255), 2) cv2.line(image, ptD, ptA, (0, 0, 255), 2) # draw the center (x, y)-coordinates of the AprilTag (cX, cY) = (int(r.center[0]), int(r.center[1])) print(cX, cY) cv2.circle(image, (cX, cY), 5, (0, 255, 0), -1) # difference in positon (diffx, diffy) = (cX - 577, cY - 204) #577, 184 (realposx, realposy) = (cX, cY) cv2.putText(image, 'Difference in x position: %02.1f' % (diffx), (30, 580), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2) cv2.putText(image, 'Difference in y position: %02.1f' % (diffy), (30, 610), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2)
def tag(img_tag): ''' img type: narry unit 8 ''' img_tag = cv2.cvtColor(img_tag, cv2.COLOR_BGR2GRAY) tag_detector = apriltag.Detector() tags = tag_detector.detect( img_tag) # The image must be a grayscale image of type numpy.uint8 # for tag in img.find_apriltags(fx=f_x, fy=f_y, cx=c_x, cy=c_y): # default family "TAG36H11" for tag in tags: cv2.circle(img, tuple(tag.corners[0].astype(int)), 4, (0, 0, 255), 2) # left-top cv2.circle(img, tuple(tag.corners[1].astype(int)), 4, (0, 0, 255), 2) # right-top cv2.circle(img, tuple(tag.corners[2].astype(int)), 4, (0, 0, 255), 2) # right-bottom cv2.circle(img, tuple(tag.corners[3].astype(int)), 4, (0, 0, 255), 2) # left-bottom
def detectTags(board, img, cameraMatrix=None, discoff=None, verbose=0): """ 检测img中的apriltag,返回一组tag,如果不输入cameraMatrix,tags中不含位姿信息 :param board: apiriltag.board 包含board的一些参数 :param img: 需要检测的图片路径 :param cameraMatrix:相机内参 :return: tags 检测到的tags """ # img = cv2.imread(img) if discoff is not None: img = cv2.undistort(img, cameraMatrix, discoff) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.convertScaleAbs(gray, alpha=1.5, beta=0) at_detector = apriltag.Detector(families=board.april_family) # for windows if cameraMatrix is None: tags = at_detector.detect(gray) if verbose == 1: img = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB) img = apriltag_cali_utils.drawTagAxis(img, tags) cv2.namedWindow("apriltag", cv2.WINDOW_NORMAL) cv2.imshow("apriltag", img) cv2.waitKey(0) else: if ~(discoff is None): gray = cv2.undistort(gray, cameraMatrix, discoff) camera_param = [cameraMatrix[0, 0], cameraMatrix[1, 1], cameraMatrix[0, 2], cameraMatrix[1, 2]] tags = at_detector.detect(gray, True, camera_param, board.tag_size) if verbose == 1: img = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB) img = apriltag_cali_utils.drawTagAxis(img, tags, cameraMatrix) cv2.namedWindow("apriltag", cv2.WINDOW_NORMAL) cv2.imshow("apriltag", cv2.resize(img,(512,512))) cv2.waitKey(0) # for tag in tags: # print(tag.tag_id) return tags
import logging from types import SimpleNamespace import cv2 import numpy as np import pupil_apriltags import file_methods as fm import video_capture from methods import normalize from stdlib_utils import unique logger = logging.getLogger(__name__) apriltag_detector = pupil_apriltags.Detector() def get_markers_data(detection, img_size, timestamp): return { "id": detection.tag_id, # verts: Corners need to be listed counter-clockwise "verts": detection.corners.tolist(), "centroid": normalize(detection.center, img_size, flip_y=True), "timestamp": timestamp, } def calc_perimeter(corners): corners = np.asarray(corners, dtype=np.float32) return cv2.arcLength(corners, closed=True)
def __setstate__(self, state): self.__detector_params = state params = self.__detector_params.to_dict() self._detector = pupil_apriltags.Detector(**params)
import logging from types import SimpleNamespace import cv2 import numpy as np import pupil_apriltags import file_methods as fm import video_capture from methods import normalize from stdlib_utils import unique logger = logging.getLogger(__name__) apriltag_detector = pupil_apriltags.Detector(nthreads=2) def get_markers_data(detection, img_size, timestamp): return { "id": detection.tag_id, # verts: Corners need to be listed counter-clockwise "verts": detection.corners.tolist(), "centroid": normalize(detection.center, img_size, flip_y=True), "timestamp": timestamp, } def calc_perimeter(corners): corners = np.asarray(corners, dtype=np.float32) return cv2.arcLength(corners, closed=True)
#!/usr/bin/env python # coding: UTF-8 # import apriltag import pupil_apriltags as apriltag # for windows import cv2 import numpy as np import sys img = cv2.imread("D:/vscodescrip/opencv_learning/tag25h9.jpg") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 创建一个apriltag检测器 # at_detector = apriltag.Detector(apriltag.DetectorOptions(families='tag36h11 tag25h9') ) at_detector = apriltag.Detector(families='tag36h11') #for windows # 进行apriltag检测,得到检测到的apriltag的列表 tags = at_detector.detect(gray) print("%d apriltags have been detected." % len(tags)) for tag in tags: cv2.circle(img, tuple(tag.corners[0].astype(int)), 4, (255, 0, 0), 2) # left-top cv2.circle(img, tuple(tag.corners[1].astype(int)), 4, (255, 0, 0), 2) # right-top cv2.circle(img, tuple(tag.corners[2].astype(int)), 4, (255, 0, 0), 2) # right-bottom cv2.circle(img, tuple(tag.corners[3].astype(int)), 4, (255, 0, 0), 2) # left-bottom cv2.imshow("apriltag_test", img) cv2.waitKey()
def rescale(frame, percent=60): scale_percent = 60 width = int(frame.shape[1]*scale_percent/100) height = int(frame.shape[0]*scale_percent/100) dim = (width, height) return cv2.resize(frame,dim, interpolation = cv2.INTER_AREA) cam = cv2.VideoCapture(0) #set camera resolution cam.set(cv2.CAP_PROP_FRAME_WIDTH, 640*2) cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 480*2) print("Current camera resolution: (" + str(cam.get(3)) + "; " + str(cam.get(4)) + ")") # create AprilTags detector with options detector = pupil_apriltags.Detector(families="tag36h11") frame_number=0 # keep looping results = [] while results == []: if not cam.isOpened(): cam.open(0) print('initializing...') (grabbed, frame) = cam.read() image = rescale(frame, percent=50) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
def detector(): return pupil_apriltags.Detector()
import pupil_apriltags as apriltag import cv2 import numpy as np import time from pupil_apriltags import Detector cap = cv2.VideoCapture(1, cv2.CAP_DSHOW) cv2.namedWindow('camera', cv2.WINDOW_AUTOSIZE) detector1 = apriltag.Detector(families='tag36h11') initial_color_done = [0, 0, 0, 0, 0, 0] #white blue green orange red white #確認各顏色為中心之面是否完成掃描 initial_color = [ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0] #white blue green orange red white , [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0] ] #各顏色為中心之面的初始顏色 now_color = [0, 0, 0, 0, 0, 0, 0, 0, 0] #讀取進來之九宮格 top_color = [0, 0, 0, 0, 0, 0, 0, 0, 0] color_detect_x = [0, 0, 0, 0, 0, 0, 0, 0, 0] #tag之x座標 color_detect_y = [0, 0, 0, 0, 0, 0, 0, 0, 0] #tag之y座標 color_detect_id = [0, 0, 0, 0, 0, 0, 0, 0, 0] #tag之id def swap_four_element(w, x, y, z):
Copyright 2020 by Magician ''' import pupil_apriltags as apriltag import cv2 import numpy as np import sys img = cv2.imread("test.jpg") gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY ) # The image must be a grayscale image of type numpy.uint8 # Build a detector for apriltag tag_detector = apriltag.Detector() # Perform apriltag detection to get a list of detected apriltag tags = tag_detector.detect(gray) print("%d apriltags have been detected." % len(tags)) for tag in tags: cv2.circle(img, tuple(tag.corners[0].astype(int)), 4, (0, 0, 255), 2) # left-top cv2.circle(img, tuple(tag.corners[1].astype(int)), 4, (0, 0, 255), 2) # right-top cv2.circle(img, tuple(tag.corners[2].astype(int)), 4, (0, 0, 255), 2) # right-bottom cv2.circle(img, tuple(tag.corners[3].astype(int)), 4, (0, 0, 255), 2) # left-bottom
return class_ids, rules, data pipeline = ( rs.pipeline() ) # <- Объект pipeline содержит методы для взаимодействия с потоком config = rs.config() # <- Дополнительный объект для хранения настроек потока colorizer = rs.colorizer() config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30) config.enable_stream(rs.stream.depth, 848, 480, rs.format.z16, 30) profile = pipeline.start(config) detector = apriltag.Detector( families="tag36h11", nthreads=1, quad_decimate=1.0, quad_sigma=0.0, refine_edges=1, decode_sharpening=0.25, debug=0, ) font = cv2.FONT_HERSHEY_COMPLEX print("START") intr = profile.get_stream( rs.stream.color).as_video_stream_profile().get_intrinsics() depth_intrin = (profile.get_stream( rs.stream.depth).as_video_stream_profile().get_intrinsics()) fx = float(intr.fx) # Focal length of x fy = float(intr.fy) # Focal length of y ppx = float(intr.ppx) # Principle Point Offsey of x (aka. cx) ppy = float(intr.ppy) # Principle Point Offsey of y (aka. cy) camera_params = [fx, fy, ppx, ppy]