public static function run($dataDir = null) { # Initialize an instance of OcrEngine $ocr_engine = new OcrEngine(); # Retrieve the OcrConfig of the OcrEngine object $ocr_config = $ocr_engine->getConfig(); # Set the Whitelist property to recognize numbers only #ocr_config.setWhitelist(['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']) # Set the Image property by loading the image from file path location $imageStream = new ImageStream(); $ocr_engine->setImage($imageStream->fromFile($dataDir . 'ocr.png')); # Set the RemoveNonText to true $ocr_engine->getConfig()->setRemoveNonText(true); # Process the image if ($ocr_engine->process()) { $text = $ocr_engine->getText(); print "Text: " . (string) $text; $expression = "(\\d+)"; # Create a Pattern object $pattern = new Pattern(); $pattern = $pattern->compile($expression); # Now create matcher object $matcher = $pattern->matcher((string) $text); if ($matcher->find()) { print "Found value: " . (string) $matcher . PHP_EOL; //->group(0); } } }
public static function run($dataDir = null) { # Initialize an instance of OcrEngine $ocr_engine = new OcrEngine(); # Clear notifier list $ocr_engine->clearNotifies(); # Clear recognition blocks $ocr_engine->getConfig()->clearRecognitionBlocks(); # Add 2 rectangle blocks to user defined recognition blocks $recognitionBlock = new RecognitionBlock(); $ocr_engine->getConfig()->addRecognitionBlock($recognitionBlock->createTextBlock(52, 48, 67, 74)); $ocr_engine->getConfig()->addRecognitionBlock($recognitionBlock->createTextBlock(100, 46, 38, 46)); # Ignore everything else on the image other than the user defined recognition blocks $ocr_engine->getConfig()->setDetectTextRegions(false); # Set Image property by loading an image from file path $imageStream = new ImageStream(); $ocr_engine->setImage($imageStream->fromFile($dataDir . 'ocr.png')); # Run recognition process if ($ocr_engine->process()) { # Retrieve an array of recognized text by parts $text = $ocr_engine->getText()->getPartsInfo(); # Iterate over the text parts $i = 0; while ($i < sizeof($text)) { $info = $text[$i]; # Display part information print "Block: " . (string) $info->getBox() . " Text: " . (string) $info->getText() . PHP_EOL; $i += 1; } } }
public static function run($dataDir = null) { # Initialize an instance of OcrEngine $ocr_engine = new OcrEngine(); # Set the Image property by loading the image from file path location $imageStream = new ImageStream(); $ocr_engine->setImage($imageStream->fromFile($dataDir . 'ocr.png')); # Set filters # Create CorrectionFilters collection $filters = new CorrectionFilters(); # Initialize Median filter $filter = new MedianFilter(5); $filters->add($filter); # Create Gaussian Blur filter $filter = new GaussBlurFilter(); $filters->add($filter); # Create Noise Removal filter $filter = new RemoveNoiseFilter(); $filters->add($filter); # Assign collection to OcrEngine $ocr_engine->getConfig()->setCorrectionFilters($filters); # Perform OCR operation $ocr_engine->process(); print "Whole result is: " . (string) $ocr_engine->getText() . PHP_EOL; }
public static function run($dataDir = null) { # Initialize an instance of OcrEngine $ocr_engine = new OcrEngine(); # Set the Image property by loading the image from file path location $imageStream = new ImageStream(); $ocr_engine->setImage($imageStream->fromFile($dataDir . 'ocr.png')); # Set the DoSpellingCorrection to true $ocr_engine->getConfig()->setDoSpellingCorrection(true); # Process the image if ($ocr_engine->process()) { # Display the result print "Text: " . (string) $ocr_engine->getText() . PHP_EOL; } }