How to do it

You need to complete the following steps:

  1. Import the necessary modules:
import cv2
import numpy as np
  1. Capture frames from the camera, detect a chessboard pattern on each frame, and accumulate the frames and corners until we have a big enough number of samples:
cap = cv2.VideoCapture(0)

pattern_size = (10, 7)

samples = []

while True:
ret, frame = cap.read()
if not ret:
break

res, corners = cv2.findChessboardCorners(frame, pattern_size)

img_show = np.copy(frame)
cv2.drawChessboardCorners(img_show, pattern_size, corners, res)
cv2.putText(img_show, 'Samples captured: %d' % len(samples), (0, 40),
cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2)
cv2.imshow('chessboard', img_show)

wait_time = 0 if res else 30
k = cv2.waitKey(wait_time)

if k == ord('s') and res:
samples.append((cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), corners))
elif k == 27:
break

cap.release()
cv2.destroyAllWindows()
  1. Refine all the detected corner points using cv2.cornerSubPix:
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-3)

for i in range(len(samples)):
img, corners = samples[i]
corners = cv2.cornerSubPix(img, corners, (10, 10), (-1,-1), criteria)
  1. Import the necessary modules, open an input image, and copy it:
pattern_points = np.zeros((1, np.prod(pattern_size), 3), np.float32)
pattern_points[0, :, :2] = np.indices(pattern_size).T.reshape(-1, 2)

images, corners = zip(*samples)

pattern_points = [pattern_points]*len(corners)

print(len(pattern_points), pattern_points[0].shape, pattern_points[0].dtype)
print(len(corners), corners[0].shape, corners[0].dtype)

rms, camera_matrix, dis t_coefs, rvecs, tvecs = \
cv2.fisheye.calibrate(pattern_points, corners, images[0].shape, None, None)

np.save('camera_mat.npy', camera_matrix)
np.save('dist_coefs.npy', dist_coefs)