Exemplo n.º 1
0
        /// <summary>
        /// This function loads PNG (Portable Network Graphics) file into an <see cref="Array2D{T}"/>.
        /// </summary>
        /// <typeparam name="T">The type of element in the image.</typeparam>
        /// <param name="path">A string that contains the name of the file from which to create the <see cref="Array2D{T}"/>.</param>
        /// <returns>The <see cref="Array2D{T}"/> this method creates.</returns>
        /// <exception cref="ArgumentException">The specified type of image is not supported.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
        /// <exception cref="FileNotFoundException">The specified file does not exist.</exception>
        /// <exception cref="ImageLoadException">Failed to load image on dlib.</exception>
        public static Array2D <T> LoadPng <T>(string path)
            where T : struct
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException($"The specified {nameof(path)} does not exist.", path);
            }

            var str = Encoding.GetBytes(path);

            var image = new Array2D <T>();

            var array2DType = image.ImageType.ToNativeArray2DType();
            var ret         = NativeMethods.load_png(array2DType, image.NativePtr, str, out var errorMessage);

            switch (ret)
            {
            case NativeMethods.ErrorType.Array2DTypeTypeNotSupport:
                throw new ArgumentException($"{image.ImageType} is not supported.");

            case NativeMethods.ErrorType.GeneralFileImageLoad:
                throw new ImageLoadException(path, StringHelper.FromStdString(errorMessage));
            }

            return(image);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This function loads image file into an <see cref="Matrix{T}"/>.
        /// </summary>
        /// <typeparam name="T">The type of element in the matrix.</typeparam>
        /// <param name="path">A string that contains the name of the file from which to create the <see cref="Matrix{T}"/>.</param>
        /// <returns>The <see cref="Matrix{T}"/> this method creates.</returns>
        /// <exception cref="ArgumentException">The specified type of matrix is not supported.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
        /// <exception cref="FileNotFoundException">The specified file does not exist.</exception>
        /// <exception cref="ImageLoadException">Failed to load image on dlib.</exception>
        public static Matrix <T> LoadImageAsMatrix <T>(string path)
            where T : struct
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException($"The specified {nameof(path)} does not exist.", path);
            }

            if (!MatrixBase.TryParse(typeof(T), out var type))
            {
                throw new NotSupportedException($"{typeof(T).Name} does not support");
            }

            var str = Encoding.GetBytes(path);

            var matrixElementType = type.ToNativeMatrixElementType();
            var ret = NativeMethods.load_image_matrix(matrixElementType, str, out var matrix, out var errorMessage);

            switch (ret)
            {
            case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                throw new ArgumentException($"{type} is not supported.");

            case NativeMethods.ErrorType.GeneralFileImageLoad:
                throw new ImageLoadException(path, StringHelper.FromStdString(errorMessage));
            }

            return(new Matrix <T>(matrix));
        }
Exemplo n.º 3
0
        public static void Serialize(ShapePredictor predictor, string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException();
            }

            predictor.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_shape_predictor(predictor.NativePtr,
                                                              str,
                                                              out var errorMessage);

            switch (ret)
            {
            case NativeMethods.ErrorType.GeneralSerialization:
                throw new SerializationException(StringHelper.FromStdString(errorMessage, true));
            }
        }
Exemplo n.º 4
0
        public static ShapePredictor Deserialize(string path)
        {
            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 strLength = str.Length;

            Array.Resize(ref str, strLength + 1);
            str[strLength] = (byte)'\0';
            var ret = NativeMethods.deserialize_shape_predictor(str,
                                                                out var predictor,
                                                                out var errorMessage);

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

            return(new ShapePredictor(predictor));
        }
Exemplo n.º 5
0
            public override string ToString()
            {
                var    ofstream = IntPtr.Zero;
                var    stdstr   = IntPtr.Zero;
                string str      = null;

                try
                {
                    ofstream = Dlib.Native.ostringstream_new();
                    Native.surf_point_des_matrix_operator_left_shift(this.NativePtr, ofstream);
                    stdstr = Dlib.Native.ostringstream_str(ofstream);
                    str    = StringHelper.FromStdString(stdstr);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }
                finally
                {
                    if (stdstr != IntPtr.Zero)
                    {
                        Dlib.Native.string_delete(stdstr);
                    }
                    if (ofstream != IntPtr.Zero)
                    {
                        Dlib.Native.ostringstream_delete(ofstream);
                    }
                }

                return(str);
            }
