-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtars_camera.py
More file actions
94 lines (78 loc) · 3.81 KB
/
tars_camera.py
File metadata and controls
94 lines (78 loc) · 3.81 KB
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
# tars_camera.py
from jetcam.csi_camera import CSICamera
import time
import cv2
from tars_config import CAMERA_WIDTH, CAMERA_HEIGHT, CAMERA_FPS
class CameraManager:
"""
카메라 리소스를 중앙에서 관리하는 싱글톤 클래스.
여러 모듈에서 동일한 카메라 인스턴스에 접근할 수 있게 합니다.
"""
_instance = None
_camera = None
_is_initialized = False
_reference_count = 0
@classmethod
def get_instance(cls):
"""싱글톤 인스턴스를 반환합니다."""
if cls._instance is None:
cls._instance = CameraManager()
return cls._instance
# def initialize_camera(self, width=640, height=480, capture_fps=30):
def initialize_camera(self, width=CAMERA_WIDTH, height=CAMERA_HEIGHT, capture_fps=CAMERA_FPS):
"""카메라를 초기화합니다. 이미 초기화된 경우 참조 카운트만 증가합니다."""
if not self._is_initialized:
print("카메라 초기화 중...")
self._camera = CSICamera(width=width, height=height, capture_fps=capture_fps)
self._camera.running = True
# 카메라가 프레임을 읽을 준비가 될 때까지 대기
print("카메라가 준비될 때까지 대기 중...")
while self._camera.value is None:
time.sleep(0.05)
print("✅ 카메라가 준비되었습니다!")
self._is_initialized = True
# 참조 카운트 증가
self._reference_count += 1
print(f"카메라 사용 시작 (참조 카운트: {self._reference_count})")
return self._camera
def release_camera(self):
"""
카메라 참조 카운트를 감소시키고, 참조 카운트가 0이 되면 리소스를 해제합니다.
"""
if not self._is_initialized:
print("카메라가 초기화되지 않았습니다.")
return
self._reference_count -= 1
print(f"카메라 사용 종료 (참조 카운트: {self._reference_count})")
if self._reference_count <= 0:
print("카메라 리소스를 해제합니다...")
self._camera.running = False
# 추가적인 cleanup이 필요한 경우
if hasattr(self._camera, 'cap') and hasattr(self._camera.cap, 'release'):
print("camera.cap 객체를 해제합니다...")
self._camera.cap.release()
print("✅ camera.cap 해제 완료!")
self._camera = None
self._is_initialized = False
self._reference_count = 0
print("✅ 카메라 리소스 해제 완료!")
def get_camera(self):
"""초기화된 카메라 인스턴스를 반환합니다."""
if not self._is_initialized:
raise RuntimeError("카메라가 초기화되지 않았습니다. initialize_camera()를 먼저 호출하세요.")
return self._camera
def get_frame(self):
"""현재 카메라 프레임을 반환합니다."""
if not self._is_initialized:
raise RuntimeError("카메라가 초기화되지 않았습니다. initialize_camera()를 먼저 호출하세요.")
return self._camera.value
def is_initialized(self):
"""카메라가 초기화되었는지 여부를 반환합니다."""
return self._is_initialized
def __del__(self):
"""소멸자: 인스턴스가 파괴될 때 리소스를 안전하게 해제합니다."""
if self._is_initialized:
self._camera.running = False
if hasattr(self._camera, 'cap') and hasattr(self._camera.cap, 'release'):
self._camera.cap.release()
print("카메라 리소스 해제 (소멸자)")