当前位置:   article > 正文

基于百度AI开放平台的人脸识别及语音合成

人工智能利用百度ai开放平台进行语音合成,将一段文字转换成mp3格式的语音文件

基于百度AI的人脸识别及语音合成课题

课题需求

(1)人脸识别
在Web界面上传人的照片,后台使用Java技术接收图片,然后对图片进行解码,调用云平台接口识别人脸特征,接收平台返回的人员年龄、性别、颜值等信息,将信息返回到Web界面进行显示。
(2)人脸比对
在Web界面上传两张人的照片,后台使用Java技术接收图片,然后对图片进行解码,调用云平台接口比对照片信息,返回相似度。
(3)语音识别
在Web页面上传语音文件,判断语音文件格式,如果不是wav格式进行转码处理,然后调用平台接口进行识别,最后将识别的文本内容返回到Web界面进行显示。
(4)语音合成
在Web界面上传文本内容和语音类型,后台接收文本内容和语音类型后,调用平台接口生成语音数据,最后将数据转码成mp3格式文件,Web界面可以下载到本地。

课题设计

课题基于客户端—服务端-平台端构架,客户端主要实现功能界面展示、数据上传和处理结果展示;服务器端接收客户端数据、数据转码处理、平台接口调用、请求结果相应;平台端介绍服务端数据、人脸识别、人脸比对、语音识别、语音合成等。

总体架构

1670577-20190424164017821-2011040102.png

总体逻辑

1670577-20190424164034371-839412247.png

前端设计(包括首页、人脸检测、人脸对比、语音识别及语音合成)

index.html

  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>人工智能 未来已来</title>
  6. <link rel="stylesheet" href="css/button.min.css" />
  7. <link rel="stylesheet" href="css/style.css" />
  8. <script type='text/javascript' src='js/jquery-1.11.1.min.js'></script>
  9. <script type='text/javascript' src='js/jquery.particleground.min.js'></script>
  10. <script type='text/javascript' src='js/ai.js'></script>
  11. </head>
  12. <body>
  13. <div id="context">
  14. <div class="intro">
  15. <div class="position">
  16. <h1>人工智能 未来已来</h1>
  17. <!--start button, nothing above this is necessary -->
  18. <div class="svg-wrapper">
  19. <svg height="140" width="450" xmlns="http://www.w3.org/2000/svg">
  20. <rect id="shape" height="140" width="300" />
  21. <div id="text">
  22. <a href="face_recognition.html"><span class="spot"></span>人脸检测</a>
  23. </div>
  24. </svg>
  25. </div>
  26. <div class="svg-wrapper">
  27. <svg height="140" width="450" xmlns="http://www.w3.org/2000/svg">
  28. <rect id="shape" height="140" width="300" />
  29. <div id="text">
  30. <a href="face_match.html"><span class="spot"></span>人脸比对</a>
  31. </div>
  32. </svg>
  33. </div>
  34. <!--Next button -->
  35. <div class="svg-wrapper">
  36. <svg height="140" width="450" xmlns="http://www.w3.org/2000/svg">
  37. <rect id="shape" height="140" width="300" />
  38. <div id="text">
  39. <a href="speech_recognition.html"><span class="spot"></span>语音识别</a>
  40. </div>
  41. </svg>
  42. </div>
  43. <!--Next button -->
  44. <div class="svg-wrapper">
  45. <svg height="140" width="450" xmlns="http://www.w3.org/2000/svg">
  46. <rect id="shape" height="140" width="300" />
  47. <div id="text">
  48. <a href="speech_produce.html"><span class="spot"></span>语音合成</a>
  49. </div>
  50. </svg>
  51. </div>
  52. <!--End button -->
  53. </div>
  54. </div>
  55. </div>
  56. </body>
  57. </html>

