使用tesseract库的Native Client插件

时间:2013-06-03 22:03:17

标签: c++ google-chrome-extension tesseract google-nativeclient

我正在尝试为Chrome构建一个包含TesseractOCR库的Native Client插件。我使用glibc NaCL工具链为linux x86-64编译了leptonica和tesseract(以及相应的依赖项)。所有动态链接的库都包含在nmf中。

当我实际尝试在chrome中加载插件时,我在javascript控制台上收到以下错误:

updateStatus() hello_tutorial.html:56
pageDidLoad if true hello_tutorial.html:39
NativeClient: NaCl module load failed: Nexe crashed during startup hello_tutorial.html:1

我不确定我是否编译错误或者我试图做一些NaCL / Chrome无法处理的事情。非常感谢任何建议和帮助!

我确实得到了加载...以显示等等。

我已在Chrome标记中禁用了nacl_gdb调试程序(由this postthis post建议)。我可以编译并运行NaCL教程中的示例。

这是加载插件的HTML文件:

<!DOCTYPE html>
<html>

  <!--
  Copyright (c) 2012 The Native Client Authors. All rights reserved.
  Use of this source code is governed by a BSD-style license that can be
  found in the LICENSE file.
  -->

<head>

  <title>hello_tutorial</title>

  <script type="text/javascript">
    HelloTutorialModule = null;  // Global application object.
    statusText = 'NO-STATUS';

    // Indicate load success.
    function moduleDidLoad() {
      HelloTutorialModule = document.getElementById('hello_tutorial');
      updateStatus('SUCCESS');
        console.log("moduleDidLoad()");
    }

    // The 'message' event handler.  This handler is fired when the NaCl module
    // posts a message to the browser by calling PPB_Messaging.PostMessage()
    // (in C) or pp::Instance.PostMessage() (in C++).  This implementation
    // simply displays the content of the message in an alert panel.
    function handleMessage(message_event) {
      alert(message_event.data);
    }

    // If the page loads before the Native Client module loads, then set the
    // status message indicating that the module is still loading.  Otherwise,
    // do not change the status message.
    function pageDidLoad() {
      if (HelloTutorialModule == null) {
        updateStatus('LOADING...');
          console.log("pageDidLoad if true");
      } else {
        // It's possible that the Native Client module onload event fired
        // before the page's onload event.  In this case, the status message
        // will reflect 'SUCCESS', but won't be displayed.  This call will
        // display the current message.
        updateStatus();
          console.log("pageDidLoad if false");
      }
    }

    // Set the global status message.  If the element with id 'statusField'
    // exists, then set its HTML to the status message as well.
    // opt_message The message test.  If this is null or undefined, then
    // attempt to set the element with id 'statusField' to the value of
    // |statusText|.
    function updateStatus(opt_message) {
        console.log("updateStatus()");
      if (opt_message)
        statusText = opt_message;
      var statusField = document.getElementById('status_field');
      if (statusField) {
          if(statusField.innerHTML.indexOf("..." >= 0)){
              statusField.innerHTML = statusText.replace(".", "");
          }
          else{
              if(statusField.innerHTML.indexOf(".." >= 0)){
                  statusField.innerHTML = statusText + ".";
              }
              else{
                  if(statusField.innerHTML.indexOf("." >= 0)){
                      statusField.innerHTML = statusText + ".";
                  }
              }
          }

        statusField.innerHTML = statusText;
      }
    }
  </script>
</head>
<body onload="pageDidLoad()">

