Repository: opencv/opencv
File: modules/dnn/src/layers/onehot_layer.cpp (C++)

Code before:
        std::vector<Mat>* out_mats = nullptr;
        std::vector<UMat>* out_umats = nullptr;
        Mat Y;
        if (outKind == _InputArray::STD_VECTOR_MAT)
        {
            out_mats = &out.getMatVecRef();
            out_mats->resize(1);
        }
        else if (outKind == _InputArray::STD_VECTOR_UMAT)
        {
            out_umats = &out.getUMatVecRef();
            out_umats->resize(1);
        }
        else
        {
            CV_Error(Error::StsNotImplemented, "OneHot: unsupported output type");
        }

        int outDepth = values.empty() ? CV_32F : values.depth();

        MatShape outShape = shape(indices);
        int ndims = (int)outShape.size();
        int insAxis = normalize_axis(axis, ndims + 1);

        MatShape finalShape;
        if (depthMat.total() == 1)
        {
            int64_t depth64 = 0;
            tensorToScalar(depthMat, CV_64S, &depth64);
            if (depth64 > 0)
            {
                outShape.insert(outShape.begin() + insAxis, (int)depth64);
                finalShape = outShape;
            }
        }
        if (!finalShape.empty())
        {
            if (outKind == _InputArray::STD_VECTOR_MAT)
            {
                out_mats->at(0).fit(finalShape, outDepth);
                Y = out_mats->at(0);
            }
            else
            {
                out_umats->at(0).fit(finalShape, outDepth);
                Y = Mat(finalShape, outDepth);
            }
        }

        switch (outDepth) {
            case CV_8U:   runKernel<uchar>(indices, depthMat, values, Y); break;

Review comment (suggestion):
depthMat must contain a single element. Don't use `if`, use `CV_Assert()`.

Code after:
        else
        {
            CV_Error(Error::StsNotImplemented, "OneHot: unsupported input type");
        }

        std::vector<Mat>* out_mats = nullptr;
        std::vector<UMat>* out_umats = nullptr;
        Mat Y;
        if (outKind == _InputArray::STD_VECTOR_MAT)
        {
            out_mats = &out.getMatVecRef();
            out_mats->resize(1);
        }
        else if (outKind == _InputArray::STD_VECTOR_UMAT)
        {
            out_umats = &out.getUMatVecRef();
            out_umats->resize(1);
        }
        else
        {
            CV_Error(Error::StsNotImplemented, "OneHot: unsupported output type");
        }

        int outDepth = values.empty() ? CV_32F : values.depth();

        MatShape outShape = shape(indices);
        int ndims = (int)outShape.size();
        int insAxis = normalize_axis(axis, ndims + 1);

        CV_Assert(depthMat.total() == 1);
        int64_t depth64 = 0;
        tensorToScalar(depthMat, CV_64S, &depth64);
        CV_CheckGT(depth64, 0, "OneHot: depth must be > 0");
        MatShape finalShape;
        outShape.insert(outShape.begin() + insAxis, (int)depth64);
        finalShape = outShape;
        if (!finalShape.empty())
        {
            if (outKind == _InputArray::STD_VECTOR_MAT)
            {
                out_mats->at(0).fit(finalShape, outDepth);
                Y = out_mats->at(0);
            }
            else
            {
                out_umats->at(0).fit(finalShape, outDepth);
                Y = Mat(finalShape, outDepth);
            }
        }

        switch (outDepth) {