Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tesseract as optional OCR engine #5

Merged
merged 7 commits into from Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Expand Up @@ -14,6 +14,7 @@ If you need to make asset changes:
* `npm install`
* Add the missing values from `.env` to a `.env.local` file
* Use `https://vision.googleapis.com/` as the `APP_GOOGLE_CLOUD_VISION_ENDPOINT`, with your own [Cloud Vision API](https://cloud.google.com/vision) key as the `APP_GOOGLE_CLOUD_VISION_KEY`. Google gives you 1,000 free lookups per month.
* Install [Tesseract](https://tesseract-ocr.github.io) and make sure it's in your `$PATH`
* `symfony serve` to start the application
* `npm run dev-server` if you need to make JS/CSS changes.
* Stop the dev-server and run `npm run build` before committing.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
@@ -1,6 +1,6 @@
{
"name": "wikimedia/wikimedia-ocr",
"description": "A simple wrapper around the Google Cloud Vision API, enabling Wikisources to submit images for OCR and retrieve the resultant text.",
"description": "A simple wrapper around multiple OCR engines, enabling Wikisources to submit images for OCR and retrieve the resultant text.",
"type": "project",
"license": "GPL-3.0-or-later",
"require": {
Expand All @@ -16,6 +16,7 @@
"symfony/twig-bundle": "5.2.*",
"symfony/webpack-encore-bundle": "^1.11",
"symfony/yaml": "5.2.*",
"thiagoalessio/tesseract_ocr": "^2.11",
"twig/extra-bundle": "^2.12|^3.0",
"twig/twig": "^2.12|^3.0",
"wikimedia/toolforge-bundle": "^1.3",
Expand Down
152 changes: 114 additions & 38 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions config/services.yaml
Expand Up @@ -26,12 +26,10 @@ services:
resource: '../src/Controller/'
tags: ['controller.service_arguments']

App\Controller\OcrController:
App\Engine\GoogleCloudVisionEngine:
arguments:
- '@request_stack'
- '@Krinkle\Intuition\Intuition'
- '%env(APP_GOOGLE_CLOUD_VISION_ENDPOINT)%'
- '%env(APP_GOOGLE_CLOUD_VISION_KEY)%'
$endpoint: '%env(APP_GOOGLE_CLOUD_VISION_ENDPOINT)%'
$key: '%env(APP_GOOGLE_CLOUD_VISION_KEY)%'

# please note that last definitions always *replace* previous ones
# add more service definitions when explicit configuration is needed
9 changes: 7 additions & 2 deletions i18n/en.json
@@ -1,14 +1,19 @@
{
"title": "Wikisource Google OCR",
"title": "Wikimedia OCR",
"image-url": "Image URL:",
"image-url-help": "This must start with 'https://upload.wikimedia.org/' and be a full URL to an actual image file.",
"image-url-error": "Image URL must begin with '$1'",
"image-alt-text": "The original image",
"language-code": "Two-letter language code (optional):",
"language-code-help": "The ISO639 code of the language of the text in the image.",
"engine": "OCR engine:",
"engine-help": "Choose between the open-source Wikimedia-hosted Tesseract, and Google's Cloud Vision API.",
"engine-name-google": "Google",
"engine-name-tesseract": "Tesseract",
"submit": "Go",
"copy-to-clipboard": "Copy to clipboard",
"copied-to-clipboard": "Copied!",
"more-info": "For more information, see:",
"limit-exceeded": "Limit exceeded: $1"
"limit-exceeded": "Limit exceeded: $1",
"image-retrieval-failed": "Image retrieval failed: $1"
}
20 changes: 12 additions & 8 deletions src/Controller/OcrController.php
Expand Up @@ -3,7 +3,9 @@

namespace App\Controller;

use App\Engine\GoogleCloudVisionEngine;
use App\Engine\EngineBase;
use App\Engine\EngineFactory;
use App\Engine\TesseractEngine;
use App\Exception\OcrException;
use Krinkle\Intuition\Intuition;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand All @@ -17,7 +19,7 @@ class OcrController extends AbstractController
/** @var Intuition */
protected $intuition;

/** @var GoogleCloudVisionEngine */
/** @var EngineBase */
protected $engine;

/** @var mixed[] Output params for the view or API response. */
Expand All @@ -33,16 +35,18 @@ class OcrController extends AbstractController
* OcrController constructor.
* @param RequestStack $requestStack
* @param Intuition $intuition
* @param string $endpoint
* @param string $key
* @param EngineFactory $engineFactory
*/
public function __construct(RequestStack $requestStack, Intuition $intuition, string $endpoint, string $key)
public function __construct(RequestStack $requestStack, Intuition $intuition, EngineFactory $engineFactory)
{
$request = $requestStack->getCurrentRequest();

// Dependencies.
$this->intuition = $intuition;
$this->engine = new GoogleCloudVisionEngine($endpoint, $key);

$request = $requestStack->getCurrentRequest();

// Engine.
$this->engine = $engineFactory->get($request->get('engine', 'google'));
$this->params['engine'] = $this->engine instanceof TesseractEngine ? 'tesseract' : 'google';

// Parameters.
$this->imageUrl = (string)$request->query->get('image');
Expand Down