face_recognition.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>人脸识别</title>
  6. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  7. <script src="js/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
  8. <script src="js/jquery.particleground.min.js" type="text/javascript" charset="utf-8"></script>
  9. <script src="js/ai.js" type="text/javascript" charset="utf-8"></script>
  10. <link rel="stylesheet" type="text/css" href="css/style.css" />
  11. <style type="text/css">
  12. .cent_bg {
  13. width: 80%;
  14. height: 22em;
  15. border: 1px solid rgba(255, 255, 255, 0.5);
  16. margin: auto;
  17. padding: 3% 6%;
  18. border-radius: 10%;
  19. font-size: 1.5em;
  20. line-height: 2em;
  21. position: relative;
  22. }
  23. .cent {
  24. margin-top: 1.5em;
  25. overflow-y: auto;
  26. height: 80%;
  27. text-indent: 2em;
  28. text-align: left;
  29. padding-right: 0.2em;
  30. }
  31. ::-webkit-scrollbar {
  32. width: 10px;
  33. background-color: rgba(255,255,255,0.2);
  34. }
  35. /*定义滚动条轨道 内阴影+圆角*/
  36. ::-webkit-scrollbar-track {
  37. border-radius: 5px;
  38. background-color: transparent;
  39. }
  40. /*定义滑块 内阴影+圆角*/
  41. ::-webkit-scrollbar-thumb {
  42. border-radius: 10px;
  43. background-color: rgba(255,255,255,0.7);
  44. }
  45. .btn{
  46. margin-top: 20px;
  47. width: 70%;
  48. height:80px;
  49. transition: 0.4s;
  50. border-radius: 1em;
  51. border: 1px solid rgba(255, 255, 255, 0.5);
  52. border-top:none;
  53. z-index: 66;
  54. background: rgba(255, 255, 255, 0.2);
  55. }
  56. .btn:hover{
  57. border: 1px solid rgba(255, 255, 255, 0.8) !important;
  58. border-top:none !important;
  59. }
  60. .btn span{
  61. width: 40%;
  62. height: 84%;
  63. margin-top:6px;
  64. line-height: 38px;
  65. display: inline-block;
  66. border: 1px solid #009FFD;
  67. color: #fff;
  68. background: rgba(255,255,255,0.4);
  69. border-radius: 4px;
  70. cursor: pointer;
  71. }
  72. .btn span:nth-of-type(1){
  73. margin-right: 15px;
  74. }
  75. .btn span:nth-of-type(2){
  76. margin-left: 15px;
  77. }
  78. .img{
  79. width: 70%;
  80. height: 100%;
  81. float: left;
  82. position: relative;
  83. }
  84. .xinxi{
  85. width: 30%;
  86. height: 100%;
  87. float: left;
  88. }
  89. .biankuang{
  90. width: 92%;
  91. height: 150%;
  92. top: -100px;
  93. position: absolute;
  94. background: url(img/biankuang.png) no-repeat;
  95. background-size: 100% 100%;
  96. }
  97. .xinxi{
  98. text-align: left;
  99. }
  100. .xinxi span{
  101. margin-top: 50px;
  102. }
  103. .xinxi span a{
  104. color: #FFFFFF;
  105. text-decoration: none;
  106. }
  107. #neirong{
  108. color: red;
  109. }
  110. </style>
  111. </head>
  112. <body>
  113. <div id="context">
  114. <div class="intro">
  115. <div class="cent_bg">
  116. <div class="img">
  117. <div class="biankuang">
  118. <img src="img/img111.jpg" id='img' style="width: 83%;height: 55%;position: absolute;left: 49px;top: 150px;"/>
  119. </div>
  120. </div>
  121. <div class="xinxi">
  122. <span style="display: block;">性别:<a href="" id="sex"></a></span>
  123. <span style="display: block;">年龄:<a href="" id="age"></a><span style="margin-left: 10px;"></span></span>
  124. <span style="display: block;">表情:<a href="" id="expression"></a></span>
  125. <span style="display: block;">颜值:<a href="" id="beauty"></a></span>
  126. <span style="display: block;"><a href="" id="neirong"></a></span>
  127. </div>
  128. </div>
  129. <div class="btn">
  130. <form id="renlian" method="post">
  131. <span style="position: relative;">提交图片
  132. <input type="file" name="image" value="" id="uploading" onchange="test()" style="opacity: 0;width: 100%;position: absolute;height: 100%;display: block;top: 0px;" />
  133. </span>
  134. <span id="tijiao">开始识别</span>
  135. </form>
  136. </div>
  137. </div>
  138. </div>
  139. </body>
  140. </html>
  141. <script type="text/javascript">
  142. function test() {
  143. var file = document.getElementById("uploading").files[0];
  144. var fr = new FileReader;
  145. var filePath = document.querySelector("#uploading").value;
  146. fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
  147. if(!fileFormat.match(/.png|.jpg|.jpeg/)) {
  148. alert('上传错误,文件格式必须为:png/jpg/jpeg');
  149. return;
  150. } else {
  151. fr.readAsDataURL(file);
  152. fr.onload = function(e) {
  153. document.getElementById("img").src = this.result;
  154. }
  155. }
  156. }
  157. $("#tijiao").click(function() {
  158. $.ajax({
  159. type: "post",
  160. url: basePath + "/FaceDetect",
  161. dataType: "json",
  162. data: new FormData($('#renlian')[0]),
  163. processData: false,
  164. contentType: false,
  165. beforeSend: function() {
  166. uploading = true;
  167. },
  168. success: function(res) {
  169. if(res.status=="200"){
  170. $("#neirong").text("");
  171. $("#sex").text(res.data.gender);
  172. $("#age").text(res.data.age);
  173. $("#expression").text(res.data.expression);
  174. $("#beauty").text(res.data.beauty);
  175. }else{
  176. $("#neirong").text("无法识别");
  177. }
  178. },
  179. error(xhr,status,error){
  180. $("#neirong").text("后台服务异常");
  181. return;
  182. }
  183. })
  184. })
  185. </script>

