-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
111 lines (96 loc) · 4.12 KB
/
camera.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
import platform
import time
import cv2
import numpy as np
class Camera:
def __init__(
self,
resolution=(1920, 1080),
show_live_capture=False,
):
# Detect if the system is a Raspberry Pi
self.is_raspberry_pi = (
platform.system() == "Linux" and "aarch64" in platform.machine()
)
# Initialize the camera based on the system
self.camera = None
self.show_live_capture = show_live_capture
self.configure_camera(resolution=resolution)
def configure_camera(self, resolution=(1920, 1080)):
"""Configures the camera based on the system (Raspberry Pi or others)."""
if self.is_raspberry_pi:
# Raspberry Pi spezifische Konfiguration mit PiCamera2
from picamera2 import Picamera2
from libcamera import controls
self.camera = Picamera2()
# Konfiguriere die PiCamera2 mit benutzerdefinierten Einstellungen
config = self.camera.create_video_configuration(main={"size": resolution})
config["controls"] = {
"AeEnable": False,
"AfMode": controls.AfModeEnum.Manual,
"NoiseReductionMode": controls.draft.NoiseReductionModeEnum.Fast,
"Saturation": 1.1,
"Sharpness": 1.2,
"Contrast": 1.2,
"ExposureTime": 1000, # 1ms
"FrameDurationLimits": (33333, 33333),
}
self.camera.configure(config)
print("📷 PiCamera2 konfiguriert.")
self.camera.start()
else:
# Standard-Webcam-Configuration for MacOS and Windows
# Doesn't need configuration as it's only for testing purposes
self.camera = cv2.VideoCapture(0)
print("📷 Webcam konfiguriert.")
if not self.camera.isOpened():
raise RuntimeError("Error: Could not open webcam.")
print("✅📷 Kamera verbunden.")
def capture_image(self):
"""Captures an image from the camera and returns it as a NumPy array (unified image format for both solutions)."""
if self.is_raspberry_pi:
# Capture image from PiCamera2 and return as NumPy array
frame = self.camera.capture_array()
# Convert the image to RGB format (OpenCV uses BGR)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
else:
# Capture image from a webcam (using OpenCV)
ret, frame = self.camera.read()
if not ret:
print("❌ Bild konnte nicht erfasst werden.")
return None
return frame
def save_image(self, frame, filename: str, with_logs=False):
"""Saves the image in the specified folder with a timestamp in the filename."""
cv2.imwrite(filename, frame)
if with_logs:
print(f"✅📷💾 Gespeichert: {filename.split('/')[-1]}")
def preview_image(self, frame):
"""Displays an image in an OpenCV window."""
cv2.imshow("Live Capture", frame)
def release_camera(self):
"""Releases the camera resources."""
if self.is_raspberry_pi:
self.camera.stop()
else:
self.camera.release()
cv2.destroyAllWindows()
# Beispiel zur Verwendung der Klasse
if __name__ == "__main__":
camera_system = Camera(show_live_capture=True)
start_time = time.time()
try:
while True:
image = camera_system.capture_image()
if image is not None:
camera_system.preview_image(image)
camera_system.save_image(image, "test.jpg")
if cv2.waitKey(1) & 0xFF == ord("q"):
break
time.sleep(0.5)
current_time = time.time()
elapsed_time = (current_time - start_time) * 1000 # Convert to milliseconds
print(f"Time since last image: {elapsed_time:.2f} ms")
start_time = current_time
except KeyboardInterrupt:
camera_system.release_camera()