精易论坛

标题: 调用Umi-OCR的API配合PHP+HTML写了简单图片识别文字 [打印本页]

作者: 精易如家酒店ジ    时间: 2024-6-28 21:06
标题: 调用Umi-OCR的API配合PHP+HTML写了简单图片识别文字
本帖最后由 精易如家酒店ジ 于 2024-6-28 21:10 编辑

Umi-OCR:https://125.confly.eu.org/forum.php?mod=viewthread&tid=14825440&highlight=umi
Umi-OCR开发文档:https://github.com/hiroi-sora/Umi-OCR/blob/main/docs/README_HTTP.md#1-%E5%9B%BE%E7%89%87ocrbase64-%E8%AF%86%E5%88%AB%E6%8E%A5%E5%8F%A3

截图OCR:打开这一页后,就可以用快捷键唤起截图,识别图中的文字。


用PHP+HTML写了一个前端识别,通过api/ocr的接口进行本地HTTP进行图片识别文字。·可以把Umi放在服务器,然后把“http://127.0.0.1:1224/api/ocr”换成你的服务器地址+端口。

需要把主机的地址选择任何可用地址!


[PHP] 纯文本查看 复制代码
<!DOCTYPE html>
<html>

<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
        }

        h1 {
            color: #333;
        }

        label {
            display: block;
            margin: 20px auto;
            cursor: pointer;
        }

        #imageDisplay {
            max-width: 300px;
            max-height: 300px;
            margin: 10px auto;
        }

        #fileInput {
            display: none;
        }

        #textOutput {
            margin: 20px auto;
            padding: 10px;
            width: 80%;
            max-width: 500px;
            resize: none;
        }

        button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }
    </style>
</head>

<body>
    <h1>图像文字识别</h1>
    <label for="fileInput">
        <img id="imageDisplay" alt="Selected Image">
        <br>
        <input type="file" id="fileInput" accept="image/*">
        <br>
        <span>选择图片</span>
    </label>
    <br>
    <textarea id="textOutput" rows="4" cols="50" placeholder="识别结果将显示在这里"></textarea>
    <br>
    <button>一键复制</button>

    <script>
        // 监听文件输入框的变化,读取选中的图片并显示在页面上
        document.getElementById('fileInput').addEventListener('change', function () {
            var file = this.files[0];
            var reader = new FileReader();
            reader.onload = function (e) {
                document.getElementById('imageDisplay').src = e.target.result;
                var imageData = e.target.result.split(',')[1]; // 提取base64数据
                sendDataToServer(imageData);
            };
            reader.readAsDataURL(file);
        });

        // 将图片数据发送到服务器进行OCR识别,并将识别结果显示在文本框中
        function sendDataToServer(imageData) {
            var data = {
                'base64': imageData,
                'options': {
                    "tbpu.parser": "none",
                    "data.format": "txet",
                    "ocr.angle": false,
                    "ocr.language": "简体中文",
                    "ocr.maxSideLen": 1024
                }
            };
            var dataJson = JSON.stringify(data);

            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://127.0.0.1:1224/api/ocr', true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var response = JSON.parse(xhr.responseText);
                    var texts = response.data.map(function (item) {
                        return item.text;
                    });
                    document.getElementById('textOutput').value = texts.join('\n');
                }
            };
            xhr.send(dataJson);
        }

        // 复制文本框中的文本到剪贴板
        function copyText() {
            var textOutput = document.getElementById('textOutput');
            textOutput.select();
            document.execCommand('copy');
        }
    </script>
</body>

</html>



作者: 花老板    时间: 2024-6-29 01:04
很不错, 标准的微软winhttp文档接口,ajax原始封装
作者: yuan71058    时间: 2024-6-29 07:57
这个ocr占CPU太高 放弃了
作者: year1970    时间: 2024-6-29 08:23
感谢分享
作者: 一指温柔    时间: 2024-6-29 09:30
支持开源~!感谢分享
作者: 396384183    时间: 2024-6-29 09:34
感谢分享,很给力!
作者: 小虎来了    时间: 2024-6-29 10:22
感谢分享。谢谢
作者: kyo9766    时间: 2024-6-29 13:25
学习一下html 感谢分享
作者: Conquer    时间: 2024-6-29 20:02
感谢分享。谢谢
作者: bianyuan456    时间: 2024-6-29 20:21
已经顶贴,感谢您对论坛的支持!
作者: jtucar    时间: 2024-6-30 06:13
感谢分享,很给力!~
作者: please    时间: 2024-6-30 09:38
感谢分享,支持开源!!!
作者: JYYeah    时间: 2024-6-30 17:55
支持开源~!感谢分享!
作者: shaojiecn    时间: 2024-7-3 01:17

谢谢,楼主分享
作者: aosheng    时间: 2024-7-3 10:21
6666666666666666666666
作者: lyjzyq    时间: 2024-7-5 10:14
666666666666666666666666666666666
作者: 守幼金成    时间: 2024-7-10 10:06
这个有用 谢谢
作者: Abstract009    时间: 2024-8-30 13:49
666666666666666666666666666666666666666666666666666666666666666666666666666666
作者: 小月虫    时间: 2025-3-10 11:23
666666666666666
作者: dikexu    时间: 2025-3-13 03:22

这个有用 谢谢




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4