thinkphp5对接阿里云ocr试卷切题
提示:切题使用的是api:RecognizeEduPaperCut
以下是基于 ThinkPHP5.14 框架调用阿里云 RecognizeEduPaperCut 接口的详细实现步骤和代码示例。
文章目录
- thinkphp5对接阿里云ocr试卷切题
- 前言
-
-
-
- 1、前置准备
- [2、 配置文件](#2、 配置文件)
- 3、控制器直接引用使用
- 4、返回参数处理
- 5、其他相关教育识别接口
-
- RecognizeEduPaperStruct
-
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
1、前置准备
在开始开发之前,请确保完成以下准备工作:
1、开通服务:在阿里云控制台开通教育场景识别服务 ,并购买相关资源包。
2、获取凭证:获取阿里云 AccessKey ID 和 AccessKeySecret,用于接口调用的身份验证。
3、安装依赖:通过 Composer 安装阿里云 SDK。运行以下命令安装:
c
composer require alibabacloud/ocr-api-20210707 3.1.2
2、 配置文件
在 ThinkPHP5 的配置文件中(如 config/aliyun.php),添加阿里云的相关配置信息:
php
return [
'access_key_id' => 'your-access-key-id',
'access_key_secret' => 'your-access-key-secret',
'region_id' => 'cn-hangzhou', // 区域ID,默认为 cn-hangzhou
];
3、控制器直接引用使用
php
<?php
namespace app\index\controller;
use Darabonba\OpenApi\OpenApiClient;
use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
use Darabonba\OpenApi\Models\Config;
use Darabonba\OpenApi\Models\Params;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\OpenApiRequest;
class Index
{
/**
* 使用AK&SK初始化账号Client
* @return OpenApiClient Client
*/
public static function createClient(){
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html。
$config = new Config([
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
"accessKeyId" => config('aliyun.access_key_id'),
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
"accessKeySecret" => config('aliyun.access_key_secret')
]);
// Endpoint 请参考 https://api.aliyun.com/product/ocr-api
$config->endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
return new OpenApiClient($config);
}
/**
* API 相关
* @return Params OpenApi.Params
*/
public static function createApiInfo(){
$params = new Params([
// 接口名称
"action" => "RecognizeEduPaperCut",
// 接口版本
"version" => "2021-07-07",
// 接口协议
"protocol" => "HTTPS",
// 接口 HTTP 方法
"method" => "POST",
"authType" => "AK",
"style" => "V3",
// 接口 PATH
"pathname" => "/",
// 接口请求体内容格式
"reqBodyType" => "json",
// 接口响应体内容格式
"bodyType" => "json"
]);
return $params;
}
/**
* @param string[] $args
* @return void
*/
public static function main($imgurl,$cuttype="question",$imgtype="photo"){
$client = self::createClient();
$params = self::createApiInfo();
// query params
$queries = [];
$queries["Url"] = $imgurl;
$queries["CutType"] = $cuttype;
$queries["ImageType"] = $imgtype;
// runtime options
$runtime = new RuntimeOptions([]);
$request = new OpenApiRequest([
"query" => OpenApiUtilClient::query($queries)
]);
// 复制代码运行请自行打印 API 的返回值
// 返回值实际为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
return $client->callApi($params, $request, $runtime);
}
//调用
public function ocr(){
$imgurl = input('imgurl');
//图片url 路径
$imgurl = "";
$cuttype = input('cuttype');
//切题类型:question:题目, answer:答案。
$cuttype = $cuttype??"question";
// var_dump($imgurl);
// var_dump($cuttype);exit;
$resurt = self::main($imgurl,$cuttype);
if(isset($resurt['code'])){//请求失败才返回code
return json(['code'=>$resurt['code'],'msg'=>$resurt['Message']]);
}else{
return json($resurt);
}
}
}
提示:请求失败才返回 code 和Message 内容
4、返回参数处理
php
{
"page_list": [
{
"angle": 0,
"doc_index": 1,
"height": 7000,
"orgHeight": 7000,
"orgWidth": 4716,
"page_id": -1,
"subject_list": [
{
"content_list_info": [
{
"doc_index": 1,
"pos": [
{
"x": 337,
"y": 1644
},
{
"x": 2313,
"y": 1641
},
{
"x": 2313,
"y": 2234
},
{
"x": 337,
"y": 2234
}
]
}
],
"ids": [
"1"
],
"is_multipage": false,
"prism_wordsInfo": [
{
"pos": [
{
"x": 337,
"y": 1644
},
{
"x": 2313,
"y": 1641
},
{
"x": 2313,
"y": 1715
},
{
"x": 337,
"y": 1719
}
],
"word": "1.[2017·高密期末]三角形按角分类可以分为()"
},
{
"pos": [
{
"x": 462,
"y": 1777
},
{
"x": 1925,
"y": 1774
},
{
"x": 1925,
"y": 1846
},
{
"x": 462,
"y": 1849
}
],
"word": "A.锐角三角形、直角三角形、钝角三角形"
},
{
"pos": [
{
"x": 456,
"y": 1904
},
{
"x": 2004,
"y": 1904
},
{
"x": 2004,
"y": 1976
},
{
"x": 456,
"y": 1976
}
],
"word": "B.等腰三角形、等边三角形、不等边三角形"
},
{
"pos": [
{
"x": 456,
"y": 2034
},
{
"x": 1633,
"y": 2033
},
{
"x": 1633,
"y": 2104
},
{
"x": 456,
"y": 2105
}
],
"word": "C.直角三角形、等腰直角三角形"
},
{
"pos": [
{
"x": 453,
"y": 2164
},
{
"x": 1261,
"y": 2164
},
{
"x": 1261,
"y": 2233
},
{
"x": 453,
"y": 2234
}
],
"word": "D.以上答案都不正确"
}
],
"text": "1.[2017·高密期末]三角形按角分类可以分为( ) A.锐角三角形、直角三角形、钝角三角形B. 等腰三角形、等边三角形、不等边三角形C.直角三角形、等腰直角三角形D.以上答案都不正确"
}
],
"width": 4716
}
]
}
5、其他相关教育识别接口
1、 印刷体数学公式识别
RecognizeEduFormula
2、口算判题
RecognizeEduOralCalculation
3、整页试卷识别
RecognizeEduPaperOcr
4、试卷切题识别
RecognizeEduPaperCut
5、题目识别
RecognizeEduQuestionOcr
6、精细版结构化切题
RecognizeEduPaperStruct
7、关于费用
每月免费200次调用,每次0.225元,
套餐包:1000次 160左右,具体已阿里云官方为准