Exemplo n.º 6
0
        public string GetDefaultOverlayRectLabel()
        {
            this.ThrowIfDisposed();
            var ret = NativeMethods.image_display_get_default_overlay_rect_label(this.NativePtr);

            return(StringHelper.FromStdString(ret, true));
        }
Exemplo n.º 7
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.");
            }
        }
Exemplo n.º 8
0
        public static Matrix <TElement> Deserialize(ProxyDeserialize deserialize, uint templateRows = 0, uint templateColumns = 0)
        {
            if (!TryParse(typeof(TElement), out var type))
            {
                throw new NotSupportedException($"{typeof(TElement).Name} does not support");
            }

            if (deserialize == null)
            {
                throw new ArgumentNullException(nameof(deserialize));
            }

            deserialize.ThrowIfDisposed();

            var ret = NativeMethods.matrix_deserialize_matrix_proxy(deserialize.NativePtr,
                                                                    type.ToNativeMatrixElementType(),
                                                                    (int)templateRows,
                                                                    (int)templateColumns,
                                                                    out var matrix,
                                                                    out var errorMessage);

            switch (ret)
            {
            case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                throw new ArgumentException($"{type} is not supported.");

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

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

            return(new Matrix <TElement>(matrix, (int)templateRows, (int)templateColumns));
        }
Exemplo n.º 9
0
 private void LogNative(IntPtr logName, LogLevel logLevel, IntPtr levelName, ulong threadId, IntPtr message)
 {
     this.Log(StringHelper.FromStdString(logName),
              logLevel,
              StringHelper.FromStdString(levelName),
              threadId,
              StringHelper.FromStdString(message));
 }
Exemplo n.º 10
0
        public static NormalizedFunction <TScalar, TFunction> Deserialize(string path, uint templateRows = 0, uint templateColumns = 0)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException();
            }

            var functionType = typeof(TFunction);
            var svmFunction  = functionType.GetGenericTypeDefinition();

            if (!FunctionTypesRepository.Types.TryGetValue(svmFunction, out var svmFunctionType))
            {
                throw new ArgumentException();
            }

            var kernelType = functionType.GenericTypeArguments[1].GetGenericTypeDefinition();

            if (!KernelTypesRepository.KernelTypes.TryGetValue(kernelType, out var svmKernelType))
            {
                throw new ArgumentException();
            }

            var elementType = functionType.GenericTypeArguments[0];

            if (!KernelTypesRepository.ElementTypes.TryGetValue(elementType, out var sampleType))
            {
                throw new ArgumentException();
            }

            var str = Dlib.Encoding.GetBytes(path);
            var err = NativeMethods.normalized_function_deserialize(svmKernelType.ToNativeKernelType(),
                                                                    sampleType.ToNativeMatrixElementType(),
                                                                    (int)templateRows,
                                                                    (int)templateColumns,
                                                                    svmFunctionType,
                                                                    str,
                                                                    str.Length,
                                                                    out IntPtr ret,
                                                                    out var errorMessage);

            switch (err)
            {
            case NativeMethods.ErrorType.SvmFunctionNotSupport:
                throw new ArgumentException($"{functionType} is not supported.");

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

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

            return(new NormalizedFunction <TScalar, TFunction>());
        }
Exemplo n.º 11
0
        public static string WrapString(string text, uint firstPad = 0, uint restPad = 0, uint maxPerLine = 79)
        {
            var str       = Encoding.GetBytes(text ?? "");
            var strLength = str.Length;

            Array.Resize(ref str, strLength + 1);
            str[strLength] = (byte)'\0';
            var ret = NativeMethods.wrap_string_char(str, firstPad, restPad, maxPerLine);

            return(StringHelper.FromStdString(ret, true));
        }
