Add stream settings to webdash and examples (#61)

pull/311/head
Peter Johnson 2019-01-13 16:59:08 -08:00 committed by GitHub
parent 42d270870e
commit 03dae15ddf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 220 additions and 64 deletions

View File

@ -41,7 +41,15 @@
"name": <property name>
"value": <property value>
}
]
],
"stream": { // optional
"properties": [
{
"name": <stream property name>
"value": <stream property value>
}
]
}
}
]
}
@ -58,6 +66,7 @@ struct CameraConfig {
std::string name;
std::string path;
wpi::json config;
wpi::json streamConfig;
};
std::vector<CameraConfig> cameraConfigs;
@ -86,6 +95,9 @@ bool ReadCameraConfig(const wpi::json& config) {
return false;
}
// stream properties
if (config.count("stream") != 0) c.streamConfig = config.at("stream");
c.config = config;
cameraConfigs.emplace_back(std::move(c));
@ -158,12 +170,16 @@ bool ReadConfig() {
cs::UsbCamera StartCamera(const CameraConfig& config) {
wpi::outs() << "Starting camera '" << config.name << "' on " << config.path
<< '\n';
auto camera = frc::CameraServer::GetInstance()->StartAutomaticCapture(
config.name, config.path);
cs::UsbCamera camera{config.name, config.path};
auto inst = frc::CameraServer::GetInstance();
auto server = inst->StartAutomaticCapture(camera);
camera.SetConfigJson(config.config);
camera.SetConnectionStrategy(cs::VideoSource::kConnectionKeepOpen);
if (config.streamConfig.is_object())
server.SetConfigJson(config.streamConfig);
return camera;
}

View File

@ -18,6 +18,8 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import edu.wpi.cscore.MjpegServer;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.cscore.VideoSource;
import edu.wpi.first.cameraserver.CameraServer;
import edu.wpi.first.networktables.NetworkTableInstance;
@ -47,7 +49,15 @@ import org.opencv.core.Mat;
"name": <property name>
"value": <property value>
}
]
],
"stream": { // optional
"properties": [
{
"name": <stream property name>
"value": <stream property value>
}
]
}
}
]
}
@ -61,6 +71,7 @@ public final class Main {
public String name;
public String path;
public JsonObject config;
public JsonElement streamConfig;
}
public static int team;
@ -99,6 +110,9 @@ public final class Main {
}
cam.path = pathElement.getAsString();
// stream properties
cam.streamConfig = config.get("stream");
cam.config = config;
cameraConfigs.add(cam);
@ -167,14 +181,19 @@ public final class Main {
*/
public static VideoSource startCamera(CameraConfig config) {
System.out.println("Starting camera '" + config.name + "' on " + config.path);
VideoSource camera = CameraServer.getInstance().startAutomaticCapture(
config.name, config.path);
UsbCamera camera = new UsbCamera(config.name, config.path);
CameraServer inst = CameraServer.getInstance();
MjpegServer server = inst.startAutomaticCapture(camera);
Gson gson = new GsonBuilder().create();
camera.setConfigJson(gson.toJson(config.config));
camera.setConnectionStrategy(VideoSource.ConnectionStrategy.kKeepOpen);
if (config.streamConfig != null) {
server.setConfigJson(gson.toJson(config.streamConfig));
}
return camera;
}

View File

@ -10,7 +10,7 @@ import json
import time
import sys
from cscore import CameraServer, VideoSource
from cscore import CameraServer, VideoSource, UsbCamera, MjpegServer
from networktables import NetworkTablesInstance
# JSON format:
@ -33,7 +33,15 @@ from networktables import NetworkTablesInstance
# "name": <property name>
# "value": <property value>
# }
# ]
# ],
# "stream": { // optional
# "properties": [
# {
# "name": <stream property name>
# "value": <stream property value>
# }
# ]
# }
# }
# ]
# }
@ -68,6 +76,9 @@ def readCameraConfig(config):
parseError("camera '{}': could not read path".format(cam.name))
return False
# stream properties
cam.streamConfig = config.get("stream")
cam.config = config
cameraConfigs.append(cam)
@ -123,12 +134,16 @@ def readConfig():
"""Start running the camera."""
def startCamera(config):
print("Starting camera '{}' on {}".format(config.name, config.path))
camera = CameraServer.getInstance() \
.startAutomaticCapture(name=config.name, path=config.path)
camera = UsbCamera(config.name, config.path)
inst = CameraServer.getInstance()
server = inst.startAutomaticCapture(camera=camera, return_server=True)
camera.setConfigJson(json.dumps(config.config))
camera.setConnectionStrategy(VideoSource.ConnectionStrategy.kKeepOpen)
if config.streamConfig is not None:
server.setConfigJson(json.dumps(config.streamConfig))
return camera
if __name__ == "__main__":

2
deps/robotpy-cscore vendored

@ -1 +1 @@
Subproject commit 64dbe28376f7a20ced32a7b26ada4b59ea31ae5f
Subproject commit 2590274ca4e9ef852093152be34bde4e56d027f3

View File

@ -38,7 +38,15 @@
"name": <property name>
"value": <property value>
}
]
],
"stream": { // optional
"properties": [
{
"name": <stream property name>
"value": <stream property value>
}
]
}
}
]
}
@ -59,6 +67,7 @@ struct CameraConfig {
std::string name;
std::string path;
wpi::json config;
wpi::json streamConfig;
};
std::vector<CameraConfig> cameras;
@ -87,6 +96,9 @@ bool ReadCameraConfig(const wpi::json& config) {
return false;
}
// stream properties
if (config.count("stream") != 0) c.streamConfig = config.at("stream");
c.config = config;
cameras.emplace_back(std::move(c));
@ -159,11 +171,15 @@ bool ReadConfig() {
void StartCamera(const CameraConfig& config) {
wpi::outs() << "Starting camera '" << config.name << "' on " << config.path
<< '\n';
auto camera = frc::CameraServer::GetInstance()->StartAutomaticCapture(
config.name, config.path);
cs::UsbCamera camera{config.name, config.path};
auto inst = frc::CameraServer::GetInstance();
auto server = inst->StartAutomaticCapture(camera);
camera.SetConfigJson(config.config);
camera.SetConnectionStrategy(cs::VideoSource::kConnectionKeepOpen);
if (config.streamConfig.is_object())
server.SetConfigJson(config.streamConfig);
}
} // namespace

