Phreak87 / LeptonicaSharp

Full featured wrapper for leptonica 1.77.0
Other
8 stars 5 forks source link

recogtrain #62

Closed teemiik closed 5 years ago

teemiik commented 5 years ago

Hi. I found 2 errors, maybe it will be useful. They belong to recogtran.

First. In function recogTrainLabeled() you must pass bytes, not a string, , otherwise the problem starts with the encoding.

public static int recogTrainLabeled(
                        L_Recog recog,
                        Pix pixs,
                        Box box = null,
                        byte[] text = null,
                        Enumerations.DebugOnOff debug = DebugOnOff.DebugOn)
        {
            if (recog == null) {throw new ArgumentNullException  ("recog cannot be Nothing");}

            if (pixs == null) {throw new ArgumentNullException  ("pixs cannot be Nothing");}

            IntPtr boxPtr = IntPtr.Zero;    if (box != null) {boxPtr = box.Pointer;}
            int _Result = Natives.recogTrainLabeled(recog.Pointer, pixs.Pointer, boxPtr,   text,  (int) debug);
            return _Result;
        }

internal static extern int recogTrainLabeled ( IntPtr recog, IntPtr pixs, IntPtr box, byte[] text, int debug);

And then pass a parameter with the encoding CP866 _All.recogTrainLabeled(recog, pix1, box, Encoding.GetEncoding("CP866").GetBytes(arrayRow[0]), 0);

Second. In function recogTrainingFinished() you must pass an array of pointers.

public static int recogTrainingFinished(
                        L_Recog precog,
                        int modifyflag,
                        int minsize,
                        Single minfract)
        {
            if (precog == null) {throw new ArgumentNullException  ("precog cannot be Nothing");}

            IntPtr[] precogPtr = new IntPtr[1];
            precogPtr[0] = precog.Pointer;

            int _Result = Natives.recogTrainingFinished(precogPtr,   modifyflag,   minsize,   minfract);
            return _Result;
        }

internal static extern int recogTrainingFinished ( IntPtr[] precog, int modifyflag, int minsize, Single minfract);

and then the function ends successfully.... Hope this useful information)))))

Phreak87 commented 5 years ago

Hi, this ist very useful. I will check the next few days and provide a update.

Phreak87 commented 5 years ago

Hi, can you please provide an example code how you use this functions? the string -> Byte() could be a problem on all String parameters (char*) if theres a problem with the encoding. the trainfinished looks more like a ByRef-Parameter but is handled as a in Parameter.

teemiik commented 5 years ago

Hi. Example:

static void recog_train(string folder)
        {
            String pathBox = folder + "\\" + "test.box";
            String pathTif = "D:\\Neural\\data\\split";       

            List<string> rows_text = ReadFile(pathBox);
            int row_count = rows_text.Count();

            //L_Recog recog = _All.recogRead("C:\\Users\\tanat\\Desktop\\ds\\train_with_size\\train9000.pa");
            L_Recog recog = _All.recogCreate(5, 40, 5, 250, 1);
            int iter = 1;
            foreach (string rowText in rows_text)
            {
                String[] arrayRow = rowText.Split(' ');

                if (arrayRow[0].Equals(""))
                {
                    String[] newarray = new String[6];
                    newarray[0] = " ";
                    newarray[1] = arrayRow[2];
                    newarray[2] = arrayRow[3];
                    newarray[3] = arrayRow[4];
                    newarray[4] = arrayRow[5];
                    newarray[5] = arrayRow[6];

                    arrayRow = newarray;
                }

                if (arrayRow.Length < 5) continue;

                Console.WriteLine(arrayRow);

                //lept.PIX pix = lept.pixReadTiff(pathTif, Integer.parseInt(arrayRow[5]));
                int i = Int32.Parse(arrayRow[5]) + 1;
                String pathPix = pathTif + "\\" + i + "-0.jpg";
                Pix pix = new Pix(pathPix);

                int hpix = (int)pix.h;
                int wpix = (int)pix.w;

                int x = Int32.Parse(arrayRow[1]);
                int y = hpix - Int32.Parse(arrayRow[4]);
                int w = Int32.Parse(arrayRow[3]) - Int32.Parse(arrayRow[1]);
                int h = Int32.Parse(arrayRow[4]) - Int32.Parse(arrayRow[2]);

                Box box = new Box(x, y, w, h);

                //x = boxH.x();
                //y = boxH.y();
                //w = boxW.w();
                //h = boxH.h();

                Pix pix1 = _All.pixConvertTo1(pix, 250);

                /*Box box_ret = new Box(0, 0, 0, 0);
                Pix pix_crop = _All.pixClipRectangle(pix, box, out box_ret);

                _All.pixWriteJpeg("C:\\Users\\tanat\\Desktop\\ds\\crop_character\\test_crop" + iter +  ".jpg", pix_crop, 40, 0);*/

                int train = _All.recogTrainLabeled(recog, pix1, box, Encoding.GetEncoding("CP866").GetBytes(arrayRow[0]), 0);

                _All.boxDestroy(ref box);
                //lept.boxDestroy(box_ret);

                _All.pixDestroy(ref pix);
                _All.pixDestroy(ref pix1);
                //lept.pixDestroy(pix_crop);

                if (iter % 1000 == 0)
                {
                    int res = _All.recogTrainingFinished(recog, 1, -1, -1.0f);

                    Console.WriteLine("Save to file");
                    _All.recogWrite("C:\\Users\\tanat\\Desktop\\ds\\train_with_size\\train" + iter + ".pa", recog);
                }

                iter++;

                Console.WriteLine("Iteration " + iter + " / " + row_count);
            }

            Console.WriteLine("Leaning finish");

        }