Exemplo n.º 12
0
        public static DecisionFunction <TScalar, TKernel> Deserialize(string path, int templateRows, int templateColumns)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException($"{path} is not found", path);
            }

            KernelFactory.TryParse <TScalar>(out MatrixElementTypes sampleType);
            KernelFactory.TryParse <TKernel>(out SvmKernelType kernelType);

            var param = new KernelBaseParameter(kernelType, sampleType, templateRows, templateColumns);

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

            Array.Resize(ref str, strLength + 1);
            str[strLength] = (byte)'\0';
            var error = NativeMethods.deserialize_decision_function(str,
                                                                    kernelType.ToNativeKernelType(),
                                                                    sampleType.ToNativeMatrixElementType(),
                                                                    templateRows,
                                                                    templateColumns,
                                                                    out var ret,
                                                                    out var errorMessage);

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

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

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

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

            return(new DecisionFunction <TScalar, TKernel>(ret, param));
        }
Exemplo n.º 13
0
        public override string ToString()
        {
            var ofstream = IntPtr.Zero;
            var stdstr   = IntPtr.Zero;
            var str      = "";

            try
            {
                ofstream = NativeMethods.ostringstream_new();
                var ret = NativeMethods.matrix_operator_left_shift(this._ElementType, this.NativePtr, this.TemplateRows, this.TemplateColumns, ofstream);
                switch (ret)
                {
                case NativeMethods.ErrorType.OK:
                    stdstr = NativeMethods.ostringstream_str(ofstream);
                    str    = StringHelper.FromStdString(stdstr) ?? "";
                    break;

                case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                    throw new ArgumentException($"Input {this._ElementType} is not supported.");

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

                default:
                    throw new ArgumentException();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                if (stdstr != IntPtr.Zero)
                {
                    NativeMethods.string_delete(stdstr);
                }
                if (ofstream != IntPtr.Zero)
                {
                    NativeMethods.ostringstream_delete(ofstream);
                }
            }

            return(str);
        }
Exemplo n.º 14
0
        public override string ToString()
        {
            var    ofstream = IntPtr.Zero;
            var    stdstr   = IntPtr.Zero;
            string str      = null;

            try
            {
                ofstream = Dlib.Native.ostringstream_new();
                var ret = Dlib.Native.vector_operator_left_shift(this._ElementType, this.NativePtr, ofstream);
                switch (ret)
                {
                case Dlib.Native.ErrorType.OK:
                    stdstr = Dlib.Native.ostringstream_str(ofstream);
                    str    = StringHelper.FromStdString(stdstr);
                    break;

                case Dlib.Native.ErrorType.InputVectorTypeNotSupport:
                    throw new ArgumentException($"Input {this._ElementType} is not supported.");

                default:
                    throw new ArgumentException();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                if (stdstr != IntPtr.Zero)
                {
                    Dlib.Native.string_delete(stdstr);
                }
                if (ofstream != IntPtr.Zero)
                {
                    Dlib.Native.ostringstream_delete(ofstream);
                }
            }

            return(str);
        }
Exemplo n.º 15
0
        public static void Serialize(string path, NormalizedFunction <TScalar, TFunction> function)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException();
            }

            function.ThrowIfDisposed();

            var svmKernelType     = function._Parameter.KernelType;
            var matrixElementType = function._Parameter.SampleType.ToNativeMatrixElementType();
            var templateColumns   = function._Parameter.TemplateColumns;
            var templateRows      = function._Parameter.TemplateRows;
            var functionType      = function._SvmFunctionType;

            var str = Dlib.Encoding.GetBytes(path);
            var err = NativeMethods.normalized_function_serialize(svmKernelType.ToNativeKernelType(),
                                                                  matrixElementType,
                                                                  templateRows,
                                                                  templateColumns,
                                                                  functionType,
                                                                  function.NativePtr,
                                                                  str,
                                                                  str.Length,
                                                                  out var errorMessage);

            switch (err)
            {
            case NativeMethods.ErrorType.SvmFunctionNotSupport:
                throw new ArgumentException($"{functionType} is not supported.");

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

            case NativeMethods.ErrorType.GeneralSerialization:
                throw new SerializationException(StringHelper.FromStdString(errorMessage, true));
            }
        }
Exemplo n.º 16
0
            public override void Serialize(byte[] filepath)
            {
                var ret = NativeMethods.object_detector_scan_fhog_pyramid_serialize(filepath,
                                                                                    this._PyramidType,
                                                                                    this._PyramidRate,
                                                                                    this._FeatureExtractorType,
                                                                                    this.NativePtr,
                                                                                    out var errorMessage);

                switch (ret)
                {
                case NativeMethods.ErrorType.FHogNotSupportExtractor:
                case NativeMethods.ErrorType.PyramidNotSupportRate:
                case NativeMethods.ErrorType.PyramidNotSupportType:
                    throw new NotSupportedException();

                case NativeMethods.ErrorType.GeneralSerialization:
                    throw new SerializationException(StringHelper.FromStdString(errorMessage, true));
                }
            }
