File size: 45,490 Bytes
be94e5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2021-2022 Intel Corporation

#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
#include <opencv2/gapi/oak/oak.hpp> // kernels()

#ifdef HAVE_OAK

#include <cstring>
#include <unordered_set>
#include <algorithm> // any_of
#include <functional> // reference_wrapper

#include <ade/util/zip_range.hpp>

#include <api/gbackend_priv.hpp>
#include <backends/common/gbackend.hpp>

#include <opencv2/gapi/infer.hpp> // GInferBase
#include <opencv2/gapi/streaming/meta.hpp> // streaming::meta_tag

#include "depthai/depthai.hpp"

#include "oak_memory_adapters.hpp"

#include <opencv2/gapi/oak/infer.hpp> // infer params

namespace cv { namespace gimpl {

// Forward declaration
class GOAKContext;
class OAKKernelParams;

class GOAKExecutable final: public GIslandExecutable {
    friend class GOAKContext;
    friend class OAKKernelParams;
    virtual void run(std::vector<InObj>&&,

                     std::vector<OutObj>&&) override {
        GAPI_Error("Not implemented");
    }

    virtual void run(GIslandExecutable::IInput &in,

                     GIslandExecutable::IOutput &out) override;

    void linkToParent(ade::NodeHandle handle);
    void linkCopy(ade::NodeHandle handle);

    class ExtractTypeHelper : protected dai::Node {
    public:
        using Input = dai::Node::Input;
        using Output = dai::Node::Output;
        using InputPtr = dai::Node::Input*;
        using OutputPtr = dai::Node::Output*;
    };

    struct OAKNodeInfo {
        std::shared_ptr<dai::Node> node = nullptr;
        std::vector<ExtractTypeHelper::InputPtr> inputs = {};
        std::vector<ExtractTypeHelper::OutputPtr> outputs = {};
    };

    struct OAKOutQueueInfo {
        std::shared_ptr<dai::node::XLinkOut> xlink_output;
        std::shared_ptr<dai::DataOutputQueue> out_queue;
        std::string out_queue_name;
        size_t gapi_out_data_index;
    };

    cv::GArg packInArg(const GArg &arg, std::vector<ExtractTypeHelper::InputPtr>& oak_ins);
    void packOutArg(const RcDesc &rc, std::vector<ExtractTypeHelper::OutputPtr>& oak_outs);

    const ade::Graph& m_g;
    GModel::ConstGraph m_gm;
    cv::GCompileArgs m_args;

    std::unordered_map<ade::NodeHandle,
                       OAKNodeInfo,
                       ade::HandleHasher<ade::Node>> m_oak_nodes;

    // Will be reworked later when XLinkIn will be introduced as input
    std::shared_ptr<dai::node::ColorCamera> m_camera_input;
    cv::Size m_camera_size;

    // Backend outputs
    std::unordered_map<ade::NodeHandle,
                       OAKOutQueueInfo,
                       ade::HandleHasher<ade::Node>> m_out_queues;

    // Backend inputs
    std::vector<std::pair<std::string, dai::Buffer>> m_in_queues;

    std::unordered_set<ade::NodeHandle,
                       ade::HandleHasher<ade::Node>> m_passthrough_copy_nodes;

    // Note: dai::Pipeline should be the only one for the whole pipeline,
    // so there is no way to insert any non-OAK node in graph between other OAK nodes.
    // The only heterogeneous case possible is if we insert other backends after or before
    // OAK island.
    std::unique_ptr<dai::Device> m_device;
    std::unique_ptr<dai::Pipeline> m_pipeline;

    // Camera config
    cv::gapi::oak::ColorCameraParams m_ccp;

    // Infer info
    std::unordered_map<ade::NodeHandle,
                       cv::gapi::oak::detail::ParamDesc,
                       ade::HandleHasher<ade::Node>> m_oak_infer_info;

public:
    GOAKExecutable(const ade::Graph& g,
                   const cv::GCompileArgs& args,
                   const std::vector<ade::NodeHandle>& nodes,
                   const std::vector<cv::gimpl::Data>& ins_data,
                   const std::vector<cv::gimpl::Data>& outs_data);
    ~GOAKExecutable() = default;

    // FIXME: could it reshape?
    virtual bool canReshape() const override { return false; }
    virtual void reshape(ade::Graph&, const GCompileArgs&) override {
        GAPI_Error("GOAKExecutable::reshape() is not supported");
    }

    virtual void handleNewStream() override;
    virtual void handleStopStream() override;
};

class GOAKContext {
public:
    // FIXME: make private?
    using Input = GOAKExecutable::ExtractTypeHelper::Input;
    using Output = GOAKExecutable::ExtractTypeHelper::Output;
    using InputPtr = GOAKExecutable::ExtractTypeHelper::Input*;
    using OutputPtr = GOAKExecutable::ExtractTypeHelper::Output*;

    GOAKContext(const std::unique_ptr<dai::Pipeline>& pipeline,
                const cv::Size& camera_size,
                std::vector<cv::GArg>& args,
                std::vector<OutputPtr>& results);

    GOAKContext(const std::unique_ptr<dai::Pipeline>& pipeline,
                const cv::Size& camera_size,
                const cv::gapi::oak::detail::ParamDesc& infer_info,
                std::vector<cv::GArg>& args,
                std::vector<OutputPtr>& results);

    // Generic accessor API
    template<typename T>

    T& inArg(int input) { return m_args.at(input).get<T>(); }

    // FIXME: consider not using raw pointers
    InputPtr& in(int input);
    OutputPtr& out(int output);

