Fixed serial cam

This commit is contained in:
BOTAlex 2025-03-07 23:32:50 +01:00
parent 5b52184480
commit 51dfac5ea3
4 changed files with 31 additions and 26 deletions

View File

@ -22,10 +22,12 @@ class CameraFactory:
@staticmethod
def get_camera_from_string_type(sourceName: str) -> ICameraSource:
sourceName = str(sourceName) # prevents int to be entered
if sourceName.lower().startswith("COM") or sourceName.lower().startswith("/dev/cu") or sourceName.lower().startswith("/dev/tty"): # Windows # macOS # Linux
if sourceName.lower().startswith("com") or sourceName.lower().startswith("/dev/cu") or sourceName.lower().startswith("/dev/tty"): # Windows # macOS # Linux
print(f"{Fore.CYAN}[INFO] Serial camera selected {Fore.RESET}")
return SerialCamera
elif sourceName.lower() == "udp":
print(f"{Fore.YELLOW}[WARN] UDP selected. Prepare for bugs from BOTAlex. Unfinished and extreme alpha. {Fore.RESET}")
return UDP_Camera
else:
print(f"{Fore.CYAN}[INFO] System camera selected {Fore.RESET}")
return SystemCamera

View File

@ -88,3 +88,27 @@ class ICameraSource:
def set_output_queue(self, camera_output_outgoing: "queue.Queue"):
self.camera_output_outgoing = camera_output_outgoing
def get_stream_fps(self):
"""Based on how many times this method gets called"""
# Calculate the fps.
current_frame_time = time.time()
delta_time = current_frame_time - self.last_frame_time
self.last_frame_time = current_frame_time
# Avoid division by zero
if delta_time > 0:
fps = 1.0 / delta_time
else:
fps = 0
# Smooth the FPS using a moving average
self.fl.append(fps)
if len(self.fl) > 60:
self.fl.pop(0) # Keep the list length constant
# Compute average FPS
fps = sum(self.fl) / len(self.fl)
return fps

View File

@ -15,6 +15,7 @@ from Camera.CameraState import CameraState
from Camera.ICameraSource import ICameraSource
import socket
WAIT_TIME = 0.1
# Serial communication protocol:
# header-begin (2 bytes)
# header-type (2 bytes)
@ -105,32 +106,11 @@ class SerialCamera(ICameraSource):
print(f"{Fore.CYAN}[INFO] Discarding the serial buffer ({conn.in_waiting} bytes){Fore.RESET}")
conn.reset_input_buffer()
self.buffer = b""
# Calculate the fps.
current_frame_time = time.time()
delta_time = current_frame_time - self.last_frame_time
self.last_frame_time = current_frame_time
fps = self.get_stream_fps()
# Avoid division by zero
if delta_time > 0:
fps = 1.0 / delta_time
else:
fps = 0
# Smooth the FPS using a moving average
self.fl.append(fps)
if len(self.fl) > 60:
self.fl.pop(0) # Keep the list length constant
# Compute average FPS
self.fps = sum(self.fl) / len(self.fl)
# Compute bandwidth per second (bps)
self.bps = image.nbytes * self.fps
# Increment frame count
self.frame_number += 1
if should_push:
self.push_image_to_queue(image, self.frame_number, self.fps)
self.push_image_to_queue(image, self.frame_number, fps)
except Exception:
print(
f"{Fore.YELLOW}[WARN] Serial capture source problem, assuming camera disconnected, waiting for reconnect.{Fore.RESET}"

View File

@ -105,7 +105,6 @@ class CameraWidget:
)
self.camera_status_queue = Queue()
CameraFactory
if not self.config.capture_source is None:
self.camera = CameraFactory.get_camera_from_string_type(self.config.capture_source)(