基于百度OCR的图片文字识别
先上图,有图有真相

首先在百度开通ORC服务,目前是免费的,普通识别每天50000次免费,非常棒!
百度文档:http://ai.baidu.com/docs#/OCR-API/top
下载百度SDK神马的就不多说了,需要包含CURL和JSON库,注意版本要求
windows下的openssl 32位和64位一键安装包顺便分享下,自己安装太麻烦
链接:https://pan.baidu.com/s/1HAuplB3deQGFk2eO8zC13A
提取码:mh34
CURL和JSON库就不贴出来了,网上随便都能找到,需要的朋友可以找我,我私发给你。
接下来进入正题,贴代码:
ImageRecogition.h
#pragma once #include "json/json.h" class CImageRecogition
{
public:
CImageRecogition();
~CImageRecogition(); public:
/*accurate_basic*/
Json::Value static accurate_basic(std::string szFile);
/*general_basic*/
Json::Value static general_basic(std::string szFile);
/*general_enhanced*/
Json::Value static general_enhanced(std::string szFile);
/*receipt*/
Json::Value static receipt(std::string szFile);
/*custom*/
Json::Value static custom(std::string szFile); /*save result to file*/
void static SaveResultToFile(Json::Value & result, std::string szFile);
private:
};
ImageRecogition.cpp
#include "stdafx.h"
#include "ImageRecogition.h" #include "baiduapi/ocr.h" #define APP_ID "xxxxxxx"
#define API_KEY "xxxxxxx"
#define SECRET_KEY "xxxxx" CImageRecogition::CImageRecogition()
{ } CImageRecogition::~CImageRecogition()
{ } Json::Value CImageRecogition::accurate_basic(std::string szFile)
{
aip::Ocr client(APP_ID, API_KEY, SECRET_KEY); std::string image;
aip::get_file_content(szFile.c_str(), &image);
std::map<std::string, std::string> options;
options["detect_direction"] = "true";
options["probability"] = "true"; std::cout << "高精识别开始:";
return client.accurate_basic(image, options);
} Json::Value CImageRecogition::general_basic(std::string szFile)
{
aip::Ocr client(APP_ID, API_KEY, SECRET_KEY); std::string image;
aip::get_file_content(szFile.c_str(), &image);
std::map<std::string, std::string> options;
//options["language_type"] = "KOR";
options["detect_direction"] = "true";
options["detect_language"] = "true";
options["probability"] = "true"; std::cout << "普通识别开始:";
return client.general_basic(image, options);
} Json::Value CImageRecogition::general_enhanced(std::string szFile)
{
aip::Ocr client(APP_ID, API_KEY, SECRET_KEY); std::string image;
aip::get_file_content(szFile.c_str(), &image);
std::map<std::string, std::string> options;
//options["language_type"] = "KOR";
options["detect_direction"] = "true";
options["detect_language"] = "true";
options["probability"] = "true"; std::cout << "生僻字识别开始:";
return client.general_enhanced(image, options);
} Json::Value CImageRecogition::receipt(std::string szFile)
{
aip::Ocr client(APP_ID, API_KEY, SECRET_KEY); std::string image;
aip::get_file_content(szFile.c_str(), &image);
std::map<std::string, std::string> options;
//options["recognize_granularity"] = "small";
options["probability"] = "true";
//options["accuracy"] = "normal";
options["detect_direction"] = "true"; std::cout << "通用票据识别开始:";
return client.receipt(image, options);
} Json::Value CImageRecogition::custom(std::string szFile)
{
aip::Ocr client(APP_ID, API_KEY, SECRET_KEY); std::string image;
aip::get_file_content(szFile.c_str(), &image);
std::map<std::string, std::string> options;
std::string templateSign = "354b9b4fd9b0e4b38aedb8096260c6de"; std::cout << "自定义模板识别开始:";
return client.custom(image, templateSign, options);
} void CImageRecogition::SaveResultToFile(Json::Value & result, std::string szFile)
{
FILE* fp = nullptr;
auto error_no = fopen_s(&fp, szFile.c_str(), "a+");
if (fp != nullptr)
{
fprintf_s(fp, "==============================================================================\n");
SYSTEMTIME st;
::GetLocalTime(&st);
fprintf_s(fp, "%04d-%02d-%02d %02d:%02d:%02d\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
fprintf_s(fp, "log_id : %d\n", result["log_id"].asInt64()); auto result_num = result["words_result"].size(); if (result_num > )
std::cout << "识别成功,识别行数:" << result_num << std::endl;
else
std::cout << "识别失败,未识别数据" << std::endl; for (int i = ; i < result_num; ++i)
fprintf_s(fp, "%s\n", result["words_result"][i]["words"].asString().c_str()); fprintf_s(fp, "==============================================================================\n");
fprintf_s(fp, "\n");
fclose(fp);
}
}
然后就是main函数的调用了
#include "stdafx.h"
#include <iostream> #include "ServiceLogic/ImageRecogition.h"
#include "curl/curl.h"
#include "DirFile/DirFile.h" int _tmain(int argc, char* argv[])
{
std::vector<std::string> vecFiles;
DirFile::ListFiles(".", vecFiles, FILETYPE_JPG | FILETYPE_PNG | FILETYPE_JPEG);
for (auto it : vecFiles)
{
std::cout << it << std::endl;
std::string szFileName = it.substr(, it.rfind("."));
std::string szPath = "./Log_" + szFileName + "/";
DirFile::CreatePath(szPath); //CImageRecogition::SaveResultToFile(CImageRecogition::accurate_basic(it), szPath + "accurate_basic.log");
CImageRecogition::SaveResultToFile(CImageRecogition::general_basic(it), szPath + "general_basic.log");
//CImageRecogition::SaveResultToFile(CImageRecogition::general_enhanced(it), szPath + "general_enhanced.log");
//CImageRecogition::SaveResultToFile(CImageRecogition::receipt(it), szPath + "receipt.log");
//CImageRecogition::SaveResultToFile(CImageRecogition::custom(it), szPath + "custom.log");
} system("pause");
return ;
}
DirFile.h是遍历当前目录下对应格式的文件,以及Log目录的创建的,这里就不贴出来了,毕竟不是这次的主题。
百度ORC的识别率,对图片要求还是比较高的,本人给朝鲜族的朋友测试韩文的发票,效果还是挺差强人意的。看大家的需求吧。
基于百度OCR的图片文字识别的更多相关文章
- 【图片识别】java 图片文字识别 ocr (转)
http://www.cnblogs.com/inkflower/p/6642264.html 最近在开发的时候需要识别图片中的一些文字,网上找了相关资料之后,发现google有一个离线的工具,以下为 ...
- java 图片文字识别 ocr
最近在开发的时候需要识别图片中的一些文字,网上找了相关资料之后,发现google有一个离线的工具,以下为java使用的demo 在此之前,使用这个工具需要在本地安装OCR工具: 下面一个是一定要安装的 ...
- 一篇文章搞定百度OCR图片文字识别API
一篇文章搞定百度OCR图片文字识别API https://www.jianshu.com/p/7905d3b12104
- 小试Office OneNote 2010的图片文字识别功能(OCR)
原文:小试Office OneNote 2010的图片文字识别功能(OCR) 自Office 2003以来,OneNote就成为了我电脑中必不可少的软件,它集各种创新功能于一身,可方便的记录下各种类型 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 18—Photo OCR 应用实例:图片文字识别
Lecture 18—Photo OCR 应用实例:图片文字识别 18.1 问题描述和流程图 Problem Description and Pipeline 图像文字识别需要如下步骤: 1.文字侦测 ...
- [C13] 应用实例:图片文字识别(Application Example: Photo OCR)
应用实例:图片文字识别(Application Example: Photo OCR) 问题描述和流程图(Problem Description and Pipeline) 图像文字识别应用所作的事是 ...
- python3 图片文字识别
最近用到了图片文字识别这个功能,从网上搜查了一下,决定利用百度的文字识别接口.通过测试发现文字识别率还可以.下面就测试过程简要说明一下 1.注册用户 链接:https://login.bce.baid ...
- 刚破了潘金莲的身份信息(图片文字识别),win7、win10实测可用(免费下载)
刚破了潘金莲的身份信息(图片文字识别),win7.win10实测可用 效果如下: 证照,车牌.身份证.名片.营业执照 等图片文字均可识别 电脑版 本人出品 大小1.3MB 下载地址:https://p ...
- Python人工智能之图片识别,Python3一行代码实现图片文字识别
1.Python人工智能之图片识别,Python3一行代码实现图片文字识别 2.tesseract-ocr安装包和中文语言包 注意:
随机推荐
- SpringBoot 2.x (14):WebFlux响应式编程
响应式编程生活案例: 传统形式: 一群人去餐厅吃饭,顾客1找服务员点餐,服务员把订单交给后台厨师,然后服务员等待, 当后台厨师做好饭,交给服务员,经过服务员再交给顾客1,依此类推,该服务员再招待顾客2 ...
- Java中的Serializable接口和transient关键字
Java中的Serializable接口和transient关键字 Table of Contents 1. 向memcached中放数据时遇到NotSerializableException异常 2 ...
- iOS-Swift相比Objective-C有哪些优缺点
Swift,是苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Objective-C共同运行于Mac OS和iOS平台,用于搭建基于苹果平台的应用程序.它是一款易学易用的编程语言,而且 ...
- Easyui validatebox后台服务端验证
Easyui validatebox的验证提示十分好用,可是在实际项目的运用中,经常会遇到需要服务器验证后并返回验证结果信息,比如验证用户名.手机号.邮箱是否已存在.于是就想着怎么拓展Easyui的验 ...
- Selenium3+webdriver学习笔记3(xpath方式元素定位)
#!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriver import time,os # about:ad ...
- sqlserver创建触发器
Create TRIGGER [dbo].[tr_Delete_AllocationedDN] --删除指定账户已分配未拣货的任务 ON [dbo].[FRU_PickLocationNew] --触 ...
- 突然心血来潮,想写写我在java面试中遇到的事。作为一个应届生,我觉得我的情况都与大部分应届生是差不多的,希望你们能在这上面得到一些有用的
面试过程吧,怎么说呢?从一开始接触面试到现在成功了几家,这中间我确实收获了许多,那我就从我第一次面试开始讲吧. 第一次面试是有人介绍过来的,总之还是有一位贵人相助,所以第一次面试时,面试官很好没有怎么 ...
- [C++讨论课] 课堂记录(一)
今天第一次参加c++讨论课,记录下了各组同学的展示的问题或者解决方法,也有一些知识点上的内容,供以后复习参考. 1.常量指针和指针常量问题 常量指针:指向常量的指针,例如const int *p = ...
- JavaScript 获取对象中第一个属性
使用 Object.keys(object) 可以取出属性名为数组,但会打乱顺序 严格意义上对象中是只有映射关系而没有顺序的,但是在存储结构里是有顺序的,如果想获取存储结构里的第一个属性可以使用for ...
- Mysql command line
show databasename; use databasename; show tables; desc tablename;