    const std::unique_ptr<dai::Pipeline>& pipeline() const;
    const cv::Size& camera_size() const;
    const cv::gapi::oak::detail::ParamDesc& ii() const;

private:
    const std::unique_ptr<dai::Pipeline>& m_pipeline;
    const cv::Size m_camera_size;
    const cv::gapi::oak::detail::ParamDesc m_infer_info;
    std::vector<cv::GArg>& m_args;
    std::vector<OutputPtr>& m_outputs;
};

GOAKContext::GOAKContext(const std::unique_ptr<dai::Pipeline>& pipeline,
                         const cv::Size& camera_size,
                         std::vector<cv::GArg>& args,
                         std::vector<OutputPtr>& results)
    : m_pipeline(pipeline), m_camera_size(camera_size),
      m_args(args), m_outputs(results) {}

GOAKContext::GOAKContext(const std::unique_ptr<dai::Pipeline>& pipeline,
                         const cv::Size& camera_size,
                         const cv::gapi::oak::detail::ParamDesc& infer_info,
                         std::vector<cv::GArg>& args,
                         std::vector<OutputPtr>& results)
    : m_pipeline(pipeline), m_camera_size(camera_size),
      m_infer_info(infer_info), m_args(args), m_outputs(results) {}

const std::unique_ptr<dai::Pipeline>& GOAKContext::pipeline() const {
    return m_pipeline;
}

const cv::Size& GOAKContext::camera_size() const {
    return m_camera_size;
}

const cv::gapi::oak::detail::ParamDesc& GOAKContext::ii() const {
    return m_infer_info;
}

GOAKContext::InputPtr& GOAKContext::in(int input) {
    return inArg<std::reference_wrapper<GOAKContext::InputPtr>>(input).get();
}

GOAKContext::OutputPtr& GOAKContext::out(int output) {
    return m_outputs.at(output);
}

class OAKKernelParams {
public:
    const std::unique_ptr<dai::Pipeline>& pipeline;
    const cv::Size& camera_size;
    const cv::gapi::oak::detail::ParamDesc& infer_info;
    std::vector<std::pair<std::string, dai::Buffer>>& in_queues;
};

namespace detail {
template<class T> struct get_in;
template<> struct get_in<cv::GFrame> {
    static GOAKContext::InputPtr& get(GOAKContext &ctx, int idx) { return ctx.in(idx); }
};
template<class T> struct get_in {
    static T get(GOAKContext &ctx, int idx) { return ctx.inArg<T>(idx); }
};
// FIXME: add support of other types

template<class T> struct get_out;
template<> struct get_out<cv::GFrame> {
    static GOAKContext::OutputPtr& get(GOAKContext &ctx, int idx) { return ctx.out(idx); }
};
template<typename U> struct get_out<cv::GArray<U>> {
    static GOAKContext::OutputPtr& get(GOAKContext &ctx, int idx) { return ctx.out(idx); }
};
template<> struct get_out<cv::GMat> {
    static GOAKContext::OutputPtr& get(GOAKContext &ctx, int idx) { return ctx.out(idx); }
};
// FIXME: add support of other types

template<typename, typename, typename>
struct OAKCallHelper;

template<typename Impl, typename... Ins, typename... Outs>
struct OAKCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> > {
    template<int... IIs, int... OIs>

    static std::shared_ptr<dai::Node> construct_impl(  GOAKContext &ctx

                                                     , std::vector<std::pair<std::string,

                                                                             dai::Buffer>>& in_queues_params

                                                     , cv::detail::Seq<IIs...>

                                                     , cv::detail::Seq<OIs...>) {
        return Impl::put(OAKKernelParams{ctx.pipeline(),
                                         ctx.camera_size(),
                                         ctx.ii(),
                                         in_queues_params},
                         get_in<Ins>::get(ctx, IIs)...,
                         get_out<Outs>::get(ctx, OIs)...);
    }

    static std::shared_ptr<dai::Node> construct(GOAKContext &ctx,

                                                std::vector<std::pair<std::string,

                                                                      dai::Buffer>>& in_queues_params) {
        return construct_impl(ctx,
                              in_queues_params,
                              typename cv::detail::MkSeq<sizeof...(Ins)>::type(),
                              typename cv::detail::MkSeq<sizeof...(Outs)>::type());
    }
};

} // namespace detail

struct GOAKKernel {
    using F = std::function<std::shared_ptr<dai::Node>(GOAKContext&,
                                                       std::vector<std::pair<std::string, dai::Buffer>>&)>;
    explicit GOAKKernel(const F& f) : m_put_f(f) {}
    const F m_put_f;
};

struct OAKComponent
{
    static const char *name() { return "OAK Component"; }
    GOAKKernel k;
};
} // namespace gimpl
} // namespace cv

using OAKGraph = ade::TypedGraph
    < cv::gimpl::Protocol
    , cv::gimpl::Op
    , cv::gimpl::NetworkParams
    , cv::gimpl::CustomMetaFunction
    // OAK specific
    , cv::gimpl::OAKComponent
    >;

using ConstOAKGraph = ade::ConstTypedGraph
    < cv::gimpl::Protocol
    , cv::gimpl::Op
    , cv::gimpl::NetworkParams
    , cv::gimpl::CustomMetaFunction
    // OAK specific
    , cv::gimpl::OAKComponent
    >;

namespace
{
std::pair<dai::TensorInfo, dai::TensorInfo>

parseDaiInferMeta(const cv::gapi::oak::detail::ParamDesc& pd) {
    dai::OpenVINO::Blob blob(pd.blob_file);

    GAPI_Assert(blob.networkInputs.size() == 1);
    GAPI_Assert(blob.networkOutputs.size() == 1);

    return {blob.networkInputs.begin()->second,
            blob.networkOutputs.begin()->second};
}

std::string

getDaiInferOutLayerName(const cv::gapi::oak::detail::ParamDesc& pd) {
    dai::OpenVINO::Blob blob(pd.blob_file);

    GAPI_Assert(blob.networkInputs.size() == 1);
    GAPI_Assert(blob.networkOutputs.size() == 1);

    return blob.networkOutputs.begin()->first;
}
} // anonymous namespace

