-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture.py
74 lines (56 loc) · 1.87 KB
/
capture.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#Capturs images for calibration
import cv2 as cv
import os
CHESS_BOARD_DIM = (10, 7)
n = 0 # image_counter
# checking if images dir is exist not, if not then create images directory
image_dir_path = "images"
CHECK_DIR = os.path.isdir(image_dir_path)
# if directory does not exist create
if not CHECK_DIR:
os.makedirs(image_dir_path)
print(f'"{image_dir_path}" Directory is created')
else:
print(f'"{image_dir_path}" Directory already Exists.')
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
def detect_checker_board(image, grayImage, criteria, boardDimension):
print("detecting checker board")
ret, corners = cv.findChessboardCorners(grayImage, boardDimension)
if ret == True:
corners1 = cv.cornerSubPix(grayImage, corners, (3, 3), (-1, -1), criteria)
image = cv.drawChessboardCorners(image, boardDimension, corners1, ret)
else:
# print("Checker board not found")
pass
return image, ret
cap = cv.VideoCapture(1)
while True:
_, frame = cap.read()
frame = cv.resize(frame, (660, 400))
copyFrame = frame.copy()
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
image, board_detected = detect_checker_board(frame, gray, criteria, CHESS_BOARD_DIM)
# print(ret)
cv.putText(
frame,
f"saved_img : {n}",
(30, 40),
cv.FONT_HERSHEY_PLAIN,
1.4,
(0, 255, 0),
2,
cv.LINE_AA,
)
cv.imshow("frame", frame)
cv.imshow("copyFrame", copyFrame)
key = cv.waitKey(1)
if key == ord("q"):
break
if key == ord("s") and board_detected == True:
# storing the checker board image
cv.imwrite(f"{image_dir_path}/image{n}.png", copyFrame)
print(f"saved image number {n}")
n += 1 # incrementing the image counter
cap.release()
cv.destroyAllWindows()
print("Total saved Images:", n)