Exemplo n.º 17
0
        public static ShapePredictor Deserialize(byte[] item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            var ret = NativeMethods.deserialize_shape_predictor2(item,
                                                                 item.Length,
                                                                 out var predictor,
                                                                 out var errorMessage);

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

            return(new ShapePredictor(predictor));
        }
Exemplo n.º 18
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.");
            }
        }
Exemplo n.º 19
0
        public static ShapePredictor Deserialize(ProxyDeserialize deserialize)
        {
            if (deserialize == null)
            {
                throw new ArgumentNullException(nameof(deserialize));
            }

            deserialize.ThrowIfDisposed();

            var ret = NativeMethods.deserialize_shape_predictor_proxy(deserialize.NativePtr,
                                                                      out var predictor,
                                                                      out var errorMessage);

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

            return(new ShapePredictor(predictor));
        }
Exemplo n.º 20
0
        public static void Serialize(DecisionFunction <TScalar, TKernel> function, string path)
        {
            if (function == null)
            {
                throw new ArgumentNullException(nameof(function));
            }
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException();
            }

            function.ThrowIfDisposed();

            var str = Dlib.Encoding.GetBytes(path);
            var ret = NativeMethods.serialize_decision_function(function._Parameter.KernelType.ToNativeKernelType(),
                                                                function._Parameter.SampleType.ToNativeMatrixElementType(),
                                                                function._Parameter.TemplateRows,
                                                                function._Parameter.TemplateColumns,
                                                                function.NativePtr,
                                                                str,
                                                                str.Length,
                                                                out var errorMessage);

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

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

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

            case NativeMethods.ErrorType.SvmKernelNotSupport:
                throw new ArgumentException($"{function._Parameter.KernelType} is not supported.");
            }
        }
Exemplo n.º 21
0
 public override string ToString()
 {
     this.ThrowIfDisposed();
     return(StringHelper.FromStdString(this.NativePtr));
 }
Exemplo n.º 22
0
        public override string ToString()
        {
            var    ofstream = IntPtr.Zero;
            var    stdstr   = IntPtr.Zero;
            string str      = null;

            try
            {
                ofstream = Dlib.Native.ostringstream_new();

                Dlib.Native.ErrorType ret;
                switch (this._ElementType)
                {
                case Dlib.Native.ElementType.OpTrans:
                    ret = Native.matrix_op_op_trans_operator_left_shift(this._MatrixElementType,
                                                                        this.NativePtr,
                                                                        this.TemplateRows,
                                                                        this.TemplateColumns,
                                                                        ofstream);
                    break;

                case Dlib.Native.ElementType.OpStdVectToMat:
                    ret = Native.matrix_op_op_std_vect_to_mat_operator_left_shift(this._MatrixElementType,
                                                                                  this.NativePtr,
                                                                                  this.TemplateRows,
                                                                                  this.TemplateColumns,
                                                                                  ofstream);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                switch (ret)
                {
                case Dlib.Native.ErrorType.OK:
                    stdstr = Dlib.Native.ostringstream_str(ofstream);
                    str    = StringHelper.FromStdString(stdstr);
                    break;

                case Dlib.Native.ErrorType.InputElementTypeNotSupport:
                    throw new ArgumentException($"Input {this._ElementType} is not supported.");

                default:
                    throw new ArgumentException();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                if (stdstr != IntPtr.Zero)
                {
                    Dlib.Native.string_delete(stdstr);
                }
                if (ofstream != IntPtr.Zero)
                {
                    Dlib.Native.ostringstream_delete(ofstream);
                }
            }

            return(str);
        }
Exemplo n.º 23
0
 public static string GetNativeDnnVersion()
 {
     return(StringHelper.FromStdString(NativeMethods.dnn_get_version(), true));
 }
Exemplo n.º 24
0
 private void NativeCallback(IntPtr file)
 {
     this._Callback.Invoke(StringHelper.FromStdString(file));
 }