face_match.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>人脸比对</title>
  6. <script src="js/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
  7. <script src="js/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
  8. <script src="js/jquery.particleground.min.js" type="text/javascript" charset="utf-8"></script>
  9. <script src="js/ai.js" type="text/javascript" charset="utf-8"></script>
  10. <link rel="stylesheet" type="text/css" href="css/style.css" />
  11. <style type="text/css">
  12. #dianji:hover{
  13. transition: 0.5s;
  14. background: skyblue;
  15. }
  16. .sss{
  17. display: inline-block;
  18. line-height:100px ;
  19. border-radius: 100%;
  20. width: 200px;
  21. margin: auto;
  22. height: 200px;
  23. border: 1px solid white;
  24. margin-top: 10%;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="context">
  30. <div class="intro">
  31. <div style="overflow: hidden; position: relative;top: 40%; overflow: hidden;margin: auto;text-align: center;">
  32. <div style="border: 1px solid white; width: 30%; height: 400px; float: left;"><img id="ig1" src="" alt="" style="width: 100%; height: 100%"/></div>
  33. <div class="sss">相似度</div>
  34. <div style="border: 1px solid white; width: 30%; height: 400px; float: right;"><img id="ig2" src="" alt="" style="width: 100%; height: 100%;"/></div>
  35. </div>
  36. <form id="tttt" method="post" style="width: 80%; margin:40px auto;">
  37. <div style="width: 40%;float: left; position: relative;">
  38. <span style="position: absolute;left: 0px;background: skyblue;display: inline-block;height: 40px;line-height: 40px;width: 160px;">
  39. 点击上传
  40. </span>
  41. <input style="opacity: 0; position: absolute;left: 0px; height: 40px;" id="uploading" type="file" onchange="upload1()" name="image1">
  42. </div>
  43. <div style="float: right;bwidth: 40%;float: right;position: relative;">
  44. <span id="" style="position: absolute;right: 0px;background: skyblue;display: inline-block;height: 40px;line-height: 40px;width: 160px;">
  45. 点击上传
  46. </span>
  47. <input id="up" type="file" onchange="upload2()" name="image2" style="opacity: 0; position: absolute;right: 0px; height: 40px;" >
  48. </div>
  49. </form>
  50. <div id="dianji" style="border: 1px solid white;width: 140px;height: 40px;line-height: 40px;border-radius: 15px; margin: auto;">开始比对</div>
  51. </div>
  52. </div>
  53. </body>
  54. <script type="text/javascript">
  55. function upload1() {
  56. var file = document.getElementById("uploading").files[0];
  57. var fr = new FileReader;
  58. var filePath = document.querySelector("#uploading").value;
  59. fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
  60. if(!fileFormat.match(/.png|.jpg|.jpeg/)) {
  61. alert('上传错误,文件格式必须为:png/jpg/jpeg');
  62. return;
  63. } else {
  64. fr.readAsDataURL(file);
  65. fr.onload = function(e){
  66. document.getElementById("ig1").src = this.result;
  67. }
  68. }
  69. }
  70. function upload2() {
  71. var file = document.getElementById("up").files[0];
  72. var fr = new FileReader;
  73. var filePath = document.querySelector("#up").value;
  74. fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
  75. if(!fileFormat.match(/.png|.jpg|.jpeg/)) {
  76. alert('上传错误,文件格式必须为:png/jpg/jpeg');
  77. return;
  78. } else {
  79. fr.readAsDataURL(file);
  80. fr.onload = function(e){
  81. document.getElementById("ig2").src = this.result;
  82. }
  83. }
  84. }
  85. $(function() {
  86. $("#dianji").click(function() {
  87. $.ajax({
  88. url: basePath+"/FaceMatch",
  89. type: 'post',
  90. cache: false,
  91. data: new FormData($('#tttt')[0]),
  92. processData: false,
  93. contentType: false,
  94. dataType: "json",
  95. beforeSend: function() {
  96. uploading = true;
  97. },
  98. success: function(data) {
  99. if(data.status==200){
  100. document.querySelector(".sss").innerHTML="相似度<br>"+data.data.score;
  101. }else{
  102. $(".sss").html("相似度<br><span style='color:red'>"+data.msg+"</span>");
  103. }
  104. },
  105. error(xhr,status,error){
  106. $(".sss").html("相似度<br><span style='color:red'>后台服务异常</span>");
  107. return;
  108. }
  109. });
  110. });
  111. });
  112. </script>
  113. </html>
  114. speech_recognition.html
  115. <!DOCTYPE html>
  116. <html>
  117. <head>
  118. <meta charset="utf-8" />
  119. <title>语音识别</title>
  120. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  121. <script src="js/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
  122. <script src="js/jquery.particleground.min.js" type="text/javascript" charset="utf-8"></script>
  123. <script src="js/ai.js" type="text/javascript" charset="utf-8"></script>
  124. <link rel="stylesheet" type="text/css" href="css/style.css" />
  125. <style type="text/css">
  126. .cent_bg {
  127. width: 80%;
  128. height: 22em;
  129. border: 1px solid rgba(255, 255, 255, 0.5);
  130. margin: auto;
  131. padding: 3% 6%;
  132. border-radius: 10%;
  133. font-size: 1.5em;
  134. line-height: 2em;
  135. position: relative;
  136. }
  137. .cent {
  138. margin-top: 1.5em;
  139. overflow-y: auto;
  140. height: 80%;
  141. text-indent: 2em;
  142. text-align: left;
  143. padding-right: 0.2em;
  144. }
  145. ::-webkit-scrollbar {
  146. width: 10px;
  147. background-color: rgba(255, 255, 255, 0.2);
  148. }
  149. /*定义滚动条轨道 内阴影+圆角*/
  150. ::-webkit-scrollbar-track {
  151. border-radius: 5px;
  152. background-color: transparent;
  153. }
  154. /*定义滑块 内阴影+圆角*/
  155. ::-webkit-scrollbar-thumb {
  156. border-radius: 10px;
  157. background-color: rgba(255, 255, 255, 0.7);
  158. }
  159. #ttttt{
  160. display: none;
  161. }
  162. .btn {
  163. margin-top: 20px;
  164. width: 70%;
  165. height: 80px;
  166. transition: 0.4s;
  167. border-radius: 1em;
  168. border: 1px solid rgba(255, 255, 255, 0.5);
  169. border-top: none;
  170. z-index: 66;
  171. overflow: hidden;
  172. position: relative;
  173. background: rgba(255, 255, 255, 0.2);
  174. }
  175. .btn:hover {
  176. border: 1px solid rgba(255, 255, 255, 0.8) !important;
  177. border-top: none !important;
  178. }
  179. .btn span {
  180. width: 40%;
  181. height: 84%;
  182. margin-top: 6px;
  183. line-height: 38px;
  184. display: inline-block;
  185. border: 1px solid #009FFD;
  186. color: #fff;
  187. background: rgba(255, 255, 255, 0.4);
  188. border-radius: 4px;
  189. cursor: pointer;
  190. }
  191. .btn span:nth-of-type(1) {
  192. margin-right: 15px;
  193. position: relative;
  194. }
  195. .btn span:nth-of-type(2) {
  196. margin-left: 15px;
  197. }
  198. input{
  199. width: 100%;
  200. height: 100%;
  201. border: 1px solid red;
  202. position: absolute;
  203. top: 0;
  204. left: 0;
  205. opacity: 0;
  206. }
  207. .img{
  208. width: 60px;
  209. height: 60px;
  210. margin: auto;
  211. text-align: center;
  212. position: relative;
  213. }
  214. .img img{
  215. width: 100%;
  216. height: 100%;
  217. position: absolute;
  218. top: 0;
  219. left: 0;
  220. }
  221. .mmm{
  222. width: 100%;
  223. height: 100%;
  224. position: absolute;
  225. top: 0;
  226. left: 0;
  227. z-index: 999;
  228. display: none;
  229. cursor: pointer;
  230. background: #b9fff4;
  231. color: aqua;
  232. line-height: 80px;
  233. }
  234. </style>
  235. </head>
  236. <body>
  237. <div id="context">
  238. <div class="intro">
  239. <div class="cent_bg">
  240. <h3>语音识别内容:</h3>
  241. <div class="cent">
  242. </div>
  243. </div>
  244. <div class="btn">
  245. <span>上传文件
  246. </span>
  247. <span>开始识别</span>
  248. <div class="mmm">
  249. 正在识别...
  250. </div>
  251. </div>
  252. <form id="ttttt" action="" method="post">
  253. <input type="file" name="voice" value="">
  254. </form>
  255. </div>
  256. </div>
  257. </body>
  258. </html>
  259. <script type="text/javascript">
  260. $(function() {
  261. $(".btn span:nth-of-type(1)").click(function() {
  262. $('#ttttt input[name="voice"]').click();
  263. })
  264. $(".btn span:nth-of-type(2)").click(function() {
  265. if(document.querySelector("input").value==""){
  266. return alert("未选择文件");
  267. }
  268. $(".mmm").css("display","block");
  269. $(".cent").html('<div class="img"><img src="img/timg.gif"/></div>');
  270. $.ajax({
  271. url: basePath + "/VoiceRecognize",
  272. type: 'post',
  273. cache: false,
  274. data: new FormData($('#ttttt')[0]),
  275. processData: false,
  276. contentType: false,
  277. dataType: "json",
  278. beforeSend: function() {
  279. uploading = true;
  280. },
  281. success: function(data) {
  282. if (data.status=="200") {
  283. $(".cent").html(data.data.text);
  284. $(".mmm").css("display","none");
  285. } else{
  286. $(".cent").html("未能识别 "+data.msg);
  287. $(".mmm").css("display","none");
  288. }
  289. }
  290. });
  291. });
  292. });
  293. </script>