// Custom meta function for OAK backend for infer
static cv::GMetaArgs customOutMeta(const ade::Graph      &gr,

                                   const ade::NodeHandle &nh,

                                   const cv::GMetaArgs   &/*in_metas*/,

                                   const cv::GArgs       &/*in_args*/) {
    cv::GMetaArgs result;
    const auto &np = ConstOAKGraph(gr).metadata(nh).get<cv::gimpl::NetworkParams>();
    const auto &pd = cv::util::any_cast<cv::gapi::oak::detail::ParamDesc>(np.opaque);

    // FIXME: Infer kernel and backend does rather the same
    auto in_out_tensor_info = parseDaiInferMeta(pd);

    GAPI_Assert(in_out_tensor_info.second.dataType ==
                dai::TensorInfo::DataType::FP16);

    // FIXME: add proper layout converter here
    GAPI_Assert(in_out_tensor_info.second.order ==
                dai::TensorInfo::StorageOrder::NCHW);

    // FIXME: DAI returns vector<unsigned>, remove workaround
    std::vector<int> wrapped_dims;
    for (const auto& d : in_out_tensor_info.second.dims) {
        wrapped_dims.push_back(d);
    }
    result = {cv::GMetaArg{cv::GMatDesc(CV_16F, 1, cv::Size(wrapped_dims[1], wrapped_dims[0]), false)}};

    return result;
}

// This function links DAI operation nodes - parent's output to child's input.
// It utilizes G-API graph to search for operation's node it's previous operation in graph
// when links them in DAI graph.
void cv::gimpl::GOAKExecutable::linkToParent(ade::NodeHandle handle)
{
    ade::NodeHandle parent;
    for (const auto& data_nh : handle.get()->inNodes()) {
        // Data node has only 1 input
        GAPI_Assert(data_nh.get()->inNodes().size() == 1);
        parent = data_nh.get()->inNodes().front();

        // Don't link if parent is copy - the case is handled differently
        // in linkCopy
        const auto& op = m_gm.metadata(parent).get<Op>();
        if (op.k.name == "org.opencv.oak.copy") {
            continue;
        }

        // Assuming that OAK nodes are aligned for linking.
        // FIXME: potential rework might be needed then
        //        counterexample is found.
        GAPI_Assert(m_oak_nodes.at(handle).inputs.size() ==
                    m_oak_nodes.at(parent).outputs.size() &&
                    "Internal OAK nodes are not aligned for linking");
        for (auto && it : ade::util::zip(ade::util::toRange(m_oak_nodes.at(parent).outputs),
                                         ade::util::toRange(m_oak_nodes.at(handle).inputs)))
        {
            auto &out = std::get<0>(it);
            auto &in = std::get<1>(it);
            out->link(*in);
        }
    }
}

// This function links DAI operations for Copy OP in G-API graph
void cv::gimpl::GOAKExecutable::linkCopy(ade::NodeHandle handle) {
    // 1. Check that there are no back-to-back Copy OPs in graph
    auto copy_out = handle.get()->outNodes();
    GAPI_Assert(copy_out.size() == 1);
    for (const auto& copy_next_op : copy_out.front().get()->outNodes()) {
        const auto& op = m_gm.metadata(copy_next_op).get<Op>();
        if (op.k.name == "org.opencv.oak.copy") {
            GAPI_Error("Back-to-back Copy operations are not supported in graph");
        }
    }

    // 2. Link passthrough case
    if (m_passthrough_copy_nodes.find(handle) != m_passthrough_copy_nodes.end()) {
        ExtractTypeHelper::OutputPtr parent;
        bool parent_is_camera = false;
        // Copy has only 1 input data
        GAPI_Assert(handle.get()->inNodes().size() == 1);
        auto in_ops = handle.get()->inNodes().front().get()->inNodes();
        if (in_ops.size() == 0) {
            // No parent nodes - parent = camera
            parent = &m_camera_input->video;
            parent_is_camera = true;
        } else {
            // Data has only 1 input
            GAPI_Assert(in_ops.size() == 1);
            auto node = m_oak_nodes.at(in_ops.front());
            // Should only have 1 output
            GAPI_Assert(node.outputs.size() == 1);
            parent = node.outputs[0];
        }

        // Now link DAI parent output to Copy's child's inputs ignoring the Copy operation
        // FIXME: simplify this loop
        auto copy_out_data = handle.get()->outNodes();
        // Copy has only 1 output
        GAPI_Assert(copy_out_data.size() == 1);
        for (const auto& copy_next_op : copy_out_data.front().get()->outNodes()) {
            if (m_oak_nodes.find(copy_next_op) != m_oak_nodes.end()) {
                // FIXME: consider a better approach
                if (parent_is_camera) {
                    if (m_oak_infer_info.find(copy_next_op) != m_oak_infer_info.end()) {
                        parent = &m_camera_input->preview;
                    } else {
                        parent = &m_camera_input->video;
                    }
                }
                // Found next Copy OP which needs to be linked to Copy's parent
                GAPI_Assert(m_oak_nodes.at(copy_next_op).inputs.size() == 1 &&
                            "Internal OAK nodes are not aligned for linking (Copy operation)");
                parent->link(*(m_oak_nodes.at(copy_next_op).inputs.front()));
            }
        }
    }

    // 3. Link output Copy case
    if (m_out_queues.find(handle) != m_out_queues.end()) {
        // DAI XLinkOutput node
        auto xout = m_out_queues[handle].xlink_output->input;

        // Find parent node
        // FIXME: copypasted from case 2 above
        ExtractTypeHelper::OutputPtr parent;
        // Copy has only 1 input data
        GAPI_Assert(handle.get()->inNodes().size() == 1);
        auto in_ops = handle.get()->inNodes().front().get()->inNodes();
        if (in_ops.size() == 0) {
            // No parent nodes - parent = camera
            parent = &m_camera_input->video;
        } else {
            // Data has only 1 input
            GAPI_Assert(in_ops.size() == 1);
            auto node = m_oak_nodes.at(in_ops.front());
            // Should only have 1 output
            GAPI_Assert(node.outputs.size() == 1);
            parent = node.outputs[0];
        }

        // Link parent to xout
        parent->link(xout);
    }
}

