opencv-rust icon indicating copy to clipboard operation
opencv-rust copied to clipboard

OCR STATUS_ACCESS_VIOLATION

Open Amourlive opened this issue 1 year ago • 8 comments

  1. Operating system: Windows 11
  2. The way you installed OpenCV:
.\vcpkg install llvm opencv4[contrib,nonfree]
  1. OpenCV version
.\vcpkg list
atl:x64-windows                                   0                   A stub package that ensures Visual Studio has Ac...
bzip2:x64-windows                                 1.0.8#5             bzip2 is a freely available, patent free, high-q...
bzip2[tool]:x64-windows                                               Builds bzip2 executable
curl:x64-windows                                  8.4.0               A library for transferring data with URLs
curl[non-http]:x64-windows                                            Enables protocols beyond HTTP/HTTPS/HTTP2
curl[schannel]:x64-windows                                            SSL support (Secure Channel)
curl[ssl]:x64-windows                                                 Default SSL backend
curl[sspi]:x64-windows                                                SSPI support
expat:x64-windows                                 2.5.0#3             XML parser library written in C
flatbuffers:x64-windows                           23.5.26             Memory Efficient Serialization Library
giflib:x64-windows                                5.2.1#3             A library for reading and writing gif images.
hdf5:x64-windows                                  1.14.2              HDF5 is a data model, library, and file format f...
hdf5[szip]:x64-windows                                                Build with szip
hdf5[zlib]:x64-windows                                                Build with zlib
hwloc:x64-windows                                 2.9.3               Portable Hardware Locality (hwloc)
leptonica:x64-windows                             1.83.1              An open source library containing software that ...
libarchive:x64-windows                            3.6.2               Library for reading and writing streaming archiveslibarchive[bzip2]:x64-windows                                         BZip2 support
libarchive[crypto]:x64-windows                                        Support for cryptographic features
libarchive[libxml2]:x64-windows                                       Libxml2 support
libarchive[lz4]:x64-windows                                           Lz4 support
libarchive[lzma]:x64-windows                                          Lzma support
libarchive[zstd]:x64-windows                                          Zstd support
libffi:x64-windows                                3.4.4#6             Portable, high level programming interface to va...
libiconv:x64-windows                              1.17#1              GNU Unicode text conversion
libjpeg-turbo:x64-windows                         3.0.1               libjpeg-turbo is a JPEG image codec that uses SI...
liblzma:x64-windows                               5.4.4               Compression library with an API similar to that ...
libpng:x64-windows                                1.6.39#1            libpng is a library implementing an interface fo...
libwebp:x64-windows                               1.3.2               WebP codec: library to encode and decode images ...
libwebp[libwebpmux]:x64-windows                                       Build the libwebpmux library
libwebp[nearlossless]:x64-windows                                     Enable near-lossless encoding
libwebp[simd]:x64-windows                                             Enable any SIMD optimization.
libwebp[unicode]:x64-windows                                          Build Unicode executables. (Adds definition UNIC...
libxml2:x64-windows                               2.11.5#1            Libxml2 is the XML C parser and toolkit develope...
libxml2[iconv]:x64-windows                                            Add ICONV support
libxml2[lzma]:x64-windows                                             Use LZMA
libxml2[zlib]:x64-windows                                             Use ZLib
llvm:x64-windows                                  17.0.2#1            The LLVM Compiler Infrastructure.
llvm[clang]:x64-windows                                               Include C Language Family Front-end.
llvm[default-targets]:x64-windows                                     Build with platform-specific default targets.
llvm[enable-bindings]:x64-windows                                     Build bindings.
llvm[enable-terminfo]:x64-windows                                     Use terminfo database if available.
llvm[enable-zlib]:x64-windows                                         Build with ZLib.
llvm[enable-zstd]:x64-windows                                         Build with zstd.
llvm[lld]:x64-windows                                                 Include LLVM linker.
llvm[target-x86]:x64-windows                                          Build with X86 backend.
llvm[tools]:x64-windows                                               Build LLVM tools.
lz4:x64-windows                                   1.9.4#1             Lossless compression algorithm, providing compre...
onnx:x64-windows                                  1.14.0              Open standard for machine learning interoperability
opencl:x64-windows                                v2023.02.06#2       C/C++ headers and ICD loader (Installable Client...
opencv4:x64-windows                               4.8.0#9             computer vision library
opencv4[contrib]:x64-windows                                          opencv_contrib module
opencv4[default-features]:x64-windows                                 Platform-dependent default features
opencv4[dnn]:x64-windows                                              Enable dnn module
opencv4[jpeg]:x64-windows                                             JPEG support for opencv
opencv4[nonfree]:x64-windows                                          allow nonfree and unredistributable libraries
opencv4[png]:x64-windows                                              PNG support for opencv
opencv4[quirc]:x64-windows                                            Enable QR code module
opencv4[tiff]:x64-windows                                             TIFF support for opencv
opencv4[webp]:x64-windows                                             WebP support for opencv
openjpeg:x64-windows                              2.5.0#1             OpenJPEG is an open-source JPEG 2000 codec writt...
openssl:x64-windows                               3.1.4#1             OpenSSL is an open source project that provides ...
pkgconf:x64-windows                               1.8.0#5             pkgconf is a program which helps to configure co...
protobuf:x64-windows                              3.21.12             Protocol Buffers - Google's data interchange format
pugixml:x64-windows                               1.14                Light-weight, simple and fast XML parser for C++...
python3:x64-windows                               3.11.5              The Python programming language
quirc:x64-windows                                 1.1#3               quirc is one of the C library available for scan...
snappy:x64-windows                                1.1.10              A fast compressor/decompressor.
sqlite3:x64-windows                               3.43.1              SQLite is a software library that implements a s...
sqlite3[json1]:x64-windows                                            Enable JSON functionality for sqlite3
szip:x64-windows                                  2.1.1#12            Szip compression software, providing lossless co...
tbb:x64-windows                                   2021.10.0#3         Intel's Threading Building Blocks.
tbb[hwloc]:x64-windows                                                Builds TBB with TBBBind support for Hybrid CPUs ...
tesseract:x64-windows                             5.3.3               An OCR Engine that was developed at HP Labs betw...
tiff:x64-windows                                  4.6.0#1             A library that supports the manipulation of TIFF...
tiff[jpeg]:x64-windows                                                Support JPEG compression in TIFF image files
tiff[lzma]:x64-windows                                                Support LZMA compression in TIFF image files
tiff[zip]:x64-windows                                                 Support ZIP/deflate compression in TIFF image files
vcpkg-cmake-config:x64-windows                    2022-02-06#1
vcpkg-cmake-get-vars:x64-windows                  2023-03-02
vcpkg-cmake:x64-windows                           2023-05-04
vcpkg-get-python-packages:x64-windows             2023-07-28
vcpkg-msbuild:x64-windows                         2023-08-08
vcpkg-pkgconfig-get-modules:x64-windows           2023-09-06
vcpkg-tool-meson:x64-windows                      0.63                Meson build system
xbyak:x64-windows                                 6.73                Xbyak is a JIT assembler for x86(IA-32)/x64(AMD6...
zlib:x64-windows                                  1.3                 A compression library
zstd:x64-windows                                  1.5.5#2             Zstandard - Fast real-time compression algorithm
  1. rustc version (rustc --version)
 rustc --version
rustc 1.73.0 (cc66ad468 2023-10-03)
  1. Attach the full output of the following command from your project directory:
$env:RUST_BACKTRACE=1; cargo build -vv
Fresh autocfg v1.1.0
Fresh cfg-if v1.0.0
Fresh scopeguard v1.2.0
Fresh simd-adler32 v0.3.7
Fresh glob v0.3.1
Fresh jobserver v0.1.26
Fresh memchr v2.6.4
Fresh adler v1.0.2
Fresh regex-syntax v0.8.2
Fresh once_cell v1.18.0
Fresh dunce v1.0.4
Fresh either v1.9.0
Fresh percent-encoding v2.3.0
Fresh pkg-config v0.3.27
Fresh weezl v0.1.7
Fresh shlex v1.2.0
Fresh vcpkg v0.2.15
Fresh color_quant v1.1.0
Fresh bit_field v0.10.2
Fresh bytemuck v1.14.0
Fresh cc v1.0.79
Fresh aho-corasick v1.1.2
Fresh miniz_oxide v0.7.1
Fresh fdeflate v0.3.1
Fresh zune-inflate v0.2.54
Fresh lebe v0.5.2
Fresh bitflags v1.3.2
Fresh smallvec v1.11.1
Fresh cty v0.2.2
Fresh half v2.2.1
Fresh qoi v0.4.1
Fresh gif v0.12.0
Fresh byteorder v1.5.0
Fresh crossbeam-utils v0.8.16
Fresh memoffset v0.9.0
Fresh libc v0.2.150
Fresh regex-automata v0.4.3
Fresh num-traits v0.2.17
Fresh lock_api v0.4.11
Fresh crc32fast v1.3.2
Fresh windows_x86_64_msvc v0.48.5
Fresh semver v1.0.20
Fresh winapi v0.3.9
Fresh raw-window-handle v0.4.3
Fresh crossbeam-epoch v0.9.15
Fresh clang-sys v1.6.1
Fresh regex v1.10.2
Fresh flate2 v1.0.28
Fresh spin v0.9.8
Fresh windows-targets v0.48.5
Fresh num-integer v0.1.45
Fresh crossbeam-deque v0.8.3
Fresh clang v2.0.0
Fresh flume v0.11.0
Fresh windows-core v0.51.1
Fresh png v0.17.10
Fresh num-rational v0.4.1
Fresh minifb v0.25.0
Fresh rayon-core v1.12.0
Fresh opencv-binding-generator v0.77.0
Fresh windows v0.51.1
Fresh rayon v1.8.0
Fresh exr v1.71.0
Fresh win-screenshot v4.0.5
Fresh jpeg-decoder v0.3.0
Fresh tiff v0.9.0
Fresh opencv v0.86.1
Fresh image v0.24.7
Dirty app_example v0.1.0 (C:\Users\Aleks\code\rust\app_example): the file `src\main.rs` has changed (13344204199.583979000s, 13m 38s after last build at 13344203381.032425400s)
Compiling app_example v0.1.0 (C:\Users\Aleks\code\rust\app_example)
Running `set CARGO=\\?\C:\Users\Aleks\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\cargo.exe&& set CARGO_BIN_NAME=app_example&& set CARGO_CRATE_NAME=app_example&& set CARGO_MANIFEST_DIR=C:\Users\Aleks\code\rust\
wbr\direct_test\app_example&& set CARGO_PKG_AUTHORS=""&& set CARGO_PKG_DESCRIPTION=""&& set CARGO_PKG_HOMEPAGE=""&& set CARGO_PKG_LICENSE=""&& set CARGO_PKG_LICENSE_FILE=""&& set CARGO_PKG_NAME=app_example&& set CARGO_PKG_RE
ADME=""&& set CARGO_PKG_REPOSITORY=""&& set CARGO_PKG_RUST_VERSION=""&& set CARGO_PKG_VERSION=0.1.0&& set CARGO_PKG_VERSION_MAJOR=0&& set CARGO_PKG_VERSION_MINOR=1&& set CARGO_PKG_VERSION_PATCH=0&& set CARGO_PKG_VERSION_PRE=""&&
set CARGO_PRIMARY_PACKAGE=1&& set PATH="C:\Users\Aleks\code\rust\app_example\target\debug\deps;C:\Users\Aleks\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin;C:\Users\Aleks\.rustup\toolchains\stable-x86_6
4-pc-windows-msvc\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2\libnvvp;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\Wind
owsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\Nsight Compute 2023.2.2\;C:\Program F
iles\Git\cmd;C:\Program Files\Docker\Docker\resources\bin;C:\Users\Aleks\.cargo\bin;C:\Users\Aleks\scoop\shims;C:\Users\Aleks\AppData\Local\Microsoft\WindowsApps;C:\Users\Aleks\AppData\Local\JetBrains\Toolbox\scripts;C:\Users\Al
eks\code\vcpkg\installed\x64-windows\tools\llvm;C:\Users\Aleks\code\vcpkg\installed\x64-windows\bin"&& rustc --crate-name app_example --edition=2021 src\main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,fut
ure-incompat --diagnostic-width=227 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=b92f73e02a50728f --out-dir C:\Users\Aleks\code\rust\app_example\target\debug\deps -C incr
emental=C:\Users\Aleks\code\rust\app_example\target\debug\incremental -L dependency=C:\Users\Aleks\code\rust\app_example\target\debug\deps --extern image=C:\Users\Aleks\code\rust\wbr\direct_te
st\app_example\target\debug\deps\libimage-65905ff392b20c53.rlib --extern minifb=C:\Users\Aleks\code\rust\app_example\target\debug\deps\libminifb-31c7636380326dcb.rlib --extern opencv=C:\Users\Aleks\code\rust\
wbr\direct_test\app_example\target\debug\deps\libopencv-752fd70cd44f4b79.rlib --extern regex=C:\Users\Aleks\code\rust\app_example\target\debug\deps\libregex-934e56fa3e26a7f6.rlib --extern win_screenshot=C:\Us
ers\Aleks\code\rust\app_example\target\debug\deps\libwin_screenshot-4babd5d4a1e0bc8a.rlib -L "native=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\atlmfc\lib\x64" -L native=C
:\Users\Aleks\code\rust\app_example\target\debug\build\opencv-00c063f1a6799fd8\out -L native=C:\Users\Aleks\code\vcpkg\installed\x64-windows\lib -L native=C:\Users\Aleks\code\vcpkg\installed\x64-windows\bin -L
native=C:\Users\Aleks\code\rust\app_example\target\debug\build\opencv-00c063f1a6799fd8\out -L C:\Users\Aleks\code\rust\app_example\target\debug\build\opencv-00c063f1a6799fd8\out -L native=C:\Users\Aleks\.cargo\registry\src\index.crates.io-6f17d22bba15001f\windows_x86_64_msvc-0.48.5\lib` 
Finished dev [unoptimized + debuginfo] target(s) in 1.04s
  1. simplified code example: In this code, I'm trying to recognize text on a small portion of an image.
use opencv::prelude::*;
use opencv::imgproc;

fn main() {
// ...
  let mat = unsafe {
          Mat::new_rows_cols_with_data(
              buf.height as i32,
              buf.width as i32,
              core::CV_8UC4, // BGRA
              buf.pixels.as_ptr() as *mut c_void,
              core::Mat_AUTO_STEP,
          )
      }.unwrap();
      
  let mut gray_mat = Mat::default();
  imgproc::cvt_color(&mat, &mut gray_mat, imgproc::COLOR_BGRA2GRAY, 0).unwrap();

  let mut ocr = text::OCRTesseract::create("", "", "", 0, 3).unwrap();
  let mut out_text = String::new();
  BaseOCRTrait::run_def(&mut ocr,&mut gray_mat, &mut out_text).unwrap();
  
  println!("{:?}", out_text);
}
  1. Result of run:
cargo run
   Compiling app_example v0.1.0 (C:\Users\Aleks\code\rust\app_example)
    Finished dev [unoptimized + debuginfo] target(s) in 1.04s
     Running `target\debug\app_example.exe`
error: process didn't exit successfully: `target\debug\app_example.exe` (exit code: 0xc0000005, STATUS_ACCESS_VIOLATION)

I'm not good at this kind of bugs, my experience with rust is about one month, what could I have done wrong? In other operations with these mat such as imgproc there is no problem, the error only occurs when using OCR.

Amourlive avatar Nov 11 '23 19:11 Amourlive

tesseract test.png output -l eng
cat .\output.txt
This package contains an OCR engine - libtesseract and a command line program - tesseract .

Calling Tesseract directly works.

If I specify the language 'eng' during OCR initialization, the result will be the following:

  let mut ocr = text::OCRTesseract::create("", "eng", "", 0, 3).unwrap();
Error: Tesseract (legacy) engine requested, but components are not present in C:\Users\Aleks\code\vcpkg\installed\x64-windows\share\tessdata/eng.traineddata!!
Failed loading language 'eng'
Tesseract couldn't load any languages!
OCRTesseract: Could not initialize tesseract.

Judging by the path shown, there are problems with converting paths from Linux style to Windows style, but that's just a guess.

Amourlive avatar Nov 14 '23 18:11 Amourlive

Such paths should work fine in Windows too. Can you make sure that the indicated file indeed exists? I'm don't really have experience working with Tesseract neither with using Tesseract from OpenCV so I'm not going to be of much help here unfortunately. If you have a working C++ or Python code I could then help with translating it to Rust. Also note that those errors are coming from OpenCV itself, not from the crate.

twistedfall avatar Nov 15 '23 08:11 twistedfall

And you're not getting the STATUS_ACCESS_VIOLATION error anymore, right?

twistedfall avatar Nov 15 '23 08:11 twistedfall

Yes, I can verify that the file exists, in two ways at once:

  1. Run teseract with English directly:
tesseract test.png output -l eng
cat .\output.txt
This package contains an OCR engine - libtesseract and a command line program - tesseract .

Obviously, the direct call would not work if there was no data for English. And if I delete the language data, then indeed I will get an error that the environment variable TESSDATA_PREFIX is not defined, or the files for English do not exist. 2. Go to the folder and make sure the file is there:

PS C:\Users\Aleks> cd C:\Users\Aleks\code\vcpkg\installed\x64-windows\share\tessdata
PS C:\Users\Aleks\code\vcpkg\installed\x64-windows\share\tessdata> ls


    Directory: C:\Users\Aleks\code\vcpkg\installed\x64-windows\share\tessdata


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         11/7/2023  10:23 AM                configs
d-----        11/14/2023  11:54 PM                tessconfigs
-a----        11/14/2023  11:51 PM        4113088 eng.traineddata

Yes, I don't get the STATUS_ACCESS_VIOLATION error when prescribing a language.

TESSDATA_PREFIX is also used when calling through OpenCV judging by the path in the error, it turns out that if you do not specify this environment variable, ocr will not work.

Honestly, I'd flesh out the OCR documentation if you need my help with that, and there will nice if i have recommendations on your part as to where and how it should be written.

As for the file search error, I'm stumped for now.

Amourlive avatar Nov 15 '23 08:11 Amourlive

Well, the error message might already imply that the file exists, but doesn't contain the components for the legacy engine data so it might be some mismatch between installed Tesseract version and what OpenCV expects.

All of the crate documentation is automatically translated from the OpenCV docs so manually fixing it is not an option. If you see a way to improve the automatic conversion I would gladly take that into account!

twistedfall avatar Nov 15 '23 08:11 twistedfall

You mean I have a version of tesseract that is incompatible with opencv?

The funny thing is that I didn't install tesseract myself, it was installed along with opencv as its dependency when using the command

.\vcpkg install llvm opencv4[contrib,nonfree]

I tried to find some declaration about compatible versions of opencv and tesseract, but so far no luck.

Amourlive avatar Nov 15 '23 09:11 Amourlive

Can you maybe also try writing the same code in C++ to see if it works or produces the same error?

twistedfall avatar Nov 17 '23 09:11 twistedfall

Any luck?

twistedfall avatar Apr 16 '24 18:04 twistedfall