概述
折腾好久, 终于发现大厂的文档也堪称混乱. 首先:要用腾讯AI有两个网址可以注册, 算是2种方案, 第一个 https://ai.qq.com/product/face.shtml#shape , 这个我不推荐. 估计是旧版本吧, 用起来很不方便. 这个网址注册出来, 会给每个应用分配一个APPID 和 APPKEY, 格式为: APPID : 2125xxxxxx , APPKEY: jgzPJhxndVxxxxxx , 注意长度明显和下文中注册得到的不一样. 要求自己按文档格式实现HTTP访问的诸多字符串准备, 编程很麻烦.
真正好用的是第二种方法, 在腾讯云上直接注册得到的, 网址是: https://cloud.tencent.com/act/event/aiAllFree#mod-hot2
不管你用哪个服务, 随便点一个接入服务, 比如我点击五官定位, 先得到 SecretId 和 SecretKey , 之后再点接入其他服务, 比如我用了"录音文件识别"和"五官定位", 在这2个服务下面都点了"0元接入" , 否则后面运行程序会提示出错的. 所有应用都用同一个secretid和secretKey, 在账号名字上点击, 出现下图, 点击"项目管理"
再点击访问密钥
新建密钥
用的是这个SecretID(长度36字符)和SecretKey(长度32), 把这个问题搞清楚, 下面的都很简单了. 我一直以为要第一个网址上注册得到的短ID和key, 浪费不少时间.
打开vs2015 , 新建一个C# winform项目, , vs菜单:工具->Nuget 管理器->管理解决方案的NuGet程序包, 安装TencentCloudSDK v3.0.42, emgu.cv, newtonjson等
腾讯云原始SDK的例子,完全把人搞懵,我要语音识别,五官定位,你叫我先整明白啥叫cvm? 虚拟主机和AI天壤之别好吗? 所有代码中的注释都完全一样.废话注释还特别多, 从百十行重复废话注释中寻找5,6行有用的代码. 真不敢相信大厂能这么写代码. 删除掉注释后发现其实腾讯SDK真的用起来好简单, 所有应用使用同一个ID和KEY, 下面的代码实现了cvm主机查询(原厂例子), 语音mp3识别文字, 五官定位并显示3个功能. 记录下来防止走弯路. 注意里面的ID/KEY替换成自己的,否则提示鉴权错误.
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Cvm.V20170312;
using TencentCloud.Cvm.V20170312.Models;
using TencentCloud.Aai.V20180522;
using TencentCloud.Aai.V20180522.Models;
using TencentCloud.Iai.V20180301;
using TencentCloud.Iai.V20180301.Models;
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Linq;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
namespace WindowsFormsApp1
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private async Task<string> GetZones()
{
try
{
// 必要步骤: // 腾讯云账户密钥对secretId,secretKey。
// 下面全部应当替换成自己的ID和key才能运行.
Credential cred = new Credential
{
SecretId = "AKIDEdw5zjxxxxxxxxxxxxxxxxxxxxxxxxxx",
SecretKey = "5FBsKbkxxxxxxxxxxxxxxxxxxxxxxxxx"
};
// 实例化一个client, 不必要的选项都屏蔽
ClientProfile clientProfile = new ClientProfile();
HttpProfile httpProfile = new HttpProfile();
// 代理服务器,当你的环境下有代理服务器时设定
httpProfile.WebProxy = Environment.GetEnvironmentVariable("HTTPS_PROXY");
clientProfile.HttpProfile = httpProfile;
// 实例化要请求产品(以cvm为例)的client对象
CvmClient client = new CvmClient(cred, "ap-guangzhou", clientProfile);
DescribeZonesRequest req = new DescribeZonesRequest();
DescribeZonesResponse resp = await client.DescribeZones(req);
// 输出json格式的字符串回包
return AbstractModel.ToJsonString(resp);
} catch (Exception e)
{
return e.ToString();
}
}
private async void Button1_Click(object sender, EventArgs e)
{
var result = await GetZones();
textBox1.Text = result;
}
private async void btnMp3ToTxt_Click(object sender, EventArgs e)
{
txtBoxMp3.Text = "";
var result = await GetTxtFromMp3( txtBoxFileName.Text);
txtBoxMp3.Text = result;
}
private async Task<string> GetTxtFromMp3(string strFullPathMp3FileName)
{
try
{
// 必要步骤:腾讯云账户密钥对secretId,secretKey。
Credential cred = new Credential
{
SecretId = "AKIDEdw5zjxxxxxxxxxxxxxxxxxxxxxxxxxx",
SecretKey = "5FBsKbkxxxxxxxxxxxxxxxxxxxxxxxxx"
};
// 实例化一个client选项
ClientProfile clientProfile = new ClientProfile();
HttpProfile httpProfile = new HttpProfile();
clientProfile.HttpProfile = httpProfile;
AaiClient client = new AaiClient(cred, "ap-guangzhou", clientProfile);
//开始语音mp3转文字, 成对使用 SentenceRecognitionRequest/ SentenceRecognitionResponse
SentenceRecognitionRequest req = new SentenceRecognitionRequest();
string strFilepath = strFullPathMp3FileName;
FileStream fs = new FileStream(strFilepath, FileMode.Open);
byte[] raw = new byte[fs.Length];
fs.Read(raw, 0, raw.Length);
string base64data = Convert.ToBase64String(raw); //mp3文件的数据用Base64编码
fs.Close();
// 语音识别request的设置。
req.ProjectId = 0;
req.SubServiceType = 2;
//必须8或16k否则返回不接受,但实际文件32k/64k也能接受,但会识别错误
req.EngSerViceType = "16k";
req.SourceType = 1;
req.VoiceFormat = "mp3";
//随便输,系统其实没返回这个ID,和文档不一样
req.UsrAudioKey = "my_usrid_5c79510a12da-whatever";
//base64编码数据设置为该Request的Data
req.Data = base64data;
req.DataLen = base64data.Length;
// 异步提交语言识别Request
SentenceRecognitionResponse resp = await client.SentenceRecognition(req);
// 输出json格式的字符串回包
return AbstractModel.ToJsonString(resp);
}
catch (Exception e)
{
return e.ToString();
}
}
private void btnOpenMp3_Click(object sender, EventArgs e)
{ // 打开mp3文件对话框
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
txtBoxFileName.Text = ofd.FileName;
}
}
private void btnOpenPic_Click(object sender, EventArgs e)
{ // 打开图片文件对话框
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
txtBoxPic.Text = ofd.FileName;
}
}
private async void btnFaceMarks_Click(object sender, EventArgs e)
{
txtBoxAnalyzeFaceResult.Text = "";
var result = await GetFaceMarks(txtBoxPic.Text);
txtBoxAnalyzeFaceResult.Text = result; //人脸json显示到textbox
}
private async Task<string> GetFaceMarks(string strFullPathPicFileName)
{
try
{
// 必要步骤: // 腾讯云账户密钥对secretId,secretKey。
Credential cred = new Credential
{
SecretId = "AKIDEdw5zjxxxxxxxxxxxxxxxxxxxxxxxxxx",
SecretKey = "5FBsKbkxxxxxxxxxxxxxxxxxxxxxxxxx"
};
// 实例化一个client选项
ClientProfile clientProfile = new ClientProfile();
HttpProfile httpProfile = new HttpProfile();
clientProfile.HttpProfile = httpProfile;
IaiClient client = new IaiClient(cred, "ap-guangzhou", clientProfile);
//这里开始 anlyzeFace ,五官定位, 成对使用 AnalyzeFaceRequest/ AnalyzeFaceResponse
AnalyzeFaceRequest faceReq = new AnalyzeFaceRequest() ;
if ( txtPicUrl.Text.Length>0 )
{ // 若faceReq同时有url和Image, 腾讯云优先用url.
faceReq.Url = txtPicUrl.Text;
} else
{
if ( strFullPathPicFileName.Length>0 )
{ // 用filestream 读取本地图片文件
string strFilepath = strFullPathPicFileName;
FileStream fs = new FileStream(strFilepath, FileMode.Open);
byte[] raw = new byte[fs.Length];
fs.Read(raw, 0, raw.Length);
string base64data = Convert.ToBase64String(raw); //图片文件的数据用Base64编码
fs.Close();
faceReq.Image = base64data; // base64编码后的字符串设置为Request的Image
}
else
{
MessageBox.Show("请选择一个本地图片或在下框输入图片网址");
return "";
}
}
// 异步方式发出request, 等待结果
AnalyzeFaceResponse resp = await client.AnalyzeFace( faceReq);
// 输出json格式的字符串回包
return AbstractModel.ToJsonString(resp);
}
catch (Exception e)
{
return e.ToString();
}
}
private async void btnGetJsonDrawPic_Click(object sender, EventArgs e)
{ // 解析五官定位的json数据并用opencv画出.
string strJson = txtBoxAnalyzeFaceResult.Text;
JObject joResult = (JObject)JsonConvert.DeserializeObject(strJson);
// 图像的宽度高度
int iWidth = Convert.ToInt32(joResult["ImageWidth"].ToString() );
int iHeight = Convert.ToInt32(joResult["ImageHeight"].ToString());
string strReqId = joResult["RequestId"].ToString();
Mat src = new Mat(txtBoxPic.Text); // OpenCV 准备显示照片
MCvScalar ptColorRed = new Bgr(0, 0, 255).MCvScalar; // 标志点设置为红色
MCvScalar ptColorBlue = new Bgr(255, 0, 0).MCvScalar; // 文字设置为蓝色
// 脸型数据
string strFaceShapeSet = joResult["FaceShapeSet"].ToString();
JArray jarFaceShpSet = (JArray)JsonConvert.DeserializeObject(strFaceShapeSet);
//获得FaceShapeSet对象的所有字段名
var itemShpSetFieldName = ((JObject)jarFaceShpSet[0]).Properties().ToList();
foreach (var itemField in itemShpSetFieldName)
{
string itemName = itemField.Name.ToString();
string itemValue = itemField.Value.ToString();
JArray jarItemDetial = (JArray)JsonConvert.DeserializeObject(itemValue);
// 把每个字段下的标志点画圆形或显示编号到照片上. 每段从0开始编号
int iIndex = 0;
foreach (var item in jarItemDetial)
{
System.Drawing.Point pt = new System.Drawing.Point(
Convert.ToInt32(item["X"].ToString()),
Convert.ToInt32(item["Y"].ToString()));
CvInvoke.Circle(src, pt, 1 , ptColorRed , 2); // 画圆点
//System.Drawing.Point pt2 = new System.Drawing.Point();
// pt2 = pt;
// pt2.Offset(1, 1); //错开一个像素,否则Line画不出来
// CvInvoke.Line(src,pt, pt2 , ptColorRed); // 用画线代替画点,在emgucv里修改一个像素点麻烦,不如直接画线,画圆太大
CvInvoke.PutText(src, iIndex.ToString(), pt, FontFace.HersheyComplex, 0.4, ptColorBlue); // 显示编号,缩小到0.4显示
CvInvoke.Imshow("test", src);
CvInvoke.WaitKey(50); // 50ms 刷一帧, 动画效果显示
iIndex++;
}
}
//CvInvoke.Resize(src, src ,new Size(src.Width * 2 , src.Height * 2) ); //图片中脸部太小就resize看结果,大脸图不用
// CvInvoke.Imshow("test", src);
}
private void btnShowImg_Click(object sender, EventArgs e)
{ // 测试emgu.cv
Mat src = new Mat(txtBoxPic.Text);
CvInvoke.Circle(src, new System.Drawing.Point(50, 50), 20, new Bgr(0, 0, 255).MCvScalar, 2);
CvInvoke.Imshow("test", src);
}
private void btnShowPngAlpha_Click(object sender, EventArgs e)
{
// Mat image = new Mat(txtBoxPic.Text);
}
}
比较逼真的漫画人物也能识别, 2个瞳孔也识别了, 这个还是可以的. 稍微不足的是发迹线没有识别.
MainForm Design的代码 , 对了,啰嗦一句,如果使用网址图片, 应当不会正确画图, 但是解析Json数据都正确 因我没有把图片下载本地, 以后去完善吧. 要显示图片是点击里面的"解析json" 按钮, "ShowImg" 按钮只是测试
namespace WindowsFormsApp1
{
partial class MainForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.btnMp3ToTxt = new System.Windows.Forms.Button();
this.txtBoxMp3 = new System.Windows.Forms.TextBox();
this.btnOpenMp3 = new System.Windows.Forms.Button();
this.txtBoxFileName = new System.Windows.Forms.TextBox();
this.btnOpenPic = new System.Windows.Forms.Button();
this.txtBoxPic = new System.Windows.Forms.TextBox();
this.btnFaceMarks = new System.Windows.Forms.Button();
this.txtBoxAnalyzeFaceResult = new System.Windows.Forms.TextBox();
this.txtPicUrl = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.btnGetJsonDrawPic = new System.Windows.Forms.Button();
this.btnShowImg = new System.Windows.Forms.Button();
this.btnShowPngAlpha = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(478, 13);
this.button1.Margin = new System.Windows.Forms.Padding(4);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(144, 30);
this.button1.TabIndex = 0;
this.button1.Text = "GetZones";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(-1, 0);
this.textBox1.Margin = new System.Windows.Forms.Padding(4);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(471, 88);
this.textBox1.TabIndex = 1;
//
// btnMp3ToTxt
//
this.btnMp3ToTxt.Location = new System.Drawing.Point(471, 95);
this.btnMp3ToTxt.Name = "btnMp3ToTxt";
this.btnMp3ToTxt.Size = new System.Drawing.Size(130, 41);
this.btnMp3ToTxt.TabIndex = 2;
this.btnMp3ToTxt.Text = "语音MP3转文字";
this.btnMp3ToTxt.UseVisualStyleBackColor = true;
this.btnMp3ToTxt.Click += new System.EventHandler(this.btnMp3ToTxt_Click);
//
// txtBoxMp3
//
this.txtBoxMp3.Location = new System.Drawing.Point(-1, 142);
this.txtBoxMp3.Multiline = true;
this.txtBoxMp3.Name = "txtBoxMp3";
this.txtBoxMp3.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtBoxMp3.Size = new System.Drawing.Size(623, 98);
this.txtBoxMp3.TabIndex = 3;
//
// btnOpenMp3
//
this.btnOpenMp3.Location = new System.Drawing.Point(366, 95);
this.btnOpenMp3.Name = "btnOpenMp3";
this.btnOpenMp3.Size = new System.Drawing.Size(75, 41);
this.btnOpenMp3.TabIndex = 4;
this.btnOpenMp3.Text = "Open";
this.btnOpenMp3.UseVisualStyleBackColor = true;
this.btnOpenMp3.Click += new System.EventHandler(this.btnOpenMp3_Click);
//
// txtBoxFileName
//
this.txtBoxFileName.Location = new System.Drawing.Point(-1, 95);
this.txtBoxFileName.Multiline = true;
this.txtBoxFileName.Name = "txtBoxFileName";
this.txtBoxFileName.Size = new System.Drawing.Size(361, 38);
this.txtBoxFileName.TabIndex = 5;
//
// btnOpenPic
//
this.btnOpenPic.Location = new System.Drawing.Point(385, 246);
this.btnOpenPic.Name = "btnOpenPic";
this.btnOpenPic.Size = new System.Drawing.Size(56, 34);
this.btnOpenPic.TabIndex = 6;
this.btnOpenPic.Text = "Open";
this.btnOpenPic.UseVisualStyleBackColor = true;
this.btnOpenPic.Click += new System.EventHandler(this.btnOpenPic_Click);
//
// txtBoxPic
//
this.txtBoxPic.Location = new System.Drawing.Point(-1, 246);
this.txtBoxPic.Multiline = true;
this.txtBoxPic.Name = "txtBoxPic";
this.txtBoxPic.Size = new System.Drawing.Size(380, 34);
this.txtBoxPic.TabIndex = 7;
this.txtBoxPic.Text = "E:\Drawings\脸部照片\test_1.jpg";
//
// btnFaceMarks
//
this.btnFaceMarks.Location = new System.Drawing.Point(449, 246);
this.btnFaceMarks.Name = "btnFaceMarks";
this.btnFaceMarks.Size = new System.Drawing.Size(75, 35);
this.btnFaceMarks.TabIndex = 8;
this.btnFaceMarks.Text = "五官定位";
this.btnFaceMarks.UseVisualStyleBackColor = true;
this.btnFaceMarks.Click += new System.EventHandler(this.btnFaceMarks_Click);
//
// txtBoxAnalyzeFaceResult
//
this.txtBoxAnalyzeFaceResult.Location = new System.Drawing.Point(-1, 332);
this.txtBoxAnalyzeFaceResult.Multiline = true;
this.txtBoxAnalyzeFaceResult.Name = "txtBoxAnalyzeFaceResult";
this.txtBoxAnalyzeFaceResult.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtBoxAnalyzeFaceResult.Size = new System.Drawing.Size(623, 180);
this.txtBoxAnalyzeFaceResult.TabIndex = 9;
//
// txtPicUrl
//
this.txtPicUrl.Location = new System.Drawing.Point(141, 287);
this.txtPicUrl.Multiline = true;
this.txtPicUrl.Name = "txtPicUrl";
this.txtPicUrl.Size = new System.Drawing.Size(492, 39);
this.txtPicUrl.TabIndex = 10;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(10, 290);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(121, 15);
this.label1.TabIndex = 11;
this.label1.Text = "或优先用Pic网址";
//
// btnGetJsonDrawPic
//
this.btnGetJsonDrawPic.Location = new System.Drawing.Point(530, 247);
this.btnGetJsonDrawPic.Name = "btnGetJsonDrawPic";
this.btnGetJsonDrawPic.Size = new System.Drawing.Size(80, 33);
this.btnGetJsonDrawPic.TabIndex = 12;
this.btnGetJsonDrawPic.Text = "解析Json";
this.btnGetJsonDrawPic.UseVisualStyleBackColor = true;
this.btnGetJsonDrawPic.Click += new System.EventHandler(this.btnGetJsonDrawPic_Click);
//
// btnShowImg
//
this.btnShowImg.Location = new System.Drawing.Point(514, 518);
this.btnShowImg.Name = "btnShowImg";
this.btnShowImg.Size = new System.Drawing.Size(96, 25);
this.btnShowImg.TabIndex = 13;
this.btnShowImg.Text = "ShowImg";
this.btnShowImg.UseVisualStyleBackColor = true;
this.btnShowImg.Click += new System.EventHandler(this.btnShowImg_Click);
//
// btnShowPngAlpha
//
this.btnShowPngAlpha.Location = new System.Drawing.Point(359, 519);
this.btnShowPngAlpha.Name = "btnShowPngAlpha";
this.btnShowPngAlpha.Size = new System.Drawing.Size(111, 24);
this.btnShowPngAlpha.TabIndex = 14;
this.btnShowPngAlpha.Text = "透明PNG显示";
this.btnShowPngAlpha.UseVisualStyleBackColor = true;
this.btnShowPngAlpha.Click += new System.EventHandler(this.btnShowPngAlpha_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(635, 555);
this.Controls.Add(this.btnShowPngAlpha);
this.Controls.Add(this.btnShowImg);
this.Controls.Add(this.btnGetJsonDrawPic);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtPicUrl);
this.Controls.Add(this.txtBoxAnalyzeFaceResult);
this.Controls.Add(this.btnFaceMarks);
this.Controls.Add(this.txtBoxPic);
this.Controls.Add(this.btnOpenPic);
this.Controls.Add(this.txtBoxFileName);
this.Controls.Add(this.btnOpenMp3);
this.Controls.Add(this.txtBoxMp3);
this.Controls.Add(this.btnMp3ToTxt);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "MainForm";
this.Text = "Tencent Cloud Demo";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button btnMp3ToTxt;
private System.Windows.Forms.TextBox txtBoxMp3;
private System.Windows.Forms.Button btnOpenMp3;
private System.Windows.Forms.TextBox txtBoxFileName;
private System.Windows.Forms.Button btnOpenPic;
private System.Windows.Forms.TextBox txtBoxPic;
private System.Windows.Forms.Button btnFaceMarks;
private System.Windows.Forms.TextBox txtBoxAnalyzeFaceResult;
private System.Windows.Forms.TextBox txtPicUrl;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnGetJsonDrawPic;
private System.Windows.Forms.Button btnShowImg;
private System.Windows.Forms.Button btnShowPngAlpha;
}
}
最后
以上就是无语毛豆为你收集整理的vs2015 C#调用腾讯AI(腾讯云SDK)实现mp3语音文件识别和五官定位2019亲测的全部内容,希望文章能够帮你解决vs2015 C#调用腾讯AI(腾讯云SDK)实现mp3语音文件识别和五官定位2019亲测所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复