### Get Recognized Text Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Retrieves the recognized text from the image as a UTF-8 string. Returns an error if text retrieval fails. ```rust pub fn get_text(&mut self) -> Result ``` -------------------------------- ### Get Mean Text Confidence Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Calculates and returns the mean confidence of the recognized text. ```rust pub fn mean_text_conf(&mut self) -> i32 ``` -------------------------------- ### Get PageSegMode as TessPageSegMode Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.PageSegMode.html Retrieves the page-segmentation mode's value as used by Tesseract. ```rust pub fn as_tess_page_seg_mode(&self) -> TessPageSegMode ``` -------------------------------- ### Get HOCR Text Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Retrieves the recognized text encoded as HTML with bounding box tags for a specific page. Returns an error if retrieval fails. ```rust pub fn get_hocr_text( &mut self, page: c_int, ) -> Result ``` -------------------------------- ### Tesseract::mean_text_conf Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Gets the mean confidence level of the recognized text. ```APIDOC ## Tesseract::mean_text_conf ### Description Returns the mean confidence level for the recognized text. The confidence is a value between 0 and 100, where 100 is the highest confidence. ### Method `Tesseract::mean_text_conf` ### Returns - `i32`: The mean confidence level of the recognized text. ``` -------------------------------- ### Get UTF-8 Text Output Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Retrieves the recognized text from the Tesseract engine as a UTF-8 encoded string. This is the most common method for obtaining OCR results. It requires the Tesseract instance to have already recognized the image. ```rust pub fn get_text(&mut self) -> Result { Ok(self .0 .get_utf8_text()?. as_ref() .to_string_lossy() .into_owned()) } ``` -------------------------------- ### Get TSV Text Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Retrieves the recognized text encoded as TSV (Tab-Separated Values), including bounding boxes and confidence scores for a specific page. Returns an error if retrieval fails. ```rust pub fn get_tsv_text( &mut self, page: c_int, ) -> Result ``` -------------------------------- ### Get TSV Text Output Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Retrieves the recognized text in TSV (Tab-Separated Values) format. This format includes bounding boxes, confidence levels, and other details for each recognized character or word, similar to HOCR but in a tabular structure. ```rust pub fn get_tsv_text( &mut self, page: c_int, ) -> Result { Ok(self .0 .get_tsv_text(page)?. as_ref() .to_string_lossy() .into_owned()) } ``` -------------------------------- ### Get HOCR Text Output Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Retrieves the recognized text encoded in the HOCR (HTML Object Character Recognition) format. HOCR includes bounding box information for each recognized word or character, making it suitable for detailed analysis or display. ```rust pub fn get_hocr_text( &mut self, page: c_int, ) -> Result { Ok(self .0 .get_hocr_text(page)?. as_ref() .to_string_lossy() .into_owned()) } ``` -------------------------------- ### Tesseract::new Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Initializes a new Tesseract instance with a specified data path and language. If `None` is provided for either, Tesseract's default behavior will be used. ```APIDOC ## Tesseract::new ### Description Initializes a new Tesseract instance with a specified data path and language. If `None` is provided for either, Tesseract's default behavior will be used. ### Method ```rust pub fn new(datapath: Option<&str>, language: Option<&str>) -> Result ``` ### Parameters * **datapath** (`Option<&str>`) - Optional path to the Tesseract data directory. * **language** (`Option<&str>`) - Optional language code (e.g., "eng") to use for OCR. ### Returns A `Result` containing a new `Tesseract` instance on success, or an `InitializeError` on failure. ``` -------------------------------- ### Initialize Tesseract OCR Engine Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Creates a new Tesseract OCR engine instance. Specify the path to Tesseract data files and the desired language(s). ```rust pub fn new(datapath: Option<&str>, language: Option<&str>) -> Result { let mut tess = Tesseract(plumbing::TessBaseApi::create()); let datapath = match datapath { Some(i) => Some(CString::new(i)?), None => None, }; let language = match language { Some(i) => Some(CString::new(i)?), None => None, }; tess.0.init_2(datapath.as_deref(), language.as_deref())?; Ok(tess) } ``` -------------------------------- ### Initialize Tesseract with Data Path and Language Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Creates a new Tesseract instance with specified data path and language. Returns an error if initialization fails. ```rust pub fn new( datapath: Option<&str>, language: Option<&str>, ) -> Result ``` -------------------------------- ### Initialize Tesseract with None Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Tests the ability to initialize the Tesseract engine with both configuration and language set to None, expecting success. ```rust #[test] fn initialize_with_none() { assert!(Tesseract::new(None, None).is_ok()); } ``` -------------------------------- ### Initialize Tesseract with OEM Mode Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Creates a new Tesseract instance with specified data path, language, and OCR Engine Mode (OEM). Returns an error if initialization fails. ```rust pub fn new_with_oem( datapath: Option<&str>, language: Option<&str>, oem: OcrEngineMode, ) -> Result ``` -------------------------------- ### Initialize Tesseract with Data Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Creates a new Tesseract instance initialized with raw data, an optional language, and an OCR engine mode. Use this when you have Tesseract data files available in memory. ```rust pub fn new_with_data( data: &[u8], language: Option<&str>, oem: OcrEngineMode, ) -> Result { let mut tess = Tesseract(plumbing::TessBaseApi::create()); let language = match language { Some(i) => Some(CString::new(i)?), None => None, }; tess.0.init_1(data, language.as_deref(), oem.to_value())?; Ok(tess) } ``` -------------------------------- ### Tesseract Constructors Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Provides methods to initialize the Tesseract API with different configurations. ```APIDOC ## `new` ### Description Initializes the Tesseract API with a specified data path and language. ### Method `pub fn new(datapath: Option<&str>, language: Option<&str>) -> Result` ## `new_with_oem` ### Description Initializes the Tesseract API with a specified data path, language, and OCR Engine Mode (OEM). ### Method `pub fn new_with_oem(datapath: Option<&str>, language: Option<&str>, oem: OcrEngineMode) -> Result` ``` -------------------------------- ### Configuration and Recognition Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Methods for configuring Tesseract settings and initiating the recognition process. ```APIDOC ## `set_rectangle` ### Description Sets a rectangular region of the image for processing. ### Method `pub fn set_rectangle(self, left: i32, top: i32, width: i32, height: i32) -> Self ## `set_source_resolution` ### Description Sets the source resolution (PPI) of the image. ### Method `pub fn set_source_resolution(self, ppi: i32) -> Self ## `set_variable` ### Description Sets a Tesseract configuration variable. ### Method `pub fn set_variable(self, name: &str, value: &str) -> Result ## `recognize` ### Description Performs the OCR recognition on the set image. ### Method `pub fn recognize(self) -> Result` ``` -------------------------------- ### Initialize Tesseract with OCR Engine Mode Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Initializes the Tesseract OCR engine with a specific OCR Engine Mode (OEM). This allows control over which Tesseract recognition engine is used. ```rust pub fn new_with_oem( datapath: Option<&str>, language: Option<&str>, oem: OcrEngineMode, ) -> Result { let mut tess = Tesseract(plumbing::TessBaseApi::create()); let datapath = match datapath { Some(i) => Some(CString::new(i)?), None => None, }; let language = match language { Some(i) => Some(CString::new(i)?), None => None, }; tess.0 .init_4(datapath.as_deref(), language.as_deref(), oem.to_value())?; Ok(tess) } ``` -------------------------------- ### Tesseract::new_with_oem Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Initializes a new Tesseract instance with a specified data path, language, and OCR engine mode (OEM). This allows for more control over the recognition process. ```APIDOC ## Tesseract::new_with_oem ### Description Initializes a new Tesseract instance with a specified data path, language, and OCR engine mode (OEM). This allows for more control over the recognition process. ### Method ```rust pub fn new_with_oem(datapath: Option<&str>, language: Option<&str>, oem: OcrEngineMode) -> Result ``` ### Parameters * **datapath** (`Option<&str>`) - Optional path to the Tesseract data directory. * **language** (`Option<&str>`) - Optional language code (e.g., "eng") to use for OCR. * **oem** (`OcrEngineMode`) - The OCR engine mode to use (e.g., `Default`, `LstmOnly`). ### Returns A `Result` containing a new `Tesseract` instance on success, or an `InitializeError` on failure. ``` -------------------------------- ### CloneToUninit Implementation for T Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.PageSegMode.html Performs copy-assignment from self to dest. This is a nightly-only experimental API. ```rust unsafe fn clone_to_uninit(&self, dest: *mut u8) ``` -------------------------------- ### Implement From for InitializeError Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Allows converting a TessBaseApiInitError into an InitializeError. ```rust fn from(source: TessBaseApiInitError) -> Self ``` -------------------------------- ### Set Source Resolution Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Sets the source resolution (PPI) of the input image. Returns the modified Tesseract instance. ```rust pub fn set_source_resolution(self, ppi: i32) -> Self ``` -------------------------------- ### Set Image from Filename Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Sets the input image for Tesseract using a filename. Returns an error if the image cannot be set. ```rust pub fn set_image(self, filename: &str) -> Result ``` -------------------------------- ### Implement Display for InitializeError Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Provides a Display implementation for the InitializeError enum, allowing it to be formatted for user-facing messages. ```rust fn fmt(&self, __formatter: &mut Formatter<'_>) -> Result ``` -------------------------------- ### Tesseract::new_with_data Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Creates a new Tesseract instance with provided image data and optional language and OCR engine mode. ```APIDOC ## Tesseract::new_with_data ### Description Initializes Tesseract with raw image data. This method allows for specifying the language model and the OCR engine mode to use. ### Method `Tesseract::new_with_data` ### Parameters - `data` (&[u8]): A slice of bytes representing the image data. - `language` (Option<&str>): An optional string slice specifying the language for OCR. - `oem` (OcrEngineMode): The OCR Engine Mode to use. ### Returns - `Result`: A `Result` containing a new `Tesseract` instance on success, or an `InitializeError` on failure. ``` -------------------------------- ### OCR Test with Image File Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html A unit test that performs OCR on a sample image file ('img.png') using English language and asserts that the output matches the content of 'img.txt'. This verifies the basic OCR functionality for file inputs. ```rust #[test] fn ocr_test() -> Result<(), TesseractError> { assert_eq!( ocr("img.png", "eng")?, include_str!("../img.txt").to_string() ); Ok(()) } ``` -------------------------------- ### PartialEq Implementation for PageSegMode Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.PageSegMode.html Tests for equality between two PageSegMode values. ```rust fn eq(&self, other: &PageSegMode) -> bool ``` -------------------------------- ### Set Image from Filename Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Sets the input image for OCR from a file path. The function returns the modified Tesseract instance. Ensure the file exists and is a supported image format. ```rust pub fn set_image(mut self, filename: &str) -> Result { let pix = plumbing::leptonica_plumbing::Pix::read(&CString::new(filename)?)?; self.0.set_image_2(&pix); Ok(self) } ``` -------------------------------- ### OCR Engine Mode Comparison Test Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Compares the text recognition results between Tesseract's legacy engine (OEM::TesseractOnly) and its LSTM engine (OEM::LstmOnly). ```rust #[test] #[ignore] // Many systems do not have legacy Tesseract data available fn oem_test() -> Result<(), TesseractError> { let only_tesseract_str = Tesseract::new_with_oem(None, Some("eng"), OcrEngineMode::TesseractOnly)? .set_image("img.png")? .recognize()? .get_text()?; let only_lstm_str = Tesseract::new_with_oem(None, Some("eng"), OcrEngineMode::LstmOnly)? .set_image("img.png")? .recognize()? .get_text()?; assert_ne!(only_tesseract_str, only_lstm_str); Ok(()) } ``` -------------------------------- ### Set Image from Memory Buffer Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Sets the input image for Tesseract from a byte slice in memory. Returns an error if the image data is invalid. ```rust pub fn set_image_from_mem(self, img: &[u8]) -> Result ``` -------------------------------- ### Basic Image Recognition Test Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Tests basic image recognition functionality by setting an image and asserting the recognized text against a known file. ```rust #[test] fn expanded_test() -> Result<(), TesseractError> { let mut cube = Tesseract::new(None, Some("eng"))? .set_image("img.png")? .set_variable("tessedit_char_blacklist", "z")? .recognize()?; assert_eq!(&cube.get_text()?, include_str!("../img.txt")); Ok(()) } ``` -------------------------------- ### Set Image from Raw Frame Data Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Sets the input image for Tesseract using raw frame data. Requires image dimensions and pixel/line byte counts. Returns an error if the image cannot be set. ```rust pub fn set_frame( self, frame_data: &[u8], width: i32, height: i32, bytes_per_pixel: i32, bytes_per_line: i32, ) -> Result ``` -------------------------------- ### TryInto for TesseractError Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.TesseractError.html Provides the `try_into` method for types that implement `TryFrom`, allowing conversion into another type with error handling. ```APIDOC ## impl TryInto for T where U: TryFrom ### Description This implementation block provides the `try_into` method for types `T` that can be converted into type `U` using `TryFrom`. It allows for fallible conversions, returning a `Result`. ### Methods #### fn try_into(self) -> Result>::Error> Performs the conversion. Returns `Ok(U)` on success or `Err(Error)` if the conversion fails. ### Type Alias #### type Error = >::Error The associated type `Error` represents the specific error type returned when the `try_into` conversion fails. This is determined by the `TryFrom` implementation for `U`. ``` -------------------------------- ### Implement From for TesseractError Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Allows converting an InitializeError into a TesseractError. ```rust fn from(source: InitializeError) -> Self ``` -------------------------------- ### TryFrom for TesseractError Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.TesseractError.html Provides the `try_from` method for types that implement `TryFrom`, allowing conversion from another type with error handling. ```APIDOC #### fn try_from(value: U) -> Result>::Error> Performs the conversion. Returns `Ok(T)` on success or `Err(Error)` if the conversion fails. ``` -------------------------------- ### Implement From for InitializeError Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Allows converting a NulError into an InitializeError. ```rust fn from(source: NulError) -> Self ``` -------------------------------- ### Set Tesseract Variable Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Sets a Tesseract configuration variable. Returns an error if the variable cannot be set. ```rust pub fn set_variable( self, name: &str, value: &str, ) -> Result ``` -------------------------------- ### Clone From Implementation for PageSegMode Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.PageSegMode.html Enables copy-assignment from a source PageSegMode value. ```rust fn clone_from(&mut self, source: &Self) ``` -------------------------------- ### Implement Debug for InitializeError Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Provides a Debug implementation for the InitializeError enum, allowing it to be formatted for debugging purposes. ```rust fn fmt(&self, f: &mut Formatter<'_>) -> Result ``` -------------------------------- ### Ne Implementation for PageSegMode Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.PageSegMode.html Tests for inequality between two PageSegMode values. ```rust fn ne(&self, other: &Rhs) -> bool ``` -------------------------------- ### TryFrom Implementation for T Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.PageSegMode.html Performs the conversion from U to T, returning a Result. ```rust type Error = Infallible ``` ```rust fn try_from(value: U) -> Result>::Error> ``` -------------------------------- ### Recognize Image Content Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Performs the OCR recognition process on the currently set image. This function should be called after setting the image and any desired configurations. It returns the Tesseract instance for further chaining. ```rust pub fn recognize(mut self) -> Result { self.0.recognize()?; Ok(self) } ``` -------------------------------- ### Set Page Segmentation Mode Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Sets the page segmentation mode for Tesseract. ```rust pub fn set_page_seg_mode(&mut self, mode: PageSegMode) ``` -------------------------------- ### Page Segmentation Mode Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Method for setting the page segmentation mode. ```APIDOC ## `set_page_seg_mode` ### Description Sets the page segmentation mode for the OCR process. ### Method `pub fn set_page_seg_mode(&mut self, mode: PageSegMode)` ``` -------------------------------- ### Implement TryFrom for T Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Provides the TryFrom trait implementation for generic types, attempting to convert U into T. ```rust fn try_from(value: U) -> Result>::Error> ``` -------------------------------- ### Implement TryInto for T Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Provides the TryInto trait implementation for generic types, attempting to convert T into U. ```rust fn try_into(self) -> Result>::Error> ``` -------------------------------- ### Tesseract::recognize Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Performs OCR recognition on the current image. ```APIDOC ## Tesseract::recognize ### Description Initiates the OCR process on the image that has been previously set. This method performs the actual text recognition. ### Method `Tesseract::recognize` ### Returns - `Result`: A `Result` containing the `Tesseract` instance after recognition, or an error if recognition fails. ``` -------------------------------- ### OCR Test with Memory Image and Resolution Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html A unit test that performs OCR on image data from a TIFF file in memory, setting a specific source resolution (PPI). It then retrieves the text and asserts it against 'img.txt'. This tests the combination of memory input and resolution setting. ```rust #[test] fn ocr_from_mem_with_ppi() -> Result<(), TesseractError> { let mut cube = Tesseract::new(None, Some("eng"))?. set_image_from_mem(include_bytes!("../img.tiff"))?. set_source_resolution(70); assert_eq!(&cube.get_text()?, include_str!("../img.txt")); } ``` -------------------------------- ### Implement From for T Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Provides the From trait implementation for generic types, returning the argument unchanged. ```rust fn from(t: T) -> T ``` -------------------------------- ### Set Image from Frame Data Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Sets the input image for OCR from raw frame data in memory. This method requires specifying image dimensions and pixel format details. Use this for images loaded directly into memory. ```rust pub fn set_frame( mut self, frame_data: &[u8], width: i32, height: i32, bytes_per_pixel: i32, bytes_per_line: i32, ) -> Result { self.0 .set_image(frame_data, width, height, bytes_per_pixel, bytes_per_line)?; Ok(self) } ``` -------------------------------- ### Tesseract::get_text Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Retrieves the recognized text from the image. ```APIDOC ## Tesseract::get_text ### Description Retrieves the recognized text as a UTF-8 encoded string after the `recognize` method has been called. ### Method `Tesseract::get_text` ### Returns - `Result`: A `Result` containing the recognized text as a `String` on success, or an error if retrieving the text fails. ``` -------------------------------- ### Clone Implementation for PageSegMode Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.PageSegMode.html Provides the ability to create a duplicate of a PageSegMode value. ```rust fn clone(&self) -> PageSegMode ``` -------------------------------- ### Set Tesseract Variable Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Sets a Tesseract configuration variable. This allows fine-tuning OCR behavior by modifying internal parameters. Both the variable name and its value must be provided as strings. ```rust pub fn set_variable(mut self, name: &str, value: &str) -> Result { self.0 .set_variable(&CString::new(name)?, &CString::new(value)?)?; Ok(self) } ``` -------------------------------- ### Image and Frame Setting Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Methods for setting the image or frame data for OCR processing. ```APIDOC ## `set_image` ### Description Sets the image to be processed from a file path. ### Method `pub fn set_image(self, filename: &str) -> Result` ## `set_frame` ### Description Sets the image to be processed from raw frame data with specified dimensions and format. ### Method `pub fn set_frame(self, frame_data: &[u8], width: i32, height: i32, bytes_per_pixel: i32, bytes_per_line: i32) -> Result` ## `set_image_from_mem` ### Description Sets the image to be processed from a byte slice in memory. ### Method `pub fn set_image_from_mem(self, img: &[u8]) -> Result` ``` -------------------------------- ### Recognize Image Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Performs OCR recognition on the currently set image. Returns an error if recognition fails. ```rust pub fn recognize(self) -> Result ``` -------------------------------- ### Set Source Resolution Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Sets the source resolution (PPI - Pixels Per Inch) of the input image. This can improve OCR accuracy, especially for images with varying resolutions. It's recommended to set this before processing the image. ```rust pub fn set_source_resolution(mut self, ppi: i32) -> Self { self.0.set_source_resolution(ppi); self } ``` -------------------------------- ### Perform OCR on Image File Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html A convenience function to perform OCR on an image file. It initializes Tesseract, sets the image, recognizes the text, and returns the result. This is a common workflow for processing single image files. ```rust pub fn ocr(filename: &str, language: &str) -> Result { Ok(Tesseract::new(None, Some(language))?. set_image(filename)?. recognize()?. get_text()?) } ``` -------------------------------- ### Set Image from Memory Buffer Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Sets the input image for OCR from a byte slice representing image data in memory. This is useful for images loaded from various sources like network streams or embedded resources. ```rust pub fn set_image_from_mem( mut self, img: &[u8], ) -> Result { let pix = plumbing::leptonica_plumbing::Pix::read_mem(img)?; self.0.set_image_2(&pix); Ok(self) } ``` -------------------------------- ### Implement Any for T Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Provides the Any trait implementation for generic types. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### Implement Into for T Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Provides the Into trait implementation for generic types, converting T into U if U implements From. ```rust fn into(self) -> U ``` -------------------------------- ### Implement Error trait for InitializeError Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Implements the standard Error trait for InitializeError, providing methods to access the error source and description. ```rust fn source(&self) -> Option<&(dyn Error + 'static)> ``` ```rust fn description(&self) -> &str ``` ```rust fn cause(&self) -> Option<&dyn Error> ``` ```rust fn provide<'a>(&'a self, request: &mut Request<'a>) ``` -------------------------------- ### Set Recognition Rectangle Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Defines a rectangular region within the image for recognition. Returns the modified Tesseract instance. ```rust pub fn set_rectangle(self, left: i32, top: i32, width: i32, height: i32) -> Self ``` -------------------------------- ### Tesseract::set_image_from_mem Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Sets the image to be processed by Tesseract from in-memory image data. ```APIDOC ## Tesseract::set_image_from_mem ### Description Sets the image for Tesseract processing using image data directly from memory. ### Method `Tesseract::set_image_from_mem` ### Parameters - `img` (&[u8]): A slice of bytes representing the image data in memory. ### Returns - `Result`: A `Result` containing the modified `Tesseract` instance on success, or an error if reading the image from memory fails. ``` -------------------------------- ### Tesseract::set_image Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Sets the image to be processed by Tesseract from a file path. ```APIDOC ## Tesseract::set_image ### Description Loads an image from a specified file path and sets it as the current image for Tesseract processing. ### Method `Tesseract::set_image` ### Parameters - `filename` (&str): The path to the image file. ### Returns - `Result`: A `Result` containing the modified `Tesseract` instance on success, or a `SetImageError` on failure. ``` -------------------------------- ### ocr_from_frame Function Signature Source: https://docs.rs/tesseract/0.15.2/tesseract/fn.ocr_from_frame.html The signature for the `ocr_from_frame` function, outlining the required parameters for image data, dimensions, and language, and its return type. ```rust pub fn ocr_from_frame( frame_data: &[u8], width: i32, height: i32, bytes_per_pixel: i32, bytes_per_line: i32, language: &str, ) -> Result ``` -------------------------------- ### Implement ToString for T Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Provides the ToString trait implementation for types that implement Display. ```rust fn to_string(&self) -> String ``` -------------------------------- ### Tesseract::set_page_seg_mode Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Sets the page segmentation mode for Tesseract. ```APIDOC ## Tesseract::set_page_seg_mode ### Description Sets the page segmentation mode (PSM) for Tesseract. The PSM controls how Tesseract analyzes the page layout and segments the text. ### Method `Tesseract::set_page_seg_mode` ### Parameters - `mode` (PageSegMode): The page segmentation mode to set. ### Returns - `()`: This method does not return a value. ``` -------------------------------- ### Tesseract::set_source_resolution Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Sets the source resolution (PPI) of the image. ```APIDOC ## Tesseract::set_source_resolution ### Description Sets the source resolution of the image in Pixels Per Inch (PPI). This can help Tesseract to better interpret the image scale and improve OCR accuracy. ### Method `Tesseract::set_source_resolution` ### Parameters - `ppi` (i32): The source resolution in PPI. ### Returns - `Self`: The modified `Tesseract` instance. ``` -------------------------------- ### From for SetImageError Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.SetImageError.html Converts a NulError into a SetImageError. This is useful for handling errors during CString creation. ```rust impl From for SetImageError { fn from(source: NulError) -> Self { SetImageError::CStringError(source) } } ``` -------------------------------- ### Perform OCR on Image Frame Data Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html A convenience function to perform OCR on image data provided as a byte slice (frame). It initializes Tesseract, sets the frame data, recognizes, and returns the text. Useful for real-time image processing or images in memory. ```rust pub fn ocr_from_frame( frame_data: &[u8], width: i32, height: i32, bytes_per_pixel: i32, bytes_per_line: i32, language: &str, ) -> Result { Ok(Tesseract::new(None, Some(language))?. set_frame(frame_data, width, height, bytes_per_pixel, bytes_per_line)?. recognize()?. get_text()?) } ``` -------------------------------- ### Tesseract::set_variable Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Sets a Tesseract configuration variable. ```APIDOC ## Tesseract::set_variable ### Description Sets a Tesseract configuration variable to a specific value. This allows fine-tuning of Tesseract's behavior and recognition process. ### Method `Tesseract::set_variable` ### Parameters - `name` (&str): The name of the variable to set. - `value` (&str): The value to set for the variable. ### Returns - `Result`: A `Result` containing the modified `Tesseract` instance on success, or a `SetVariableError` if the variable could not be set. ``` -------------------------------- ### From for TesseractError Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.SetImageError.html Converts a SetImageError into a TesseractError. This allows integrating SetImageError into a broader Tesseract error handling strategy. ```rust impl From for TesseractError { fn from(source: SetImageError) -> Self { TesseractError::SetImageError(source) } } ``` -------------------------------- ### HOCR Text Generation Test Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Tests the generation of HOCR (HTML Output Recognition) text from an image and checks for a specific HTML structure. ```rust #[test] fn hocr_test() -> Result<(), TesseractError> { let mut cube = Tesseract::new(None, Some("eng"))?.set_image("img.png")?; assert!(&cube.get_hocr_text(0)?.contains("
`: A `Result` containing the HOCR encoded text as a `String` on success, or an error if retrieval fails. ``` -------------------------------- ### OCR Test with Image Frame Data Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html A unit test that performs OCR on image data loaded from a TIFF file ('img.tiff') as a byte array. It specifies image dimensions and asserts the recognized text against 'img.txt'. This tests OCR processing from memory buffers. ```rust #[test] fn ocr_from_frame_test() -> Result<(), TesseractError> { assert_eq!( ocr_from_frame(include_bytes!("../img.tiff"), 2256, 324, 3, 2256 * 3, "eng")?, include_str!("../img.txt").to_string() ); Ok(()) } ``` -------------------------------- ### Implement Borrow for T Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Provides the Borrow trait implementation for generic types. ```rust fn borrow(&self) -> &T ``` -------------------------------- ### Set Page Segmentation Mode Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Sets the page segmentation mode for Tesseract. This mode determines how Tesseract analyzes the page layout to identify text blocks, lines, and words. Choose a mode that best suits your document's structure. ```rust pub fn set_page_seg_mode(&mut self, mode: PageSegMode) { self.0.set_page_seg_mode(mode.as_tess_page_seg_mode()); } ``` -------------------------------- ### InitializeError Enum Definition Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html This snippet shows the definition of the InitializeError enum, detailing its variants. ```APIDOC ## Enum InitializeError ### Summary ```rust pub enum InitializeError { CStringError(NulError), TessBaseAPIInitError(TessBaseApiInitError), } ``` ### Variants * **CStringError(NulError)**: Represents an error during CString conversion, wrapping a `NulError`. * **TessBaseAPIInitError(TessBaseApiInitError)**: Represents an error during the initialization of the Tesseract Base API, wrapping a `TessBaseApiInitError`. ``` -------------------------------- ### Tesseract OCR Function Signature Source: https://docs.rs/tesseract/0.15.2/tesseract/fn.ocr.html This is the function signature for performing OCR. It takes a filename and language code as input and returns the recognized text or an error. ```rust pub fn ocr(filename: &str, language: &str) -> Result ``` -------------------------------- ### Tesseract::get_tsv_text Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Retrieves the recognized text encoded as TSV with bounding boxes and confidence. ```APIDOC ## Tesseract::get_tsv_text ### Description Retrieves the recognized text in TSV (Tab-Separated Values) format. This format includes bounding box coordinates, confidence levels, and the recognized text for each word or line. ### Method `Tesseract::get_tsv_text` ### Parameters - `page` (c_int): The page number to retrieve TSV text for. ### Returns - `Result`: A `Result` containing the TSV encoded text as a `String` on success, or an error if retrieval fails. ``` -------------------------------- ### ocr Source: https://docs.rs/tesseract/0.15.2/tesseract/index.html Performs OCR on an image. ```APIDOC ## ocr ### Description Performs Optical Character Recognition (OCR) on an image. ### Parameters (No specific parameters are detailed in the source for this function.) ### Response (No specific response details are detailed in the source for this function.) ### Request Example (No request example is available in the source.) ### Response Example (No response example is available in the source.) ``` -------------------------------- ### Tesseract Struct Definition Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Defines the Tesseract struct with private fields. ```rust pub struct Tesseract(/* private fields */); ``` -------------------------------- ### ToOwned Implementation for T Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.PageSegMode.html Creates owned data from borrowed data, usually by cloning. ```rust type Owned = T ``` ```rust fn to_owned(&self) -> T ``` ```rust fn clone_into(&self, target: &mut T) ``` -------------------------------- ### From for SetImageError Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.SetImageError.html Converts a PixReadError into a SetImageError. This is useful for handling errors when reading Pix images. ```rust impl From for SetImageError { fn from(source: PixReadError) -> Self { SetImageError::PixReadError(source) } } ``` -------------------------------- ### InitializeError Enum Definition Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Defines the InitializeError enum with its possible variants. ```rust pub enum InitializeError { CStringError(NulError), TessBaseAPIInitError(TessBaseApiInitError), } ``` -------------------------------- ### OcrEngineMode Enum Definition Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.OcrEngineMode.html Defines the different operating modes for the Tesseract OCR engine. Use these to control whether to use Tesseract only, LSTM only, or a combination. ```rust pub enum OcrEngineMode { Default, LstmOnly, TesseractLstmCombined, TesseractOnly, } ``` -------------------------------- ### Implement BorrowMut for T Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.InitializeError.html Provides the BorrowMut trait implementation for generic types. ```rust fn borrow_mut(&mut self) -> &mut T ``` -------------------------------- ### PageSegMode Enum Definition Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.PageSegMode.html Defines the different page segmentation modes accepted by Tesseract. ```rust pub enum PageSegMode { Show 14 variants PsmOsdOnly, PsmAutoOsd, PsmAutoOnly, PsmAuto, PsmSingleColumn, PsmSingleBlockVertText, PsmSingleBlock, PsmSingleLine, PsmSingleWord, PsmCircleWord, PsmSingleChar, PsmSparseText, PsmSparseTextOsd, PsmRawLine, } ``` -------------------------------- ### Tesseract::set_frame Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Sets the image to be processed by Tesseract from raw frame data. ```APIDOC ## Tesseract::set_frame ### Description Sets the image for Tesseract processing using raw pixel data from a frame buffer. This is useful for processing images directly from memory or camera feeds. ### Method `Tesseract::set_frame` ### Parameters - `frame_data` (&[u8]): Slice of bytes containing the image pixel data. - `width` (i32): The width of the image in pixels. - `height` (i32): The height of the image in pixels. - `bytes_per_pixel` (i32): The number of bytes per pixel. - `bytes_per_line` (i32): The number of bytes per line (stride). ### Returns - `Result`: A `Result` containing the modified `Tesseract` instance on success, or an error if setting the image fails. ``` -------------------------------- ### LSTM Only OCR Test Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Tests the LSTM-only OCR engine mode, asserting that the recognized text matches a known file. ```rust #[test] fn oem_ltsm_only_test() -> Result<(), TesseractError> { let only_lstm_str = Tesseract::new_with_oem(None, Some("eng"), OcrEngineMode::LstmOnly)? .set_image("img.png")? .recognize()? .get_text()?; assert_eq!(only_lstm_str, include_str!("../img.txt")); Ok(()) } ``` -------------------------------- ### Text Retrieval Source: https://docs.rs/tesseract/0.15.2/tesseract/struct.Tesseract.html Methods for retrieving the recognized text in various formats. ```APIDOC ## `get_text` ### Description Retrieves the recognized text as a UTF-8 string. ### Method `pub fn get_text(&mut self) -> Result ## `mean_text_conf` ### Description Retrieves the mean confidence level of the recognized text. ### Method `pub fn mean_text_conf(&mut self) -> i32 ## `get_hocr_text` ### Description Retrieves the recognized text encoded as HTML with bounding box tags. ### Method `pub fn get_hocr_text(&mut self, page: c_int) -> Result ## `get_tsv_text` ### Description Retrieves the recognized text encoded as TSV, including bounding boxes and confidence. ### Method `pub fn get_tsv_text(&mut self, page: c_int) -> Result` ``` -------------------------------- ### ocr_from_frame Source: https://docs.rs/tesseract/0.15.2/tesseract/fn.ocr_from_frame.html Performs OCR on raw image frame data. This function takes detailed information about the image buffer, including dimensions, pixel format, and language, to extract text. ```APIDOC ## Function ocr_from_frame ### Description Performs Optical Character Recognition (OCR) on raw image frame data. This function requires detailed specifications of the image buffer, such as dimensions, pixel format, and the language for recognition, to accurately extract text. ### Signature ```rust pub fn ocr_from_frame( frame_data: &[u8], width: i32, height: i32, bytes_per_pixel: i32, bytes_per_line: i32, language: &str, ) -> Result ``` ### Parameters * **frame_data** (`&[u8]`) - A slice containing the raw pixel data of the image frame. * **width** (`i32`) - The width of the image frame in pixels. * **height** (`i32`) - The height of the image frame in pixels. * **bytes_per_pixel** (`i32`) - The number of bytes used to represent each pixel. * **bytes_per_line** (`i32`) - The number of bytes from one image line to the next. * **language** (`&str`) - The language model to use for OCR (e.g., "eng" for English). ### Returns * `Result` - On success, returns a `String` containing the extracted text. On failure, returns a `TesseractError`. ``` -------------------------------- ### SetImageError Enum Definition Source: https://docs.rs/tesseract/0.15.2/tesseract/enum.SetImageError.html Defines the SetImageError enum with its possible error variants. ```rust pub enum SetImageError { CStringError(NulError), PixReadError(PixReadError), } ``` -------------------------------- ### PageSegMode Enum Definition Source: https://docs.rs/tesseract/0.15.2/src/tesseract/page_seg_mode.rs.html Defines the PageSegMode enum with variants representing different page segmentation strategies for Tesseract. Each variant includes a brief description of its purpose. ```rust 1// ⚠️ This file is generated 2// ⚠️ Regenerate with `make src/page_seg_mode.rs` 3 4use tesseract_sys::TessPageSegMode; 5 6/// Enum representing different PageSegMode options accepted by Tesseract 7#[derive(Debug, Clone, Copy, PartialEq, Eq)] 8pub enum PageSegMode { 9 /// Orientation and script detection only. 10 PsmOsdOnly, 11 /// Automatic page segmentation with orientation and script detection. (OSD) 12 PsmAutoOsd, 13 /// Automatic page segmentation, but no OSD, or OCR. 14 PsmAutoOnly, 15 /// Fully automatic page segmentation, but no OSD. 16 PsmAuto, 17 /// Assume a single column of text of variable sizes. 18 PsmSingleColumn, 19 /// Assume a single uniform block of vertically aligned text. 20 PsmSingleBlockVertText, 21 /// Assume a single uniform block of text. (Default.) 22 PsmSingleBlock, 23 /// Treat the image as a single text line. 24 PsmSingleLine, 25 /// Treat the image as a single word. 26 PsmSingleWord, 27 /// Treat the image as a single word in a circle. 28 PsmCircleWord, 29 /// Treat the image as a single character. 30 PsmSingleChar, 31 /// Find as much text as possible in no particular order. 32 PsmSparseText, 33 /// Sparse text with orientation and script det. 34 PsmSparseTextOsd, 35 /// Treat the image as a single text line, bypassing hacks that are Tesseract-specific. 36 PsmRawLine, 37} 38 39impl PageSegMode { 40 /// Get the page-seg-mode's value as used by Tesseract 41 pub fn as_tess_page_seg_mode(&self) -> TessPageSegMode { 42 match self { 43 PageSegMode::PsmOsdOnly => tesseract_sys::TessPageSegMode_PSM_OSD_ONLY, 44 PageSegMode::PsmAutoOsd => tesseract_sys::TessPageSegMode_PSM_AUTO_OSD, 45 PageSegMode::PsmAutoOnly => tesseract_sys::TessPageSegMode_PSM_AUTO_ONLY, 46 PageSegMode::PsmAuto => tesseract_sys::TessPageSegMode_PSM_AUTO, 47 PageSegMode::PsmSingleColumn => tesseract_sys::TessPageSegMode_PSM_SINGLE_COLUMN, 48 PageSegMode::PsmSingleBlockVertText => { 49 tesseract_sys::TessPageSegMode_PSM_SINGLE_BLOCK_VERT_TEXT 50 } 51 PageSegMode::PsmSingleBlock => tesseract_sys::TessPageSegMode_PSM_SINGLE_BLOCK, 52 PageSegMode::PsmSingleLine => tesseract_sys::TessPageSegMode_PSM_SINGLE_LINE, 53 PageSegMode::PsmSingleWord => tesseract_sys::TessPageSegMode_PSM_SINGLE_WORD, 54 PageSegMode::PsmCircleWord => tesseract_sys::TessPageSegMode_PSM_CIRCLE_WORD, 55 PageSegMode::PsmSingleChar => tesseract_sys::TessPageSegMode_PSM_SINGLE_CHAR, 56 PageSegMode::PsmSparseText => tesseract_sys::TessPageSegMode_PSM_SPARSE_TEXT, 57 PageSegMode::PsmSparseTextOsd => tesseract_sys::TessPageSegMode_PSM_SPARSE_TEXT_OSD, 58 PageSegMode::PsmRawLine => tesseract_sys::TessPageSegMode_PSM_RAW_LINE, 59 } 60 } 61} ``` -------------------------------- ### Tesseract::set_rectangle Source: https://docs.rs/tesseract/0.15.2/src/tesseract/lib.rs.html Sets a rectangular region of interest on the image. ```APIDOC ## Tesseract::set_rectangle ### Description Defines a rectangular region within the current image to focus OCR processing on. This can be used to process only a specific part of an image. ### Method `Tesseract::set_rectangle` ### Parameters - `left` (i32): The x-coordinate of the top-left corner of the rectangle. - `top` (i32): The y-coordinate of the top-left corner of the rectangle. - `width` (i32): The width of the rectangle. - `height` (i32): The height of the rectangle. ### Returns - `Self`: The modified `Tesseract` instance. ```