<h1>Native Client Module HelloTutorial</h1>
<p>
  <!-- Load the published .nexe.  This includes the 'nacl' attribute which
  shows how to load multi-architecture modules.  Each entry in the "nexes"
  object in the .nmf manifest file is a key-value pair: the key is the
  instruction set architecture ('x86-32', 'x86-64', etc.); the value is a URL
  for the desired NaCl module.
  To load the debug versions of your .nexes, set the 'nacl' attribute to the
  _dbg.nmf version of the manifest file.

  Note: Since this NaCl module does not use any real-estate in the browser,
  it's width and height are set to 0.

  Note: The <EMBED> element is wrapped inside a <DIV>, which has both a 'load'
  and a 'message' event listener attached.  This wrapping method is used
  instead of attaching the event listeners directly to the <EMBED> element to
  ensure that the listeners are active before the NaCl module 'load' event
  fires.  This also allows you to use PPB_Messaging.PostMessage() (in C) or
  pp::Instance.PostMessage() (in C++) from within the initialization code in
  your NaCl module.
  -->
  <div id="listener">
    <script type="text/javascript">
      var listener = document.getElementById('listener');
      listener.addEventListener('load', moduleDidLoad, true);
      listener.addEventListener('message', handleMessage, true);
    </script>

    <embed name="nacl_module"
       id="hello_tutorial"
       width=0 height=0
       src="hello_tutorial.nmf"
       type="application/x-nacl" />
  </div>
</p>

<h2>Status</h2>
<div id="status_field">NO-STATUS</div>
</body>
</html>

这是我正在尝试为插件编译的C ++文件:

#include <cstdio>
#include <string>
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var.h"
#include <baseapi.h>
#include <allheaders.h>


/// The Instance class.  One of these exists for each instance of your NaCl
/// module on the web page.  The browser will ask the Module object to create
/// a new Instance for each occurence of the <embed> tag that has these
/// attributes:
///     type="application/x-nacl"
///     src="hello_tutorial.nmf"
/// To communicate with the browser, you must override HandleMessage() for
/// receiving messages from the browser, and use PostMessage() to send messages
/// back to the browser.  Note that this interface is asynchronous.
class HelloTutorialInstance : public pp::Instance {
 public:
  /// The constructor creates the plugin-side instance.
  /// @param[in] instance the handle to the browser-side plugin instance.
  explicit HelloTutorialInstance(PP_Instance instance) : pp::Instance(instance)
  {}
  virtual ~HelloTutorialInstance() {}

  /// Handler for messages coming in from the browser via postMessage().  The
  /// @a var_message can contain anything: a JSON string; a string that encodes
  /// method names and arguments; etc.  For example, you could use
  /// JSON.stringify in the browser to create a message that contains a method
  /// name and some parameters, something like this:
  ///   var json_message = JSON.stringify({ "myMethod" : "3.14159" });
  ///   nacl_module.postMessage(json_message);
  /// On receipt of this message in @a var_message, you could parse the JSON to
  /// retrieve the method name, match it to a function call, and then call it
  /// with the parameter.
  /// @param[in] var_message The message posted by the browser.
  virtual void HandleMessage(const pp::Var& var_message) {
    // TODO(sdk_user): 1. Make this function handle the incoming message.
  }
};

/// The Module class.  The browser calls the CreateInstance() method to create
/// an instance of your NaCl module on the web page.  The browser creates a new
/// instance for each <embed> tag with type="application/x-nacl".
class HelloTutorialModule : public pp::Module {
 public:
  HelloTutorialModule() : pp::Module() {}
  virtual ~HelloTutorialModule() {}

  /// Create and return a HelloTutorialInstance object.
  /// @param[in] instance The browser-side instance.
  /// @return the plugin-side instance.
  virtual pp::Instance* CreateInstance(PP_Instance instance) {
    return new HelloTutorialInstance(instance);
  }
};

namespace pp {
/// Factory function called by the browser when the module is first loaded.
/// The browser keeps a singleton of this module.  It calls the
/// CreateInstance() method on the object you return to make instances.  There
/// is one instance per <embed> tag on the page.  This is the main binding
/// point for your NaCl module with the browser.
Module* CreateModule() {
  return new HelloTutorialModule();
}
}  // namespace pp

它的编译器命令:

