ããã«ã¡ã¯ï¼Mercari Advent Calendar 2020 ã®3æ¥ç®ã¯ãã¡ã«ã«ãªWebPlatformãã¼ã /Software Engineerã®@_mkazutaka ããéããã¾ããæ®æ®µã¯ã¡ã«ã«ãªã®ã¦ã§ãå¨ãã®éçºããã¦ããGoãPHPãTypeScriptãæ¸ãã¦ãã¾ãã
ã¡ã«ã«ãªã§ã¯åæã«ä¸åº¦ã¨ã³ã¸ãã¢ã®ããã®ãç¥ãMercari Hack Weekãéå¬ãã¦ãã¾ãããã®è¨äºã§ã¯ã第2åMercari Hack Weekããçè ãåãçµãã§ããRust/Wasmã使ã£ããã¼ã³ã¼ããªã¼ãã«ã¤ãã¦ç´¹ä»ãã¾ãã
ãã¡ããããã¯ã·ã§ã³ã«ã¯åºãã¦ããã®ã§ããã¾ããããé¡ãããã°ãããã¯ã·ã§ã³ã¸ã®ãªãªã¼ã¹ã許ãã¦ããããã¨æãã¾ãããçè èªèº«ãåºããªãé¸æè¢ãåã£ãã®ã§ãããå«ãã¦ç´¹ä»ãã¾ãã
(注é: ããã¤ãã®ç»åå¦çã®è©±ãåºã¦ãã¾ãããçè ã¯ç»åå¦çã®å°é家ã§ããªããã°å¤§å¦é¢ã§ç 究ãã¦ãããã§ãããã¾ãããç»åå¦çã¯ã»ã¨ãã©åãã¦ã§ãã使ã£ã¦ããææ³ãå¤ãããããã¾ããããè¦è¦ããç¹ãã£ããç³ã訳ãªãã§ãããäºæ¿ã®ã»ã©ãé¡ããã¾ãã
ã¯ããã«
ã¡ã«ã«ãªã®ã¢ããªã«ã¯ååã®ãã¼ã³ã¼ããã¹ãã£ã³ããã¨æ å ±ãèªåã§èªã¿åã£ã¦è¨è¿°ãã¦ããããã¼ã³ã¼ãåºåã¨ããæ©è½ãããã¾ããä¸æ¹ã§ã¦ã§ãã«ãã¼ã³ã¼ãåºåã®æ©è½ã¯ããã¾ãããããã¯ã¦ã§ããµã¼ãã¹ã ããã§ããªãã¨ããããã§ã¯ããã¾ãããå®éã«eBayã¯ã¦ã§ãä¸ã§åããã¼ã³ã¼ããªã¼ããéçºãã¦ãã¾ã(åè: WebAssembly at eBay: A Real-World Use Case)ã
ã¦ã§ãã§ããã¼ã³ã¼ãåºåãå®ç¾ããã«ã¯ããã¼ã³ã¼ããèªã¿åãããã®ãã¼ã³ã¼ããªã¼ããå®è£ ããªããã°ãªãã¾ãããããã¤ãæ¹æ³ãããã¾ããä¾ãã°
ã»JavaScriptã®ã©ã¤ãã©ãªã使ã
ã»Cç³»ã®è¨èªã§æ¸ããã¦ãããã®ãWasmã«ã³ã³ãã¤ã«ãã¦ä½¿ç¨ãã
ãªã©ãããã¾ããæãçæéã§å®ç¾å¯è½ãªææ³ã¯åè ã®JavaScriptãªã®ã§ããä»åã¯Wasmã使ãããã£ãã®ã§é¸æè¢ããå¤ãã¾ããå¾è ã®Wasmã¯ãµã¤ãºã大ãããªããã¡ã§ããã¦ã§ãã®å ´åã端æ«ã«ãã£ãã·ã¥ãªã©ãããªãéããã¢ã¯ã»ã¹æ¯ã«ã客ãã¾ã®ãããã¯ã¼ã¯ãªã½ã¼ã¹ãæ¶è²»ãã¦ãã¾ãããã®ããå¿ è¦ä»¥ä¸ã«ãµã¤ãºã大ãããã¦ãã¾ãå¾è ã®ææ³ãå人çã«é¿ãããã®ã§ããã¡ããé¸æè¢ããå¤ãã¾ãã
ã¨ããããã§ãWasmã§ãã¤ãªãã¹ããµã¤ãºã®å°ãããã¼ã³ã¼ããªã¼ããä½ããã¨ãç®æ¨ã«0ããéçºãã¾ããã
ãã¼ã³ã¼ããèªã¿åãã¾ã§ã®æµã
ãã¼ã³ã¼ãèªã¿åãã®æµãã3ã¤ã«ãããã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
- ç»åã®ä¸å¦ç
- ãã¼ã³ã¼ãã®æ¤åº
- ãã¼ã³ã¼ãã®èªã¿åã
æµããç»åã«ããã¨ä¸å³ã«ãªãã¾ãã
ç»åã®ä¸å¦ç
ç»åã®ä¸å¦çãã§ã¼ãºã§ã¯ãç»åã®äºå¤åãè¡ãã¾ããäºå¤åã¨ã¯ããé¾å¤ã«å¾ã£ã¦ç»åãç½ãé»ã«ãããå¦çãããã¾ãã
ä»åã¯ç»åã®åé¢åº¦ãæ大ã¨ãªããã¨ãç®æããå¤å¥åææ³(大津ã®äºå¤å)ãç¨ãã¦ç»åã®äºå¤åãè¡ãã¾ããOpenCVã¨ãã£ãã©ã¤ãã©ãªã«ã¯å¤å¥åææ³ãå®è£
ããã¦ããã®ã§ãããæ®å¿µãªããä»åã®ç®æ¨ã¨åããã®ã¯ãªãã£ãã®ã§èªåã§ä½ãã¾ãããªããé¾å¤ã®ä½æã«ã¯Redå¤ã®ã¿ãã¿ã¦ãã¾ãããã¼ã³ã¼ãã¯ã»ã¨ãã©ã®å ´åé»è²ãªã®ã§åä¸ã®ã«ã©ã¼å¤ãè¦ãã°ååã ããã¨ããèãããã§ãã
ãããå®è£
ããã³ã¼ãã¯ãã¡ãã«ãªãã¾ãã
次ã®ãã¼ã³ã¼ãæ¤åºãã§ã¼ãºã§ã¯ç»åãç½ã®ç»ç´ ã§å²ã¾ãã¦ãããã¨ãåæã¨ãªã£ã¦ããã¾ããç»åãç½ã®ç»ç´ ã§å²ãå¦çãè¡ãã¾ãã
ãããå®è£
ããã³ã¼ãã¯ãã¡ãã«ãªãã¾ãã
ãã§ã¼ãºãçµäºããã¨ç»åã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
âBefore âAfter
ãã¼ã³ã¼ãã®æ¤åº
次ã«ãã¼ã³ã¼ãæ¤åºãè¡ãã¾ãã
æ¬è¨äºã§å®è£
ããå¦çãç´¹ä»ããåã«ãJavaScriptã§ä½ããã¦ãããã¼ã³ã¼ããªã¼ãã®ä¸ã¤QuaggaJSã«ã¤ãã¦ç´¹ä»ãã¾ãã
QuaggaJSã¯ãç»åãæ ¼åç¶ã«åå²ãããã®ä¸ããè¤æ°ã®å¹³è¡ç·ããã¤ãã®ãè¦ã¤ãããã¨ã§ãã¼ã³ã¼ããæ¤åºãã¦ãã¾ã(詳ããã¯How barcode-localization works in QuaggaJS)ãã¹ãããã¨ãã¦ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
- ç»åãæ ¼åä¸ã®ã»ã«ã«åå²
- å¹³è¡ç·ãè¦ã¤ããããã®ç»åã®ã¹ã±ã«ãã³å
- ã¹ã±ã«ãã³åã«ãã£ã¦ä½æãããç·ãã©ããªã³ã°
- ã©ããªã³ã°ãããç·ã®ç¹å¾´ããããã«ã»ã«ãåé¡
- åé¡ããã»ã«ä¸ãããã¼ã³ã¼ãã°ã«ã¼ãã®é¸æ
ã©ããªã³ã°ã«ã¯Fu Changãã® "A Linear-Time Component-Labeling Algorithm Using Contour Tracing Technique" ã®ææ³ãåèã«ãã¦ãã¾ãã
æ¬è¨äºã®å®è£ ã¯QuaggaJSã®ã¹ããããåèã«ãã¦ãã¾ãããããã¤ãã¯çç¥ãã¦ãã¾ããæ¬è¨äºã§ã®å®è£ ã¹ãããã¨ãã¦ã¯ä¸è¨ã®ããã«ãªãã¾ãã
- ç»åå ¨ä½ãã¹ã±ã«ãã³åããç·ãã©ããªã³ã°
- ã©ããªã³ã°ãããç·ãåé¡
- åé¡ããç·ãããã¼ã³ã¼ãã°ã«ã¼ãã®é¸æ
ã¹ã±ã«ãã³åã¨ã©ããªã³ã°ã«ã¯QuaggaJSã¨åæ§ã« "A Linear-Time Component-Labeling Algorithm Using Contour Tracing Technique" ã®å®è£
(Code Project)ãåå®è£
ãã¦ãã¾ãã
ãããå®è£
ããã³ã¼ãã¯ãã¡ãã«ãªãã¾ãã
ã©ããªã³ã°ã®çµæã¯ä¸å³ã«ãªãã¾ããã¾ãããããã®ç·ãã³ã³ãã¼ãã³ãã¨å¼ã³ã¾ãã
次ã«ã³ã³ãã¼ãã³ãã®åé¡ããã¾ããããããã®ã³ã³ãã¼ãã³ãã®éç¹ã»è§åº¦ã»æ辺ãªã©ãæ±ãåé¡ãã¦ãã¾ã(å¦è¡çã«ã¯ãããããããæãéè¦ãªã®ã§ãããæéããªãã£ãã®ã§ç°¡æå®è£
ã§æ¸ã¾ãã¦ãã¾ã)ãåé¡å¾ãã¼ã³ã¼ãã°ã«ã¼ããæ¤åºããçµæã以ä¸ã®ããã«ãªãã¾ãã
ãããå®è£
ããã³ã¼ãã¯ãã¡ãã«ãªãã¾ãã
ãã¼ã³ã¼ãã®èªã¿åã
ãã¼ã³ã¼ãã°ã«ã¼ããèªã¿åãåã«ãç°¡åã«ãã¼ã³ã¼ãã«ã¤ãã¦èª¬æãã¾ãã
ãã¼ã³ã¼ãã¯ç½ã®ééã¨é»ã®ç·å¹
ã®çµã¿åããã§çªå·ã示ãã¾ããä¾ãã°ç½é»ã®æ¯çã3:2:1:1ã®å ´åã¯0
ã2:2:2:1ã®å ´åã¯1
ã表ãã¾ã(詳ããã¯ãã¼ã³ã¼ãã®ä»çµã¿ã¨ä½ææ¹æ³ â piyajk.com)
ç½é»ã®æ¯çã¯ãããåã§è¡¨ãã¾ããä¾ãã°0
ã表ç¾ããå ´åããããåã¯[0, 0, 0, 1, 1, 0, 1]
ã¨ãªãã¾ãããã¼ã³ã¼ããèªã¿åãã«ã¯ããã¼ã³ã¼ãã®ãããåã®æ¯çã¨ãã¼ã³ã¼ãã§å®ãããã¦ããæ°åã®æ¯çãæ¯ã¹æãããããããã®ãé¸æãã¾ãã
ãããå®è£
ããã³ã¼ãã¯ãã¡ãã«ãªãã¾ãã
ãã¼ã³ã¼ãã®ãããåãç»åããèªã¿åãããã«ç´ç·ãå¼ãã¾ããæ¬è¨äºã§ã¯æ¬ä¼¼çãªç´ç·ãå¼ãããã«ãã¬ã¼ã³ãã ã®ã¢ã«ã´ãªãºã ã使ãã¾ããå®è£ ã«ã¯ expenses/line_drawing ã使ãã¾ãããã¬ã¼ã³ãã ã®ã¢ã«ã´ãªãºã ã使ãç´ç·ãå¼ãã¨ä¸å³ã®ããã«ãªãã¾ãã
èªã¿åã£ãç»ç´ åãããã¼ã³ã¼ãèªã¿åããè¡ãã°å®äºã§ãï¼
ãã¢
å®éã«ã³ã¼ããåããããã¢ãä¸è¨ã®ããã«ãªãã¾ãããã¼ã³ã¼ããèªã¿åãããã¨ããããã¾ãã
ãªããããã¯ã·ã§ã³ã«åºããªãã£ãã®ã
ã¡ã«ã«ãªã¯ãããã¯ã·ã§ã³ã«åºãããã¨ããã°èª¿æ´ããã¦ãããè¯ãä¼æ¥ã§ã(ãã¶ã)ã
ã¨ã¯ãããä½ã£ãããã¨ããçç±ã ãã§ãããã¯ã·ã§ã³ã«åºãã®ã¯ä¼ç¤¾ã«ã¨ã£ã¦ãããã価å¤ã¯ãªãã ããã¨çè
ãå¤æãã¾ããã
å ·ä½çã«ã¯ä»¥ä¸ã®ç¹ã§ãã
ã»éè¦ãããããªããããããå°ãªã
ã»é·ãã¡ã³ããã³ã¹ããã¦ããQuaggaJSãZXingã¨ãã£ããªã¼ãã³ã½ã¼ã¹ã©ã¤ãã©ãªããã§ã«ãã
ã»ã¡ã³ããã³ã¹ã³ã¹ãã»å¦ç¿ã³ã¹ããé«ããRust/Wasmã®ç¥èã®ã¿ãªããããã¼ã³ã¼ãã«å¯¾ããç¥èãå¿
è¦
ä¸è¨ã®çç±ããå®è£ ãã¦ã¡ã³ããã³ã¹ãã¦ããã«ã¯ã³ã¹ãããã©ã¼ãã³ã¹ãæªãã¨æããã®ã§ããã°è¨äºã¨ããå½¢ã§è½ã¡çããããã¨ã«ãã¾ããã
ã¨ã¯ãããOSSã§å ¬éãã¦ããã®ã§èå³ãããæ¹ã¯ãã²ä½¿ã£ã¦ã¿ã¦ãã ããã
ä½ã£ã¦ã¿ã¦ã®ææ³
ããã¾ã§èªãã§ããã ããããã¨ããããã¾ããããã¼ã³ã¼ããªã¼ãã¨ãã¦ã®å½¢ãæçµçã«ä½ãã¦è¯ãã£ãã§ãã
æ®æ®µã®å®è£ 以ä¸ã«è¦å´ããã®ã¯ã©ã®è«æãé¸ã¶ãã§ãããè¨äºä¸ã§ã¯1ã¤ããè«æã«ã¤ãã¦è¿°ã¹ã¦ããªãã®ã§ãããå®éã«ã¯ãã¾ãã¾ãªè«æãèªã¿ã¾ãããå®è£ ãé²ãã¦ãããã¡ã«é度ãé ãäºã«æ°ã¥ãã¦å¥ã®ææ³ã«æ¸ãç´ããããã¾ããã
æ¬å½ã¯ããå°ãå°ãããµã¤ãºã§å®ç¾ã§ããã¨æã£ã¦ããã®ã§ãã(10KBããã)ãèãéããã¨30KBã»ã©ã«ãªã£ã¦ãã¾ãããã¡ã¢ãªã¢ãã±ã¼ã¿å¨ãã®è¨å®ãå¤ãããªã©ããã°å°ãããªãæ°ããã¾ãã
ãã¨ã¯JavascriptããWasmã«ç»åãã¡ã¤ã«ã渡ãéã®ãªã¼ãã¼ãããã¯ããã®ãã¨ãQuaggaã¨æ¯è¼ãã¦ã精度ã»é度ã¯ã©ãããããã¯èå¯ãããã§ãããæéãããã°è¿½è¨ãã¾ãã
ä»åã®å 容ã¯ããããHack Weekã¨ããæéããªããã°ããããææ¦ãããã¨ã¯ãªãã£ãã§ããã¡ã«ã«ãªã®è¦æ¨¡ã®ä¼ç¤¾ã§ã¼ãç¥èã§ç»åå¦çã«ææ¦ã§ããæ©ä¼ã¯ã»ã¨ãã©ãªãã®ã§é常ã«ããæ©ä¼ãããã ãã¾ãããä»åãçè ã¯ä¸äººã§å®è£ ã«æã¿ã¾ããããHack Weekã¯ãããã¯ãããã¼ã¸ã£ã¨ã¿ãã°çµãã§åå ãå¯è½ã§ãããªã«ããWasmã¨ãRustã¨ãã§èªç±ã«éçºã§ããã®ã§æé«ã§ããï¼
ãããã«
ä»åã®è¨äºãèªãã§é ããçæ§ã®ãå½¹ã«ç«ã¦ã°å¹¸ãã§ãã
ã¡ã«ã«ãªã§ã¯ããã·ã§ã³ã»ããªã¥ã¼ã«å ±æã§ããã½ããã¦ã§ã¢ã¨ã³ã¸ãã¢ãåéãã¦ãã¾ããHack Weekã¨ãã£ãã¨ã³ã¸ãã¢ãèªç±ã«éçºããªã©ãªã©ã§ããæéãããã®ã§æ¥½ããåãã¾ãï¼
ææ¥ã®Mercari Advent Calendar 2020 å·çæ å½ã¯ã Engineering Office ã® Rafael ããã§ããå¼ãç¶ãã楽ãã¿ãã ãããæå¾ã¾ã§ãèªã¿é ããããã¨ããããã¾ããã