cv::GArg
cv::gimpl::GOAKExecutable::packInArg(const GArg &arg,
                                     std::vector<ExtractTypeHelper::InputPtr>& oak_ins) {
    if (arg.kind != cv::detail::ArgKind::GOBJREF) {
        GAPI_Assert(   arg.kind != cv::detail::ArgKind::GMAT
                    && arg.kind != cv::detail::ArgKind::GSCALAR
                    && arg.kind != cv::detail::ArgKind::GARRAY
                    && arg.kind != cv::detail::ArgKind::GOPAQUE
                    && arg.kind != cv::detail::ArgKind::GFRAME);
        // All other cases - pass as-is, with no transformations to
        // GArg contents.
        return const_cast<cv::GArg&>(arg);
    }
    const cv::gimpl::RcDesc &ref = arg.get<cv::gimpl::RcDesc>();
    switch (ref.shape) {
    case GShape::GFRAME:
        oak_ins.push_back(nullptr);
        return GArg(std::reference_wrapper<ExtractTypeHelper::InputPtr>(oak_ins.back()));
        break;
    default:
        util::throw_error(std::logic_error("Unsupported GShape type in OAK backend"));
        break;
    }
}

void cv::gimpl::GOAKExecutable::packOutArg(const RcDesc &rc,
                                           std::vector<ExtractTypeHelper::OutputPtr>& oak_outs) {
    switch (rc.shape) {
    case GShape::GFRAME:
    case GShape::GARRAY:
    case GShape::GMAT:
        oak_outs.push_back(nullptr);
        break;
    default:
        util::throw_error(std::logic_error("Unsupported GShape type in OAK backend"));
        break;
    }
}

namespace {
static dai::CameraBoardSocket extractCameraBoardSocket(cv::gapi::oak::ColorCameraParams ccp) {
    switch (ccp.board_socket) {
        case cv::gapi::oak::ColorCameraParams::BoardSocket::RGB:
            return dai::CameraBoardSocket::RGB;
        // FIXME: extend
        default:
            // basically unreachable
            GAPI_Assert("Unsupported camera board socket");
            return {};
    }
}

static dai::ColorCameraProperties::SensorResolution

extractCameraResolution(cv::gapi::oak::ColorCameraParams ccp) {
    switch (ccp.resolution) {
        case cv::gapi::oak::ColorCameraParams::Resolution::THE_1080_P:
            return dai::ColorCameraProperties::SensorResolution::THE_1080_P;
        // FIXME: extend
        default:
            // basically unreachable
            GAPI_Assert("Unsupported camera board socket");
            return {};
    }
}
} // anonymous namespace

