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)
[((226, 260), 'numpy.zeros', 'np.zeros', (['(ny * nx, 3)', 'np.float32'], {}), '((ny * nx, 3), np.float32)\n', (234, 260), True, 'import numpy as np\n'), ((538, 560), 'glob.glob', 'glob.glob', (['images_list'], {}), '(images_list)\n', (547, 560), False, 'import glob\n'), ((1393, 1465), 'cv2.calibrateCamera', 'cv2.calibrateCamera', (['objpoints', 'imgpoints', 'gray.shape[1::-1]', 'None', 'None'], {}), '(objpoints, imgpoints, gray.shape[1::-1], None, None)\n', (1412, 1465), False, 'import cv2\n'), ((601, 629), 'matplotlib.pyplot.figure', 'plt.figure', ([], {'figsize': '(30, 30)'}), '(figsize=(30, 30))\n', (611, 629), True, 'import matplotlib.pyplot as plt\n'), ((782, 799), 'cv2.imread', 'cv2.imread', (['fname'], {}), '(fname)\n', (792, 799), False, 'import cv2\n'), ((815, 852), 'cv2.cvtColor', 'cv2.cvtColor', (['img', 'cv2.COLOR_BGR2GRAY'], {}), '(img, cv2.COLOR_BGR2GRAY)\n', (827, 852), False, 'import cv2\n'), ((914, 961), 'cv2.findChessboardCorners', 'cv2.findChessboardCorners', (['gray', '(nx, ny)', 'None'], {}), '(gray, (nx, ny), None)\n', (939, 961), False, 'import cv2\n'), ((1205, 1259), 'cv2.drawChessboardCorners', 'cv2.drawChessboardCorners', (['img', '(nx, ny)', 'corners', 'ret'], {}), '(img, (nx, ny), corners, ret)\n', (1230, 1259), False, 'import cv2\n'), ((1280, 1312), 'matplotlib.pyplot.subplot', 'plt.subplot', (['rows', 'cols', '(idx + 1)'], {}), '(rows, cols, idx + 1)\n', (1291, 1312), True, 'import matplotlib.pyplot as plt\n'), ((1365, 1380), 'matplotlib.pyplot.imshow', 'plt.imshow', (['img'], {}), '(img)\n', (1375, 1380), True, 'import matplotlib.pyplot as plt\n')]