import numpy as np
import cv2
import glob
import matplotlib.pyplot as plt

def camera_calibrate(images_list, nx=9, ny=6, show_corners=False):
    # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
    objp = np.zeros((ny*nx,3), np.float32)
    objp[:,:2] = np.mgrid[0:nx,0:ny].T.reshape(-1,2)

    # Arrays to store object points and image points from all the images.
    objpoints = [] # 3d points in real world space
    imgpoints = [] # 2d points in image plane.

    # Make a list of calibration images
    images = glob.glob(images_list)
    
    if show_corners:
        fig = plt.figure(figsize=(30, 30))
        rows = 5
        cols = 4

    # Step through the list and search for chessboard corners
    for idx, fname in enumerate(images):
        img = cv2.imread(fname)
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

        # Find the chessboard corners
        ret, corners = cv2.findChessboardCorners(gray, (nx,ny),None)

        # If found, add object points, image points
        if ret == True:
            objpoints.append(objp)
            imgpoints.append(corners)

            # Draw and display the corners
            if show_corners:
                img = cv2.drawChessboardCorners(img, (nx,ny), corners, ret)
                ax = plt.subplot(rows, cols, idx + 1)
                ax.set_title(fname)
                plt.imshow(img)

    return cv2.calibrateCamera(objpoints, imgpoints, gray.shape[1::-1], None, None)
[((8, 11, 8, 42), 'numpy.zeros', 'np.zeros', ({(8, 20, 8, 29): '(ny * nx, 3)', (8, 31, 8, 41): 'np.float32'}, {}), '((ny * nx, 3), np.float32)', True, 'import numpy as np\n'), ((16, 13, 16, 35), 'glob.glob', 'glob.glob', ({(16, 23, 16, 34): 'images_list'}, {}), '(images_list)', False, 'import glob\n'), ((43, 11, 43, 83), 'cv2.calibrateCamera', 'cv2.calibrateCamera', ({(43, 31, 43, 40): 'objpoints', (43, 42, 43, 51): 'imgpoints', (43, 53, 43, 70): 'gray.shape[1::-1]', (43, 72, 43, 76): 'None', (43, 78, 43, 82): 'None'}, {}), '(objpoints, imgpoints, gray.shape[1::-1], None, None)', False, 'import cv2\n'), ((19, 14, 19, 42), 'matplotlib.pyplot.figure', 'plt.figure', (), '', True, 'import matplotlib.pyplot as plt\n'), ((25, 14, 25, 31), 'cv2.imread', 'cv2.imread', ({(25, 25, 25, 30): 'fname'}, {}), '(fname)', False, 'import cv2\n'), ((26, 15, 26, 51), 'cv2.cvtColor', 'cv2.cvtColor', ({(26, 28, 26, 31): 'img', (26, 32, 26, 50): 'cv2.COLOR_BGR2GRAY'}, {}), '(img, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((29, 23, 29, 68), 'cv2.findChessboardCorners', 'cv2.findChessboardCorners', ({(29, 49, 29, 53): 'gray', (29, 55, 29, 62): '(nx, ny)', (29, 63, 29, 67): 'None'}, {}), '(gray, (nx, ny), None)', False, 'import cv2\n'), ((38, 22, 38, 75), 'cv2.drawChessboardCorners', 'cv2.drawChessboardCorners', ({(38, 48, 38, 51): 'img', (38, 53, 38, 60): '(nx, ny)', (38, 62, 38, 69): 'corners', (38, 71, 38, 74): 'ret'}, {}), '(img, (nx, ny), corners, ret)', False, 'import cv2\n'), ((39, 21, 39, 53), 'matplotlib.pyplot.subplot', 'plt.subplot', ({(39, 33, 39, 37): 'rows', (39, 39, 39, 43): 'cols', (39, 45, 39, 52): 'idx + 1'}, {}), '(rows, cols, idx + 1)', True, 'import matplotlib.pyplot as plt\n'), ((41, 16, 41, 31), 'matplotlib.pyplot.imshow', 'plt.imshow', ({(41, 27, 41, 30): 'img'}, {}), '(img)', True, 'import matplotlib.pyplot as plt\n')]