Ejemplo n.º 1
0
        public static void Deserialize(string path, ref Krls <TScalar, TKernel> krls)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException($"{path} is not found", path);
            }

            var str   = Dlib.Encoding.GetBytes(path);
            var error = NativeMethods.deserialize_krls(str,
                                                       krls.Parameter.KernelType.ToNativeKernelType(),
                                                       krls.Parameter.SampleType.ToNativeMatrixElementType(),
                                                       krls.Parameter.TemplateRows,
                                                       krls.Parameter.TemplateColumns,
                                                       krls.NativePtr,
                                                       out var errorMessage);

            switch (error)
            {
            case NativeMethods.ErrorType.GeneralSerialization:
                throw new SerializationException(StringHelper.FromStdString(errorMessage, true));

            case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                throw new ArgumentException($"{krls.Parameter.SampleType} is not supported.");

            case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport:
                throw new ArgumentException($"{nameof(krls.Parameter.TemplateColumns)} or {nameof(krls.Parameter.TemplateRows)} is not supported.");

            case NativeMethods.ErrorType.SvmKernelNotSupport:
                throw new ArgumentException($"{krls.Parameter.KernelType} is not supported.");
            }
        }
Ejemplo n.º 2
0
        public static void Serialize(Krls <TScalar, TKernel> krls, string path)
        {
            if (krls == null)
            {
                throw new ArgumentNullException(nameof(krls));
            }
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException();
            }

            krls.ThrowIfDisposed();

            var str       = Dlib.Encoding.GetBytes(path);
            var strLength = str.Length;

            Array.Resize(ref str, strLength + 1);
            str[strLength] = (byte)'\0';
            var ret = NativeMethods.serialize_krls(krls._Parameter.KernelType.ToNativeKernelType(),
                                                   krls._Parameter.SampleType.ToNativeMatrixElementType(),
                                                   krls._Parameter.TemplateRows,
                                                   krls._Parameter.TemplateColumns,
                                                   krls.NativePtr,
                                                   str,
                                                   out var errorMessage);

            switch (ret)
            {
            case NativeMethods.ErrorType.GeneralSerialization:
                throw new SerializationException(StringHelper.FromStdString(errorMessage, true));

            case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                throw new ArgumentException($"{krls.Parameter.SampleType} is not supported.");

            case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport:
                throw new ArgumentException($"{nameof(krls.Parameter.TemplateColumns)} or {nameof(krls.Parameter.TemplateRows)} is not supported.");

            case NativeMethods.ErrorType.SvmKernelNotSupport:
                throw new ArgumentException($"{krls.Parameter.KernelType} is not supported.");
            }
        }