javascript - 基于文本内容自动旋转图像 - PHP/JS

标签 javascript php ocr imagick

我希望能够根据文本内容自动旋转图像,以便正确(垂直)显示文本。我希望语言是 Javascript 或 PHP。

坏例子

enter image description here

正确的例子

enter image description here

比如 GIMP 和 PS 在导入图片时是这样的:

enter image description here

我如何使用 JS/PHP 准确地自动旋转图像,以便文本正确显示(垂直,如果你愿意的话)?

--注意--

我不想根据“EXIF 方向”数据旋转,而是根据图像中文本的方向旋转。显然,EXIF 数据仅跟踪照片相对于地面的拍摄方向。

最佳答案

我想到的一个可能的解决方案是使用 OCR 检测图像中的字符并在所有 4 个方向(除了原始方向之外,旋转 90 度 3 次)测试图像。返回最高匹配字符的位置可能是文本的正确方向。

可以使用以下 PHP 库:https://github.com/thiagoalessio/tesseract-ocr-for-php .配合 imagerotate(),可以根据从 OCR 返回的字符数量找出图像的最佳方向。

理论上

require_once '/path/to/TesseractOCR/TesseractOCR.php';

$filename='path/to/some/image.jpg';
$photo = // create photo from $filename
$results = array();

for ($i=0; $i<4; $i++) {
    $new = imagerotate($photo, $i*90, 0);
    $new_path = // save the new rotated photo and get path
    $tesseract = new TesseractOCR($new_path);
    $results[$i] = strlen($tesseract->recognize());
}

/* Highest output is the best orientation for the image in respects to the text in it */
echo "Original Orientation: " . $results[0];
echo "Rotated 90 degrees: " . $results[1];
echo "Rotated 180 degrees: " . $results[2];
echo "Rotated 270 degrees: " . $results[3];

优点 - 利用现有库(带 PHP 包装器的 Tesseract、imagerotate php 函数)

缺点 - 计算密集型。一张图片需要旋转3次&OCR 4次

关于javascript - 基于文本内容自动旋转图像 - PHP/JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30379570/

相关文章:

PHP artisan key :generate does not update . env 文件(AWS EC2 Ubuntu 上的 laravel 5.2)

python - 使用python提取Pytesseract外语

algorithm - 了解 OCR 的 Freeman 链码

javascript - 如何在异步调用中跟踪同步变量

javascript - 检查 IF 语句内的函数解释

PHP Laravel GraphQL 查询声明不兼容问题

mysql - 根据给定的词群,根据 OCR 错误自动构建可能词的输出列表

javascript - Node ,Express - 无法获取路线

javascript - 无法用js click()打开多个A

javascript - 创建实时价格计算表格