cv::gimpl::GOAKExecutable::GOAKExecutable(const ade::Graph& g,
                                          const cv::GCompileArgs &args,
                                          const std::vector<ade::NodeHandle>& nodes,
                                          const std::vector<cv::gimpl::Data>& ins_data,
                                          const std::vector<cv::gimpl::Data>& outs_data)
    : m_g(g), m_gm(m_g), m_args(args),
      m_device(nullptr), m_pipeline(new dai::Pipeline)
    {
        // FIXME: currently OAK backend only works with camera as input,
        //        so it must be a single object
        GAPI_Assert(ins_data.size() == 1);

        // Check that there is only one OAK island in graph since there
        // can only be one instance of dai::Pipeline in the application
        auto isl_graph = m_gm.metadata().get<IslandModel>().model;
        GIslandModel::Graph gim(*isl_graph);
        size_t oak_islands = 0;

        for (const auto& nh : gim.nodes())
        {
            if (gim.metadata(nh).get<NodeKind>().k == NodeKind::ISLAND)
            {
                const auto isl = gim.metadata(nh).get<FusedIsland>().object;
                if (isl->backend() == cv::gapi::oak::backend())
                {
                    ++oak_islands;
                }
                if (oak_islands > 1) {
                    util::throw_error
                        (std::logic_error
                            ("There can only be one OAK island in graph"));
                }
            }
        }

        m_ccp = cv::gimpl::getCompileArg<cv::gapi::oak::ColorCameraParams>(args)
                    .value_or(cv::gapi::oak::ColorCameraParams{});

        // FIXME: change the hard-coded behavior (XLinkIn path)
        auto camRgb = m_pipeline->create<dai::node::ColorCamera>();
        // FIXME: extract camera compile arguments here and properly convert them for dai
        camRgb->setBoardSocket(extractCameraBoardSocket(m_ccp));
        camRgb->setResolution(extractCameraResolution(m_ccp));
        camRgb->setInterleaved(m_ccp.interleaved);

        // Extract infer params
        for (const auto& nh : nodes) {
            if (m_gm.metadata(nh).get<NodeType>().t == NodeType::OP) {
                if (ConstOAKGraph(m_g).metadata(nh).contains<cv::gimpl::NetworkParams>()) {
                    const auto &np = ConstOAKGraph(m_g).metadata(nh).get<cv::gimpl::NetworkParams>();
                    const auto &pp = cv::util::any_cast<cv::gapi::oak::detail::ParamDesc>(np.opaque);
                    m_oak_infer_info[nh] = pp;
                    break;
                }
            }
        }

        // FIXME: handle multiple infers
        if (!m_oak_infer_info.empty()) {
            GAPI_Assert(m_oak_infer_info.size() == 1);
            // FIXME: move to infer node?
            auto in_out_tensor_info = parseDaiInferMeta(m_oak_infer_info.begin()->second);

            if (in_out_tensor_info.first.dataType ==
                dai::TensorInfo::DataType::FP16 ||
                in_out_tensor_info.first.dataType ==
                dai::TensorInfo::DataType::FP32) {
                camRgb->setFp16(true);
            } else {
                camRgb->setFp16(false);
            }

            // FIXME: add proper layout converter here
            GAPI_Assert(in_out_tensor_info.first.order ==
                        dai::TensorInfo::StorageOrder::NCHW);
            camRgb->setPreviewSize(in_out_tensor_info.first.dims[0], in_out_tensor_info.first.dims[1]);
        }

        m_camera_input = camRgb;
        // FIXME: change when other camera censors are introduced
        std::tuple<int, int> video_size = m_camera_input->getVideoSize();
        m_camera_size = cv::Size{std::get<0>(video_size), std::get<1>(video_size)};

        // Prepare XLinkOut nodes for each output object in graph
        for (size_t i = 0; i < outs_data.size(); ++i) {
            auto xout = m_pipeline->create<dai::node::XLinkOut>();
            std::string xout_name = "xout" + std::to_string(i);
            xout->setStreamName(xout_name);

            // Find parent OP's nh
            ade::NodeHandle parent_op_nh;
            for (const auto& nh : nodes) {
                for (const auto& outdata : nh.get()->outNodes()) {
                    if (m_gm.metadata(outdata).get<NodeType>().t == NodeType::DATA) {
                        auto rc = m_gm.metadata(outdata).get<cv::gimpl::Data>().rc;
                        auto shape = m_gm.metadata(outdata).get<cv::gimpl::Data>().shape;
                        // Match outs_data with the actual operation
                        if (rc == outs_data[i].rc && shape == outs_data[i].shape) {
                            parent_op_nh = nh;
                        }
                    }
                }
            }

            m_out_queues[parent_op_nh] = {xout, nullptr, xout_name, i};
        }

        // Create OAK node for each node in this backend
        for (const auto& nh : nodes) {
            if (m_gm.metadata(nh).get<NodeType>().t == NodeType::OP) {
                const auto& op = m_gm.metadata(nh).get<Op>();
                const auto &u = ConstOAKGraph(m_g).metadata(nh).get<OAKComponent>();
                // pass kernel input args and compile args to prepare OAK node and
                // store it to link later
                m_oak_nodes[nh] = {};
                m_oak_nodes.at(nh).inputs.reserve(op.args.size());
                m_oak_nodes.at(nh).outputs.reserve(op.outs.size());

                // Copy operation in graph can fall into 3 cases:
                // 1) Copy is an output of the island -
                //    in that case we link it to XLinkOut node from m_out_queues
                // 2) Copy is between other two operations in the same OAK island -
                //    in that case we link its parent operation (could be camera) to
                //    the child one (those copy operations are placed in m_passthrough_copy_nodes)
                // 3) Copy can fall into cases 1) and 2) at the same time

                // Prepare passthrough Copy operations
                if (op.k.name == "org.opencv.oak.copy") {
                    // Copy has only 1 output
                    auto copy_out = nh.get()->outNodes();
                    GAPI_Assert(copy_out.size() == 1);
                    for (const auto& copy_next_op : copy_out.front().get()->outNodes()) {
                        // Check that copy is a passthrough OP
                        if (std::find(nodes.begin(), nodes.end(), copy_next_op) != nodes.end()) {
                            m_passthrough_copy_nodes.insert(nh);
                            break;
                        }
                    }
                }

                std::vector<cv::GArg> in_ctx_args;
                in_ctx_args.reserve(op.args.size());
                for (auto &op_arg : op.args) in_ctx_args.push_back(packInArg(op_arg,
                                                                             m_oak_nodes.at(nh).inputs));
                for (auto &&op_out : op.outs) packOutArg(op_out, m_oak_nodes.at(nh).outputs);
                GAPI_Assert(!m_oak_nodes.at(nh).inputs.empty());
                GAPI_Assert(!m_oak_nodes.at(nh).outputs.empty());

                if (ConstOAKGraph(m_g).metadata(nh).contains<cv::gimpl::NetworkParams>()) {
                    GOAKContext ctx(m_pipeline, m_camera_size, m_oak_infer_info[nh],

                                    in_ctx_args, m_oak_nodes.at(nh).outputs);
                    m_oak_nodes.at(nh).node = u.k.m_put_f(ctx, m_in_queues);
                } else {
                    GOAKContext ctx(m_pipeline, m_camera_size,
                                    in_ctx_args, m_oak_nodes.at(nh).outputs);
                    m_oak_nodes.at(nh).node = u.k.m_put_f(ctx, m_in_queues);
                }

                // Check that all inputs and outputs are properly filled after constructing kernels
                // to then link it together
                // FIXME: add more logging
                const auto& node_info = m_oak_nodes.at(nh);
                // Copy operations don't set their inputs/outputs properly
                if (op.k.name != "org.opencv.oak.copy") {
                    GAPI_Assert(node_info.node != nullptr);
                    if (std::any_of(node_info.inputs.cbegin(), node_info.inputs.cend(),
                                    [](ExtractTypeHelper::InputPtr ptr) {
                            return ptr == nullptr;
                        })) {
                        GAPI_Error("DAI input are not set");
                    }

                    if (std::any_of(node_info.outputs.cbegin(), node_info.outputs.cend(),
                                    [](ExtractTypeHelper::OutputPtr ptr) {
                            return ptr == nullptr;
                        })) {
                        GAPI_Error("DAI outputs are not set");
                    }
                }
            }
        }

        // Prepare nodes for linking
        std::unordered_set<ade::NodeHandle,
                           ade::HandleHasher<ade::Node>> in_nodes;
        std::unordered_set<ade::NodeHandle,
                           ade::HandleHasher<ade::Node>> out_nodes;
        std::unordered_set<ade::NodeHandle,
                           ade::HandleHasher<ade::Node>> inter_nodes;
        std::unordered_set<ade::NodeHandle,
                           ade::HandleHasher<ade::Node>> copy_nodes;

        // TODO: optimize this loop
        for (const auto& node : m_oak_nodes) {
            auto nh = node.first;
            // Check if it's a Copy OP - will be handled differently when linking
            GAPI_Assert(m_gm.metadata(nh).get<NodeType>().t == NodeType::OP);
            const auto& op = m_gm.metadata(nh).get<Op>();
            if (op.k.name == "org.opencv.oak.copy") {
                copy_nodes.insert(nh);
                continue;
            }

            // Fill input op nodes
            for (const auto& d : ins_data) {
                for (const auto& indata : nh.get()->inNodes()) {
                    auto rc = m_gm.metadata(indata).get<cv::gimpl::Data>().rc;
                    auto shape = m_gm.metadata(indata).get<cv::gimpl::Data>().shape;
                    if (rc == d.rc && shape == d.shape) {
                        in_nodes.insert(nh);
                    }
                }
            }
            // Fill output op nodes
            for (const auto& d : outs_data) {
                for (const auto& outdata : nh.get()->outNodes()) {
                    auto rc = m_gm.metadata(outdata).get<cv::gimpl::Data>().rc;
                    auto shape = m_gm.metadata(outdata).get<cv::gimpl::Data>().shape;
                    if (rc == d.rc && shape == d.shape) {
                        out_nodes.insert(nh);
                    }
                }
            }
            // Fill internal op nodes
            if (in_nodes.find(nh) == in_nodes.end() &&
                out_nodes.find(nh) == in_nodes.end()) {
                inter_nodes.insert(nh);
            }
        }

        // Properly link all nodes
        // 1. Link input nodes to camera
        for (const auto& nh : in_nodes) {
            GAPI_Assert(m_oak_nodes.at(nh).inputs.size() == 1);
            // FIXME: convert other camera outputs
            // Link preview to infer, video to all other nodes
            if (m_oak_infer_info.find(nh) == m_oak_infer_info.end()) {
                m_camera_input->video.link(*(m_oak_nodes.at(nh).inputs[0]));
            } else {
                m_camera_input->preview.link(*(m_oak_nodes.at(nh).inputs[0]));
            }
        }

        // 2. Link output nodes to XLinkOut nodes
        for (const auto& nh : out_nodes) {
            for (const auto& out : m_oak_nodes.at(nh).outputs) {
                out->link(m_out_queues[nh].xlink_output->input);
            }
            // Input nodes in OAK doesn't have parent operation - just camera (for now)
            if (in_nodes.find(nh) == in_nodes.end()) {
                linkToParent(nh);
            }
        }

        // 3. Link internal nodes to their parents
        for (const auto& nh : inter_nodes) {
            linkToParent(nh);
        }

        // 4. Link copy nodes
        for (const auto& nh : copy_nodes) {
            linkCopy(nh);
        }

        m_device = std::unique_ptr<dai::Device>(new dai::Device(*m_pipeline));

        // Prepare OAK output queues
        GAPI_Assert(m_out_queues.size() == outs_data.size());
        for (const auto out_it : ade::util::indexed(m_out_queues))
        {
            auto& q = ade::util::value(out_it).second;
            GAPI_Assert(q.out_queue == nullptr); // shouldn't be not filled till this point
            // FIXME: add queue parameters
            // Currently: 4 - max DAI queue capacity, true - blocking queue
            q.out_queue = m_device->getOutputQueue(q.out_queue_name, 4, true);
        }
    }

