-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
121 lines (105 loc) · 3.02 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//go:generate compogen readme --operator ./config ./README.mdx
package image
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"image"
"sync"
_ "embed" // embed
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"google.golang.org/protobuf/types/known/structpb"
"github.com/instill-ai/component/pkg/base"
)
var (
//go:embed config/definition.json
definitionJSON []byte
//go:embed config/tasks.json
tasksJSON []byte
once sync.Once
op *operator
)
// Operator is the derived operator
type operator struct {
base.Operator
}
// Execution is the derived execution
type execution struct {
base.OperatorExecution
}
// Init initializes the operator
func Init(bo base.Operator) *operator {
once.Do(func() {
op = &operator{Operator: bo}
err := op.LoadOperatorDefinition(definitionJSON, tasksJSON, nil)
if err != nil {
panic(err)
}
})
return op
}
func (o *operator) CreateExecution(sysVars map[string]any, task string) (*base.ExecutionWrapper, error) {
return &base.ExecutionWrapper{Execution: &execution{
OperatorExecution: base.OperatorExecution{Operator: o, SystemVariables: sysVars, Task: task},
}}, nil
}
// Execute executes the derived execution
func (e *execution) Execute(_ context.Context, inputs []*structpb.Struct) ([]*structpb.Struct, error) {
outputs := []*structpb.Struct{}
var base64ByteImg []byte
for _, input := range inputs {
b, err := base64.StdEncoding.DecodeString(base.TrimBase64Mime(input.Fields["image"].GetStringValue()))
if err != nil {
return nil, err
}
img, _, err := image.Decode(bytes.NewReader(b))
if err != nil {
return nil, err
}
switch e.Task {
case "TASK_DRAW_CLASSIFICATION":
base64ByteImg, err = drawClassification(img, input.Fields["category"].GetStringValue(), input.Fields["score"].GetNumberValue())
if err != nil {
return nil, err
}
case "TASK_DRAW_DETECTION":
base64ByteImg, err = drawDetection(img, input.Fields["objects"].GetListValue().Values)
if err != nil {
return nil, err
}
case "TASK_DRAW_KEYPOINT":
base64ByteImg, err = drawKeypoint(img, input.Fields["objects"].GetListValue().Values)
if err != nil {
return nil, err
}
case "TASK_DRAW_OCR":
base64ByteImg, err = drawOCR(img, input.Fields["objects"].GetListValue().Values)
if err != nil {
return nil, err
}
case "TASK_DRAW_INSTANCE_SEGMENTATION":
base64ByteImg, err = drawInstanceSegmentation(img, input.Fields["objects"].GetListValue().Values)
if err != nil {
return nil, err
}
case "TASK_DRAW_SEMANTIC_SEGMENTATION":
base64ByteImg, err = drawSemanticSegmentation(img, input.Fields["stuffs"].GetListValue().Values)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("not supported task: %s", e.Task)
}
output := structpb.Struct{Fields: make(map[string]*structpb.Value)}
output.Fields["image"] = &structpb.Value{
Kind: &structpb.Value_StringValue{
StringValue: fmt.Sprintf("data:image/jpeg;base64,%s", string(base64ByteImg)),
},
}
outputs = append(outputs, &output)
}
return outputs, nil
}