TTMSFNCWXOCR
Component that recognizes text in an image. This component uses Tesseract.js library.
Keep in mind
This component is not supported on Linux!
Having multiple instances of a WEB project using this component open, might cause out-of-memory crashes!
Language
Property to set the language to use for the OCR. There are over a 100 language packs available. You can see all available languages here: https://tesseract-ocr.github.io/tessdoc/Data-Files#datafiles-for-version-400-november-29-2016.
You can load multiple languages by combining them with a
+
. Example: eng+deu+ned
WorkerCount
This property sets the number of workers that will be initialized. Assigning more workers will let you
complete more OCR processes simultaneously. The number of workers is minimum 1
and is capped
at 15
. More than 15
of these workers, cause the application to go out of memory and crash. Please
note that the more workers are needed, the longer the initialization of the component is going to
take, and the more resources are required. When you start an OCR process the component will
assign it to a scheduler and will start the process the moment a worker is available.
Methods
There are 4 methods for starting a OCR on an image. The difference between both is the way that
the images are loaded. For getting the results of these methods it’s important to subscribe to the
OnFinishOCR
event which forwards the result. As the whole process is something that happens
asyncronous. Only PNG, JPG and JPEG are supported file formats for these methods. The results of
these 2 methods will also be added to a ResultList
which you can access like this:
TTMSFNCWXOCR.ResultList
. This list is of the type TObjectList
, so you can manipulate this list al
you want.
RecognizeURL
This method accepts a URL of an image somewhere on the internet and then starts the OCR Progress You can call this method like this:
RecognizeFile
This method accepts a TTMSFNCUtilsFile
which can be a local file and converts it to a base64 image
and then start the OCR progress.
For uploading a file in TMS Web Core you first have to convert from a TFile
to a TTMSFNCUtilsFile
.
Here is a snippet on how to do this using a TWebFilePicker
:
procedure TForm3.WebFilePicker1Change(Sender: TObject);
begin
TTMSFNCUtils.LoadFile(WebFilePicker1.Files[0].FileObject, @DoFileLoaded);
end;
procedure TForm3.DoFileLoaded(const AFile: TTMSFNCUtilsFile);
begin
TMSFNCWXOCR1.RecognizeFile(AFile);
end;
In VCL an FMX you can just use this:
OpenDialog1.InitialDir := GetCurrentDir;
OpenDialog1.Filter := 'Images |*.PNG;*.JPEG;*.JPG';
if OpenDialog1.Execute then
begin
Image1.Picture.LoadFromFile( OpenDIalog1.Files[0]);
TMSFNCWXOCR1.RecognizeFile(OpenDialog1.Files[0]);
end;
RecognizeBytes
This method accepts a TBytes
array and converts it to base64
and starts the OCR Progress.
RecognizeBase64
This method accepts a base64
string and starts the OCR Progress.
Terminate
This method cancels all processes and terminates all workers. This causes all workers to reinitialize as well.
Events
OnProgressOCR
This event triggers every time there is an update progress. This send a TTMSFNCWXOCRProgress
object along. This object contains a status, progress and a WorkerID
which can be used to show
status updates of the OCR Process.
procedure TForm3.TMSFNCWXOCR1ProgressOCR(Sender: TObject;
Progress: TTMSFNCWXOCRProgress);
begin
Label1.Text := Progress.Status + ': ';
ProgressBar1.Value := Progress.Progress * 100;
end;
OnFinishOCR
This Event Triggers when the OCR Progress has finished. This event exposes a TTMSFNCOCRResult
object.
This object contains all information needed for handling the OCR.
procedure TForm3.TMSFNCWXOCR1FinishOCR(Sender: TObject;
Result: TTMSFNCWXOCRResult);
begin
TMSFNCWXJSONFormatter1.JSON.Text := Result.JSONString;
WebMemo1.Text := Result.Text;
WebLabel2.caption := 'Confidence: ' + FloatToStr(Result.Confidence);
end;
OnOCRError
This event triggers when an error occurs during the OCR process. This error is forwarded as a string in the event listener. This error is a direct message from the library. This means that we have no influence on the messages sent.
OnOCRInitialized
This event triggers when all workers have been initialized. After this you can freely start OCR processes.
TTMSFNCOCRResult
Property name | Description |
---|---|
Text | The result in plain text |
HOCR | The result in HTML format |
TSV | The result in TSV format |
Confidence | Score of how certain the process is about this text |
Paragraphs | List of TTMSFNCParagraphs |
Lines | List of TTMSFNCLines |
Words | List of TTMSFNCWords |
JSONString | Full JSON of TTMSFNCOCRResult |
TTMSFNCOCRParagraph
Property name | Description |
---|---|
Text | Plain text in this paragraph |
Confidence | Score of how certain the process is about this paragraph |
Lines | List of TTMSFNCLines in this paragraph |
BBox | Object containing coordinates of where this paragraph is situated in the image. |
TTMSFNCWXOCRLine
Property name | Description |
---|---|
Text | Plain text of this line |
Confidence | Score of how certain the process is about this line |
Words | List of TTMSFNCWords in this line |
Bbox | Object containing the coordinates of where this line is situated in the image |
TTMSFNCWXOCRWord
Property name | Description |
---|---|
Text | Plain text of this word |
Confidence | Score of how certain the process is about this word |
BBox | Object containing the coordinates of where this word is situated in the image |