Skip to content

Commit

Permalink
[RSDK-7392] vision service client (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma authored Apr 25, 2024
1 parent 9e496d4 commit 661bffb
Show file tree
Hide file tree
Showing 8 changed files with 539 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/src/resource/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Subtype {
abstract class Resource {
abstract String name;

/// Send/Receive arbitrary commands to the [Resource]
Future<Map<String, dynamic>> doCommand(Map<String, dynamic> command) {
throw UnimplementedError();
}
Expand Down
79 changes: 79 additions & 0 deletions lib/src/services/vision.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'package:fixnum/fixnum.dart';
import 'package:grpc/grpc_connection_interface.dart';

import '../../protos/common/common.dart';
import '../../protos/service/vision.dart';
import '../media/image.dart';
import '../resource/base.dart';
import '../utils.dart';

class VisionClient implements ResourceRPCClient {
final String name;

@override
ClientChannelBase channel;

@override
VisionServiceClient get client => VisionServiceClient(channel);

VisionClient(this.name, this.channel);

/// Get a list of [Detection]s from the camera named [cameraName].
Future<List<Detection>> detectionsFromCamera(String cameraName, {Map<String, dynamic>? extra}) async {
final request = GetDetectionsFromCameraRequest(name: name, cameraName: cameraName, extra: extra?.toStruct());
final response = await client.getDetectionsFromCamera(request);
return response.detections;
}

/// Get a list of [Detection]s from the provided [image].
Future<List<Detection>> detections(ViamImage image, {Map<String, dynamic>? extra}) async {
final request = GetDetectionsRequest(
name: name,
image: image.raw,
width: Int64(image.image?.width ?? 0),
height: Int64(image.image?.height ?? 0),
mimeType: image.mimeType.name,
extra: extra?.toStruct());
final response = await client.getDetections(request);
return response.detections;
}

/// Get a list of [Classification]s from the camera named [cameraName].
/// The maximum number of [Classification]s returned is [count].
Future<List<Classification>> classificationsFromCamera(String cameraName, int count, {Map<String, dynamic>? extra}) async {
final request = GetClassificationsFromCameraRequest(name: name, cameraName: cameraName, n: count, extra: extra?.toStruct());
final response = await client.getClassificationsFromCamera(request);
return response.classifications;
}

/// Get a list of [Classification]s from the provided [image].
/// The maximum number of [Classification]s returned is [count].
Future<List<Classification>> classifications(ViamImage image, int count, {Map<String, dynamic>? extra}) async {
final request = GetClassificationsRequest(
name: name,
image: image.raw,
width: image.image?.width,
height: image.image?.height,
mimeType: image.mimeType.name,
n: count,
extra: extra?.toStruct());
final response = await client.getClassifications(request);
return response.classifications;
}

/// Get a list of [PointCloudObject]s from the camera named [cameraName].
Future<List<PointCloudObject>> objectPointClouds(String cameraName, {Map<String, dynamic>? extra}) async {
final request = GetObjectPointCloudsRequest(name: name, cameraName: cameraName, mimeType: MimeType.pcd.name, extra: extra?.toStruct());
final response = await client.getObjectPointClouds(request);
return response.objects;
}

/// Send/Receive arbitrary commands to the Resource
Future<Map<String, dynamic>> doCommand(Map<String, dynamic> command) async {
final request = DoCommandRequest()
..name = name
..command = command.toStruct();
final response = await client.doCommand(request);
return response.result.toMap();
}
}
3 changes: 3 additions & 0 deletions lib/viam_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ export 'src/robot/client.dart';
/// RPC
export 'src/rpc/dial.dart';

/// Services
export 'src/services/vision.dart';

/// Misc
export 'src/viam_sdk.dart';
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_webrtc: ^0.9.47
flutter_webrtc: ^0.10.3
grpc: ^3.2.3
protobuf: ^3.0.0
image: ^4.0.16
Expand Down
2 changes: 1 addition & 1 deletion test/unit_test/app/app_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {
late MockAppServiceClient serviceClient;
late AppClient appClient;

setUp(() async {
setUp(() {
serviceClient = MockAppServiceClient();
appClient = AppClient(serviceClient);
});
Expand Down
2 changes: 2 additions & 0 deletions test/unit_test/mocks/service_clients_mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import 'package:viam_sdk/src/gen/app/data/v1/data.pbgrpc.dart';
import 'package:viam_sdk/src/gen/app/v1/app.pbgrpc.dart';
import 'package:viam_sdk/src/gen/provisioning/v1/provisioning.pbgrpc.dart';
import 'package:viam_sdk/src/gen/robot/v1/robot.pbgrpc.dart';
import 'package:viam_sdk/src/gen/service/vision/v1/vision.pbgrpc.dart';

@GenerateNiceMocks([
MockSpec<ClientChannelBase>(),
MockSpec<RobotServiceClient>(),
MockSpec<AppServiceClient>(),
MockSpec<DataServiceClient>(),
MockSpec<ProvisioningServiceClient>(),
MockSpec<VisionServiceClient>(),
])
void main() {}
Loading

0 comments on commit 661bffb

Please sign in to comment.