github.com/instill-ai/component@v0.16.0-beta/pkg/connector/instill/v0/object_detection.go (about)

     1  package instill
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"google.golang.org/grpc/metadata"
     8  	"google.golang.org/protobuf/encoding/protojson"
     9  	"google.golang.org/protobuf/types/known/structpb"
    10  
    11  	"github.com/instill-ai/component/pkg/base"
    12  	modelPB "github.com/instill-ai/protogen-go/model/model/v1alpha"
    13  )
    14  
    15  func (e *execution) executeObjectDetection(grpcClient modelPB.ModelPublicServiceClient, modelName string, inputs []*structpb.Struct) ([]*structpb.Struct, error) {
    16  	if len(inputs) <= 0 {
    17  		return nil, fmt.Errorf("invalid input: %v for model: %s", inputs, modelName)
    18  	}
    19  
    20  	if grpcClient == nil {
    21  		return nil, fmt.Errorf("uninitialized client")
    22  	}
    23  
    24  	taskInputs := []*modelPB.TaskInput{}
    25  	for _, input := range inputs {
    26  		inputJSON, err := protojson.Marshal(input)
    27  		if err != nil {
    28  			return nil, err
    29  		}
    30  
    31  		detectionInput := &modelPB.DetectionInput{}
    32  		err = protojson.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(inputJSON, detectionInput)
    33  		if err != nil {
    34  			return nil, err
    35  		}
    36  		detectionInput.Type = &modelPB.DetectionInput_ImageBase64{
    37  			ImageBase64: base.TrimBase64Mime(detectionInput.GetImageBase64()),
    38  		}
    39  
    40  		modelInput := &modelPB.TaskInput_Detection{
    41  			Detection: detectionInput,
    42  		}
    43  		taskInputs = append(taskInputs, &modelPB.TaskInput{Input: modelInput})
    44  	}
    45  
    46  	req := modelPB.TriggerUserModelRequest{
    47  		Name:       modelName,
    48  		TaskInputs: taskInputs,
    49  	}
    50  
    51  	ctx := metadata.NewOutgoingContext(context.Background(), getRequestMetadata(e.SystemVariables))
    52  	res, err := grpcClient.TriggerUserModel(ctx, &req)
    53  	if err != nil || res == nil {
    54  		return nil, err
    55  	}
    56  
    57  	taskOutputs := res.GetTaskOutputs()
    58  	if len(taskOutputs) <= 0 {
    59  		return nil, fmt.Errorf("invalid output: %v for model: %s", taskOutputs, modelName)
    60  	}
    61  
    62  	outputs := []*structpb.Struct{}
    63  	for idx := range inputs {
    64  		objDetectionOutput := taskOutputs[idx].GetDetection()
    65  		if objDetectionOutput == nil {
    66  			return nil, fmt.Errorf("invalid output: %v for model: %s", objDetectionOutput, modelName)
    67  		}
    68  		outputJSON, err := protojson.MarshalOptions{
    69  			UseProtoNames:   true,
    70  			EmitUnpopulated: true,
    71  		}.Marshal(objDetectionOutput)
    72  		if err != nil {
    73  			return nil, err
    74  		}
    75  		output := &structpb.Struct{}
    76  		err = protojson.Unmarshal(outputJSON, output)
    77  		if err != nil {
    78  			return nil, err
    79  		}
    80  		outputs = append(outputs, output)
    81  
    82  	}
    83  
    84  	return outputs, nil
    85  }