mirror of
https://github.com/EyeTrackVR/EyeTrackVR.git
synced 2025-11-04 14:39:42 +08:00
Fixed serial cam
This commit is contained in:
parent
5b52184480
commit
51dfac5ea3
@ -22,10 +22,12 @@ class CameraFactory:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def get_camera_from_string_type(sourceName: str) -> ICameraSource:
|
def get_camera_from_string_type(sourceName: str) -> ICameraSource:
|
||||||
sourceName = str(sourceName) # prevents int to be entered
|
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
|
return SerialCamera
|
||||||
elif sourceName.lower() == "udp":
|
elif sourceName.lower() == "udp":
|
||||||
print(f"{Fore.YELLOW}[WARN] UDP selected. Prepare for bugs from BOTAlex. Unfinished and extreme alpha. {Fore.RESET}")
|
print(f"{Fore.YELLOW}[WARN] UDP selected. Prepare for bugs from BOTAlex. Unfinished and extreme alpha. {Fore.RESET}")
|
||||||
return UDP_Camera
|
return UDP_Camera
|
||||||
else:
|
else:
|
||||||
|
print(f"{Fore.CYAN}[INFO] System camera selected {Fore.RESET}")
|
||||||
return SystemCamera
|
return SystemCamera
|
||||||
@ -88,3 +88,27 @@ class ICameraSource:
|
|||||||
|
|
||||||
def set_output_queue(self, camera_output_outgoing: "queue.Queue"):
|
def set_output_queue(self, camera_output_outgoing: "queue.Queue"):
|
||||||
self.camera_output_outgoing = camera_output_outgoing
|
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
|
||||||
|
|||||||
@ -15,6 +15,7 @@ from Camera.CameraState import CameraState
|
|||||||
from Camera.ICameraSource import ICameraSource
|
from Camera.ICameraSource import ICameraSource
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
|
WAIT_TIME = 0.1
|
||||||
# Serial communication protocol:
|
# Serial communication protocol:
|
||||||
# header-begin (2 bytes)
|
# header-begin (2 bytes)
|
||||||
# header-type (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}")
|
print(f"{Fore.CYAN}[INFO] Discarding the serial buffer ({conn.in_waiting} bytes){Fore.RESET}")
|
||||||
conn.reset_input_buffer()
|
conn.reset_input_buffer()
|
||||||
self.buffer = b""
|
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
|
|
||||||
|
|
||||||
# Avoid division by zero
|
fps = self.get_stream_fps()
|
||||||
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:
|
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:
|
except Exception:
|
||||||
print(
|
print(
|
||||||
f"{Fore.YELLOW}[WARN] Serial capture source problem, assuming camera disconnected, waiting for reconnect.{Fore.RESET}"
|
f"{Fore.YELLOW}[WARN] Serial capture source problem, assuming camera disconnected, waiting for reconnect.{Fore.RESET}"
|
||||||
|
|||||||
@ -105,7 +105,6 @@ class CameraWidget:
|
|||||||
)
|
)
|
||||||
|
|
||||||
self.camera_status_queue = Queue()
|
self.camera_status_queue = Queue()
|
||||||
CameraFactory
|
|
||||||
|
|
||||||
if not self.config.capture_source is None:
|
if not self.config.capture_source is None:
|
||||||
self.camera = CameraFactory.get_camera_from_string_type(self.config.capture_source)(
|
self.camera = CameraFactory.get_camera_from_string_type(self.config.capture_source)(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user