string resultString = ""; // final result string List <Tuple <string, float> > SendAndReceive(byte[] imageData) { var tf_channel = new Channel(ipAddress, Convert.ToInt32(port), ChannelCredentials.Insecure); var tf_client = new PredictionService.PredictionServiceClient(tf_channel); try { //Create prediction request var request = new PredictRequest() { ModelSpec = new ModelSpec() { Name = "inception", SignatureName = "predict_images" } }; var imageTensor = TensorProtoBuilder.TensorProtoFromImage(imageData); // Add image tensor to request request.Inputs.Add("images", imageTensor); // Send request and get response PredictResponse predictResponse = tf_client.Predict(request); // Decode response var classesTensor = predictResponse.Outputs["classes"]; string[] classes = TensorProtoDecoder.TensorProtoToStringArray(classesTensor); var scoresTensor = predictResponse.Outputs["scores"]; float[] scores = TensorProtoDecoder.TensorProtoToFloatArray(scoresTensor); List <Tuple <string, float> > predictResult = new List <Tuple <string, float> >(); for (int i = 0; i < classes.Length; i++) { predictResult.Add(new Tuple <string, float>(classes[i], scores[i])); } return(predictResult); } catch (Exception e) { if (e is RpcException) { RpcException re = (RpcException)e; Debug.Log(re.Status.Detail); Debug.Log(re.StatusCode); } Debug.Log(e.Message); throw; } }
public async Task recognize(uint[] rgb_image, int height, int width) { var request = new PredictRequest() { ModelSpec = new ModelSpec() { Name = "default" } }; request.Inputs.Add("inputs", TensorBuilder.CreateTensorFromImage(rgb_image, height, width, 3)); // Run the prediction var predictResponse = await client.PredictAsync(request); //float num_classes= TensorProtoDecoder.TensorProtoToFloat(predictResponse.Outputs["num_classes"]); int num_detections = (int)TensorProtoDecoder.TensorProtoToFloat(predictResponse.Outputs["num_detections"]); float[] classes = TensorProtoDecoder.TensorProtoToFloatArray(predictResponse.Outputs["detection_classes"]); float[] bboxes = TensorProtoDecoder.TensorProtoToFloatArray(predictResponse.Outputs["detection_boxes"]); float[] scores = TensorProtoDecoder.TensorProtoToFloatArray(predictResponse.Outputs["detection_scores"]); for (var i = 0; i < num_detections; i++) { float[] bbox = new float[4]; Array.Copy(bboxes, i * 4, bbox, 0, 4); detectedObjects.Add(new Detection { boundingBox = bbox, objectClass = label_list[(int)classes[i]], confidence = scores[i] }); } readyForNextFrame = true; }