void cv::gimpl::GOAKExecutable::handleNewStream() {
    // do nothing
}

void cv::gimpl::GOAKExecutable::handleStopStream() {
    // do nothing
}

void cv::gimpl::GOAKExecutable::run(GIslandExecutable::IInput  &in,
                                    GIslandExecutable::IOutput &out) {
    const auto in_msg = in.get();

    if (cv::util::holds_alternative<cv::gimpl::EndOfStream>(in_msg)) {
        out.post(cv::gimpl::EndOfStream{});
        return;
    }

    for (const auto& in_q : m_in_queues) {
        auto q = m_device->getInputQueue(in_q.first);
        q->send(in_q.second);
    }

    for (size_t i = 0; i < m_in_queues.size(); ++i) {
        auto q = m_device->getInputQueue(m_in_queues[i].first);
        q->send(m_in_queues[i].second);
    }

    for (const auto el : m_out_queues) {
        const auto out_q = el.second;
        auto& q = out_q.out_queue;

        auto out_arg = out.get(out_q.gapi_out_data_index);

        // FIXME: misc info to be utilized in switch below
        cv::GRunArg::Meta meta;
        std::shared_ptr<dai::ImgFrame> oak_frame;

        switch(out_arg.index()) {
        case cv::GRunArgP::index_of<cv::MediaFrame*>():
        {
            oak_frame = q->get<dai::ImgFrame>();
            // FIXME: hard-coded NV12
            *cv::util::get<cv::MediaFrame*>(out_arg) =
                    cv::MediaFrame::Create<cv::gapi::oak::OAKMediaAdapter>(
                            cv::Size(static_cast<int>(oak_frame->getWidth()),
                                     static_cast<int>(oak_frame->getHeight())),
                            cv::MediaFormat::NV12,
                            std::move(oak_frame->getData()));

            using namespace cv::gapi::streaming::meta_tag;
            meta[timestamp] = oak_frame->getTimestamp();
            meta[seq_id]    = oak_frame->getSequenceNum();

            break;
        }
        case cv::GRunArgP::index_of<cv::detail::VectorRef>():
        {
            oak_frame = q->get<dai::ImgFrame>();
            cv::util::get<cv::detail::VectorRef>(out_arg).wref<uint8_t>() = std::move(oak_frame->getData());

            using namespace cv::gapi::streaming::meta_tag;
            meta[timestamp] = oak_frame->getTimestamp();
            meta[seq_id]    = oak_frame->getSequenceNum();

            break;
        }
        case cv::GRunArgP::index_of<cv::RMat*>(): // only supported for infer
        {
            auto nn_data = q->get<dai::NNData>();

            auto out_layer_name = getDaiInferOutLayerName(m_oak_infer_info.begin()->second);
            auto in_out_tensor_info = parseDaiInferMeta(m_oak_infer_info.begin()->second);

            auto layer = std::move(nn_data->getLayerFp16(out_layer_name));

            // FIXME: add proper layout converter here
            GAPI_Assert(in_out_tensor_info.second.order ==
                        dai::TensorInfo::StorageOrder::NCHW);
            // FIMXE: only 1-channel data is supported for now
            GAPI_Assert(in_out_tensor_info.second.dims[2] == 1);

            *cv::util::get<cv::RMat*>(out_arg) =
                    cv::make_rmat<cv::gapi::oak::OAKRMatAdapter>(
                        cv::Size(in_out_tensor_info.second.dims[1],
                                 in_out_tensor_info.second.dims[0]),
                        CV_16F, // FIXME: cover other precisions
                        std::move(layer)
                    );

            using namespace cv::gapi::streaming::meta_tag;
            meta[timestamp] = nn_data->getTimestamp();
            meta[seq_id]    = nn_data->getSequenceNum();

            break;
        }
        // FIXME: Add support for remaining types
        default:
            GAPI_Error("Unsupported type in OAK backend");
        }

        out.meta(out_arg, meta);
        out.post(std::move(out_arg));
    }
}

