調用百度OCR API實現身份證文字識別


通過調用百度OCR的兩個接口,實現身份證圖像識別。

首先要在百度雲注冊賬號,並創建應用,以獲取AppID,API Key,Secret Key。

官網文字識別模塊地址:https://cloud.baidu.com/product/ocr.html

點擊文字識別

創建應用

 

 之后就可以使用相關接口了,這里我們使用的是身份證識別

附上身份證識別文檔地址:https://cloud.baidu.com/doc/OCR/OCR-API.html#.E8.BA.AB.E4.BB.BD.E8.AF.81.E8.AF.86.E5.88.AB

代碼:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <input type="file" id="img" onchange="getImg(event)" />
        <img id="showImg" src="" />
    </body>
    <script type="text/javascript" src="jquery-3.3.1.min.js" ></script>
    <script>
        var access_token = "這里填寫你的access_token";
        // 監聽圖片選擇事件
        function getImg (event) {
            var imageBase = "";
            var reader = new FileReader();
            reader.readAsDataURL(event.target.files[0]);
            reader.onload = function (e) {
                imageBase = e.target.result.replace("data:image/png;base64,","");
                $("#showImg").prop("src", "data:image/png;base64," + imageBase);
                $.ajax({
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    type: "post",
                    url: "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard",
                    async: true,
                    data: {
                        access_token: access_token,
                        id_card_side: "front",
                        image: imageBase
                    },
                    dataType: "json",
                    timeout: 30000,
                    success: function (data) {
                        console.log("解析成功");
                        console.log(data);
                    },
                    error: function (xhr) {
                        console.log("請求解析失敗");
                    }
                });
            }
        }
    </script>
</html>

最終效果圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM