github.com/instill-ai/component@v0.16.0-beta/pkg/operator/image/v0/draw_test.go (about)

     1  package image
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	_ "embed" // embed
     8  
     9  	"google.golang.org/protobuf/types/known/structpb"
    10  )
    11  
    12  var (
    13  	//go:embed testdata/cls-dog.json
    14  	clsDogJSON []byte
    15  	//go:embed testdata/det-coco-1.json
    16  	detCOCO1JSON []byte
    17  	//go:embed testdata/det-coco-2.json
    18  	detCOCO2JSON []byte
    19  	//go:embed testdata/kp-coco-1.json
    20  	kpCOCO1JSON []byte
    21  	//go:embed testdata/kp-coco-2.json
    22  	kpCOCO2JSON []byte
    23  	//go:embed testdata/ocr-mm.json
    24  	ocrMMJSON []byte
    25  	//go:embed testdata/inst-seg-coco-1.json
    26  	instSegCOCO1JSON []byte
    27  	//go:embed testdata/inst-seg-coco-2.json
    28  	instSegCOCO2JSON []byte
    29  	//go:embed testdata/inst-seg-stomata.json
    30  	instSegStomataJSON []byte
    31  	//go:embed testdata/sem-seg-cityscape.json
    32  	semSegCityscapeJSON []byte
    33  )
    34  
    35  // TestDrawClassification tests the drawClassification function
    36  func TestDrawClassification(t *testing.T) {
    37  
    38  	inputDog := &structpb.Struct{}
    39  	if err := json.Unmarshal(clsDogJSON, inputDog); err != nil {
    40  		if err != nil {
    41  			panic(err)
    42  		}
    43  	}
    44  
    45  	inputs := []*structpb.Struct{
    46  		inputDog,
    47  	}
    48  
    49  	e := &execution{}
    50  	e.Task = "TASK_DRAW_CLASSIFICATION"
    51  
    52  	if _, err := e.Execute(inputs); err != nil {
    53  		t.Fatalf("drawClassification returned an error: %v", err)
    54  	}
    55  }
    56  
    57  // TestDrawDetection tests the drawDetection function
    58  func TestDrawDetection(t *testing.T) {
    59  
    60  	inputCOCO1 := &structpb.Struct{}
    61  	if err := json.Unmarshal(detCOCO1JSON, inputCOCO1); err != nil {
    62  		if err != nil {
    63  			panic(err)
    64  		}
    65  	}
    66  
    67  	inputCOCO2 := &structpb.Struct{}
    68  	if err := json.Unmarshal(detCOCO2JSON, inputCOCO2); err != nil {
    69  		if err != nil {
    70  			panic(err)
    71  		}
    72  	}
    73  
    74  	inputs := []*structpb.Struct{
    75  		inputCOCO1,
    76  		// inputCOCO2,
    77  	}
    78  
    79  	e := &execution{}
    80  	e.Task = "TASK_DRAW_DETECTION"
    81  
    82  	if _, err := e.Execute(inputs); err != nil {
    83  		t.Fatalf("drawDetection returned an error: %v", err)
    84  	}
    85  }
    86  
    87  // TestDrawKeypoint tests the drawKeypoint function
    88  func TestDrawKeypoint(t *testing.T) {
    89  
    90  	inputCOCO1 := &structpb.Struct{}
    91  	if err := json.Unmarshal(kpCOCO1JSON, inputCOCO1); err != nil {
    92  		if err != nil {
    93  			panic(err)
    94  		}
    95  	}
    96  
    97  	inputCOCO2 := &structpb.Struct{}
    98  	if err := json.Unmarshal(kpCOCO2JSON, inputCOCO2); err != nil {
    99  		if err != nil {
   100  			panic(err)
   101  		}
   102  	}
   103  
   104  	inputs := []*structpb.Struct{
   105  		inputCOCO1,
   106  		// inputCOCO2,
   107  	}
   108  
   109  	e := &execution{}
   110  	e.Task = "TASK_DRAW_KEYPOINT"
   111  
   112  	if _, err := e.Execute(inputs); err != nil {
   113  		t.Fatalf("drawKeypoint returned an error: %v", err)
   114  	}
   115  }
   116  
   117  // TestDrawOCR tests the drawOCR function
   118  func TestDrawOCR(t *testing.T) {
   119  
   120  	inputMM := &structpb.Struct{}
   121  	if err := json.Unmarshal(ocrMMJSON, inputMM); err != nil {
   122  		if err != nil {
   123  			panic(err)
   124  		}
   125  	}
   126  
   127  	inputCOCO2 := &structpb.Struct{}
   128  	if err := json.Unmarshal(kpCOCO2JSON, inputCOCO2); err != nil {
   129  		if err != nil {
   130  			panic(err)
   131  		}
   132  	}
   133  
   134  	inputs := []*structpb.Struct{
   135  		inputMM,
   136  	}
   137  
   138  	e := &execution{}
   139  	e.Task = "TASK_DRAW_OCR"
   140  
   141  	if _, err := e.Execute(inputs); err != nil {
   142  		t.Fatalf("drawKeypoint returned an error: %v", err)
   143  	}
   144  }
   145  
   146  // TestDrawInstanceSegmentation tests the drawInstanceSegmentation function
   147  func TestDrawInstanceSegmentation(t *testing.T) {
   148  
   149  	inputCOCO1 := &structpb.Struct{}
   150  	if err := json.Unmarshal(instSegCOCO1JSON, inputCOCO1); err != nil {
   151  		if err != nil {
   152  			panic(err)
   153  		}
   154  	}
   155  
   156  	inputCOCO2 := &structpb.Struct{}
   157  	if err := json.Unmarshal(instSegCOCO2JSON, inputCOCO2); err != nil {
   158  		if err != nil {
   159  			panic(err)
   160  		}
   161  	}
   162  
   163  	inputStomata := &structpb.Struct{}
   164  	if err := json.Unmarshal(instSegStomataJSON, inputStomata); err != nil {
   165  		if err != nil {
   166  			panic(err)
   167  		}
   168  	}
   169  
   170  	inputs := []*structpb.Struct{
   171  		inputCOCO1,
   172  		inputCOCO2,
   173  		inputStomata,
   174  	}
   175  
   176  	e := &execution{}
   177  	e.Task = "TASK_DRAW_INSTANCE_SEGMENTATION"
   178  
   179  	if _, err := e.Execute(inputs); err != nil {
   180  		t.Fatalf("drawInstanceSegmentation returned an error: %v", err)
   181  	}
   182  }
   183  
   184  // TestDrawSemanticSegmentation tests the drawSemanticSegmentation function
   185  func TestDrawSemanticSegmentation(t *testing.T) {
   186  
   187  	inputCityscape := &structpb.Struct{}
   188  	if err := json.Unmarshal(semSegCityscapeJSON, inputCityscape); err != nil {
   189  		if err != nil {
   190  			panic(err)
   191  		}
   192  	}
   193  
   194  	inputs := []*structpb.Struct{
   195  		inputCityscape,
   196  	}
   197  
   198  	e := &execution{}
   199  	e.Task = "TASK_DRAW_SEMANTIC_SEGMENTATION"
   200  
   201  	if _, err := e.Execute(inputs); err != nil {
   202  		t.Fatalf("drawSemanticSegmentation returned an error: %v", err)
   203  	}
   204  }