View File

@ -41,7 +41,7 @@ function displayStatus(message) {
// Enable and disable buttons based on connection status
var connectedButtonIds = ['systemRestart', 'networkApproach', 'networkAddress', 'networkMask', 'networkGateway', 'networkDNS', 'visionUp', 'visionDown', 'visionTerm', 'visionKill', 'systemReadOnly', 'systemWritable', 'visionClient', 'visionTeam', 'visionDiscard', 'addConnectedCamera', 'addCamera', 'applicationType'];
var connectedButtonClasses = ['cameraName', 'cameraPath', 'cameraAlternatePaths', 'cameraPixelFormat', 'cameraWidth', 'cameraHeight', 'cameraFps', 'cameraBrightness', 'cameraWhiteBalance', 'cameraExposure', 'cameraProperties', 'cameraRemove', 'cameraCopyConfig']
var connectedButtonClasses = ['cameraName', 'cameraPath', 'cameraAlternatePaths', 'cameraPixelFormat', 'cameraWidth', 'cameraHeight', 'cameraFps', 'cameraBrightness', 'cameraWhiteBalance', 'cameraExposure', 'cameraProperties', 'streamWidth', 'streamHeight', 'streamFps', 'streamCompression', 'streamDefaultCompression', 'cameraRemove', 'cameraCopyConfig']
var writableButtonIds = ['networkSave', 'visionSave', 'applicationSave'];
var systemStatusIds = ['systemMemoryFree1s', 'systemMemoryFree5s',
'systemMemoryAvail1s', 'systemMemoryAvail5s',
@ -340,6 +340,18 @@ $('#visionClient').change(function() {
}
});
function getCameraPropertyValue(data, name) {
if (data === null) {
return null;
}
for (var i = 0; i < data.length; i++) {
if (data[i].name === name) {
return data[i].value;
}
}
return null;
}
function updateVisionCameraView(camera, value) {
if ('name' in value) {
camera.find('.cameraTitle').text('Camera ' + value.name);
@ -356,6 +368,19 @@ function updateVisionCameraView(camera, value) {
camera.find('.cameraWhiteBalance').val(value['white balance']);
camera.find('.cameraExposure').val(value.exposure);
camera.find('.cameraProperties').val(JSON.stringify(value.properties));
if ('stream' in value && 'properties' in value.stream) {
camera.find('.streamWidth').val(getCameraPropertyValue(value.stream.properties, 'width'));
camera.find('.streamHeight').val(getCameraPropertyValue(value.stream.properties, 'height'));
camera.find('.streamFps').val(getCameraPropertyValue(value.stream.properties, 'fps'));
camera.find('.streamCompression').val(getCameraPropertyValue(value.stream.properties, 'compression'));
camera.find('.streamDefaultCompression').val(getCameraPropertyValue(value.stream.properties, 'default_compression'));
} else {
camera.find('.streamWidth').val('');
camera.find('.streamHeight').val('');
camera.find('.streamFps').val('');
camera.find('.streamCompression').val('');
camera.find('.streamDefaultCompression').val('');
}
}
function updateVisionCameraDataFromJson(i, data) {
@ -548,6 +573,33 @@ $('#visionSave').click(function() {
} catch (err) {
delete value['properties'];
}
value.stream = {'properties': []};
var streamWidth = parseInt(camera.find('.streamWidth').val(), 10);
if (!isNaN(streamWidth)) {
value.stream.properties.push({'name': 'width', 'value': streamWidth});
}
var streamHeight = parseInt(camera.find('.streamHeight').val(), 10);
if (!isNaN(streamHeight)) {
value.stream.properties.push({'name': 'height', 'value': streamHeight});
}
var streamFps = parseInt(camera.find('.streamFps').val(), 10);
if (!isNaN(streamFps)) {
value.stream.properties.push({'name': 'fps', 'value': streamFps});
}
var streamCompression = parseInt(camera.find('.streamCompression').val(), 10);
if (!isNaN(streamCompression)) {
value.stream.properties.push({'name': 'compression', 'value': streamCompression});
}
var streamDefaultCompression = parseInt(camera.find('.streamDefaultCompression').val(), 10);
if (!isNaN(streamDefaultCompression)) {
value.stream.properties.push({'name': 'default_compression', 'value': streamDefaultCompression});
}
});
var msg = {
type: 'visionSave',

View File

@ -280,59 +280,97 @@
</div>
</div>
</div>
<div class="form-group">
<div class="custom-file">
<input type="file" class="custom-file-input cameraSettingsFile" accept=".json,text/json,application/json" id="cameraSettingsFileNEW">
<label class="custom-file-label" for="cameraSettingsFileNEW">Load Source Config From JSON File</label>
<div class="card">
<div class="card-header">
Camera Settings
</div>
<div class="card-body">
<div class="form-group">
<div class="custom-file">
<input type="file" class="custom-file-input cameraSettingsFile" accept=".json,text/json,application/json" id="cameraSettingsFileNEW">
<label class="custom-file-label" for="cameraSettingsFileNEW">Load Source Config From JSON File</label>
</div>
</div>
<div class="form-group">
<button class="btn btn-sm cameraCopyConfig" type="button">
<span data-feather="copy"></span>
Copy Source Config From Camera
</button>
</div>
<div class="form-row">
<div class="form-group col-auto">
<label for="cameraPixelFormatNEW">Pixel Format</label>
<select class="form-control cameraPixelFormat" id="cameraPixelFormatNEW">
<option value="mjpeg">MJPEG</option>
<option value="yuyv">YUYV</option>
<option value="rgb565">RGB565</option>
<option value="bgr">BGR</option>
<option value="gray">Gray</option>
</select>
</div>
<div class="form-group col-auto">
<label for="cameraWidthNEW">Width</label>
<input type="number" class="form-control cameraWidth" id="cameraWidthNEW" size="5">
</div>
<div class="form-group col-auto">
<label for="cameraHeightNEW">Height</label>
<input type="number" class="form-control cameraHeight" id="cameraHeightNEW" size="5">
</div>
<div class="form-group col-auto">
<label for="cameraFpsNEW">FPS</label>
<input type="number" class="form-control cameraFps" id="cameraFpsNEW" size="4">
</div>
</div>
<div class="form-row">
<div class="form-group col-auto">
<label for="cameraBrightnessNEW">Brightness</label>
<input type="text" class="form-control cameraBrightness" id="cameraBrightnessNEW" size="4">
</div>
<div class="form-group col-auto">
<label for="cameraWhiteBalanceNEW">White Balance</label>
<input type="text" class="form-control cameraWhiteBalance" id="cameraWhiteBalanceNEW" size="4">
</div>
<div class="form-group col-auto">
<label for="cameraExposureNEW">Exposure</label>
<input type="text" class="form-control cameraExposure" id="cameraExposureNEW" size="4">
</div>
</div>
<div class="form-group">
<label for="cameraPropertiesNEW">Custom Properties JSON</label>
<textarea class="form-control cameraProperties" id="cameraPropertiesNEW" rows="3"></textarea>
</div>
</div>
</div>
<div class="form-group">
<button class="btn btn-sm cameraCopyConfig" type="button">
<span data-feather="copy"></span>
Copy Source Config From Camera
</button>
</div>
<div class="form-row">
<div class="form-group col-auto">
<label for="cameraPixelFormatNEW">Pixel Format</label>
<select class="form-control cameraPixelFormat" id="cameraPixelFormatNEW">
<option value="mjpeg">MJPEG</option>
<option value="yuyv">YUYV</option>
<option value="rgb565">RGB565</option>
<option value="bgr">BGR</option>
<option value="gray">Gray</option>
</select>
<div class="card">
<div class="card-header">
Video Stream Defaults
</div>
<div class="form-group col-auto">
<label for="cameraWidthNEW">Width</label>
<input type="number" class="form-control cameraWidth" id="cameraWidthNEW" size="5">
<div class="card-body">
<div class="form-row">
<div class="form-group col-auto">
<label for="streamWidthNEW">Width</label>
<input type="number" class="form-control streamWidth" id="streamWidthNEW" size="5">
</div>
<div class="form-group col-auto">
<label for="streamHeightNEW">Height</label>
<input type="number" class="form-control streamHeight" id="streamHeightNEW" size="5">
</div>
<div class="form-group col-auto">
<label for="streamFpsNEW">FPS</label>
<input type="number" class="form-control streamFps" id="streamFpsNEW" size="4">
</div>
</div>
<div class="form-row">
<div class="form-group col-auto">
<label for="streamCompressionNEW">Compression</label>
<input type="number" class="form-control streamCompression" id="streamWidthNEW" size="5">
</div>
<div class="form-group col-auto">
<label for="streamDefaultCompressionNEW">Default Compression</label>
<input type="number" class="form-control streamDefaultCompression" id="streamDefaultCompressionNEW" size="4">
</div>
</div>
</div>
<div class="form-group col-auto">
<label for="cameraHeightNEW">Height</label>
<input type="number" class="form-control cameraHeight" id="cameraHeightNEW" size="5">
</div>
<div class="form-group col-auto">
<label for="cameraFpsNEW">FPS</label>
<input type="number" class="form-control cameraFps" id="cameraFpsNEW" size="4">
</div>
</div>
<div class="form-row">
<div class="form-group col-auto">
<label for="cameraBrightnessNEW">Brightness</label>
<input type="text" class="form-control cameraBrightness" id="cameraBrightnessNEW" size="4">
</div>
<div class="form-group col-auto">
<label for="cameraWhiteBalanceNEW">White Balance</label>
<input type="text" class="form-control cameraWhiteBalance" id="cameraWhiteBalanceNEW" size="4">
</div>
<div class="form-group col-auto">
<label for="cameraExposureNEW">Exposure</label>
<input type="text" class="form-control cameraExposure" id="cameraExposureNEW" size="4">
</div>
</div>
<div class="form-group">
<label for="cameraPropertiesNEW">Custom Properties JSON</label>
<textarea class="form-control cameraProperties" id="cameraPropertiesNEW" rows="3"></textarea>
</div>
</form>
</div>