Text

When multiple threads are executed concurrently, the recognition results are often empty

  

package com.myscript.iink.getstarted

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.myscript.certificate.MyCertificate
import com.myscript.iink.Engine
import com.myscript.iink.MimeType
import com.myscript.iink.PointerEvent
import com.myscript.iink.PointerEventType
import org.json.JSONArray
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit

class MainActivity : AppCompatActivity() {

    private lateinit var engine: Engine

    private val maxExecutorThread = Runtime.getRuntime().availableProcessors()

    private val executor =
        ThreadPoolExecutor(
            maxExecutorThread,
            maxExecutorThread,
            60,
            TimeUnit.SECONDS,
            LinkedBlockingQueue()
        )

    private val data =
        "[{\"x\":13.25879970932007,\"y\":134.1424748840332,\"t\":1744461612000,\"event\":\"DOWN\"},{\"x\":13.26155680847168,\"y\":134.1707522277832,\"t\":1744461612011,\"event\":\"MOVE\"},{\"x\":13.2876496925354,\"y\":134.18484439086913,\"t\":1744461612021,\"event\":\"MOVE\"},{\"x\":13.297505195617676,\"y\":134.2068779296875,\"t\":1744461612030,\"event\":\"MOVE\"},{\"x\":13.285398376464844,\"y\":134.21978411865234,\"t\":1744461612040,\"event\":\"MOVE\"},{\"x\":13.325023864746093,\"y\":134.29145416259766,\"t\":1744461612050,\"event\":\"MOVE\"},{\"x\":13.342877426147462,\"y\":134.38541354370116,\"t\":1744461612060,\"event\":\"MOVE\"},{\"x\":13.371788452148438,\"y\":134.492255859375,\"t\":1744461612070,\"event\":\"MOVE\"},{\"x\":13.400882606506348,\"y\":134.58918017578125,\"t\":1744461612081,\"event\":\"MOVE\"},{\"x\":13.449402900695802,\"y\":134.92729907226564,\"t\":1744461612092,\"event\":\"MOVE\"},{\"x\":13.47807702255249,\"y\":135.3160892944336,\"t\":1744461612100,\"event\":\"MOVE\"},{\"x\":13.505754112243652,\"y\":135.69328720092773,\"t\":1744461612110,\"event\":\"MOVE\"},{\"x\":13.53607202911377,\"y\":135.93385391235353,\"t\":1744461612122,\"event\":\"MOVE\"},{\"x\":13.611846473693848,\"y\":136.517236907959,\"t\":1744461612134,\"event\":\"MOVE\"},{\"x\":13.660980102539062,\"y\":136.99002200317383,\"t\":1744461612144,\"event\":\"MOVE\"},{\"x\":13.6849408493042,\"y\":137.41093817138673,\"t\":1744461612155,\"event\":\"MOVE\"},{\"x\":13.685629760742188,\"y\":137.6926767883301,\"t\":1744461612167,\"event\":\"MOVE\"},{\"x\":13.711461032867431,\"y\":138.09968682861327,\"t\":1744461612181,\"event\":\"MOVE\"},{\"x\":13.708743175506592,\"y\":138.19579724121093,\"t\":1744461612202,\"event\":\"MOVE\"},{\"x\":13.68304852294922,\"y\":138.2526774902344,\"t\":1744461612216,\"event\":\"MOVE\"},{\"x\":13.626316520690919,\"y\":138.31592944335938,\"t\":1744461612225,\"event\":\"MOVE\"},{\"x\":12.999719593048097,\"y\":138.5468455810547,\"t\":1744461612234,\"event\":\"UP\"},{\"x\":15.849599418640137,\"y\":133.22807720947264,\"t\":1744461613000,\"event\":\"DOWN\"},{\"x\":15.849574710845948,\"y\":133.23460006713867,\"t\":1744461613013,\"event\":\"MOVE\"},{\"x\":15.84920118713379,\"y\":133.25454071044922,\"t\":1744461613026,\"event\":\"MOVE\"},{\"x\":15.847483268737793,\"y\":133.2810274658203,\"t\":1744461613040,\"event\":\"MOVE\"},{\"x\":15.842431251525879,\"y\":133.3226760864258,\"t\":1744461613051,\"event\":\"MOVE\"},{\"x\":15.78885167312622,\"y\":133.54118600463866,\"t\":1744461613063,\"event\":\"MOVE\"},{\"x\":15.709856494903566,\"y\":133.78378747558594,\"t\":1744461613085,\"event\":\"MOVE\"},{\"x\":15.60678865814209,\"y\":134.06646789550783,\"t\":1744461613094,\"event\":\"MOVE\"},{\"x\":15.508633312225342,\"y\":134.38289044189455,\"t\":1744461613106,\"event\":\"MOVE\"},{\"x\":15.349231704711915,\"y\":134.8758603515625,\"t\":1744461613124,\"event\":\"MOVE\"},{\"x\":15.25066068649292,\"y\":135.10473010253907,\"t\":1744461613134,\"event\":\"MOVE\"},{\"x\":15.04187982559204,\"y\":135.84935906982423,\"t\":1744461613144,\"event\":\"UP\"},{\"x\":15.81912017440796,\"y\":134.40156372070314,\"t\":1744461613000,\"event\":\"DOWN\"},{\"x\":15.842335327148438,\"y\":134.3925061340332,\"t\":1744461613010,\"event\":\"MOVE\"},{\"x\":15.890934104919435,\"y\":134.3892621459961,\"t\":1744461613031,\"event\":\"MOVE\"},{\"x\":15.958110237121582,\"y\":134.38275091552734,\"t\":1744461613041,\"event\":\"MOVE\"},{\"x\":16.03749201965332,\"y\":134.36677514648437,\"t\":1744461613052,\"event\":\"MOVE\"},{\"x\":16.296859909057616,\"y\":134.30532540893554,\"t\":1744461613061,\"event\":\"MOVE\"},{\"x\":16.483043312072756,\"y\":134.26333959960937,\"t\":1744461613070,\"event\":\"MOVE\"},{\"x\":16.662137031555176,\"y\":134.2215863342285,\"t\":1744461613079,\"event\":\"MOVE\"},{\"x\":16.803331901550294,\"y\":134.185495513916,\"t\":1744461613088,\"event\":\"MOVE\"},{\"x\":17.094273445129396,\"y\":134.11117446899414,\"t\":1744461613097,\"event\":\"MOVE\"},{\"x\":17.22247201538086,\"y\":134.08185067749022,\"t\":1744461613106,\"event\":\"MOVE\"},{\"x\":17.308635360717773,\"y\":134.0565964050293,\"t\":1744461613120,\"event\":\"MOVE\"},{\"x\":17.39062163543701,\"y\":134.05357333374025,\"t\":1744461613130,\"event\":\"MOVE\"},{\"x\":17.604735019683837,\"y\":134.02235430908203,\"t\":1744461613139,\"event\":\"MOVE\"},{\"x\":17.714548084259032,\"y\":134.00986669921875,\"t\":1744461613147,\"event\":\"MOVE\"},{\"x\":17.787424449920653,\"y\":134.00547161865234,\"t\":1744461613156,\"event\":\"MOVE\"},{\"x\":17.85735186767578,\"y\":134.00658782958985,\"t\":1744461613165,\"event\":\"MOVE\"},{\"x\":17.962086753845217,\"y\":133.99962313842772,\"t\":1744461613174,\"event\":\"MOVE\"},{\"x\":18.01850772857666,\"y\":134.00392520141602,\"t\":1744461613183,\"event\":\"MOVE\"},{\"x\":18.067940757751465,\"y\":134.0059599609375,\"t\":1744461613192,\"event\":\"MOVE\"},{\"x\":18.12246795272827,\"y\":134.0114712524414,\"t\":1744461613202,\"event\":\"MOVE\"},{\"x\":18.190548099517823,\"y\":134.01895916748046,\"t\":1744461613211,\"event\":\"MOVE\"},{\"x\":18.222335403442383,\"y\":134.04439947509766,\"t\":1744461613220,\"event\":\"MOVE\"},{\"x\":18.24609994125366,\"y\":134.0579567871094,\"t\":1744461613238,\"event\":\"MOVE\"},{\"x\":18.257993110656738,\"y\":134.08318780517578,\"t\":1744461613248,\"event\":\"MOVE\"},{\"x\":18.27602834701538,\"y\":134.14498635864257,\"t\":1744461613256,\"event\":\"MOVE\"},{\"x\":18.281147220611572,\"y\":134.2125287475586,\"t\":1744461613266,\"event\":\"MOVE\"},{\"x\":18.282366622924805,\"y\":134.29591900634765,\"t\":1744461613276,\"event\":\"MOVE\"},{\"x\":18.265558055877687,\"y\":134.37835583496093,\"t\":1744461613286,\"event\":\"MOVE\"},{\"x\":18.18741893005371,\"y\":134.70141751098632,\"t\":1744461613297,\"event\":\"MOVE\"},{\"x\":18.139532318115236,\"y\":134.94500729370117,\"t\":1744461613306,\"event\":\"MOVE\"},{\"x\":18.091625358581542,\"y\":135.1490297241211,\"t\":1744461613315,\"event\":\"MOVE\"},{\"x\":18.057306232452394,\"y\":135.29197448730469,\"t\":1744461613325,\"event\":\"MOVE\"},{\"x\":17.98636434173584,\"y\":135.54707519531252,\"t\":1744461613334,\"event\":\"MOVE\"},{\"x\":17.9403090133667,\"y\":135.66632373046875,\"t\":1744461613344,\"event\":\"MOVE\"},{\"x\":17.90241452407837,\"y\":135.75950408935546,\"t\":1744461613355,\"event\":\"MOVE\"},{\"x\":17.708879825592042,\"y\":135.97128186035155,\"t\":1744461613366,\"event\":\"UP\"},{\"x\":16.443959941864016,\"y\":135.4988455810547,\"t\":1744461614000,\"event\":\"DOWN\"},{\"x\":16.464859828948974,\"y\":135.51200756835937,\"t\":1744461614019,\"event\":\"MOVE\"},{\"x\":16.497417434692384,\"y\":135.4973572998047,\"t\":1744461614051,\"event\":\"MOVE\"},{\"x\":16.520291038513182,\"y\":135.46940551757814,\"t\":1744461614061,\"event\":\"MOVE\"},{\"x\":16.540153198242187,\"y\":135.45569705200197,\"t\":1744461614070,\"event\":\"MOVE\"},{\"x\":16.60942803955078,\"y\":135.40833947753907,\"t\":1744461614084,\"event\":\"MOVE\"},{\"x\":16.690739936828614,\"y\":135.39215441894532,\"t\":1744461614093,\"event\":\"MOVE\"},{\"x\":16.755953979492187,\"y\":135.37256259155274,\"t\":1744461614102,\"event\":\"MOVE\"},{\"x\":16.826286895751952,\"y\":135.3596912841797,\"t\":1744461614111,\"event\":\"MOVE\"},{\"x\":17.002947624206545,\"y\":135.3189612121582,\"t\":1744461614120,\"event\":\"MOVE\"},{\"x\":17.083864196777345,\"y\":135.2959626159668,\"t\":1744461614137,\"event\":\"MOVE\"},{\"x\":17.20976494216919,\"y\":135.28167279052735,\"t\":1744461614146,\"event\":\"MOVE\"},{\"x\":17.307806922912597,\"y\":135.27402209472658,\"t\":1744461614155,\"event\":\"MOVE\"},{\"x\":17.436142112731932,\"y\":135.26422036743165,\"t\":1744461614165,\"event\":\"MOVE\"},{\"x\":17.484226387023927,\"y\":135.2716036376953,\"t\":1744461614182,\"event\":\"MOVE\"},{\"x\":17.56854536819458,\"y\":135.2981950378418,\"t\":1744461614191,\"event\":\"MOVE\"},{\"x\":17.609269626617433,\"y\":135.33792517089844,\"t\":1744461614200,\"event\":\"MOVE\"},{\"x\":17.666106273651124,\"y\":135.45522033691407,\"t\":1744461614209,\"event\":\"MOVE\"},{\"x\":17.63417653656006,\"y\":135.5630044555664,\"t\":1744461614219,\"event\":\"MOVE\"},{\"x\":17.608763843536376,\"y\":135.6649168395996,\"t\":1744461614229,\"event\":\"MOVE\"},{\"x\":17.549024757385254,\"y\":135.7813399658203,\"t\":1744461614240,\"event\":\"MOVE\"},{\"x\":17.36045341873169,\"y\":136.1030877685547,\"t\":1744461614252,\"event\":\"MOVE\"},{\"x\":17.21914809036255,\"y\":136.28653005981445,\"t\":1744461614263,\"event\":\"MOVE\"},{\"x\":17.0971628036499,\"y\":136.46060083007814,\"t\":1744461614271,\"event\":\"MOVE\"},{\"x\":16.657320465087892,\"y\":136.9466409301758,\"t\":1744461614280,\"event\":\"UP\"},{\"x\":16.352519302368165,\"y\":137.2666795349121,\"t\":1744461614000,\"event\":\"DOWN\"},{\"x\":16.357203609466552,\"y\":137.24772720336915,\"t\":1744461614010,\"event\":\"MOVE\"},{\"x\":16.39725203704834,\"y\":137.21643841552734,\"t\":1744461614020,\"event\":\"MOVE\"},{\"x\":16.447702445983886,\"y\":137.1845217590332,\"t\":1744461614028,\"event\":\"MOVE\"},{\"x\":16.49837522506714,\"y\":137.162406829834,\"t\":1744461614037,\"event\":\"MOVE\"},{\"x\":16.682706996917723,\"y\":137.07883053588867,\"t\":1744461614045,\"event\":\"MOVE\"},{\"x\":16.81325862121582,\"y\":136.98928948974608,\"t\":1744461614053,\"event\":\"MOVE\"},{\"x\":16.90921206665039,\"y\":136.91610791015626,\"t\":1744461614069,\"event\":\"MOVE\"},{\"x\":16.97835319519043,\"y\":136.8695177307129,\"t\":1744461614079,\"event\":\"MOVE\"},{\"x\":17.145440380096435,\"y\":136.72608462524414,\"t\":1744461614091,\"event\":\"MOVE\"},{\"x\":17.207958366394042,\"y\":136.66393725585937,\"t\":1744461614103,\"event\":\"MOVE\"},{\"x\":17.327879825592042,\"y\":136.4284748840332,\"t\":1744461614115,\"event\":\"UP\"},{\"x\":16.291559360504152,\"y\":135.8798455810547,\"t\":1744461614000,\"event\":\"DOWN\"},{\"x\":16.31111775970459,\"y\":135.8841708984375,\"t\":1744461614011,\"event\":\"MOVE\"},{\"x\":16.33479945373535,\"y\":135.92251739501953,\"t\":1744461614033,\"event\":\"MOVE\"},{\"x\":16.33452621459961,\"y\":135.97097955322266,\"t\":1744461614044,\"event\":\"MOVE\"},{\"x\":16.328786739349365,\"y\":136.0299526977539,\"t\":1744461614056,\"event\":\"MOVE\"},{\"x\":16.307200847625733,\"y\":136.23366119384767,\"t\":1744461614067,\"event\":\"MOVE\"},{\"x\":16.304132720947266,\"y\":136.40334851074218,\"t\":1744461614079,\"event\":\"MOVE\"},{\"x\":16.304528045654298,\"y\":136.542933013916,\"t\":1744461614088,\"event\":\"MOVE\"},{\"x\":16.299262378692628,\"y\":136.66563482666015,\"t\":1744461614097,\"event\":\"MOVE\"},{\"x\":16.31254209136963,\"y\":136.94418759155275,\"t\":1744461614114,\"event\":\"MOVE\"},{\"x\":16.356771949768067,\"y\":137.14425677490235,\"t\":1744461614124,\"event\":\"MOVE\"},{\"x\":16.386645126342774,\"y\":137.33983786010742,\"t\":1744461614134,\"event\":\"MOVE\"},{\"x\":16.40973964691162,\"y\":137.50583935546877,\"t\":1744461614146,\"event\":\"MOVE\"},{\"x\":16.44357479095459,\"y\":137.8380167541504,\"t\":1744461614157,\"event\":\"MOVE\"},{\"x\":16.489723136901855,\"y\":137.99024002075197,\"t\":1744461614176,\"event\":\"MOVE\"},{\"x\":16.541135696411132,\"y\":138.08924560546876,\"t\":1744461614199,\"event\":\"MOVE\"},{\"x\":16.58436270904541,\"y\":138.16047381591798,\"t\":1744461614220,\"event\":\"MOVE\"},{\"x\":16.848843658447265,\"y\":138.23938760375978,\"t\":1744461614229,\"event\":\"MOVE\"},{\"x\":17.028514377593993,\"y\":138.22705114746094,\"t\":1744461614238,\"event\":\"MOVE\"},{\"x\":17.15550517272949,\"y\":138.22316766357423,\"t\":1744461614247,\"event\":\"MOVE\"},{\"x\":17.272748016357422,\"y\":138.21307525634765,\"t\":1744461614256,\"event\":\"MOVE\"},{\"x\":17.563135814666747,\"y\":138.1799842529297,\"t\":1744461614267,\"event\":\"MOVE\"},{\"x\":17.784187728881836,\"y\":138.172775390625,\"t\":1744461614284,\"event\":\"MOVE\"},{\"x\":17.97984439086914,\"y\":138.14396319580078,\"t\":1744461614295,\"event\":\"MOVE\"},{\"x\":18.15784806060791,\"y\":138.12094134521485,\"t\":1744461614306,\"event\":\"MOVE\"},{\"x\":18.530200332641602,\"y\":138.06245654296876,\"t\":1744461614320,\"event\":\"MOVE\"},{\"x\":18.640426162719727,\"y\":138.03573724365233,\"t\":1744461614332,\"event\":\"MOVE\"},{\"x\":18.72610697937012,\"y\":138.00772732543945,\"t\":1744461614343,\"event\":\"MOVE\"},{\"x\":18.771022842407227,\"y\":137.99099578857422,\"t\":1744461614354,\"event\":\"MOVE\"},{\"x\":18.86270329284668,\"y\":137.95527703857422,\"t\":1744461614366,\"event\":\"MOVE\"},{\"x\":18.90873536682129,\"y\":137.93426669311523,\"t\":1744461614377,\"event\":\"MOVE\"},{\"x\":18.954077075958253,\"y\":137.91139599609374,\"t\":1744461614388,\"event\":\"MOVE\"},{\"x\":18.965042976379394,\"y\":137.9017803039551,\"t\":1744461614399,\"event\":\"MOVE\"},{\"x\":18.992795642852784,\"y\":137.85094619750976,\"t\":1744461614410,\"event\":\"MOVE\"},{\"x\":18.98430488204956,\"y\":137.80160037231445,\"t\":1744461614421,\"event\":\"MOVE\"},{\"x\":18.965640323638915,\"y\":137.70238549804688,\"t\":1744461614432,\"event\":\"MOVE\"},{\"x\":18.960060722351074,\"y\":137.51832696533202,\"t\":1744461614442,\"event\":\"MOVE\"},{\"x\":18.897599418640137,\"y\":136.55039767456054,\"t\":1744461614455,\"event\":\"UP\"}]"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        engine = Engine.create(MyCertificate.getBytes())

