Skip to content

Commit 857626a

Browse files
Fix UVC Camera's not changing + List COM ports
1 parent d4fa6d1 commit 857626a

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

BabbleApp/camera.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ def run(self):
8787
if (
8888
self.config.capture_source is not None and self.config.capture_source != ""
8989
):
90-
self.current_capture_source = self.config.capture_source
91-
92-
if "COM" in str(self.config.capture_source) and self.config.capture_source not in self.camera_list:
90+
if "COM" in str(self.config.capture_source):
9391
if (
9492
self.serial_connection is None
9593
or self.camera_status == CameraState.DISCONNECTED
@@ -114,7 +112,7 @@ def run(self):
114112
if self.config.capture_source not in self.camera_list:
115113
self.current_capture_source = self.config.capture_source
116114
else:
117-
self.current_capture_source = get_camera_index_by_name(self.current_capture_source)
115+
self.current_capture_source = get_camera_index_by_name(self.config.capture_source)
118116

119117
if self.config.use_ffmpeg:
120118
self.cv2_camera = cv2.VideoCapture(self.current_capture_source, cv2.CAP_FFMPEG)
@@ -139,7 +137,8 @@ def run(self):
139137
if should_push and not self.capture_event.wait(timeout=0.02):
140138
continue
141139
if self.config.capture_source is not None:
142-
if "COM" in str(self.config.capture_source):
140+
ports = ("COM", "/dev/tty")
141+
if any(x in str(self.config.capture_source) for x in ports):
143142
self.get_serial_camera_picture(should_push)
144143
else:
145144
self.get_cv2_camera_picture(should_push)

BabbleApp/camera_widget.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from config import BabbleConfig
1111
from landmark_processor import LandmarkProcessor
1212
from osc import Tab
13-
from utils.misc_utils import PlaySound, SND_FILENAME, SND_ASYNC, list_camera_names
13+
from utils.misc_utils import PlaySound, SND_FILENAME, SND_ASYNC, list_camera_names, get_camera_index_by_name
1414

1515

1616
class CameraWidget:
@@ -245,9 +245,13 @@ def render(self, window, event, values):
245245
self.config.use_ffmpeg = False
246246
# Try storing ints as ints, for those using wired cameras.
247247
if value not in self.camera_list:
248-
self.config.capture_source = int(value)
249-
else:
250248
self.config.capture_source = value
249+
else:
250+
#if "COM" not in value:
251+
ports = ("COM", "/dev/tty")
252+
if any(x in str(self.config.capture_source) for x in ports):
253+
self.config.capture_source = get_camera_index_by_name(value)
254+
else: self.config.capture_source = value
251255
except ValueError:
252256
if value == "":
253257
self.config.capture_source = None

BabbleApp/utils/misc_utils.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import os
21
import typing
2+
import serial
3+
import sys
4+
import glob
35
from pygrabber.dshow_graph import FilterGraph
46

5-
is_nt = True if os.name == "nt" else False
7+
is_nt = True if sys.platform.startswith('win') else False
68
graph = FilterGraph()
79

810

@@ -11,18 +13,54 @@ def list_camera_names():
1113
cam_names = []
1214
for index, name in enumerate(cam_list):
1315
cam_names.append(name)
16+
cam_names = cam_names + list_serial_ports()
1417
return cam_names
1518

19+
def list_serial_ports():
20+
""" Lists serial port names
21+
22+
:raises EnvironmentError:
23+
On unsupported or unknown platforms
24+
:returns:
25+
A list of the serial ports available on the system
26+
"""
27+
if sys.platform.startswith('win'):
28+
ports = ['COM%s' % (i + 1) for i in range(256)]
29+
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
30+
# this excludes your current terminal "/dev/tty"
31+
ports = glob.glob('/dev/tty[A-Za-z]*')
32+
elif sys.platform.startswith('darwin'):
33+
ports = glob.glob('/dev/tty.*')
34+
else:
35+
raise EnvironmentError('Unsupported platform')
36+
37+
result = []
38+
for port in ports:
39+
try:
40+
s = serial.Serial(port)
41+
s.close()
42+
result.append(port)
43+
except (OSError, serial.SerialException):
44+
pass
45+
return result
46+
1647

1748
def get_camera_index_by_name(name):
49+
#cam_list = list_camera_names()
1850
cam_list = graph.get_input_devices()
51+
#rint(f"name: {name}")
52+
#rint(f"cam_list: {cam_list}")
1953

2054
for i, device_name in enumerate(cam_list):
2155
if device_name == name:
2256
return i
2357

2458
return None
2559

60+
#def get_serial_port(name):
61+
# for i, device in enumerate(cam_list):
62+
63+
2664

2765
def PlaySound(*args, **kwargs): pass
2866

0 commit comments

Comments
 (0)