-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
152 lines (128 loc) · 5.55 KB
/
setup.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import argparse
parser = argparse.ArgumentParser(description="This script performs various operations related to device setup and testing, such as checking CUDA availability, checking connected cameras, and capturing images.")
parser.add_argument('--check_cuda_available', action='store_true', help='Check if CUDA is available on this machine.')
parser.add_argument('--check_model_latest_version', action='store_true', help='Check the latest version of the model.')
parser.add_argument('--local_camera_check', action='store_true', help='List all local cameras and check their availability.')
parser.add_argument('--check_resolution', action='store_true', help='Check the resolution settings of the specified camera.')
parser.add_argument('--capture_image', action='store_true', help='Capture an image using the specified camera settings.')
parser.add_argument('--clear_logs', action='store_true', help='Clear log files and directories.')
args = parser.parse_args()
if args.check_cuda_available:
print("\n\n======== Cuda check ======== ")
print("checking...")
import torch
if torch.cuda.is_available():
print("(info) Cuda : True, Cuda is available !")
else:
print("(info) Cuda : False, Cuda is not available.")
print("(Warning) Model inference using the CPU may have slower inference speed.")
elif args.check_model_latest_version:
print("\n\n======== Model Version ======== ")
print("checking...")
from utils.util_functions import check_model
latest_version = check_model()
print(f"- latest model version : {latest_version}")
elif args.local_camera_check:
print("\n\n======== Local Camera List ======== ")
print("checking...")
from matplotlib import pyplot as plt
import cv2
camera_indices = [0, 1, 2, 3, 4, 5]
num_cameras = len(camera_indices)
cols = 3
rows = (num_cameras + 1) // cols
fig, axs = plt.subplots(rows, cols, figsize=(10, 5 * rows))
axs = axs.flatten()
valid_camera_index = []
print(f"- checking camera index ... ", end=" ")
for index, ax in zip(camera_indices, axs):
print(index, end=" ")
cap = cv2.VideoCapture(index)
ret, frame = cap.read()
if not ret:
ax.axis("off")
else:
ax.set_title(f"Camera at index {index}")
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
ax.imshow(rgb_frame)
ax.axis("off")
valid_camera_index.append(index)
cap.release()
for ax in axs[num_cameras:]:
ax.axis("off")
plt.tight_layout()
plt.show()
elif args.check_resolution:
import config as cf
from utils.util_functions import get_resolution_config
print("\n\n======== Camera Resolution Check ======== ")
print("Camera information")
print(f"- camera_idx : {cf.camera_idx}")
print(f"- camera_rotation_180 : {cf.camera_rotation_180}")
print("------------------- \n")
print("checking...")
import time
import cv2
cap = cv2.VideoCapture(cf.camera_idx)
resolutions = ["FHD", "HD", "qHD"]
for idx, target_resolution in enumerate(resolutions):
width, height, _, __ = get_resolution_config(target_resolution)
print(f"- {target_resolution} ({width}x{height}) : ", end="")
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
actual_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
actual_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
valid_resolution = (actual_width == width) and (actual_height == height)
if valid_resolution:
print(f"O / FPS: {int(cap.get(cv2.CAP_PROP_FPS))}")
else:
print("X")
cap.release()
elif args.capture_image:
import config as cf
from utils.util_functions import get_resolution_config
print("\n\n======== Image Capture ======== ")
print("Camera information")
print(f"- camera_idx : {cf.camera_idx}")
print(f"- resolution : {cf.resolution}")
print(f"- camera_rotation_180 : {cf.camera_rotation_180}")
print("------------------- \n")
print("capturing...")
import cv2
from matplotlib import pyplot as plt
capture_img_path = "source/capture.jpg"
off_track_temp_path = "source/off_track.temp.jpg"
off_fence_temp_path = "source/off_fence.temp.jpg"
cap = cv2.VideoCapture(cf.camera_idx)
width, height, _, __ = get_resolution_config(cf.resolution)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
ret, frame = cap.read()
if cf.camera_rotation_180:
frame = frame[::-1, ::-1]
cap.release()
cv2.imwrite(capture_img_path, frame)
print(f"(info) The captured image has been saved to {capture_img_path}")
cv2.imwrite(off_track_temp_path, frame)
print(f"(info) The captured image has been saved to {off_track_temp_path}")
cv2.imwrite(off_fence_temp_path, frame)
print(f"(info) The captured image has been saved to {off_fence_temp_path}")
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
plt.imshow(rgb_frame)
plt.show()
elif args.clear_logs:
print("clearing...")
import hparams as hp
import shutil, os
def remove_folder(folder):
shutil.rmtree(folder)
os.makedirs(folder, exist_ok=True)
print(f"(info) {folder} has been cleared")
def remove_file(file_path):
if os.path.exists(file_path):
os.remove(file_path)
print(f"(info) {file_path} has been cleared")
remove_file(hp.df_logs_path)
remove_file(hp.excel_logs_path)
remove_folder(hp.logs_pickle_path)
remove_folder(hp.logs_excel_path)