/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-g++ hello_tutorial.cc -o hello_tutorial_x86_64.nexe -I/var/www/native_client/projects/leptonica_src/src -I/var/www/native_client/projects/tesseract_build/include/tesseract -I/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/x86_64-nacl/usr/include/glib-2.0 -I/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/x86_64-nacl/usr/include/webp -I/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/x86_64-nacl/usr/include/libpng12 -I/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/x86_64-nacl/usr/include -I/var/www/native_client/projects/leptonica_src/src -I/var/www/native_client/nacl_sdk/pepper_26/include -L/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/x86_64-nacl/usr/lib -L/var/www/native_client/projects/leptonica_src/src/.libs -L/var/www/native_client/projects/tesseract_build/lib -lppapi_cpp -lppapi -pthread -lglib-2.0 -lstdc++ -ldl -lz -lwebp -lpng -ltesseract -llept -lnosys -lm

我用来编译leptonica的命令:

> ./configure --host=nacl SOURCES="-I/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/x86_64-nacl/usr/include" AR="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/x86_64-nacl/bin/ar" CC="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-g++" CFLAGS=" -g -O2 -fPIC -I/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/x86_64-nacl/usr/include/webp -I/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/x86_64-nacl/usr/include" CPP="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-g++ -E" CXX="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-g++" CXXCPP="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-g++ -E" CXXFLAGS="-g -O2 -fPIC " LD="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-ld -L /var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/x86_64-nacl/usr/lib -lplatform -ldl" NM="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-nm -B" OBJDUMP="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-objdump" RANLIB="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-ranlib" ac_ct_CXX="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-g++"
> MAKE

编译Tesseract

> ./configure --host=nacl --prefix=/var/www/native_client/projects/tesseract_build  SOURCES="-I/var/www/native_client/projects/leptonica_src_compiled_x86-64/src" AR="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/x86_64-nacl/bin/ar" CC="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-g++" CFLAGS=" -g -O2 -fPIC -I/var/www/native_client/projects/leptonica_src_compiled_x86-64/src" CPP="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-g++ -E" CXX="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-g++" CXXCPP="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-g++ -E" CXXFLAGS="-g -O2 -fPIC -I/var/www/native_client/projects/leptonica_src_compiled_x86-64/src" LD="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-ld -L/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/x86_64-nacl/usr/lib -L/var/www/native_client/projects/leptonica_src_compiled_x86-64/src/.libs -lplatform -ldl" LIBLEPT_HEADERSDIR="/var/www/native_client/projects/leptonica_src_compiled_x86-64/src" LIBS="-L/var/www/native_client/projects/leptonica_src/src/.libs -L/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/x86_64-nacl/usr/lib -llept -lpng -lwebp -lz" NM="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-nm -B" OBJDUMP="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-objdump" RANLIB="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-ranlib" ac_ct_CXX="/var/www/native_client/nacl_sdk/pepper_26/toolchain/linux_x86_glibc/bin/x86_64-nacl-g++"
> MAKE

1 个答案:

答案 0 :(得分:1)

尝试调试可能最简单。见https://developers.google.com/native-client/devguide/devcycle/debugging#gdb

上面的链接提供了很多细节,但以下是基础知识:

  1. 在命令行上使用--enable-nacl-debug运行chrome,然后导航到您的页面。 NEXE将不会加载,而是等待调试器连接。
  2. 启动本机客户端调试器:$NACL_SDK_ROOT/toolchain/linux_x86_newlib/bin/x86_64-nacl-gdb
  3. 在gdb中,键入:nacl-manifest hello_tutorial.nmf然后target remote :4014(上面提到的链接提及正在运行nacl-irt ...,如果崩溃不在您的代码中,它会提供其他信息,但通常不会必要)
  4. 此时,您应该连接到暂停的NEXE,输入c继续
  5. NEXE应该像以前一样崩溃。键入bt以打印堆栈回溯。