欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot+OCR?實現(xiàn)圖片文字識別

 更新時間:2021年12月17日 11:26:15   作者:ripen、梓玖  
本文主要介紹了SpringBoot+OCR 實現(xiàn)圖片文字識別,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本篇介紹的是基于百度人工智能接口的文字識別實現(xiàn)。

1. 注冊百度云,獲得AppID

此處百度云非百度云盤,而是百度智能云。

大家可進入https://cloud.baidu.com/ 自行注冊,這里就不多說了。

接下來,我們進行應用的創(chuàng)建

第一步

第二步

所需接口根據(jù)實際勾選,我們暫時只需前四個即可。

第三步

2. 日常demo操作

pom.xml:

<dependencies>
    <!-- 百度人工智能依賴 -->
    <!-- https://mvnrepository.com/artifact/com.baidu.aip/java-sdk -->
    <dependency>
        <groupId>com.baidu.aip</groupId>
        <artifactId>java-sdk</artifactId>
        <version>4.11.3</version>
    </dependency>
    <!-- 對象轉換成json -->
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.8</version>
    </dependency>
</dependencies>

JsonChange.class:(json處理工具類)

public class JsonChange {

    /**
     * json字符串轉換為map
     */
    public static <T> Map<String, Object> json2map(String jsonString) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return mapper.readValue(jsonString, Map.class);
    }

}

OcrController.class:
AipOcr client = new AipOcr(“AppID”, “API Key”, “Secret Key”) 切記換成剛剛創(chuàng)建的應用的AppID,而且三個參數(shù)均是String類型。

@RestController
public class OcrController {

    @PostMapping(value = "/ocr")
    public Map<Object, Object> ocr(MultipartFile file) throws Exception {
        AipOcr client = new AipOcr("AppID", "API Key", "Secret Key");
        // 傳入可選參數(shù)調(diào)用接口
        HashMap<String, String> options = new HashMap<String, String>(4);
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");

        // 參數(shù)為二進制數(shù)組
        byte[] buf = file.getBytes();
        JSONObject res = client.basicGeneral(buf, options);

        Map map = JsonChange.json2map(res.toString());
        return map;
    }
    
}

如果只想要識別出來的文字即可,可加入

//  提取并打印出識別的文字
List list = (List) map.get("words_result");
int len = ((List) map.get("words_result")).size();
for(int i=0; i<len; i++) {
    str = str + ((Map) list.get(i)).get("words") + "\n";
}

接下來 postman 測試

識別的全部信息

ocr識別出的全部數(shù)據(jù)輸出

提取識別的文字

提取其中識別的文字,剔除其他信息

源碼下載

到此這篇關于SpringBoot+OCR 實現(xiàn)圖片文字識別的文章就介紹到這了,更多相關SpringBoot OCR 圖片文字識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論