forked from Dat-Bois/droneAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture_calib_images.py
79 lines (61 loc) · 2.29 KB
/
capture_calib_images.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
75
76
77
78
79
import cv2 as cv
import os
CHESS_BOARD_DIM = (8, 5)
n = 0 # image counter
# Check if images directory exists; if not, create it
image_dir_path = "images"
CHECK_DIR = os.path.isdir(image_dir_path)
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):
# Attempt to find chessboard corners
ret, corners = cv.findChessboardCorners(grayImage, boardDimension)
if ret: # If chessboard corners are found
corners1 = cv.cornerSubPix(grayImage, corners, (3, 3), (-1, -1), criteria)
image = cv.drawChessboardCorners(image, boardDimension, corners1, ret)
return image, True # Return the modified image and True if corners are detected
else:
# Return the original image and False if no corners are detected
return image, False
cap = cv.VideoCapture(1)
while True:
ret, frame = cap.read()
if not ret:
print("Error: Failed to capture image from the camera.")
break
copyFrame = frame.copy()
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# Call the updated function to detect chessboard corners
image, board_detected = detect_checker_board(frame, gray, criteria, CHESS_BOARD_DIM)
# Display the number of saved images
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
# Save the image if 's' is pressed and a board is detected
if key == ord("s") and board_detected:
if n < 30:
cv.imwrite(f"{image_dir_path}/image{n}.png", copyFrame)
print(f"Saved image number {n}")
n += 1 # Increment the image counter
else:
print("Reached the limit of 30 images.")
break # Exit the loop after saving 30 images
cap.release()
cv.destroyAllWindows()
print("Total saved images:", n)