-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulticam.py
More file actions
190 lines (160 loc) · 5.52 KB
/
multicam.py
File metadata and controls
190 lines (160 loc) · 5.52 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
#----------------------------------------------------------------------------
# Copyright (c) 2018 FIRST. All Rights Reserved.
# Open Source Software - may be modified and shared by FRC teams. The code
# must be accompanied by the FIRST BSD license file in the root directory of
# the project.
#----------------------------------------------------------------------------
import json
import time
import sys
import os
from cscore import CameraServer, VideoSource, UsbCamera, MjpegServer
from networktables import NetworkTablesInstance
# JSON format:
# {
# "team": <team number>,
# "ntmode": <"client" or "server", "client" if unspecified>
# "cameras": [
# {
# "name": <camera name>
# "path": <path, e.g. "/dev/video0">
# "pixel format": <"MJPEG", "YUYV", etc> // optional
# "width": <video mode width> // optional
# "height": <video mode height> // optional
# "fps": <video mode fps> // optional
# "brightness": <percentage brightness> // optional
# "white balance": <"auto", "hold", value> // optional
# "exposure": <"auto", "hold", value> // optional
# "properties": [ // optional
# {
# "name": <property name>
# "value": <property value>
# }
# ],
# "stream": { // optional
# "properties": [
# {
# "name": <stream property name>
# "value": <stream property value>
# }
# ]
# }
# }
# ]
# }
configFile = "/boot/frc.json"
class CameraConfig: pass
team = None
server = False
cameraConfigs = []
def parseError(str):
"""Report parse error."""
print("config error in '" + configFile + "': " + str, file=sys.stderr)
def readCameraConfig(config):
"""Read single camera configuration."""
cam = CameraConfig()
# name
try:
cam.name = config["name"]
except KeyError:
parseError("could not read camera name")
return False
# path
try:
cam.path = config["path"]
except KeyError:
parseError("camera '{}': could not read path".format(cam.name))
return False
# stream properties
cam.streamConfig = config.get("stream")
cam.config = config
cameraConfigs.append(cam)
return True
def readConfig():
"""Read configuration file."""
global team
global server
# parse file
try:
with open(configFile, "rt") as f:
j = json.load(f)
except OSError as err:
print("could not open '{}': {}".format(configFile, err), file=sys.stderr)
return False
# top level must be an object
if not isinstance(j, dict):
parseError("must be JSON object")
return False
# team number
try:
team = j["team"]
except KeyError:
parseError("could not read team number")
return False
# ntmode (optional)
if "ntmode" in j:
str = j["ntmode"]
if str.lower() == "client":
server = False
elif str.lower() == "server":
server = True
else:
parseError("could not understand ntmode value '{}'".format(str))
# cameras
try:
cameras = j["cameras"]
except KeyError:
parseError("could not read cameras")
return False
for camera in cameras:
if not readCameraConfig(camera):
return False
return True
def startCamera(config):
"""Start running the camera."""
print("Starting camera '{}' on {}".format(config.name, config.path))
inst = CameraServer.getInstance()
camera = UsbCamera(config.name, config.path)
server = inst.startAutomaticCapture(camera=camera, return_server=True)
#server.setResolution(640, 360)
camera.setConfigJson(json.dumps(config.config))
if config.path == "/dev/video0":
camera.setConnectionStrategy(VideoSource.ConnectionStrategy.kKeepOpen)
#camera.setExposureManual(1)
#camera.setWhiteBalanceManual(50)
#os.system("v4l2-ctl -d /dev/video0 -c auto_exposure=1")
#os.system("v4l2-ctl -d /dev/video0 -c exposure_time_absolute=100")
#os.system("")
camera.getProperty("auto_exposure").set(1)
camera.getProperty("exposure_time_absolute").set(50)
camera.getProperty("contrast").set(100)
#camera.getProperty("white_balance_temperature_auto").set(0)
#for prop in camera.enumerateProperties():
#print(prop.getName())
if config.streamConfig is not None:
server.setConfigJson(json.dumps(config.streamConfig))
return camera
def startCameraServer():
if len(sys.argv) >= 2:
configFile = sys.argv[1]
# read configuration
if not readConfig():
sys.exit(1)
# start NetworkTables
ntinst = NetworkTablesInstance.getDefault()
if server:
print("Setting up NetworkTables server")
ntinst.startServer()
else:
print("Setting up NetworkTables client for team {}".format(team))
ntinst.startClientTeam(team)
# start cameras
cameras = []
for cameraConfig in cameraConfigs:
cameras.append(startCamera(cameraConfig))
return cameras
if __name__ == '__main__':
startCameraServer()
while True:
pass