speech_produce.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>语音合成</title>
  6. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  7. <script src="js/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
  8. <script src="js/jquery.particleground.min.js" type="text/javascript" charset="utf-8"></script>
  9. <script src="js/ai.js" type="text/javascript" charset="utf-8"></script>
  10. <link rel="stylesheet" type="text/css" href="css/style.css" />
  11. <style type="text/css">
  12. #error {
  13. width: 60%;
  14. height: 120px;
  15. line-height: 120px;
  16. text-align: center;
  17. font-size: 1.5em;
  18. position: absolute;
  19. top: 50%;
  20. left: 50%;
  21. transform: translate(-50%, -65%);
  22. z-index: 999;
  23. }
  24. .cent_bg {
  25. width: 80%;
  26. height: 22em;
  27. border: 1px solid rgba(255, 255, 255, 0.5);
  28. margin: auto;
  29. padding: 3% 6%;
  30. border-radius: 10%;
  31. font-size: 1.5em;
  32. line-height: 2em;
  33. position: relative;
  34. }
  35. .cent {
  36. margin-top: 1em;
  37. width: 100%;
  38. background: rgba(0, 0, 0, .5);
  39. overflow-y: auto;
  40. height: 80%;
  41. color: white;
  42. font-size: 1.3em;
  43. text-indent: 1em;
  44. padding: .8em;
  45. border: none;
  46. resize: none;
  47. outline-color: white;
  48. }
  49. ::-webkit-scrollbar {
  50. width: 10px;
  51. background-color: rgba(255, 255, 255, 0.2);
  52. }
  53. /*定义滚动条轨道 内阴影+圆角*/
  54. ::-webkit-scrollbar-track {
  55. border-radius: 5px;
  56. background-color: transparent;
  57. }
  58. /*定义滑块 内阴影+圆角*/
  59. ::-webkit-scrollbar-thumb {
  60. border-radius: 10px;
  61. background-color: rgba(255, 255, 255, 0.7);
  62. }
  63. .btn {
  64. width: 70%;
  65. height: 40px;
  66. transition: 0.4s;
  67. border-bottom-left-radius: 1em;
  68. border-bottom-right-radius: 1em;
  69. border: 1px solid rgba(255, 255, 255, 0);
  70. border-top: none;
  71. z-index: 66;
  72. background: rgba(255, 255, 255, 0.2);
  73. padding: 5px 0px;
  74. }
  75. .btn #btnSub:hover {
  76. border: 1px solid rgba(255, 255, 255) !important;
  77. color: white;
  78. }
  79. .btn #btnSub {
  80. width: 40%;
  81. height: 100%;
  82. line-height: 25px;
  83. display: inline-block;
  84. border: 1px solid #009FFD;
  85. color: #fff;
  86. background: rgba(255, 255, 255, 0.4);
  87. border-radius: 4px;
  88. font-size: 1.1em;
  89. cursor: pointer;
  90. }
  91. #select {
  92. margin: auto;
  93. margin-top: 10px;
  94. width: 70%;
  95. height: 30px;
  96. border-top-left-radius: 1em;
  97. border-top-right-radius: 1em;
  98. transition: 0.4s;
  99. border-top: none;
  100. z-index: 66;
  101. background: rgba(255, 255, 255, 0.2);
  102. }
  103. #selectBox {
  104. width: 70%;
  105. height: 30px;
  106. margin: 0 auto;
  107. }
  108. #mantext,
  109. #womantext {
  110. font-size: 1.25em;
  111. line-height: 30px;
  112. float: left;
  113. }
  114. #mantext {
  115. text-align: center;
  116. }
  117. #woman {}
  118. .inputBox {
  119. width: 50%;
  120. height: 30px;
  121. float: left;
  122. }
  123. #man,
  124. #woman {
  125. margin-top: 6px;
  126. display: block;
  127. float: left;
  128. width: 20px;
  129. height: 20px;
  130. }
  131. </style>
  132. </head>
  133. <body>
  134. <div id="error">请在此输入文本内容</div>
  135. <div id="context">
  136. <div class="intro">
  137. <form method="post" enctype="multipart/form-data" id="Voice">
  138. <div class="cent_bg">
  139. <h3>请输入要合成的语音文本:</h3>
  140. <textarea id="Content" class="cent" name="text"></textarea>
  141. </div>
  142. <div id="select">
  143. <div id="selectBox">
  144. <div class="inputBox">
  145. <label id="mantext" for="man" style="float: right;">男声</label><input id="man" type="radio" name="voiceType" checked value="1" style="float: right;" />
  146. </div>
  147. <div class="inputBox">
  148. <input id="woman" type="radio" name="voiceType" value="2" /><label id="womantext" for="woman">女声</label>
  149. </div>
  150. </div>
  151. </div>
  152. <div class="btn">
  153. <button id="btnSub" type="submit">合成语音</button>
  154. </div>
  155. </form>
  156. </div>
  157. </div>
  158. </body>
  159. <script>
  160. $(function() {
  161. var cor=0;
  162. var stop=setInterval(function(){
  163. $('#error').fadeToggle(700);
  164. cor++;
  165. if(cor==3){
  166. clearInterval(stop);
  167. }
  168. },700);
  169. $('#btnSub').on('click', function() {
  170. cor=0;
  171. var Text = $('#Content').val();
  172. if(Text.length == 0) {
  173. var stop=setInterval(function(){
  174. $('#error').fadeToggle(700);
  175. cor++;
  176. if(cor==4){
  177. clearInterval(stop);
  178. }
  179. },700);
  180. return false;
  181. }
  182. });
  183. // 服务器请求地址
  184. $('#Voice').attr('action', basePath+"/VoiceGen");
  185. });
  186. </script>
  187. </html>