namespace cv {
namespace gimpl {
namespace oak {

namespace {
static dai::VideoEncoderProperties::Profile convertEncProfile(cv::gapi::oak::EncoderConfig::Profile pf) {
    switch (pf) {
        case cv::gapi::oak::EncoderConfig::Profile::H264_BASELINE:
            return dai::VideoEncoderProperties::Profile::H264_BASELINE;
        case cv::gapi::oak::EncoderConfig::Profile::H264_HIGH:
            return dai::VideoEncoderProperties::Profile::H264_HIGH;
        case cv::gapi::oak::EncoderConfig::Profile::H264_MAIN:
            return dai::VideoEncoderProperties::Profile::H264_MAIN;
        case cv::gapi::oak::EncoderConfig::Profile::H265_MAIN:
            return dai::VideoEncoderProperties::Profile::H265_MAIN;
        case cv::gapi::oak::EncoderConfig::Profile::MJPEG:
            return dai::VideoEncoderProperties::Profile::MJPEG;
        default:
            // basically unreachable
            GAPI_Assert("Unsupported encoder profile");
            return {};
    }
}
} // anonymous namespace

// Kernels ///////////////////////////////////////////////////////////////

// FIXME: consider a better solution - hard-coded API
//        Is there a way to extract API from somewhereelse/utilize structs
//        like in streaming/infer backends (mainly infer and copy operations)
template<class Impl, class K, class InArgs = typename K::InArgs, class OutArgs = typename K::OutArgs>
class GOAKKernelImpl: public detail::OAKCallHelper<Impl, InArgs, OutArgs>
                    , public cv::detail::KernelTag {
    using P = detail::OAKCallHelper<Impl, InArgs, OutArgs>;
public:
    using API = K;
    static cv::gapi::GBackend   backend() { return cv::gapi::oak::backend();  }
    static GOAKKernel kernel()  { return GOAKKernel(&P::construct); }
};

#define GAPI_OAK_KERNEL(Name, API) \
    struct Name: public cv::gimpl::oak::GOAKKernelImpl<Name, API>

#define GAPI_OAK_FIXED_API_KERNEL(Name, API, InArgs, OutArgs) \
    struct Name: public cv::gimpl::oak::GOAKKernelImpl<Name, API, InArgs, OutArgs>

namespace {
GAPI_OAK_FIXED_API_KERNEL(GOAKInfer, cv::GInferBase, std::tuple<cv::GFrame>, std::tuple<cv::GMat>) {
    static std::shared_ptr<dai::Node> put(const cv::gimpl::OAKKernelParams& params,

                                          GOAKContext::InputPtr& in,

                                          GOAKContext::OutputPtr& out) {
        auto nn = params.pipeline->create<dai::node::NeuralNetwork>();

        nn->input.setBlocking(true);
        nn->input.setQueueSize(1);

        // FIXME: add G-API built-in preproc here (currently it's only setPreviewSize() on the camera node)
        // Note: for some reason currently it leads to:
        // "Fatal error. Please report to developers. Log: 'ImageManipHelper' '61'"

        nn->setBlobPath(params.infer_info.blob_file);

        in = &(nn->input);
        out = &(nn->out);

        return nn;
    }
};

GAPI_OAK_KERNEL(GOAKCopy, cv::gapi::oak::GCopy) {
    static std::shared_ptr<dai::Node> put(const cv::gimpl::OAKKernelParams&,

                                          GOAKContext::InputPtr&,

                                          GOAKContext::OutputPtr&) {
        // Do nothing in Copy OP since it's either already represented
        // by XLinkOut node (bonded to output queues) or it's a passthrough OP
        return nullptr;
    }
};

GAPI_OAK_KERNEL(GOAKEncFrame, cv::gapi::oak::GEncFrame) {
    static std::shared_ptr<dai::Node> put(const cv::gimpl::OAKKernelParams& params,

                                          GOAKContext::InputPtr& in,

                                          const cv::gapi::oak::EncoderConfig& cfg,

                                          GOAKContext::OutputPtr& out) {
        auto videoEnc = params.pipeline->create<dai::node::VideoEncoder>();

        // FIXME: convert all the parameters to dai
        videoEnc->setDefaultProfilePreset(cfg.frameRate,
                                          convertEncProfile(cfg.profile));

        in = &(videoEnc->input);
        out = &(videoEnc->bitstream);

        return videoEnc;
    }
};

GAPI_OAK_KERNEL(GOAKSobelXY, cv::gapi::oak::GSobelXY) {
    static std::shared_ptr<dai::Node> put(const cv::gimpl::OAKKernelParams& params,

                                          GOAKContext::InputPtr& in,

                                          const cv::Mat& hk,

                                          const cv::Mat& vk,

                                          GOAKContext::OutputPtr& out) {
        auto edgeDetector = params.pipeline->create<dai::node::EdgeDetector>();

        edgeDetector->setMaxOutputFrameSize(params.camera_size.width * params.camera_size.height);

        auto xinEdgeCfg = params.pipeline->create<dai::node::XLinkIn>();
        xinEdgeCfg->setStreamName("sobel_cfg");

        auto mat2vec = [&](cv::Mat m) {
            std::vector<std::vector<int>> v(m.rows);
            for (int i = 0; i < m.rows; ++i)
            {
                m.row(i).reshape(1,1).copyTo(v[i]);
            }
            return v;
        };

        dai::EdgeDetectorConfig cfg;
        cfg.setSobelFilterKernels(mat2vec(hk), mat2vec(vk));

        xinEdgeCfg->out.link(edgeDetector->inputConfig);

        params.in_queues.push_back({"sobel_cfg", cfg});

        in = &(edgeDetector->inputImage);
        out = &(edgeDetector->outputImage);

        return edgeDetector;
    }
};

} // anonymous namespace
} // namespace oak
} // namespace gimpl
} // namespace cv