        val config = engine.configuration
        config.setStringArray(
            "recognizer.configuration-manager.search-path",
            arrayOf("zip://${application.packageCodePath}!/assets/conf")
        )
        config.setString("recognizer.lang", "zh_CN")
        config.setString("recognizer.text.configuration.name", "text")
        config.setBoolean("export.jiix.strokes", false)

        val dots = arrayListOf<PointerEvent>()

        val array = JSONArray(data)
        for (i in 0 until array.length()) {
            val x = array.getJSONObject(i).getDouble("x")
            val y = array.getJSONObject(i).getDouble("y")
            val t = array.getJSONObject(i).getLong("t")
            val event = array.getJSONObject(i).getString("event")
            dots.add(PointerEvent().apply {
                this.x = x.toFloat()
                this.y = y.toFloat()
                this.t = t
                eventType = PointerEventType.valueOf(event)
            })
        }

        for (i in 0 until 10) {
            batchRecognize(dots.toTypedArray())
        }
    }

    fun batchRecognize(dots: Array<PointerEvent>) {
        executor.execute {
            val recognizer = engine.createRecognizer(1f, 1f, "Text")

            val config = engine.configuration
            config.setStringArray(
                "recognizer.configuration-manager.search-path",
                arrayOf("zip://${application.packageCodePath}!/assets/conf")
            )
            config.setString("recognizer.lang", "zh_CN")
            config.setString("recognizer.text.configuration.name", "text")
            config.setBoolean("export.jiix.strokes", false)

            recognizer.pointerEvents(dots)
            recognizer.waitForIdle()

            val result = recognizer.getResult(MimeType.JIIX)
            Log.d("batchRecognize", result)

            recognizer.close()
        }
    }
}

 

 I have a lot of small data sets that need to be recognized, so I tried to use multithreading, 

but I found that the recognition results are often empty. Is there any problem? 

See the code for the minimum reproducible path. When the app starts, it will execute 10 times with a set of arrays, and a few random sets will have no recognition results.


My Configuration

Bundle-Version: 1.0
Bundle-Name: zh_CN
Configuration-Script:
AddResDir ../resources/

# 默认配置,标准模型 + 标准 lk
Name: text
Type: Text
Configuration-Script:
AddResource zh_CN/zh_CN-ak-cur.res
AddResource zh_CN/zh_CN-lk-text.lite.res
SetHalfWidthFullWidthScheme AUTO
EnableAlienCharacters
SetTextListSize 1
SetWordListSize 5
SetCharListSize 1

image

Hello Kongyan4696,

You are currently in direct contact with our support team.

It is not relevant to duplicate the threads, one with the team and one on the support forum.

In order to save time for everyone, we will close the topics on the forum.

Best regards,

Etienne

Thanks for your reply, the forum is public, maybe it can help others who encounter similar problems. :)

  • Support
  • Forums
  • Text
  • When multiple threads are executed concurrently, the recognition results are often empty