ai.js

  1. // 服务器主地址
  2. var basePath="127.0.0.1:8080/AIProject"
  3. // 背景效果
  4. $(document).ready(function() {
  5. $('#context').particleground({
  6. dotColor: '#5cbdaa',
  7. lineColor: '#5cbdaa'
  8. });
  9. $('.intro').css({
  10. 'margin-top': -($('.intro').height() / 2)
  11. });
  12. });

style.css

  1. /*********CSS初始化*********/
  2. html,
  3. body,
  4. div,
  5. span,
  6. applet,
  7. object,
  8. iframe,
  9. h1,
  10. h2,
  11. h3,
  12. h4,
  13. h5,
  14. h6,
  15. p,
  16. blockquote,
  17. pre,
  18. a,
  19. abbr,
  20. acronym,
  21. address,
  22. big,
  23. cite,
  24. code,
  25. del,
  26. dfn,
  27. em,
  28. img,
  29. ins,
  30. kbd,
  31. q,
  32. s,
  33. samp,
  34. small,
  35. strike,
  36. strong,
  37. sub,
  38. sup,
  39. tt,
  40. var,
  41. b,
  42. u,
  43. i,
  44. center,
  45. dl,
  46. dt,
  47. dd,
  48. ol,
  49. ul,
  50. li,
  51. fieldset,
  52. form,
  53. label,
  54. legend,
  55. table,
  56. caption,
  57. tbody,
  58. tfoot,
  59. thead,
  60. tr,
  61. th,
  62. td,
  63. article,
  64. aside,
  65. canvas,
  66. details,
  67. embed,
  68. figure,
  69. figcaption,
  70. footer,
  71. header,
  72. hgroup,
  73. menu,
  74. nav,
  75. output,
  76. ruby,
  77. section,
  78. summary,
  79. time,
  80. mark,
  81. audio,
  82. video {
  83. margin: 0;
  84. padding: 0;
  85. border: 0;
  86. font-size: 100%;
  87. font: inherit;
  88. vertical-align: baseline;
  89. }
  90. article,
  91. aside,
  92. details,
  93. figcaption,
  94. figure,
  95. footer,
  96. header,
  97. hgroup,
  98. menu,
  99. nav,
  100. section {
  101. display: block;
  102. }
  103. body {
  104. line-height: 1;
  105. }
  106. ol,
  107. ul {
  108. list-style: none;
  109. }
  110. blockquote,
  111. q {
  112. quotes: none;
  113. }
  114. blockquote:before,
  115. blockquote:after,
  116. q:before,
  117. q:after {
  118. content: '';
  119. content: none;
  120. }
  121. table {
  122. border-collapse: collapse;
  123. border-spacing: 0;
  124. }
  125. /* particleground demo */
  126. *{
  127. -webkit-box-sizing: border-box;
  128. -moz-box-sizing: border-box;
  129. box-sizing: border-box;
  130. }
  131. html,
  132. body {
  133. width: 100%;
  134. height: 100%;
  135. /*overflow: scroll;*/
  136. }
  137. /*********CSS初始化结束*********/
  138. body {
  139. background: #202AA3;
  140. font-family: 'Montserrat', sans-serif;
  141. color: #fff;
  142. line-height: 1.3;
  143. -webkit-font-smoothing: antialiased;
  144. }
  145. #particles {
  146. width: 100%;
  147. height: 100%;
  148. overflow: hidden;
  149. }
  150. .intro {
  151. position: absolute;
  152. left: 0;
  153. top: 50%;
  154. padding: 0 20px;
  155. width: 100%;
  156. text-align: center;
  157. }
  158. h1 {
  159. text-transform: uppercase;
  160. font-size: 85px;
  161. font-weight: 700;
  162. letter-spacing: 0.015em;
  163. }
  164. h1::after {
  165. content: '';
  166. width: 60%;
  167. display: block;
  168. background: #fff;
  169. height: 10px;
  170. margin: 30px auto;
  171. line-height: 1.1;
  172. }
  173. p {
  174. margin: 0 0 30px 0;
  175. font-size: 24px;
  176. }
  177. .btn {
  178. display: inline-block;
  179. padding: 15px 30px;
  180. border: 2px solid #fff;
  181. text-transform: uppercase;
  182. letter-spacing: 0.015em;
  183. font-size: 18px;
  184. font-weight: 700;
  185. line-height: 1;
  186. color: #fff;
  187. text-decoration: none;
  188. -webkit-transition: all 0.4s;
  189. -moz-transition: all 0.4s;
  190. -o-transition: all 0.4s;
  191. transition: all 0.4s;
  192. }
  193. .btn:hover {
  194. color: #005544;
  195. border-color: #005544;
  196. }
  197. @media only screen and (max-width: 1000px) {
  198. h1 {
  199. font-size: 70px;
  200. }
  201. }
  202. @media only screen and (max-width: 800px) {
  203. h1 {
  204. font-size: 48px;
  205. }
  206. h1::after {
  207. height: 8px;
  208. }
  209. }
  210. @media only screen and (max-width: 568px) {
  211. .intro {
  212. padding: 0 10px;
  213. }
  214. h1 {
  215. font-size: 30px;
  216. }
  217. h1::after {
  218. height: 6px;
  219. }
  220. p {
  221. font-size: 18px;
  222. }
  223. .btn {
  224. font-size: 16px;
  225. }
  226. }
  227. @media only screen and (max-width: 320px) {
  228. h1 {
  229. font-size: 28px;
  230. }
  231. h1::after {
  232. height: 4px;
  233. }
  234. }

