天天看点

移动端网页录音上传,服务端智能语音识别移动端网页录音上传,服务端智能语音识别

移动端网页录音上传,服务端智能语音识别

最近,看了创业时代的魔镜,想法突如起来,能不能手机发送一条语音,语音上传到后台,自动识别语音的信息,转化为文字,将文字分析,然后回复用户艳学网的资源。

我们的资源以源码为主,一起编集艳学情缘。我们不仅分享源码 http://47.98.237.162/index ,还分享高清的无码 http://47.98.237.162/movie/index 。陆续,我们还推出,二维码海报分享活动,搜索源码等。一切将以“码”为主题,今天,我们开发一款“码上说”的demo。

移动端网页录音上传

为什么移动端,不用安卓,苹果,网页,微信公众号,小程序等。因为百度了一下,移动端实现录音的资料少之又少,但html5的诞生眷顾了很多移动端网页开发的小伙伴。

录音

<input name="file" type="file" accept="audio/*" capture="microphone" />
           

没错,就一句,即可使用移动端网页录音。

上传

这里我们使用java上传:

@RequestMapping(value = "/saveVoice", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	@ResponseBody
	public String saveVoice(HttpServletRequest request) throws Exception {
		MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
		Iterator<String> t = multiRequest.getFileNames();
		MultipartFile fileDetail = multiRequest.getFile(t.next());
		String fileName = System.currentTimeMillis() + ".mp3";
		String path = null;
			path = "D:\\voice\\";
		File file = new File(path);
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
		}
		File file2 = new File(path+fileName);
		if(!file2.exists()){
			file2.createNewFile();
		}
		fileDetail.transferTo(file2);
		return "上传成功!";
	}
           

mp3转pcm

由于上传到服务器的文件是mp3格式,而大多数语音识别api上传的格式是pcm,所以得转一下:

/**
     * MP3转换PCM文件方法
     *
     * @param mp3filepath
     *            原始文件路径
     * @param pcmfilepath
     *            转换文件的保存路径
     * @throws Exception
     */
    public static void convertMP32PCM(String mp3filepath, String pcmfilepath) throws Exception {
        AudioInputStream audioInputStream = getPcmAudioInputStream(mp3filepath);
        AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, new File(pcmfilepath));
    }

    private static AudioInputStream getPcmAudioInputStream(String mp3filepath) {
        File mp3 = new File(mp3filepath);
        AudioInputStream audioInputStream = null;
        AudioFormat targetFormat = null;
        try {
            // = null;
            MpegAudioFileReader mp = new MpegAudioFileReader();
            AudioInputStream in = mp.getAudioInputStream(mp3);
            AudioFormat baseFormat = in.getFormat();
            targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16,
                    baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
            audioInputStream = AudioSystem.getAudioInputStream(targetFormat, in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return audioInputStream;
    }

    public static void main(String[] args) {
        String path = "D://1";
        String mp3filepath = path + ".mp3";
        String pcmfilepath = path + ".pcm";

        try {
            MP3ToPcm.convertMP32PCM(mp3filepath, pcmfilepath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
           

服务端智能语音识别

智能AI

现在,进入的是AI时代,为何这么说,最近去了本市最贵最奢侈的消费地方——医院,拿个药都要扫一扫,病历信息化,诊断结果一键生成,并及时反馈给自己的就诊医生,信息安全且自动化。出个门都是无人超市,无人餐厅,无人驾驶。

享受成果的同时,也要研究一下:

以上,是我本人个人观点,参考一下就好。

语音识别

/**
     * 语音识别
     * @param fileName
     * @return
     */
    public static String getPcm(String fileName) throws Exception {
        AipSpeech client = getAipSpeech();
        JSONObject res = client.asr(fileName, "pcm", 16000, null);
        Object obj = res.get("result");
        return obj.toString().replace("[\"", "").replace(",\"]", "");
    }

    private static AipSpeech getAipSpeech() {
        AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        return client;
    }

    /**
     * 语音合成
     * @param text 文字
     * @param output 输出mp3
     */
    public static void getVoice(String text, String output) {
        AipSpeech client = getAipSpeech();
        //发音人选择, 0为女声,1为男声,
        //3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女
        // www.yanhui.fun
        HashMap<String, Object> options = new HashMap<String, Object>();
//        options.put("spd", "5");
//        options.put("pit", "5");
        options.put("per", "1");

        TtsResponse res = client.synthesis(text, "zh", 1, options);
        byte[] data = res.getData();
        JSONObject res1 = res.getResult();
        if (data != null) {
            try {
                Util.writeBytesToFileSystem(data, output);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (res1 != null) {
            System.out.println(res1.toString());
        }
    }
           

优图AI api接口

//            response = faceYoutu.GeneralOcrUrl("http://open.youtu.qq.com/static/img/ocr_common04.22dc0ca.jpg"); //通用印刷体文字识别
//            response = faceYoutu.HandwritingOcrUrl("http://open.youtu.qq.com./static/img/ocr_hw_01.3b5e11a.jpg"); // 通用手写体文字识别
//            response = faceYoutu.EhOcrUrl("http://open.youtu.qq.com./static/img/ocr_hw_01.3b5e11a.jpg"); // 手写体英文识别
//            response = faceYoutu.IdCardOcrUrl("http://open.youtu.qq.com./static/img/ocr_id_01.883a2df.jpg", 0); // 身份证识别
//            response = faceYoutu.BcOcrUrl("http://open.youtu.qq.com./static/img/ocr_namecard_01.988383b.jpg"); // 名片识别
            response = faceYoutu.BizLicenseOcrUrl("http://open.youtu.qq.com./static/img/ocr_yyzz_01.1d874f9.jpg"); // 营业执照识别
           

腾讯不错不错,又多了个平台。

如需获取文本源码,请加QQ490647751,并回复“开通vip——移动端网页录音上传,服务端智能语音识别”

[1]: https://open.youtu.qq.com/#/open

[2]: https://open.tencent.com/

[3]: https://ai.qq.com/

[4]: http://ai.sogou.com/

[5]: http://ai.baidu.com/

[6]: https://ai.google/

[7]: https://www.xfyun.cn/

[8]: https://ai.aliyun.com/

即将开放:http://www.yanhui.fun/ai/index 2019年将推出“ai码平台“,提供免费开发接口及演示示例(需网站注册及微信号关注), 技术革命,跟随“人工智能,大数据,云技术”的步伐。

移动端网页录音上传,服务端智能语音识别移动端网页录音上传,服务端智能语音识别

继续阅读