如何用PHP从屏幕截图中获取字幕?

发布于 2024-12-25 13:37:11 字数 251 浏览 2 评论 0原文

我从电影截图中抓取字幕。 一个例子 在此处输入图像描述

它将抓取

嘿,我们为什么不放松一下呢?

与字幕无关。是截图。由于它是一个副标题,我们知道字体大小等,如果这会让它更容易抓取。

我知道你们大多数人都会说 PHP OCR 库,但由于背景总是不同,看起来它不起作用。

I grab subtitle from movie screenshot.
An example
enter image description here

It will grab

Hey, why don't we all just relax, huh?

It has no relation with subtitle. It is screenshot. Since it is a subtitle we know the font type size etc if this will make it easier to grab.

I know most of you will say PHP OCR library but since the background is always different, it looks like it won't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2025-01-01 13:37:11

背景不同应该不是问题,您可以使用图像库来删除任何不是文本颜色的内容。

这是一个简单的示例,可以很好地理解我的意思,它将任何低于 #f5f5f5 的颜色替换为 #000000

<?php
$im = imagecreatefromjpeg("img.jpg");

for ($x = imagesx($im); $x--;) 
{
    for ($y = imagesy($im); $y--;) 
    {
        $rgb = imagecolorat($im, $x, $y);
        
        if ((($rgb >> 16) & 0xFF) <= 245 
            && (($rgb >> 8) & 0xFF) <= 245 
            && ($rgb & 0xFF) <= 245) 
        {
            $black = imagecolorallocate($im, 0, 0, 0);
            imagesetpixel($im, $x, $y, $black);
        }
    }
}

header("Content-Type: image/jpeg");
imagejpeg($im);

如下:

结果 顶部的大部分内容都已关闭,因为您知道字幕将位于底部。然后只需通过 OCR 库运行它即可。

最好使用外部 OCR 库或命令行工具并从 PHP 调用它。对于外部工具,有 tesseractocropus (我相信 ocropus 也是由 Google 赞助的)。

The background being different shouldn't be a problem, you can just use an image library to remove anything that isn't the text colour.

Here's a quick example that gives a decent idea of what I mean, it replaces any colour lower than #f5f5f5 with #000000,

<?php
$im = imagecreatefromjpeg("img.jpg");

for ($x = imagesx($im); $x--;) 
{
    for ($y = imagesy($im); $y--;) 
    {
        $rgb = imagecolorat($im, $x, $y);
        
        if ((($rgb >> 16) & 0xFF) <= 245 
            && (($rgb >> 8) & 0xFF) <= 245 
            && ($rgb & 0xFF) <= 245) 
        {
            $black = imagecolorallocate($im, 0, 0, 0);
            imagesetpixel($im, $x, $y, $black);
        }
    }
}

header("Content-Type: image/jpeg");
imagejpeg($im);

Here's how the result looks:

You can probably chop most of the top part off since you know the subtitles will be at the bottom. Then just run it through an OCR library.

It's probably better to use an external OCR library or command line tool and call it from PHP. For external tools, there's tesseract and ocropus (I believe ocropus is sponsored by Google too).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文