接口规范

数据交互类型:JSON
请求数据:请求数据除了请求参数以外,还需另外发送以下参数:(否则会返回403状态码)

返回数据格式:
{"status": "200","msg":"","data": {"namename":"user","password":"password"}}
1670577-20190424164302353-1713735814.png

(1)人脸识别
接口名:FaceDetect
请求参数:
1670577-20190424164324105-2146475225.png

返回参数:
1670577-20190424164348261-148075998.png

(2)人脸比对
接口名:FaceMatch
请求参数:
1670577-20190424164427669-777771077.png

返回参数:
1670577-20190424164446543-1945507329.png

(3)语音识别
接口名:VoiceRecognize
请求参数:
1670577-20190424164504835-1762499704.png

返回参数:
1670577-20190424164538950-1505695273.png

(4)语音生成
接口名:VoiceGen
1670577-20190424164554763-108736294.png

返回参数:
Mp3音频格式文件

请求注意事项

 请求体格式化:Content-Type为application/json,通过json格式化请求体。
 Base64编码:请求的图片需经过Base64编码,图片的base64编码指将图片数据编码成一串字符串,使用该字符串代替图像地址。您可以首先得到图片的二进制,然后用Base64格式编码即可。需要注意的是,图片的base64编码是不包含图片头的,如data:image/jpg;base64,
 图片格式:现支持PNG、JPG、JPEG、BMP,不支持GIF图片

