public function __construct(Manager $manager, array $config) { parent::__construct($manager, $config); $this->rules = Manager::readConfig($config, self::CFG_RULES); $this->button = Manager::readConfig($config, self::CFG_BUTTON); Screen::assertRules($this->rules); Position::assertRect($this->button); }
public static function assertDialogRule(array &$dialog) { if (!isset($dialog[self::DIALOG_JUDGE]) || !isset($dialog[self::DIALOG_BUTTON])) { throw new \InvalidArgumentException('Rules should be an array.'); } Screen::assertRules($dialog[self::DIALOG_JUDGE]); Position::assertRect($dialog[self::DIALOG_BUTTON]); }
public function __construct(Manager $manager, array $config) { parent::__construct($manager, $config); $this->setRetryLimit(Manager::readConfig($config, Manager::RES_CONFIG_RETRY, self::DEFAULT_RETRY_LIMIT)); $this->setRetryDelay(Manager::readConfig($config, Manager::RES_CONFIG_RETRY, self::DEFAULT_RETRY_DELAY)); $this->retryOffset = intval($this->retryDelay / 10); $this->position = Position::instance($manager, $this->app); $this->delay = Delay::instance($manager, $this->app); $this->adb = ADB::instance($manager, $this->app); $this->scr = Screen::instance($manager, $this->app); }
public function run(array $context = []) { $ocr = Number::instance($this->manager, $this->app); $align = Manager::readConfig($context, 'align', 'L'); $mode = Manager::readConfig($context, 'scan', Number::SCAN_FIXED); $rule = Manager::readConfig($context, 'rule', 'Test'); $color = Manager::readConfig($context, 'color', '000000'); $rect = $align == 'L' ? Position::makeRectangle(0, 0, 1.0, 1.0) : Position::makeRectangle(1.0, 1.0, 0, 0); $result = $ocr->ocr($rule, $rect, [Number::CFG_COLOR => $color, Number::CFG_SCAN_MODE => $mode, Number::CFG_WIDTH => 0.25, Number::CFG_MARGIN => 0]); $this->logger->info('OCR Result %s', [$result]); return Manager::RET_LOOP; }
public function run(array $context = []) { $ocr = Number::instance($this->manager, $this->app); $point = Manager::readConfig($context, 'point'); Position::assertPoint($point); $this->logger->info('Test for X=%.4f, Y=%.4f', $point[Position::X], $point[Position::Y]); $ocr->setRules(['Test' => ['J' => [$point], 'T' => 1, 'F' => 0]]); $rect = Position::makeRectangle(0, 0, 1, 1); for ($i = 0; $i <= 9; $i++) { $this->screen->load(sprintf('tests/digits/%u.png', $i), Screen::PORTRAIT); $this->logger->info('OCR Result for digit %u is %s', [$i, $ocr->ocr('Test', $rect, [Number::CFG_COLOR => '000000', Number::CFG_WIDTH => 1, Number::CFG_MARGIN => 0])]); } return Manager::RET_LOOP; }
public static function getRectVertex(array &$rect) { Position::assertRect($rect); return [Position::makePoint($rect[Position::X1], $rect[Position::Y1]), Position::makePoint($rect[Position::X2], $rect[Position::Y2])]; }
public function ocr($rule, array &$rect, array $override = []) { if (!isset($this->rules[$rule])) { throw new \InvalidArgumentException("Undefined OCR rule '{$rule}'."); } $this->rule =& $this->rules[$rule]; $this->setConfig($override); $this->align = self::getRectAlign($rect); $this->result = ''; if ($this->mode === self::SCAN_FIXED) { $minX = min($rect[Position::X1], $rect[Position::X2]); $maxX = max($rect[Position::X1], $rect[Position::X2]); if ($this->width == 0) { throw new \InvalidArgumentException('OCR Width is not initialized or passed.'); } $scanAt = $this->align == self::ALIGN_LEFT ? $minX : $maxX - $this->width; $this->logger->debug('Ready to OCR with fixed stepping, Align=%s, Range=%.4f-%.4f, Start=%.4f, Width=%.4f, Margin=%.4f, Step=%.4f', [$this->align, $minX, $maxX, $scanAt, $this->width, $this->margin, $this->ocrStep()]); while ($scanAt >= $minX && $scanAt + $this->width <= $maxX) { $ocrRect = Position::makeRectangle($scanAt, $rect[Position::Y1], $scanAt + $this->width, $rect[Position::Y2]); $char = $this->ocrChar($this->rule, $ocrRect); if (is_null($char) && $this->ocrFailure() === Manager::RET_FINISH) { break; } $this->ocrConcat($char); $scanAt += $this->ocrStep(); $this->logger->debug('Forward=%.4f', [$scanAt]); } } elseif ($this->mode === self::SCAN_ADAPTIVE) { $this->screen->translateRect($rect); $this->scanCache = []; $minX = min($rect[Position::X1], $rect[Position::X2]); $maxX = max($rect[Position::X1], $rect[Position::X2]); $minY = min($rect[Position::Y1], $rect[Position::Y2]); $maxY = max($rect[Position::Y1], $rect[Position::Y2]); $scanAt = $rect[Position::X1]; $step = $this->align === self::ALIGN_LEFT ? 1 : -1; $this->logger->debug('Ready to OCR with adaptive width. Align=%s, Range=%u-%u, Step=%d', [$this->align, $minX, $maxX, $step]); while ($scanAt >= $minX && $scanAt <= $maxX) { $Y1 = $maxY; $Y2 = $minY; // Get X1 while ($scanAt >= $minX && $scanAt <= $maxX) { if ($Y1 = $this->searchInLine(Position::Y, $scanAt, $minY, $maxY)) { break; } $scanAt += $step; } $X1 = $scanAt; // Get X2, Y1 while ($scanAt >= $minX && $scanAt <= $maxX && ($foundY = $this->searchInLine(Position::Y, $scanAt, $minY, $maxY))) { if ($foundY < $Y1) { $Y1 = $foundY; } $scanAt += $step; } $X2 = $scanAt; if ($scanAt < $minX || $scanAt > $maxX) { break; } // Get Y2 for ($scanY = $maxY; $scanY > $Y1; $scanY--) { if ($this->searchInLine(Position::X, $scanY, $X1, $X2)) { $Y2 = $scanY; break; } } // OCR $ocrRect = Position::makeRectangle($X1, $Y1, $X2, $Y2); $this->logger->info('Possible character from (%u,%u) to (%u,%u)', [$X1, $Y1, $X2, $Y2]); $char = $this->ocrChar($this->rule, $ocrRect); if (is_null($char) && $this->ocrFailure() === Manager::RET_FINISH) { break; } $this->ocrConcat($char); $scanAt += $step; $this->logger->debug('Forward=%u', [$scanAt]); } } else { throw new \LogicException("OCR scan mode is exceptional."); } return $this->ocrComplete($this->result); }
public function swipeLine($line) { Position::assertRect($line); return $this->swipe($line[Position::X1], $line[Position::Y1], $line[Position::X2], $line[Position::Y2]); }
public static function assertRules(array $rules) { foreach ($rules as $color => $positions) { if (!preg_match(self::REGEXP_COLOR, $color)) { throw new \InvalidArgumentException("Invalid color notation '{$color}'."); } if (!is_array($positions)) { throw new \InvalidArgumentException('Points array in rule entry does not exist.'); } foreach ($positions as $position) { try { Position::assertPoint($position); } catch (\Exception $e) { Position::assertRect($position); } } } return true; }