class GOAKBackendImpl final : public cv::gapi::GBackend::Priv {
    virtual void unpackKernel(ade::Graph            &graph,

                              const ade::NodeHandle &op_node,

                              const cv::GKernelImpl &impl) override {
        using namespace cv::gimpl;

        OAKGraph gm(graph);

        const auto &kimpl  = cv::util::any_cast<GOAKKernel>(impl.opaque);
        gm.metadata(op_node).set(OAKComponent{kimpl});

        // Set custom meta for infer
        if (gm.metadata(op_node).contains<cv::gimpl::NetworkParams>()) {
            gm.metadata(op_node).set(CustomMetaFunction{customOutMeta});
        }
    }

    virtual EPtr compile(const ade::Graph &graph,

                         const cv::GCompileArgs &args,

                         const std::vector<ade::NodeHandle> &nodes,

                         const std::vector<cv::gimpl::Data>& ins_data,

                         const std::vector<cv::gimpl::Data>& outs_data) const override {
        cv::gimpl::GModel::ConstGraph gm(graph);
        // FIXME: pass streaming/non-streaming option to support non-camera case
        // NB: how could we have non-OAK source in streaming mode, then OAK backend in
        //     streaming mode but without camera input?
        if (!gm.metadata().contains<cv::gimpl::Streaming>()) {
            GAPI_Error("OAK backend only supports Streaming mode for now");
        }
        return EPtr{new cv::gimpl::GOAKExecutable(graph, args, nodes, ins_data, outs_data)};
    }

    virtual cv::GKernelPackage auxiliaryKernels() const override {
        return cv::gapi::kernels< cv::gimpl::oak::GOAKInfer
                                >();
    }
};

cv::gapi::GBackend cv::gapi::oak::backend() {
    static cv::gapi::GBackend this_backend(std::make_shared<GOAKBackendImpl>());
    return this_backend;
}

namespace cv {
namespace gapi {
namespace oak {

cv::gapi::GKernelPackage kernels() {
    return cv::gapi::kernels< cv::gimpl::oak::GOAKEncFrame
                            , cv::gimpl::oak::GOAKSobelXY
                            , cv::gimpl::oak::GOAKCopy
                            >();
}

} // namespace oak
} // namespace gapi
} // namespace cv

#else

namespace cv {
namespace gapi {
namespace oak {

cv::gapi::GKernelPackage kernels() {
    GAPI_Error("Built without OAK support");
}

cv::gapi::GBackend backend() {
    GAPI_Error("Built without OAK support");
}

} // namespace oak
} // namespace gapi
} // namespace cv

#endif // HAVE_OAK