实例代码

1. 人脸识别实例代码

  1. // 配置请求参数
  2. HashMap<String, String> options = new HashMap<String, String>();
  3. options.put("face_field", "age,gender,glasses,beauty,expression");
  4. options.put("max_face_num", "2");
  5. options.put("face_type", "LIVE");
  6. // 转换成base64
  7. String image = Base64Util.part2Base64(imagePart);
  8. String imageType = "BASE64";
  9. // 接口调用,并返回JSON数据
  10. JSONObject json = client.detect(image, imageType, options);
  11. // 响应数据处理
  12. Map<String, Object> map = new HashMap<>();
  13. // 获取人脸信息列表
  14. JSONObject result = json.getJSONObject("result").getJSONArray("face_list").getJSONObject(0);
  15. // 响应数据:性别
  16. JSONObject genderObj = result.getJSONObject("gender");
  17. String genderStr = genderObj.getString("type");
  18. if(genderObj.getDouble("probability") >= 0.6) {//概率并转换
  19. if("female".equals(genderStr)) {
  20. genderStr = "女";
  21. }else if("male".equals(genderStr)) {
  22. genderStr = "男";
  23. }
  24. }
  25. map.put("gender", genderStr);
  26. // 返回接口数据
  27. return ResponseData.success(map);

2. 人脸对比实例代码

  1. // 转换成base64
  2. String image1 = Base64Util.part2Base64(imagePart1);
  3. String image2 = Base64Util.part2Base64(imagePart2);
  4. // 封装平台接口请求对象
  5. MatchRequest req1 = new MatchRequest(image1, "BASE64");
  6. MatchRequest req2 = new MatchRequest(image2, "BASE64");
  7. ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
  8. requests.add(req1);
  9. requests.add(req2);
  10. // 人脸匹配
  11. JSONObject json = client.match(requests);
  12. // 响应数据处理
  13. Map<String, Object> map = new HashMap<>();
  14. // 匹配分值
  15. double score = json.getJSONObject("result").getDouble("score");
  16. return ResponseData.success(map);