And run model. Example:

static void recog_run()
        {
            //C:\Users\tanat\Desktop\ds\test_lept\test31.jpg

            //C:\Users\tanat\Desktop\ds\test_lept\1-0.jpg

            Pix pix = new Pix("C:\\Users\\tanat\\Desktop\\ds\\test_lept\\1-0.jpg");

            /*lept.PIXA lepta_pix_read = lept.pixaRead("C:\\Users\\tanat\\Desktop\\ds\\train.pa");
            lept.L_RECOG recog = lept.recogCreateFromPixa(lepta_pix_read, 0, 40, 5, 128, 1);*/

            L_Recog recog = _All.recogRead("C:\\Users\\tanat\\Desktop\\ds\\train.pa");

            Boxa pboxa = null;
            Pixa ppixa = null;
            Pix ppixdb = null;

            int res = _All.recogIdentifyMultiple(recog, pix, 0, 1, out pboxa, out ppixa, out ppixdb, 0);

            int countPixa = _All.pixaGetCount(ppixa);
            for (int i = 0; i < countPixa; i++)
            {
                Pix pix_res = _All.pixaGetPix(ppixa, i, 1);

                _All.pixWriteJpeg("C:\\Users\\tanat\\Desktop\\ds\\test" + i + ".jpg", pix_res, 100, 0);
            }

            Boxaa baa = null;
            Numaa naa = null;

            Sarray sa = _All.recogExtractNumbers(recog, pboxa, 0.5f, -1, out baa, out naa);

            string[] arr_sa = sa.array;

            int countString = arr_sa.Length;

            for (int i = 0; i < countString; i++)
            {
                byte[] smthIn1163 = Encoding.Default.GetBytes(arr_sa[i]);
                string out_str = new string(Encoding.GetEncoding(866).GetChars(smthIn1163));
            }

            //BytePointer out = lept.sarrayGetString(sa, 0, 1);

            //BytePointer byteP = new BytePointer();

            //lept.recogGetClassString(recog, 3, byteP);

            Pix pixdb = null;

            Pixa pixa = _All.showExtractNumbers(pix, sa, baa, naa, out pixdb);

            countPixa = _All.pixaGetCount(pixa);
            for (int i = 0; i < countPixa; i++)
            {
                Pix pix_res = _All.pixaGetPix(pixa, i, 1);

                _All.pixWriteJpeg("C:\\Users\\tanat\\Desktop\\ds\\test" + i + ".jpg", pix_res, 100, 0);
            }
        }
teemiik commented 5 years ago

Encoding problems start when you use Cyrillic.

Phreak87 commented 5 years ago

That's great code. Thank you. i will see what I can do. (Are you German speaking?)

Am Mi., 26. Dez. 2018, 10:02 hat teemiik notifications@github.com geschrieben:

Encoding problems start when you use Cyrillic.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Phreak87/LeptonicaSharp/issues/62#issuecomment-449933805, or mute the thread https://github.com/notifications/unsubscribe-auth/AHvBIN_2k6ihZDv0rC8BIT5vdmSGZKiMks5u8zsngaJpZM4Zf-mK .

Phreak87 commented 5 years ago

Updated trainingFinished to ByRef. additionally all recog-functions are available via the instanciated recog-class. please check if it works for your code too.