<commit_before>__author__ = 'Mark'

import cv2
from pattern_type import PatternType
import numpy as np

class CameraCalibrator:
    def __init__(self, pattern_type, pattern_dims):
        if pattern_type is not PatternType.Checkerboard:
            raise NotImplementedError("Currently implemented only for checkerboard pattern")

        self.pattern_dims = pattern_dims

        #Position of calibration points in real world: just a grid on x,y dimensions (z=0)
        self._pattern_object_points = np.zeros((self.pattern_dims[0] * self.pattern_dims[1], 3), np.float32)
        self._pattern_object_points[:, :2] = np.mgrid[0:self.pattern_dims[0], 0: self.pattern_dims[1]].T.reshape(-1, 2)

        self.number_of_samples = 0
        self._calibration_object_points = []
        self._calibration_samples = []

    def clear(self):
        self.number_of_samples = 0
        self._calibration_object_points = []
        self._calibration_samples = []


    def add_sample(self, pattern_points):
        self._calibration_object_points.append(self._pattern_object_points)
        self._calibration_samples.append(pattern_points)
        self.number_of_samples += 1

    def calibrate(self, input_image_shape):
        h, w = input_image_shape[0:2]
        ret, camera_matrix, dist, rvecs, tvecs = \
            cv2.calibrateCamera(self._calibration_object_points, self._calibration_samples, (w, h), None, None)
        new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist, (w, h), 0, (w, h))

        #map_x, map_y = cv2.initUndistortRectifyMap(camera_matrix, dist, None, new_camera_matrix, (w, h), 5)

        map_x, map_y = cv2.initUndistortRectifyMap(camera_matrix, dist, None, new_camera_matrix, (w, h), 5)


        print("Width, Height = {}, {}".format(w, h))
        print("Original Camera Matrix = \n{}".format(camera_matrix))
        matrix, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist, (w, h), 0, (w, h))
        print("0 Camera Matrix = \n{} \nroi = {}".format(matrix, roi))
        matrix, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist, (w, h), 1, (w, h))
        print("1 Camera Matrix = \n{} \nroi = {}".format(matrix, roi))
        print("distortion coeffs (k1,k2,p1,p2[,k3[,k4,k5,k6]]) = {}".format(dist))
        return ret, map_x, map_y, roi<commit_msg>Cleanup save calibration results to file<commit_after>import time

__author__ = 'Mark'

import cv2
from pattern_type import PatternType
import numpy as np

class CameraCalibrator:
    def __init__(self, pattern_type, pattern_dims):
        if pattern_type is not PatternType.Checkerboard:
            raise NotImplementedError("Currently implemented only for checkerboard pattern")

        self.pattern_dims = pattern_dims

        #Position of calibration points in real world: just a grid on x,y dimensions (z=0)
        self._pattern_object_points = np.zeros((self.pattern_dims[0] * self.pattern_dims[1], 3), np.float32)
        self._pattern_object_points[:, :2] = np.mgrid[0:self.pattern_dims[0], 0: self.pattern_dims[1]].T.reshape(-1, 2)

        self.number_of_samples = 0
        self._calibration_object_points = []
        self._calibration_samples = []

    def clear(self):
        self.number_of_samples = 0
        self._calibration_object_points = []
        self._calibration_samples = []


    def add_sample(self, pattern_points):
        self._calibration_object_points.append(self._pattern_object_points)
        self._calibration_samples.append(pattern_points)
        self.number_of_samples += 1

    def calibrate(self, input_image_shape):
        h, w = input_image_shape[0:2]
        ret, camera_matrix, dist, rvecs, tvecs = \
            cv2.calibrateCamera(self._calibration_object_points, self._calibration_samples, (w, h), None, None)

        # Calculate new camera matrix
        #new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist, (w, h), 0, (w, h))
        #map_x, map_y = cv2.initUndistortRectifyMap(camera_matrix, dist, None, new_camera_matrix, (w, h), 5)

        # Use the same camera_matrix
        roi = None
        map_x, map_y = cv2.initUndistortRectifyMap(camera_matrix, dist, None, camera_matrix, (w, h), 5)

        # print("Width, Height = {}, {}".format(w, h))
        #print("Original Camera Matrix = \n{}".format(camera_matrix))
        #matrix, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist, (w, h), 0, (w, h))
        #print("0 Camera Matrix = \n{} \nroi = {}".format(matrix, roi))
        #matrix, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist, (w, h), 1, (w, h))
        #print("1 Camera Matrix = \n{} \nroi = {}".format(matrix, roi))
        print("distortion coeffs (k1,k2,p1,p2[,k3[,k4,k5,k6]]) = {}".format(dist))

        np.savetxt("results/{}dist_coeffs".format(time.time()), np.array(dist), delimiter=", ")
        np.savetxt("results/{}camera_matrix".format(time.time()), np.array(camera_matrix), delimiter=", ")

        return ret, map_x, map_y, roi
{'fraction_non_alphanumeric': 0.12781350482315113, 'fraction_numerical': 0.01607717041800643, 'mean_word_length': 3.5, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2716309', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1651, 'n_words': 356}