gpt4 book ai didi

c++ - 使用 tesseract 库的 Native Client 插件

转载 作者:行者123 更新时间:2023-11-28 07:35:06 25 4
gpt4 key购买 nike

我正在尝试为 Chrome 构建一个 Native Client 插件,其中将包含 TesseractOCR 库。我已经使用适用于 linux x86-64 的 glibc NaCL 工具链编译了 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 无法处理的事情。非常感谢任何建议和帮助!

我确实得到了 LOADING... 来显示等等。

我已经在 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

最佳答案

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

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

  1. 在命令行上使用 --enable-nacl-debug 运行 chrome,然后导航到您的页面。 NEXE 不会加载,而是等待调试器连接。
  2. 启动 native 客户端调试器:$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 以打印堆栈回溯。

关于c++ - 使用 tesseract 库的 Native Client 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16906600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com