医疗检验报告单识别
更新时间:2022-11-15
接口描述
支持识别全国各地医疗检验报告单的姓名、性别、医院名称、报告单名称等关键字段,支持识别检查具体项目的项目名称、结果、单位、参考区间、结果提示等明细字段。
在线调试
您可以在 示例代码中心 中调试该接口,可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/medical_report_detection
URL参数:
参数 | 值 |
---|---|
access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
参数 | 值 |
---|---|
Content-Type | application/x-www-form-urlencoded |
Body中放置请求参数,参数详情如下:
请求参数
参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
---|---|---|---|---|
image | 和url二选一 | string | - | 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式 |
url | 和image二选一 | string | - | 图片完整url,url长度不超过1024字节,url对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式,当image字段存在时url字段失效请注意关闭URL防盗链 |
location | 否 | true/false | - | 是否返回字段的位置信息,默认为 false,可缺省- false:不返回字段位置信息- true:返回字段的位置信息,包括上边距(top)、左边距(left)、宽度(width)、高度(height) |
probability | 否 | true/false | - | 是否返回字段识别结果的置信度,默认为 false,可缺省- false:不返回字段识别结果的置信度- true:返回字段识别结果的置信度,包括字段识别结果中各字符置信度的平均值(average)和最小值(min) |
请求代码示例
提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。
提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。
1curl -i -k 'https://aip.baidubce.com/rest/2.0/ocr/v1/medical_report_detection?access_token=【调用鉴权接口获取的token】' --data 'image=【图片Base64编码,需UrlEncode】' -H 'Content-Type:application/x-www-form-urlencoded'
1# encoding:utf-8
2
3import requests
4import base64
5
6'''
7医疗检验报告单识别
8'''
9
10request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/medical_report_detection"
11# 二进制方式打开图片文件
12f = open('[本地文件]', 'rb')
13img = base64.b64encode(f.read())
14
15params = {"image":img}
16access_token = '[调用鉴权接口获取的token]'
17request_url = request_url + "?access_token=" + access_token
18headers = {'content-type': 'application/x-www-form-urlencoded'}
19response = requests.post(request_url, data=params, headers=headers)
20if response:
21 print (response.json())
1package com.baidu.ai.aip;
2
3import com.baidu.ai.aip.utils.Base64Util;
4import com.baidu.ai.aip.utils.FileUtil;
5import com.baidu.ai.aip.utils.HttpUtil;
6
7import java.net.URLEncoder;
8
9/**
10* 医疗检验报告单识别
11*/
12public class MedicalReportDetection{
13
14 /**
15 * 重要提示代码中所需工具类
16 * FileUtil,Base64Util,HttpUtil,GsonUtils请从
17 * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
18 * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
19 * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
20 * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
21 * 下载
22 */
23 public static String medicalReportDetection() {
24 // 请求url
25 String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/medical_report_detection";
26 try {
27 // 本地文件路径
28 String filePath = "[本地文件路径]";
29 byte[] imgData = FileUtil.readFileByBytes(filePath);
30 String imgStr = Base64Util.encode(imgData);
31 String imgParam = URLEncoder.encode(imgStr, "UTF-8");
32
33 String param = "image=" + imgParam;
34
35 // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
36 String accessToken = "[调用鉴权接口获取的token]";
37
38 String result = HttpUtil.post(url, accessToken, param);
39 System.out.println(result);
40 return result;
41 } catch (Exception e) {
42 e.printStackTrace();
43 }
44 return null;
45 }
46
47 public static void main(String[] args) {
48 MedicalReportDetection.medicalReportDetection();
49 }
50}
1#include <iostream>
2#include <curl/curl.h>
3
4// libcurl库下载链接:https://curl.haxx.se/download.html
5// jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/
6const static std::string request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/medical_report_detection";
7static std::string medicalReportDetection_result;
8/**
9* curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
10* @param 参数定义见libcurl文档
11* @return 返回值定义见libcurl文档
12*/
13static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {
14 // 获取到的body存放在ptr中,先将其转换为string格式
15 medicalReportDetection_result = std::string((char *) ptr, size * nmemb);
16 return size * nmemb;
17}
18/**
19* 医疗检验报告单识别
20* @return 调用成功返回0,发生错误返回其他错误码
21*/
22int medicalReportDetection(std::string &json_result, const std::string &access_token) {
23 std::string url = request_url + "?access_token=" + access_token;
24 CURL *curl = NULL;
25 CURLcode result_code;
26 int is_success;
27 curl = curl_easy_init();
28 if (curl) {
29 curl_easy_setopt(curl, CURLOPT_URL, url.data());
30 curl_easy_setopt(curl, CURLOPT_POST, 1);
31 curl_httppost *post = NULL;
32 curl_httppost *last = NULL;
33 curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, "【base64_img】", CURLFORM_END);
34
35 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
36 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
37 result_code = curl_easy_perform(curl);
38 if (result_code != CURLE_OK) {
39 fprintf(stderr, "curl_easy_perform() failed: %s\n",
40 curl_easy_strerror(result_code));
41 is_success = 1;
42 return is_success;
43 }
44 json_result = medicalReportDetection_result;
45 curl_easy_cleanup(curl);
46 is_success = 0;
47 } else {
48 fprintf(stderr, "curl_easy_init() failed.");
49 is_success = 1;
50 }
51 return is_success;
52}
1<?php
2/**
3* 发起http post请求(REST API), 并获取REST请求的结果
4* @param string $url
5* @param string $param
6* @return - http response body if succeeds, else false.
7*/
8function request_post($url = '', $param = '')
9{
10 if (empty($url) || empty($param)) {
11 return false;
12 }
13
14 $postUrl = $url;
15 $curlPost = $param;
16 // 初始化curl
17 $curl = curl_init();
18 curl_setopt($curl, CURLOPT_URL, $postUrl);
19 curl_setopt($curl, CURLOPT_HEADER, 0);
20 // 要求结果为字符串且输出到屏幕上
21 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
22 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
23 // post提交方式
24 curl_setopt($curl, CURLOPT_POST, 1);
25 curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
26 // 运行curl
27 $data = curl_exec($curl);
28 curl_close($curl);
29
30 return $data;
31}
32
33$token = '[调用鉴权接口获取的token]';
34$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/medical_report_detection?access_token=' . $token;
35$img = file_get_contents('[本地文件路径]');
36$img = base64_encode($img);
37$bodys = array(
38 'image' => $img
39);
40$res = request_post($url, $bodys);
41
42var_dump($res);
返回说明
返回参数
字段 | 是否必输出 | 类型 | 说明 |
---|---|---|---|
log_id | 是 | uint64 | 调用日志id,用于问题定位 |
words_result | 是 | object | 识别结果 |
CommonData_result_num | 是 | uint32 | 患者具体信息的识别结果数,表示CommonData的元素个数 |
Item_row_num | 是 | uint32 | 检查项目的行数,表示Item中的数组个数 |
+ CommonData | 是 | array[] | 患者具体信息 |
+ Item | 是 | array[] | 检查项目 |
++ word_name | 是 | string | 字段名,详见下方表格区说明 |
++ word | 是 | string | word_name字段对应的识别结果 |
++ location | 否 | object | 字段位置信息,当请求参数 location=true 时,以上各字段均包含此参数 |
+++ top | 否 | uint32 | 字段的上边距 |
+++ left | 否 | uint32 | 字段的左边距 |
+++ height | 否 | uint32 | 字段的高度 |
+++ width | 否 | uint32 | 字段的宽度 |
++ probability | 否 | object | 字段识别结果置信度,当请求参数 probability=true 时,以上各字段均包含此参数 |
+++ average | 否 | float | 字段识别结果中各字符的置信度平均值 |
+++ min | 否 | float | 字段识别结果中各字符的置信度最小值 |
CommonData字段包含多个object,见以下参数
字段 | 说明 |
---|---|
++ word_name | 字段名,包括:医院、报告单名称、姓名、性别、年龄、科室、标本种类、临床诊断、时间、临床症状、检查项目、标本情况、检查目的、建议、检查结果 |
++ word | word_name字段对应的识别结果 |
Item字段包含多个array,每个数组包含多个object,见以下参数
字段 | 说明 |
---|---|
++ word_name | 字段名,包括:项目名称、项目代号、结果、单位、参考区间、结果提示、测试方法、仪器类型 |
++ word | word_name字段对应的识别结果 |
返回示例
JSON
1{
2 "Item_row_num": 5,
3 "words_result": {
4 "Item": [
5 [
6 {
7 "word_name": "仪器类型",
8 "word": ""
9 },
10 {
11 "word_name": "单位",
12 "word": "10^9/L"
13 },
14 {
15 "word_name": "参考区间",
16 "word": "3.97--9.15"
17 },
18 {
19 "word_name": "测试方法",
20 "word": ""
21 },
22 {
23 "word_name": "结果",
24 "word": "18.82"
25 },
26 {
27 "word_name": "结果提示",
28 "word": ""
29 },
30 {
31 "word_name": "项目代号",
32 "word": "WBC"
33 },
34 {
35 "word_name": "项目名称",
36 "word": "白细胞计数"
37 }
38 ],
39 [
40 {
41 "word_name": "仪器类型",
42 "word": ""
43 },
44 {
45 "word_name": "单位",
46 "word": "10^12/L"
47 },
48 {
49 "word_name": "参考区间",
50 "word": "3.68-574"
51 },
52 {
53 "word_name": "测试方法",
54 "word": ""
55 },
56 {
57 "word_name": "结果",
58 "word": "5.13"
59 },
60 {
61 "word_name": "结果提示",
62 "word": ""
63 },
64 {
65 "word_name": "项目代号",
66 "word": "RBC"
67 },
68 {
69 "word_name": "项目名称",
70 "word": "红细胞计数"
71 }
72 ],
73 [
74 {
75 "word_name": "仪器类型",
76 "word": ""
77 },
78 {
79 "word_name": "单位",
80 "word": "g/L"
81 },
82 {
83 "word_name": "参考区间",
84 "word": "113--172"
85 },
86 {
87 "word_name": "测试方法",
88 "word": ""
89 },
90 {
91 "word_name": "结果",
92 "word": "132.00"
93 },
94 {
95 "word_name": "结果提示",
96 "word": ""
97 },
98 {
99 "word_name": "项目代号",
100 "word": "HGB"
101 },
102 {
103 "word_name": "项目名称",
104 "word": "血红蛋白浓度"
105 }
106 ],
107 [
108 {
109 "word_name": "仪器类型",
110 "word": ""
111 },
112 {
113 "word_name": "单位",
114 "word": "10^9/L"
115 },
116 {
117 "word_name": "参考区间",
118 "word": "101--320"
119 },
120 {
121 "word_name": "测试方法",
122 "word": ""
123 },
124 {
125 "word_name": "结果",
126 "word": "419.101"
127 },
128 {
129 "word_name": "结果提示",
130 "word": ""
131 },
132 {
133 "word_name": "项目代号",
134 "word": "PLT"
135 },
136 {
137 "word_name": "项目名称",
138 "word": "血小板计数"
139 }
140 ],
141 [
142 {
143 "word_name": "仪器类型",
144 "word": ""
145 },
146 {
147 "word_name": "单位",
148 "word": "%"
149 },
150 {
151 "word_name": "参考区间",
152 "word": "50-70"
153 },
154 {
155 "word_name": "测试方法",
156 "word": ""
157 },
158 {
159 "word_name": "结果",
160 "word": "68.54"
161 },
162 {
163 "word_name": "结果提示",
164 "word": ""
165 },
166 {
167 "word_name": "项目代号",
168 "word": "NEUT%"
169 },
170 {
171 "word_name": "项目名称",
172 "word": "中性粒细胞百分比"
173 }
174 ]
175 ],
176 "CommonData": [
177 {
178 "word_name": "临床症状",
179 "word": ""
180 },
181 {
182 "word_name": "临床诊断",
183 "word": ""
184 },
185 {
186 "word_name": "医院",
187 "word": ""
188 },
189 {
190 "word_name": "姓名",
191 "word": "刘安洋"
192 },
193 {
194 "word_name": "年龄",
195 "word": "3岁"
196 },
197 {
198 "word_name": "建议",
199 "word": ""
200 },
201 {
202 "word_name": "性别",
203 "word": "女"
204 },
205 {
206 "word_name": "报告单名称",
207 "word": "霍林郭勒市中蒙医医院检验报告单"
208 },
209 {
210 "word_name": "时间",
211 "word": "20201031"
212 },
213 {
214 "word_name": "标本情况",
215 "word": ""
216 },
217 {
218 "word_name": "标本种类",
219 "word": "血液"
220 },
221 {
222 "word_name": "检查目的",
223 "word": ""
224 },
225 {
226 "word_name": "检查结果",
227 "word": ""
228 },
229 {
230 "word_name": "检查项目",
231 "word": ""
232 },
233 {
234 "word_name": "科室",
235 "word": "儿科门诊"
236 }
237 ]
238 },
239 "CommonData_result_num": 15,
240 "log_id": 1455012594820522020
241}