3. 语音识别实例代码

  1. // 文件类型
  2. String fileType = voicePart.getContentType();
  3. if(fileType.endsWith("mp3")) {
  4. fileType = MP3;
  5. }else if(fileType.endsWith("wav")) {
  6. fileType = WAV;
  7. }else {
  8. return ResponseData.fail("请上传mp3、wav音频");
  9. }
  10. try {
  11. // 获取音频字符流
  12. InputStream is = voicePart.getInputStream();
  13. // 保存临时音频文件
  14. String filename = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar.getInstance().getTime());
  15. File tmpVoice = new File(workspace + File.separator + filename+fileType);
  16. VoiceUtil.saveVoiceFile(is, tmpVoice);
  17. if(fileType.equals(MP3)) {
  18. File mp3File = tmpVoice;
  19. tmpVoice = new File(tmpVoice.getPath().replace(MP3, WAV));
  20. if(!VoiceUtil.mp3ToWav(mp3File, tmpVoice)) {
  21. return ResponseData.fail("mp3音频文件错误,请用wav音频。");
  22. }
  23. mp3File.delete();
  24. }
  25. // 调用百度接口
  26. JSONObject json = client.asr(tmpVoice.getPath(), "wav", 16000, null);
  27. tmpVoice.delete();
  28. Integer status = json.getInt("err_no"); //状态码
  29. if(status != 0) {
  30. // 异常响应处理
  31. String msg = json.getString("err_msg");
  32. log.warn("百度接口调用响应异常,error_code:"+status + " error_msg:"+msg);
  33. return ResponseData.fail(msg);
  34. }
  35. // 响应数据处理
  36. Map<String, Object> map = new HashMap<>();
  37. // 获取结果
  38. JSONArray jsonArray = json.getJSONArray("result");
  39. // 识别文本
  40. String text = jsonArray.getString(0);
  41. map.put("text", text);
  42. return ResponseData.success(map);
  43. } catch (Exception e) {
  44. log.warn("识别音频文件错误:", e);;
  45. }

4. 语音合成实例代码

  1. // 请求参数
  2. HashMap<String, Object> options = new HashMap<String, Object>();
  3. // 语速,取值0-9,默认为5中语速
  4. options.put("spd", "5");
  5. // 音调,取值0-9,默认为5中语调
  6. options.put("pit", "5");
  7. // 发音人选择, 0为女声,1为男声, 3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女
  8. options.put("per", voiceType);
  9. // 调用百度api接口
  10. TtsResponse res = client.synthesis(text, "zh", 1, options);
  11. byte[] data = res.getData();
  12. return data;

效果展示

1670577-20190424164815590-589723684.png

1670577-20190424164924247-1113678498.png

1670577-20190424164944131-723848397.png

1670577-20190424165021819-488227002.png

1670577-20190424165133041-954755429.png

源码下载地址:https://github.com/jcdjor/AIProject

PS:欢迎大家给予评论、建议和下载学习,下面为源码的一些说明

  • AIProject.zip 为后端代码,为eclipse项目,app.properties文件需要自己配置百度云开发平台的AppID、APIKey、SecretKey。
  • Web.zip 为前端代码,前后端分离,可直接运行使用 。
  • 运行环境,对版本没太大要求,但jdk和tomcat要对应 JDK:我使用的版本为JDK1.8,官方下载地址
    Tomcat:,我使用的版本为tomcat 9,官方下载地址
  • 注意需要百度云开放平台的AppID、APIKey、SecretKey,百度云AI开放平台:http://ai.baidu.com/

声明:本文欢迎大家评论和转载,使用本文章或代码还请声明,且在使用处的明显位置给出。如有其它问题或有什么建议,可在下方评论,或加QQ(1414782205),或发邮箱jcdjor@163.com。

转载于:https://www.cnblogs.com/jcdjor/p/10762738.html

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

闽ICP备14008679号