This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
/
image_classification.go
80 lines (71 loc) 路 2.31 KB
/
image_classification.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package instill
import (
"context"
"fmt"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"
"github.com/instill-ai/component/pkg/base"
modelPB "github.com/instill-ai/protogen-go/model/model/v1alpha"
)
func (e *Execution) executeImageClassification(grpcClient modelPB.ModelPublicServiceClient, modelName string, inputs []*structpb.Struct) ([]*structpb.Struct, error) {
if len(inputs) <= 0 {
return nil, fmt.Errorf("invalid input: %v for model: %s", inputs, modelName)
}
if grpcClient == nil {
return nil, fmt.Errorf("uninitialized client")
}
taskInputs := []*modelPB.TaskInput{}
for _, input := range inputs {
inputJSON, err := protojson.Marshal(input)
if err != nil {
return nil, err
}
classificationInput := &modelPB.ClassificationInput{}
err = protojson.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(inputJSON, classificationInput)
if err != nil {
return nil, err
}
classificationInput.Type = &modelPB.ClassificationInput_ImageBase64{
ImageBase64: base.TrimBase64Mime(classificationInput.GetImageBase64()),
}
taskInput := &modelPB.TaskInput_Classification{
Classification: classificationInput,
}
taskInputs = append(taskInputs, &modelPB.TaskInput{Input: taskInput})
}
req := modelPB.TriggerUserModelRequest{
Name: modelName,
TaskInputs: taskInputs,
}
ctx := metadata.NewOutgoingContext(context.Background(), getRequestMetadata(e.Config))
res, err := grpcClient.TriggerUserModel(ctx, &req)
if err != nil || res == nil {
return nil, err
}
taskOutputs := res.GetTaskOutputs()
if len(taskOutputs) <= 0 {
return nil, fmt.Errorf("invalid output: %v for model: %s", taskOutputs, modelName)
}
outputs := []*structpb.Struct{}
for idx := range inputs {
imgClassificationOp := taskOutputs[idx].GetClassification()
if imgClassificationOp == nil {
return nil, fmt.Errorf("invalid output: %v for model: %s", imgClassificationOp, modelName)
}
outputJSON, err := protojson.MarshalOptions{
UseProtoNames: true,
EmitUnpopulated: true,
}.Marshal(imgClassificationOp)
if err != nil {
return nil, err
}
output := &structpb.Struct{}
err = protojson.Unmarshal(outputJSON, output)
if err != nil {
return nil, err
}
outputs = append(outputs, output)
}
return outputs, nil
}