Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

camera #17

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.viam.sdk.core.component.camera;

import com.google.protobuf.Struct;
import com.viam.common.v1.Common;
import com.viam.component.camera.v1.Camera.Format;
import com.viam.component.camera.v1.Camera.Image;
import com.viam.sdk.core.component.Component;
import com.viam.sdk.core.resource.Resource;
import com.viam.sdk.core.resource.Subtype;
import com.viam.sdk.core.robot.RobotClient;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;

/**
* MovementSensor reports information about the robot's direction, position, and speed
*/
public abstract class Camera extends Component {

public static final Subtype SUBTYPE = new Subtype(
Subtype.NAMESPACE_RDK,
Subtype.RESOURCE_TYPE_COMPONENT,
"camera");

public Camera(final String name) {
super(SUBTYPE, named(name));
}

/**
* Get the ResourceName of the component
*
* @param name the name of the component
* @return the component's ResourceName
*/
public static Common.ResourceName named(final String name) {
return Resource.named(SUBTYPE, name);
}

/**
* Get the component with the provided name from the provided robot.
*
* @param robot the RobotClient
* @param name the name of the component
* @return the component
*/
public static Camera fromRobot(final RobotClient robot, final String name) {
return robot.getResource(Camera.class, named(name));
}

public abstract Image getImage(final Format format,
final Optional<Struct> extra);

public abstract Entry<List<Image>, Common.ResponseMetadata> getImages();

static Format mimeToFormat(final String mimeType) {
switch (mimeType) {
case "image/jpeg":
return Format.FORMAT_JPEG;
case "image/png":
return Format.FORMAT_PNG;
case "image/vnd.viam.rgba":
return Format.FORMAT_RAW_RGBA;
case "image/vnd.viam.dep":
return Format.FORMAT_RAW_DEPTH;
default:
return Format.FORMAT_UNSPECIFIED;
}
}

static String formatToMime(final Format format) {
switch (format) {
case FORMAT_JPEG:
return "image/jpeg";
case FORMAT_PNG:
return "image/png";
case FORMAT_RAW_RGBA:
return "image/vnd.viam.rgba";
case FORMAT_RAW_DEPTH:
return "image/vnd.viam.dep";
default:
return "unknown";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.viam.sdk.core.component.camera;

import com.google.protobuf.Struct;
import com.google.protobuf.Value;
import com.viam.common.v1.Common;
import com.viam.common.v1.Common.GetGeometriesRequest;
import com.viam.common.v1.Common.ResponseMetadata;
import com.viam.component.camera.v1.Camera.Format;
import com.viam.component.camera.v1.Camera.GetImageRequest;
import com.viam.component.camera.v1.Camera.GetImageResponse;
import com.viam.component.camera.v1.Camera.GetImagesRequest;
import com.viam.component.camera.v1.Camera.GetImagesResponse;
import com.viam.component.camera.v1.Camera.Image;
import com.viam.component.camera.v1.CameraServiceGrpc;
import com.viam.sdk.core.rpc.Channel;
import java.util.AbstractMap.SimpleEntry;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

/**
* gRPC Client for a Camera component
*/
public class CameraRPCClient extends com.viam.sdk.core.component.camera.Camera {

private final CameraServiceGrpc.CameraServiceBlockingStub client;

public CameraRPCClient(final String name, final Channel chan) {
super(name);
final CameraServiceGrpc.CameraServiceBlockingStub client = CameraServiceGrpc.newBlockingStub(
chan);
if (chan.getCallCredentials().isPresent()) {
this.client = client.withCallCredentials(chan.getCallCredentials().get());
} else {
this.client = client;
}
}

@Override
public Struct doCommand(final Map<String, Value> command) {
return client.doCommand(Common.DoCommandRequest.newBuilder().
setName(getName().getName()).
setCommand(Struct.newBuilder().putAllFields(command).build()).
build()).getResult();
}

@Override
public List<Common.Geometry> getGeometries(final Optional<Struct> extra) {
final GetGeometriesRequest.Builder builder = Common.GetGeometriesRequest.newBuilder().
setName(getName().getName());
extra.ifPresent(builder::setExtra);
return client.getGeometries(builder.build()).getGeometriesList();
}

@Override
public Image getImage(final Format format,
Optional<Struct> extra) {
final GetImageRequest.Builder builder = GetImageRequest.newBuilder().
setName(getName().getName()).
setMimeType(Camera.formatToMime(format));
extra.ifPresent(builder::setExtra);
final GetImageResponse resp = client.getImage(builder.build());
final Image.Builder imgBuilder = Image.newBuilder().
setSourceName(getName().getName()).
setImage(resp.getImage());

return imgBuilder.setFormat(Camera.mimeToFormat(resp.getMimeType())).build();
}

@Override
public Entry<List<Image>, ResponseMetadata> getImages() {
final GetImagesRequest.Builder builder = GetImagesRequest.newBuilder().
setName(getName().getName());
final GetImagesResponse resp = client.getImages(builder.build());
return new SimpleEntry<>(resp.getImagesList(), resp.getResponseMetadata());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.viam.sdk.core.component.camera;

import com.google.protobuf.Struct;
import com.viam.common.v1.Common;
import com.viam.common.v1.Common.Geometry;
import com.viam.common.v1.Common.GetGeometriesResponse;
import com.viam.common.v1.Common.ResponseMetadata;
import com.viam.component.camera.v1.Camera.GetImageRequest;
import com.viam.component.camera.v1.Camera.GetImageResponse;
import com.viam.component.camera.v1.Camera.GetImagesRequest;
import com.viam.component.camera.v1.Camera.GetImagesResponse;
import com.viam.component.camera.v1.Camera.Image;
import com.viam.component.camera.v1.CameraServiceGrpc;
import com.viam.sdk.core.resource.ResourceManager;
import com.viam.sdk.core.resource.ResourceRPCService;
import io.grpc.stub.StreamObserver;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;

/**
* gRPC Service for a Camera component
*/
public class CameraRPCService extends
CameraServiceGrpc.CameraServiceImplBase implements
ResourceRPCService<Camera> {

private final ResourceManager manager;

public CameraRPCService(final ResourceManager manager) {
this.manager = manager;
}

@Override
public void doCommand(Common.DoCommandRequest request,
StreamObserver<Common.DoCommandResponse> responseObserver) {
final Camera camera = getResource(
Camera.named(request.getName()));
final Struct result = camera.doCommand(request.getCommand().getFieldsMap());
responseObserver.onNext(Common.DoCommandResponse.newBuilder().setResult(result).build());
responseObserver.onCompleted();
}

@Override
public void getGeometries(Common.GetGeometriesRequest request,
StreamObserver<Common.GetGeometriesResponse> responseObserver) {
final Camera camera = getResource(
Camera.named(request.getName()));
final List<Geometry> result = camera.getGeometries(Optional.of(request.getExtra()));
responseObserver.onNext(GetGeometriesResponse.newBuilder().addAllGeometries(result).build());
responseObserver.onCompleted();
}

@Override
public void getImage(GetImageRequest request,
StreamObserver<GetImageResponse> responseObserver) {
final Camera camera = getResource(
Camera.named(request.getName()));
final Image result = camera.getImage(Camera.mimeToFormat(request.getMimeType()),
Optional.of(request.getExtra()));

responseObserver.onNext(
GetImageResponse.newBuilder().setImage(result.getImage())
.setMimeType(Camera.formatToMime(result.getFormat())).build());
responseObserver.onCompleted();
}

@Override
public void getImages(GetImagesRequest request,
StreamObserver<GetImagesResponse> responseObserver) {
final Camera camera = getResource(
Camera.named(request.getName()));
final Entry<List<Image>, ResponseMetadata> result = camera.getImages();
responseObserver.onNext(
GetImagesResponse.newBuilder().addAllImages(result.getKey())
.setResponseMetadata(result.getValue()).build());
responseObserver.onCompleted();
}

@Override
public Class<Camera> getResourceClass() {
return Camera.class;
}

@Override
public ResourceManager getManager() {
return manager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import com.viam.common.v1.Common;
import com.viam.common.v1.Common.ResourceName;
import com.viam.component.camera.v1.CameraServiceGrpc;
import com.viam.component.generic.v1.GenericServiceGrpc;
import com.viam.component.gripper.v1.GripperServiceGrpc;
import com.viam.component.movementsensor.v1.MovementSensorServiceGrpc;
import com.viam.component.sensor.v1.SensorServiceGrpc;
import com.viam.sdk.core.component.camera.Camera;
import com.viam.sdk.core.component.camera.CameraRPCClient;
import com.viam.sdk.core.component.camera.CameraRPCService;
import com.viam.sdk.core.component.generic.Generic;
import com.viam.sdk.core.component.generic.GenericRPCClient;
import com.viam.sdk.core.component.generic.GenericRPCService;
Expand Down Expand Up @@ -173,6 +177,12 @@ public synchronized void close() {
SensorRPCService::new,
SensorRPCClient::new
));
Registry.registerSubtype(new ResourceRegistration<>(
Camera.SUBTYPE,
CameraServiceGrpc.SERVICE_NAME,
CameraRPCService::new,
CameraRPCClient::new
));
Registry.registerSubtype(new ResourceRegistration<>(
Sensors.SUBTYPE,
SensorsServiceGrpc.SERVICE_NAME,
Expand Down
4 changes: 1 addition & 3 deletions java/services/mlmodel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ ext.pomDisplayName = "Viam Java SDK - MLModel Service"

dependencies {
api project(':core:services:viam-core-sdk-mlmodel-service')
implementation "ai.djl:core:0.26.0"
implementation "ai.djl:api:0.26.0"
runtimeOnly "ai.djl.pytorch:pytorch-engine:0.26.0"
runtimeOnly "ai.djl.pytorch:pytorch-native-auto:0.26.0"
runtimeOnly "ai.djl.onnxruntime:onnxruntime-engine:0.26.0"
runtimeOnly "ai.djl.onnxruntime:onnxruntime-native-auto:0.26.0"
}

java {
Expand Down