-
Notifications
You must be signed in to change notification settings - Fork 1
/
cam_check.py
67 lines (52 loc) · 2.44 KB
/
cam_check.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
# ----------------------------------------------------------------------------
# Copyright (C) [2023] Byounggun Park
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ----------------------------------------------------------------------------
import cv2
def calculate_aspect_ratio(frame_width, frame_height):
aspect_ratio = frame_width / frame_height
return aspect_ratio
def main():
# USB 카메라를 사용하기 위해 카메라 인덱스를 설정합니다.
camera_index = 0 # 일반적으로 0부터 시작하여 증가시키면 다른 카메라를 선택할 수 있습니다.
cap = cv2.VideoCapture(camera_index)
if not cap.isOpened():
print(f"Error: Unable to access the camera with index {camera_index}.")
return
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
while True:
ret, frame = cap.read()
if not ret:
print("Error: Failed to grab frame.")
break
# 영상 처리 등 추가 작업을 원한다면 여기에 추가
aspect_ratio = calculate_aspect_ratio(frame_width, frame_height)
# 비율 출력
print(f"The aspect ratio of the camera stream is: {aspect_ratio:.2f}")
cv2.imshow("Camera Stream", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()