代码仓库 Hyperf tesseract-ocr-for-phpphp
composer create hyperf/biz-skeleton tesseract-demo
复制代码
composer require thiagoalessio/tesseract_ocr
复制代码
首先,咱们编写接口单元测试。当没有传 url
时,错误码返回 1000
,当传入图片地址后,返回对应的识别信息。git
<?php
declare(strict_types=1);
/** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://doc.hyperf.io * @contact group@hyperf.io * @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE */
namespace HyperfTest\Cases;
use HyperfTest\HttpTestCase;
/** * @internal * @coversNothing */
class ImageTest extends HttpTestCase {
public function testImageReadFailed() {
$res = $this->json('/image/read');
$this->assertSame(1000, $res['code']);
}
public function testImageRead() {
$res = $this->json('/image/read', [
'url' => 'https://raw.githubusercontent.com/thiagoalessio/tesseract-ocr-for-php/master/tests/EndToEnd/images/8055.png',
]);
$this->assertSame(0, $res['code']);
$this->assertSame('8055', $res['data']);
}
}
复制代码
由于咱们要使用的 thiagoalessio/tesseract_ocr
库不肯定是否能够进行协程调度,因此咱们把具体逻辑放到 Task 中执行。github
composer require hyperf/task
复制代码
改造咱们的 server.php
docker
<?php
declare(strict_types=1);
use Hyperf\Server\SwooleEvent;
return [
// 这里省略了其它不相关的配置项
'settings' => [
// Task Worker 数量,根据您的服务器配置而配置适当的数量
'task_worker_num' => 8,
// 由于 `Task` 主要处理没法协程化的方法,因此这里推荐设为 `false`,避免协程下出现数据混淆的状况
'task_enable_coroutine' => false,
],
'callbacks' => [
// Task callbacks
SwooleEvent::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
SwooleEvent::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
],
];
复制代码
实现逻辑,很是简单,咱们把网图保存到本地,而后经过 tesseract
来读取便可。json
<?php
declare(strict_types=1);
/** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://doc.hyperf.io * @contact group@hyperf.io * @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE */
namespace App\Service;
use Hyperf\Guzzle\ClientFactory;
use Hyperf\Task\Annotation\Task;
use thiagoalessio\TesseractOCR\TesseractOCR;
class ImageService {
public function read(string $url) {
$path = $this->save($url);
return $this->tesseract($path);
}
/** * @Task */
public function tesseract(string $path) {
return (new TesseractOCR($path))->run();
}
protected function save(string $url): string {
$client = di()->get(ClientFactory::class)->create();
$content = $client->get($url)->getBody()->getContents();
$path = BASE_PATH . '/runtime/' . uniqid();
file_put_contents($path, $content);
return $path;
}
}
复制代码
控制器代码以下bash
<?php
declare(strict_types=1);
/** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://doc.hyperf.io * @contact group@hyperf.io * @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE */
namespace App\Controller;
use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Service\ImageService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
/** * @AutoController(prefix="/image") */
class ImageController extends Controller {
/** * @Inject * @var ImageService */
protected $service;
public function read() {
$url = $this->request->input('url');
if (empty($url)) {
throw new BusinessException(ErrorCode::PARAMS_INVALID);
}
$result = $this->service->read($url);
return $this->response->success($result);
}
}
复制代码
$ composer test
> co-phpunit -c phpunit.xml --colors=always
Scanning ...
Scan completed.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Di\Listener\BootApplicationListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Paginator\Listener\PageResolverListener listener.
PHPUnit 7.5.14 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 1.11 seconds, Memory: 42.00 MB
OK (3 tests, 13 assertions)
复制代码
固然,也能够经过 CURL 访问,查看结果服务器
$ curl http://127.0.0.1:9501/image/read -H 'Content-Type:application/json' -d '{"url":"https://raw.githubusercontent.com/thiagoalessio/tesseract-ocr-for-php/master/tests/EndToEnd/images/8055.png"}'
{"code":0,"data":"8055"}%
复制代码
由于默认的 Docker环境
没有 tesseract
,因此咱们修改一下 Dockerfile
php7
# Default Dockerfile
#
# @link https://www.hyperf.io
# @document https://doc.hyperf.io
# @contact group@hyperf.io
# @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
FROM hyperf/hyperf:7.2-alpine-cli
LABEL maintainer="Hyperf Developers <group@hyperf.io>" version="1.0" license="MIT"
##
# ---------- env settings ----------
##
# --build-arg timezone=Asia/Shanghai
ARG timezone
ENV TIMEZONE=${timezone:-"Asia/Shanghai"} \
COMPOSER_VERSION=1.8.6 \
APP_ENV=prod
# update
RUN set -ex \ && apk update \ # 安装 tesseract-ocr && apk add tesseract-ocr \ # install composer && cd /tmp \
&& wget https://github.com/composer/composer/releases/download/${COMPOSER_VERSION}/composer.phar \
&& chmod u+x composer.phar \
&& mv composer.phar /usr/local/bin/composer \
# show php version and extensions
&& php -v \
&& php -m \
# ---------- some config ----------
&& cd /etc/php7 \
# - config PHP
&& { \
echo "upload_max_filesize=100M"; \
echo "post_max_size=108M"; \
echo "memory_limit=1024M"; \
echo "date.timezone=${TIMEZONE}"; \
} | tee conf.d/99-overrides.ini \
# - config timezone
&& ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
&& echo "${TIMEZONE}" > /etc/timezone \
# ---------- clear works ----------
&& rm -rf /var/cache/apk/* /tmp/* /usr/share/man \
&& echo -e "\033[42;37m Build Completed :).\033[0m\n"
WORKDIR /opt/www
COPY . /opt/www
RUN composer install --no-dev \ && composer dump-autoload -o \ && php /opt/www/bin/hyperf.php di:init-proxy
EXPOSE 9501
ENTRYPOINT ["php", "/opt/www/bin/hyperf.php", "start"]
复制代码
docker build -t tesseract-demo .
复制代码
docker run --rm -p 9501:9501 -d --name tesseract-demo tesseract-demo
复制代码
$ curl http://127.0.0.1:9501/image/read -H 'Content-Type:application/json' -d '{"url":"https://raw.githubusercontent.com/thiagoalessio/tesseract-ocr-for-php/master/tests/EndToEnd/images/8055.png"}'
{"code":0,"data":"8055"}%
复制代码