1. 程式人生 > >移動端網頁錄音上傳,服務端智慧語音識別

移動端網頁錄音上傳,服務端智慧語音識別

移動端網頁錄音上傳,服務端智慧語音識別

最近,看了創業時代的魔鏡,想法突如起來,能不能手機發送一條語音,語音上傳到後臺,自動識別語音的資訊,轉化為文字,將文字分析,然後回覆使用者豔學網的資源。
我們的資源以原始碼為主,一起編集豔學情緣。我們不僅分享原始碼 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時代,為何這麼說,最近去了本市最貴最奢侈的消費地方——醫院,拿個藥都要掃一掃,病歷資訊化,診斷結果一鍵生成,並及時反饋給自己的就診醫生,資訊保安且自動化。出個門都是無人超市,無人餐廳,無人駕駛。
享受成果的同時,也要研究一下:
/**

  • @Description AI語音
  • 思路:
  • 1、 mp3-pcm-百度語音識別 (識別率差)
  • 2、 mp3-baseUrl-騰訊ai-文字 很好免費 識別率高
  • 3、 mp3-pcm-阿里雲 要錢 用於呼喚
  • 4、 mp3-pcm-訊飛專業 要錢 最專業
  • @Author yanhui
  • @Date 2018-11-04 1:23
    */
    以上,是我本人個人觀點,參考一下就好。

語音識別

/**
     * 語音識別
     * @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碼平臺“,提供免費開發介面及演示示例(需網站註冊及微訊號關注), 技術革命,跟隨“人工智慧,大資料,雲技術”的步伐。
在這裡插入圖片描述