当前位置:   article > 正文

java开发腾讯AI,共探人工智能_腾讯ai生成图片 java

腾讯ai生成图片 java

前言:跟你们说说羊群效应嘛,听过的切勿吐槽,就是1000只羊在一边吃草,这时来了一只狗,汪汪几声,1000只养竟然跑到另一边。对,是竟然,1000只羊打不过一只狗?每只羊舔一下那只狗都能舔死那只狗辣,试想1000个女轮着舔一个男的,当然羊不是这样想的,羊会想我上去舔,其他的羊却没有上去,这就是1对1,这不是舔咯,是濑,濑是服务咯。。。现在,是AI时代,大数据时代。你还不开发AI,你就后悔了。当然,有人问我为什么选择TC,李白已经预言了。床前明月光,年尾钱紧张。。。好了,说了那么多,是时候展现真正的技术了。。。

这里写图片描述

腾讯ai官网:https://ai.qq.com/
这里只实现自然语言处理这模块的接口。。。
1、通用自然语言处理接口

public static String getNaturalLanguage(String baseUrl, String text, int type, String encoding) throws Exception{
        SignEntity signEntity = new SignEntity(text,type);
        String url = baseUrl+"?app_id=" + signEntity.getApp_id() + "&time_stamp="
                + signEntity.getTime_stamp() + "&nonce_str=" + signEntity.getNonce_str() + "&sign="
                + signEntity.getSign() + "&text=" + URLEncoder.encode(text.trim(), encoding);
        String message = _httpUtil.doHttpGet(url,encoding);
        System.out.println(message);
        JSONObject demoJson = JSONObject.fromObject(message);
        String data = demoJson.getString("data");
        return data;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

参数说明:
baseUrl:腾讯ai接口地址
text:文本文字
type:1POST处理 2GET处理
encoding:编码 UTF-8、GBK

首先这个类就是获取sign的:SignEntity signEntity = new SignEntity(text,type);
要传入文本文字和编码类型,有些要GBK编码,有些要UTF-8编码,详情请参考官网。

然后就是拼接请求参数,最后返回json格式字符串

2、机器人聊天
这个接口好像图灵机器人一样

/**
     * 机器人聊天
     * @param baseUrl
     * @param text
     * @param session
     * @return
     * @throws Exception
     */
    public static String getNaturalLanguageChat(String baseUrl, String text, String session) throws Exception{
        int app_id = TencentAPI.APP_ID_AI;
        String app_key = TencentAPI.APP_KEY_AI;
        int time_stamp = (int) (System.currentTimeMillis() / 1000) ;
        String nonce_str = _stringUtil.getCharAndNumr(17, 3);
        Map<String, String> person_Id_body = new HashMap<String, String>();
        person_Id_body.put("app_id", app_id+"");
        person_Id_body.put("time_stamp", time_stamp+"");
        person_Id_body.put("nonce_str", nonce_str);
        person_Id_body.put("question", text);
        person_Id_body.put("session", session);
        String sign = TencentAISignSort.getSignature(person_Id_body);
        SignEntity signEntity = new SignEntity();
        signEntity.setApp_id(app_id);
        signEntity.setApp_key(app_key);
        signEntity.setNonce_str(nonce_str);
        signEntity.setSign(sign.toUpperCase());
        signEntity.setTime_stamp(time_stamp);
        String url = baseUrl+"?app_id=" + signEntity.getApp_id() + "&time_stamp="
                + signEntity.getTime_stamp() + "&nonce_str=" + signEntity.getNonce_str() + "&sign="
                + signEntity.getSign() + "&session="+URLEncoder.encode(session.trim(), "UTF-8")+"&question=" + URLEncoder.encode(text.trim(), "UTF-8");
        String message = _httpUtil.doHttpGet(url,"UTF-8");
        System.out.println(message);
        JSONObject demoJson = JSONObject.fromObject(message);
        String data = demoJson.getString("data");
        return data;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

3、中译英文本翻译
这也与百度翻译一样效果。。。

/**
     * 文本翻译
     * @param baseUrl
     * @param text
     * @param type
     * @return
     * @throws Exception
     */
    public static String getNaturalLanguageTrans(String baseUrl, String text, int type) throws Exception{
        int app_id = TencentAPI.APP_ID_AI;
        String app_key = TencentAPI.APP_KEY_AI;
        int time_stamp = (int) (System.currentTimeMillis() / 1000) ;
        String nonce_str = _stringUtil.getCharAndNumr(17, 3);
        Map<String, String> person_Id_body = new HashMap<String, String>();
        person_Id_body.put("app_id", app_id+"");
        person_Id_body.put("time_stamp", time_stamp+"");
        person_Id_body.put("nonce_str", nonce_str);
        person_Id_body.put("text", text);
        person_Id_body.put("type", type+"");
        String sign = TencentAISignSort.getSignature(person_Id_body);
        SignEntity signEntity = new SignEntity();
        signEntity.setApp_id(app_id);
        signEntity.setApp_key(app_key);
        signEntity.setNonce_str(nonce_str);
        signEntity.setSign(sign.toUpperCase());
        signEntity.setTime_stamp(time_stamp);
        String url = baseUrl+"?app_id=" + signEntity.getApp_id() + "&time_stamp="
                + signEntity.getTime_stamp() + "&nonce_str=" + signEntity.getNonce_str() + "&sign="
                + signEntity.getSign() + "&type="+type+"&text=" + URLEncoder.encode(text.trim(), "UTF-8");
        String message = _httpUtil.doHttpGet(url,"UTF-8");
        System.out.println(message);
        JSONObject demoJson = JSONObject.fromObject(message);
        String data = demoJson.getString("data");
        return data;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

4、图片翻译
这个挺有意思,好像ocr一样,但腾讯ai也有ocr接口,这个是将识别图片中的文字和文字的位置等,这里是用image base64编码,有点长。。。

/**
     * 图片翻译
     * @param appId
     * @param nonce_str
     * @param image
     * @param session_id
     * @param time_stamp
     * @return
     * @throws Exception
     */
    public static String getNaturalLanguageImgTrans(String baseUrl, String image) throws Exception{
        String sign = "";
        int app_id = TencentAPI.APP_ID_AI;
        int time_stamp = (int) (System.currentTimeMillis() / 1000) ;
        String nonce_str = _stringUtil.getCharAndNumr(17, 3);
        String session_id = _stringUtil.getCharAndNumr(10, 1);
        sign = appSignAI4ImgDetect(TencentAPI.APP_ID_AI, nonce_str,image,session_id,time_stamp);
        String message = _httpUtil.doHttpPost(baseUrl,"app_id=" + URLEncoder.encode((app_id+"").trim(), "UTF-8") 
                                                                                + "&image="+URLEncoder.encode(image.trim(), "UTF-8")+"&nonce_str=" + URLEncoder.encode(nonce_str.trim(), "UTF-8") 
                                                                                + "&scene="+URLEncoder.encode("doc", "UTF-8")+"&session_id=" + URLEncoder.encode(session_id.trim(), "UTF-8")
                                                                                +"&sign="+sign+"&source="+URLEncoder.encode("zh", "UTF-8")+"&target="+URLEncoder.encode("en", "UTF-8")
                                                                                + "&time_stamp=" + URLEncoder.encode((time_stamp+"").trim(), "UTF-8"));
        JSONObject demoJson = JSONObject.fromObject(message);
        String data = demoJson.getString("data");
        System.out.println("session_id:"+session_id);
        return data;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

5、语音翻译
给一个mp3,要求有时间限制的,然后能识别出文字出来。。。

/**
     * 语音翻译
     * @param baseUrl
     * @param speechChunk
     * @return
     * @throws Exception
     */
    public static String getNaturalLanguageSpeechTrans(String baseUrl, String speechChunk) throws Exception{
        String sign = "";
        int app_id = TencentAPI.APP_ID_AI;
        int time_stamp = (int) (System.currentTimeMillis() / 1000) ;
        String nonce_str = _stringUtil.getCharAndNumr(17, 3);
        String session_id = _stringUtil.getCharAndNumr(10, 1);
        int format = 8;
        int seq = 0;
        int end = 1;
        String source = "zh";
        String target = "en";
        sign = appSignAI4SpeechDetect(TencentAPI.APP_ID_AI, time_stamp, nonce_str, format , 
                seq, end, session_id, speechChunk,source, target);
        String message = _httpUtil.doHttpPost(baseUrl,"app_id=" + URLEncoder.encode(app_id+"","UTF-8") + "&end=" + URLEncoder.encode(end+"","UTF-8") 
        + "&format=" + URLEncoder.encode(format+"","UTF-8") 
        +"&nonce_str=" + URLEncoder.encode(nonce_str,"UTF-8")+ "&seq="+URLEncoder.encode(seq+"", "UTF-8")
        +"&session_id=" + URLEncoder.encode(session_id.trim(), "UTF-8") +"&sign=" + sign +"&source="+URLEncoder.encode(source, "UTF-8")
        +"&speech_chunk="+URLEncoder.encode(speechChunk, "UTF-8")
        +"&target="+URLEncoder.encode(target, "UTF-8") + "&time_stamp=" + URLEncoder.encode(time_stamp+"","UTF-8"));
        JSONObject demoJson = JSONObject.fromObject(message);
        String data = demoJson.getString("data");
        System.out.println("session_id:"+session_id);
        return data;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

6、语种识别
输入一段文本,可以识别文字是中文还是英文或者其他语言。。。

/**
     * 语种识别
     * @param baseUrl
     * @param speechChunk
     * @return
     * @throws Exception
     */
    public static String getNaturalLanguageTextDetect(String baseUrl, String text) throws Exception{
        String sign = "";
        int app_id = TencentAPI.APP_ID_AI;
        int time_stamp = (int) (System.currentTimeMillis() / 1000) ;
        String nonce_str = _stringUtil.getCharAndNumr(17, 3);
        int force = 0;
        String candidate_langs = "zh";
        sign = appSignAI4SpeechDetect(TencentAPI.APP_ID_AI,candidate_langs, force, nonce_str, text , time_stamp);

        String message = _httpUtil.doHttpPost(baseUrl,"app_id=" + URLEncoder.encode(app_id+"","UTF-8") + "&candidate_langs=" + URLEncoder.encode(candidate_langs+"","UTF-8") 
        + "&force=" + URLEncoder.encode(force+"","UTF-8") 
        +"&nonce_str=" + URLEncoder.encode(nonce_str,"UTF-8")+"&sign=" + sign+ "&text="+URLEncoder.encode(text+"", "UTF-8")
        + "&time_stamp=" + URLEncoder.encode(time_stamp+"","UTF-8"));
        JSONObject demoJson = JSONObject.fromObject(message);
        String data = demoJson.getString("data");
        return data;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

当然,ai还有很多,这只是一部分,有图片的,视频的等等。。。

需要所有源代码,可加QQ490647751回复‘java开发腾讯AI,共探人工智能’获取。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/998448
推荐阅读
相关标签
  

闽ICP备14008679号