项目实训进度记录【4.12-4.13】

摘要

完成三个ai工具(语音转文字、图片转文字)的接口调用。

过程

我使用的是百度智能云的接口,用了它语音转文字(AsrMain)、图片转文字(OCR)、关键词生成、摘要提取和标题生成(NLP)接口。这些AI接口在一定时间内和一定次数内免费开放,但也要注册账号,并从个人账户中创建项目并获取app_id, api_key和secret_key。
在这里插入图片描述
我在百度智能云上找到了这几个sdk文档,根据文档和demo的描述调用相应的服务:

package project.ourcourseassistant.util;

import com.baidu.aip.nlp.AipNlp;
import com.baidu.aip.ocr.AipOcr;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;

public class apiUtil {
    public static final String APP_ID = "25938499";
    public static final String API_KEY = "j0crYgROlMfTcd3bSd0fjBLg";
    public static final String SECRET_KEY = "SN8eaV28ftBiLdw36Gsp1OYHhQPq8F4D";

    public static JSONObject doOcr(String filepath) {
        System.out.println("doOcr");
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        JSONObject res = client.basicGeneral(filepath, new HashMap<String, String>());
//        System.out.println(res.toString(2));
        return res;
    }

    public static JSONObject doAbstract(String content) {
        AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);
//        String content = "麻省理工学院的研究团队为无人机在仓库中使用RFID技术进行库存查找等工作,创造了一种聪明的新方式。使用RFID标签更换仓库中的条形码,将帮助提升自动化并提高库存管理的准确性。几家公司已经解决了无人机读取RFID的技术问题。麻省理工学院的新解决方案,名为Rfly,允许无人机阅读RFID标签,而不用捆绑巨型读卡器。无人机接收从远程RFID读取器发送的信号,然后转发它读取附近的标签。";
        int maxSummaryLen = 15;

        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        options.put("title", "标题");

        // 新闻摘要接口
        JSONObject res = client.newsSummary(content, maxSummaryLen, options);
        System.out.println(res.toString(2));
        return res;
    }

    public static JSONObject doTag(String content){
        AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);
//        String content = "麻省理工学院的研究团队为无人机在仓库中使用RFID技术进行库存查找等工作,创造了一种聪明的新方式。使用RFID标签更换仓库中的条形码,将帮助提升自动化并提高库存管理的准确性。几家公司已经解决了无人机读取RFID的技术问题。麻省理工学院的新解决方案,名为Rfly,允许无人机阅读RFID标签,而不用捆绑巨型读卡器。无人机接收从远程RFID读取器发送的信号,然后转发它读取附近的标签。";

        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();


        int maxSummaryLen = 15;
        // 传入可选参数调用接口
        HashMap<String, Object> options2 = new HashMap<String, Object>();
        options.put("title", "标题");
        JSONObject title_res = client.newsSummary(content, maxSummaryLen, options2);//它就提取第一句话,所以把keyword带跑偏了

        // 文章标签
        try {
            JSONObject res = client.keyword(title_res.getString("summary"), content, options);
            System.out.println(res.toString(2));
            return res;
        }catch (Exception e){
            return null;
        }
    }





//    public static void main(String[] args) {
    // 初始化一个AipOcr
//        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

    // 可选:设置网络连接参数
//        client.setConnectionTimeoutInMillis(2000);
//        client.setSocketTimeoutInMillis(60000);

    // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

    // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
    // 也可以直接通过jvm启动参数设置此环境变量
//        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

    // 调用接口
//        String path = "test.jpeg";
//        JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
//        System.out.println(res.toString(2));

    //    }
    public static void main(String[] args) {
        String content="笑死我了我用作例子的字符串中含有政治因素都过不了审";
//        JSONObject jo=doTag(content,"疫情攻坚战");
//        System.out.println(jo);
//        JSONObject js=apiUtil.doTag(content,title);
//        System.out.println(js);
        String ans="Keywords: ";
        JSONArray js=apiUtil.doTag(content).getJSONArray("items");
        for(int i=0;i<js.length();i++){
            JSONObject jo=js.getJSONObject(i);
            ans+=jo.getString("tag");
            ans+=" ";
        }
        System.out.println(ans);
    }
}

用到项目中的效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值