Merge pull request #41 from Dev-Sox/main

Added support for camera selection
This commit is contained in:
Rames The Generic authored and GitHub committed 2024-08-13 12:54:11 -04:00
commit 67637fd4df
4 files changed
+120 -64

No files matched your search

+33 -17
View File
@@ -8,6 +8,7 @@ import time
from colorama import Fore
from config import BabbleConfig, BabbleSettingsConfig
from utils.misc_utils import get_camera_index_by_name, list_camera_names
from enum import Enum
WAIT_TIME = 0.1
@@ -39,11 +40,14 @@ class Camera:
camera_output_outgoing: "queue.Queue(maxsize=2)",
settings: BabbleSettingsConfig,
):
self.current_capture_sourc = None
self.camera_status = CameraState.CONNECTING
self.config = config
self.settings = settings
self.camera_index = camera_index
self.camera_address = config.capture_source
self.camera_list = list_camera_names()
# The variable is not used
# self.camera_address = config.capture_source
self.camera_status_outgoing = camera_status_outgoing
self.camera_output_outgoing = camera_output_outgoing
self.capture_event = capture_event
@@ -81,10 +85,11 @@ class Camera:
# If things aren't open, retry until they are. Don't let read requests come in any earlier
# than this, otherwise we can deadlock ourselves.
if (
self.config.capture_source != None and self.config.capture_source != ""
self.config.capture_source is not None and self.config.capture_source != ""
):
self.current_capture_source = self.config.capture_source
if ("COM" in str(self.config.capture_source)):
if "COM" in str(self.config.capture_source) and self.config.capture_source not in self.camera_list:
if (
self.serial_connection is None
or self.camera_status == CameraState.DISCONNECTED
@@ -105,14 +110,23 @@ class Camera:
# firmware. Fickle things.
if self.cancellation_event.wait(WAIT_TIME):
return
self.current_capture_source = self.config.capture_source
if self.config.use_ffmpeg == True:
if self.config.capture_source not in self.camera_list:
self.current_capture_source = self.config.capture_source
else:
self.current_capture_source = get_camera_index_by_name(self.current_capture_source)
if self.config.use_ffmpeg:
self.cv2_camera = cv2.VideoCapture(self.current_capture_source, cv2.CAP_FFMPEG)
else:
self.cv2_camera = cv2.VideoCapture(self.current_capture_source)
if not self.settings.gui_cam_resolution_x == 0: self.cv2_camera.set(cv2.CAP_PROP_FRAME_WIDTH, self.settings.gui_cam_resolution_x)
if not self.settings.gui_cam_resolution_y == 0: self.cv2_camera.set(cv2.CAP_PROP_FRAME_HEIGHT, self.settings.gui_cam_resolution_y)
if not self.settings.gui_cam_framerate == 0: self.cv2_camera.set(cv2.CAP_PROP_FPS, self.settings.gui_cam_framerate)
if not self.settings.gui_cam_resolution_x == 0: self.cv2_camera.set(cv2.CAP_PROP_FRAME_WIDTH,
self.settings.gui_cam_resolution_x)
if not self.settings.gui_cam_resolution_y == 0: self.cv2_camera.set(cv2.CAP_PROP_FRAME_HEIGHT,
self.settings.gui_cam_resolution_y)
if not self.settings.gui_cam_framerate == 0: self.cv2_camera.set(cv2.CAP_PROP_FPS,
self.settings.gui_cam_framerate)
should_push = False
else:
# We don't have a capture source to try yet, wait for one to show up in the GUI.
@@ -124,8 +138,8 @@ class Camera:
# python event as a context-less, resettable one-shot channel.
if should_push and not self.capture_event.wait(timeout=0.02):
continue
if self.config.capture_source != None:
if ("COM" in str(self.config.capture_source)):
if self.config.capture_source is not None:
if "COM" in str(self.config.capture_source):
self.get_serial_camera_picture(should_push)
else:
self.get_cv2_camera_picture(should_push)
@@ -162,7 +176,8 @@ class Camera:
if should_push:
self.push_image_to_queue(image, frame_number, self.fps)
except:
print(f"{Fore.YELLOW}[WARN] Capture source problem, assuming camera disconnected, waiting for reconnect.{Fore.RESET}")
print(
f"{Fore.YELLOW}[WARN] Capture source problem, assuming camera disconnected, waiting for reconnect.{Fore.RESET}")
self.camera_status = CameraState.DISCONNECTED
pass
@@ -182,8 +197,8 @@ class Camera:
def get_next_jpeg_frame(self):
beg, end = self.get_next_packet_bounds()
jpeg = self.buffer[beg+ETVR_HEADER_LEN:end+ETVR_HEADER_LEN]
self.buffer = self.buffer[end+ETVR_HEADER_LEN:]
jpeg = self.buffer[beg + ETVR_HEADER_LEN:end + ETVR_HEADER_LEN]
self.buffer = self.buffer[end + ETVR_HEADER_LEN:]
return jpeg
def get_serial_camera_picture(self, should_push):
@@ -226,7 +241,8 @@ class Camera:
if should_push:
self.push_image_to_queue(image, self.frame_number, self.fps)
except Exception:
print(f"{Fore.YELLOW}[WARN] Serial capture source problem, assuming camera disconnected, waiting for reconnect.{Fore.RESET}")
print(
f"{Fore.YELLOW}[WARN] Serial capture source problem, assuming camera disconnected, waiting for reconnect.{Fore.RESET}")
conn.close()
self.camera_status = CameraState.DISCONNECTED
pass
@@ -244,13 +260,13 @@ class Camera:
return
try:
conn = serial.Serial(
baudrate = 3000000,
port = port,
baudrate=3000000,
port=port,
xonxoff=False,
dsrdtr=False,
rtscts=False)
# Set explicit buffer size for serial.
conn.set_buffer_size(rx_size = 32768, tx_size = 32768)
conn.set_buffer_size(rx_size=32768, tx_size=32768)
print(f"{Fore.CYAN}[INFO] ETVR Serial Tracker device connected on {port}{Fore.RESET}")
self.serial_connection = conn
+61 -46
View File
@@ -1,19 +1,17 @@
import PySimpleGUI as sg
from config import BabbleConfig
from config import BabbleSettingsConfig
from collections import deque
from threading import Event, Thread
from babble_processor import BabbleProcessor, CamInfoOrigin
from landmark_processor import LandmarkProcessor
from enum import Enum
from queue import Queue, Empty
from camera import Camera, CameraState
from osc import Tab
from threading import Event, Thread
import PySimpleGUI as sg
import cv2
import sys
from utils.misc_utils import PlaySound,SND_FILENAME,SND_ASYNC
import traceback
import numpy as np
from babble_processor import BabbleProcessor, CamInfoOrigin
from camera import Camera, CameraState
from config import BabbleConfig
from landmark_processor import LandmarkProcessor
from osc import Tab
from utils.misc_utils import PlaySound, SND_FILENAME, SND_ASYNC, list_camera_names
class CameraWidget:
def __init__(self, widget_id: Tab, main_config: BabbleConfig, osc_queue: Queue):
@@ -44,6 +42,7 @@ class CameraWidget:
self.settings_config = main_config.settings
self.config = main_config.cam
self.settings = main_config.settings
self.camera_list = list_camera_names()
if self.cam_id == Tab.CAM:
self.config = main_config.cam
else:
@@ -92,17 +91,17 @@ class CameraWidget:
self.roi_layout = [
[
sg.Button("Auto ROI", key=self.gui_autoroi, button_color='#539e8a', tooltip = "Automatically set ROI",),
sg.Button("Auto ROI", key=self.gui_autoroi, button_color='#539e8a', tooltip="Automatically set ROI", ),
],
[
sg.Graph(
(640, 480),
(0, 480),
(640, 0),
key=self.gui_roi_selection,
drag_submits=True,
enable_events=True,
background_color='#424042',
sg.Graph(
(640, 480),
(0, 480),
(640, 0),
key=self.gui_roi_selection,
drag_submits=True,
enable_events=True,
background_color='#424042',
)
]
]
@@ -117,12 +116,14 @@ class CameraWidget:
orientation="h",
key=self.gui_rotation_slider,
background_color='#424042',
tooltip = "Adjust the rotation of your cameras, make them level.",
tooltip="Adjust the rotation of your cameras, make them level.",
),
],
[
sg.Button("Start Calibration", key=self.gui_restart_calibration, button_color='#539e8a', tooltip = "Start calibration. Look all arround to all extreams without blinking until sound is heard.",),
sg.Button("Stop Calibration", key=self.gui_stop_calibration, button_color='#539e8a', tooltip = "Stop calibration manualy.",),
sg.Button("Start Calibration", key=self.gui_restart_calibration, button_color='#539e8a',
tooltip="Start calibration. Look all arround to all extreams without blinking until sound is heard.", ),
sg.Button("Stop Calibration", key=self.gui_stop_calibration, button_color='#539e8a',
tooltip="Stop calibration manualy.", ),
],
[
sg.Checkbox(
@@ -152,7 +153,7 @@ class CameraWidget:
default=self.config.gui_vertical_flip,
key=self.gui_vertical_flip,
background_color='#424042',
tooltip = "Vertically flip camera feed.",
tooltip="Vertically flip camera feed.",
),
sg.Checkbox(
"Horizontal Flip:",
@@ -171,14 +172,19 @@ class CameraWidget:
self.widget_layout = [
[
sg.Text("Camera Address", background_color='#424042'),
sg.InputText(self.config.capture_source, key=self.gui_camera_addr, tooltip = "Enter the IP address or UVC port of your camera. (Include the 'http://')",),
sg.InputCombo(self.camera_list, default_value=self.config.capture_source,
key=self.gui_camera_addr,
tooltip="Enter the IP address or UVC port of your camera. (Include the 'http://')",
enable_events=True)
],
[
sg.Button("Save and Restart Tracking", key=self.gui_save_tracking_button, button_color='#539e8a'),
],
[
sg.Button("Tracking Mode", key=self.gui_tracking_button, button_color='#539e8a', tooltip = "Go here to track your mouth.",),
sg.Button("Cropping Mode", key=self.gui_roi_button, button_color='#539e8a', tooltip = "Go here to crop out your mouth.",),
sg.Button("Tracking Mode", key=self.gui_tracking_button, button_color='#539e8a',
tooltip="Go here to track your mouth.", ),
sg.Button("Cropping Mode", key=self.gui_roi_button, button_color='#539e8a',
tooltip="Go here to crop out your mouth.", ),
],
[
sg.Column(self.tracking_layout, key=self.gui_tracking_layout, background_color='#424042'),
@@ -237,29 +243,39 @@ class CameraWidget:
changed = False
# If anything has changed in our configuration settings, change/update those.
if (
event == self.gui_save_tracking_button
and values[self.gui_camera_addr] != self.config.capture_source
event == self.gui_save_tracking_button
and values[self.gui_camera_addr] != self.config.capture_source
):
print("\033[94m[INFO] New value: {}\033[0m".format(values[self.gui_camera_addr]))
value = values[self.gui_camera_addr]
print("\033[94m[INFO] New value: {}\033[0m".format(value))
try:
self.config.use_ffmpeg = False
# Try storing ints as ints, for those using wired cameras.
self.config.capture_source = int(values[self.gui_camera_addr])
if value not in self.camera_list:
self.config.capture_source = int(value)
else:
self.config.capture_source = value
except ValueError:
if values[self.gui_camera_addr] == "":
if value == "":
self.config.capture_source = None
else:
if len(values[self.gui_camera_addr]) > 5 and "http" not in values[self.gui_camera_addr] and ".mp4" not in values[self.gui_camera_addr] and "udp" not in values[self.gui_camera_addr]: # If http is not in camera address, add it.
self.config.capture_source = f"http://{values[self.gui_camera_addr]}/"
elif "udp" in values[self.gui_camera_addr]:
# If http is not in camera address, add it.
self.config.capture_source = value
if "udp" in value:
self.config.use_ffmpeg = True
self.config.capture_source = values[self.gui_camera_addr]
else:
self.config.capture_source = values[self.gui_camera_addr]
elif (
"http" not in value
and ".mp4" not in value
and "udp" not in value
and "COM" not in value
and "/dev/tty" not in value
and value not in self.camera_list
): # If http is not in camera address, add it.
self.config.capture_source = f"http://{values[self.gui_camera_addr]}/"
changed = True
if self.config.rotation_angle != values[self.gui_rotation_slider]:
self.config.rotation_angle = int(values[self.gui_rotation_slider])
changed = True
@@ -302,9 +318,9 @@ class CameraWidget:
# Event for mouse button up in ROI mode
self.is_mouse_up = True
if self.x1 < 0:
self.x1 = 0
self.x1 = 0
if self.y1 < 0:
self.y1 = 0
self.y1 = 0
if abs(self.x0 - self.x1) != 0 and abs(self.y0 - self.y1) != 0:
self.config.roi_window_x = min([self.x0, self.x1])
self.config.roi_window_y = min([self.y0, self.y1])
@@ -370,7 +386,7 @@ class CameraWidget:
window[self.gui_tracking_bps].update(self._movavg_bps(self.camera.bps))
if self.in_roi_mode:
try:
try:
if self.roi_queue.empty():
self.capture_event.set()
maybe_image = self.roi_queue.get(block=False)
@@ -399,7 +415,6 @@ class CameraWidget:
imgbytes = cv2.imencode(".ppm", maybe_image)[1].tobytes()
window[self.gui_tracking_image].update(data=imgbytes)
# Relay information to OSC
if cam_info.info_type != CamInfoOrigin.FAILURE:
self.osc_queue.put((self.cam_id, cam_info))
+25 -1
View File
@@ -1,13 +1,37 @@
import os
import typing
from pygrabber.dshow_graph import FilterGraph
is_nt = True if os.name == "nt" else False
graph = FilterGraph()
def list_camera_names():
cam_list = graph.get_input_devices()
cam_names = []
for index, name in enumerate(cam_list):
cam_names.append(name)
return cam_names
def get_camera_index_by_name(name):
cam_list = graph.get_input_devices()
for i, device_name in enumerate(cam_list):
if device_name == name:
return i
return None
def PlaySound(*args, **kwargs): pass
SND_FILENAME = SND_ASYNC = 1
if is_nt:
import winsound
PlaySound = winsound.PlaySound
SND_FILENAME = winsound.SND_FILENAME
SND_ASYNC = winsound.SND_ASYNC
SND_ASYNC = winsound.SND_ASYNC
+1
View File
@@ -11,3 +11,4 @@ pyserial
colorama
winotify
poetry
pygrabber