using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Net.Sockets;
|
using System.Net;
|
using System.Text;
|
using System.Threading.Tasks;
|
using TencentCloud.Ocr.V20181119;
|
using TencentCloud.Ocr.V20181119.Models;
|
using Newtonsoft.Json;
|
using TencentCloud.Common;
|
using TencentCloud.Common.Profile;
|
using Furion.DependencyInjection;
|
using Furion;
|
using TencentCloud.Iai.V20200303;
|
using TencentCloud.Iai.V20200303.Models;
|
|
|
|
|
namespace EzTencentCloud
|
{
|
public class TencentCloudService: ITencentCloudService, IScoped
|
{
|
|
|
|
|
public TencentCloudService()
|
{
|
|
}
|
#region orc 图文识别
|
/// <summary>
|
/// 剪裁后的身份证图片
|
/// </summary>
|
public string CropIdCard { get; set; }
|
|
public string GetIdCordImg()
|
{
|
if (string.IsNullOrEmpty(CropIdCard))
|
return "";
|
else
|
return CropIdCard;
|
|
}
|
|
public IDCardOCRResponse IdCord(string ImageBase64, bool isFRONT)
|
{
|
try
|
{
|
|
Credential cred = new Credential
|
{
|
SecretId = App.Configuration["TencentCloud:SecretId"] ?? "AKIDIPFp9CyThfMmvoQlpeCl34pKYVBahY9T",
|
SecretKey = App.Configuration["TencentCloud:SecretKey"] ?? "4rNcaHhrkMhmb9QQ9bmgKipfFZcOt86n"
|
};
|
|
ClientProfile clientProfile = new ClientProfile();
|
HttpProfile httpProfile = new HttpProfile();
|
|
|
httpProfile.Endpoint = ("ocr.tencentcloudapi.com");
|
clientProfile.HttpProfile = httpProfile;
|
|
var _ocrClient = new OcrClient(cred, "ap-shanghai", clientProfile);
|
|
IDCardOCRRequest req = new IDCardOCRRequest();
|
if (isFRONT)
|
req.CardSide = "FRONT";
|
else
|
req.CardSide = "BACK";
|
req.ImageBase64 = ImageBase64;
|
|
req.Config = JsonConvert.SerializeObject(new
|
{
|
//身份证照片裁剪(去掉证件外多余的边缘、自动矫正拍摄角度)
|
CropIdCard = true,
|
// CropPortrait = true,
|
//边框和框内遮挡告警
|
BorderCheckWarn = true,
|
//PS检测告警
|
DetectPsWarn = true,
|
//临时身份证告警
|
TempIdWarn = true,
|
//身份证有效日期不合法告警
|
InvalidDateWarn = true,
|
//图片质量分数(评价图片的模糊程度)
|
Quality = true,
|
|
});
|
IDCardOCRResponse resp = _ocrClient.IDCardOCRSync(req);
|
|
if (resp == null)
|
throw new Exception("图片无法识别,请重新选择身份证图片");
|
|
|
var adv = JsonConvert.DeserializeObject<AdvancedInfo>(resp.AdvancedInfo);
|
if (adv == null)
|
throw new Exception("图片无法识别,请重新选择身份证图片");
|
if (adv?.BorderCodeValue != null && adv.BorderCodeValue > 50)
|
throw new Exception("身份证图片不完整,请重新选择身份证图片");
|
if (adv?.Quality != null && adv.Quality < 50)
|
throw new Exception("图片模糊,请重新选择身份证图片");
|
if (adv?.WarnInfos?.Where(x => x == -9100) == null)
|
throw new Exception("身份证日期不合法,请重新选择身份证图片");
|
if (adv?.WarnInfos?.Where(x => x == -9106) == null)
|
throw new Exception("该图片可能是被PS过,请重新选择身份证图片");
|
|
CropIdCard = adv.IdCard;
|
return resp;
|
}
|
catch (Exception)
|
{
|
throw;
|
}
|
|
|
}
|
|
|
|
public BizLicenseOCRResponse BizLicenseOCR(string ImageBase64)
|
{
|
try
|
{
|
|
Credential cred = new Credential
|
{
|
SecretId = App.Configuration["TencentCloud:SecretId"] ?? "AKIDIPFp9CyThfMmvoQlpeCl34pKYVBahY9T",
|
SecretKey = App.Configuration["TencentCloud:SecretKey"] ?? "4rNcaHhrkMhmb9QQ9bmgKipfFZcOt86n"
|
};
|
|
ClientProfile clientProfile = new ClientProfile();
|
HttpProfile httpProfile = new HttpProfile();
|
|
|
httpProfile.Endpoint = ("ocr.tencentcloudapi.com");
|
clientProfile.HttpProfile = httpProfile;
|
|
var _ocrClient = new OcrClient(cred, "ap-shanghai", clientProfile);
|
|
var ret = _ocrClient.BizLicenseOCRSync(new BizLicenseOCRRequest()
|
{
|
ImageBase64 = ImageBase64,
|
});
|
|
return ret;
|
}
|
catch (Exception)
|
{
|
throw;
|
}
|
|
|
}
|
|
|
#endregion
|
|
#region iai 人脸识别
|
public void IaiCreatGroup()
|
{
|
|
Credential cred = new Credential
|
{
|
SecretId = App.Configuration["TencentCloud:SecretId"] ?? "AKIDIPFp9CyThfMmvoQlpeCl34pKYVBahY9T",
|
SecretKey = App.Configuration["TencentCloud:SecretKey"] ?? "4rNcaHhrkMhmb9QQ9bmgKipfFZcOt86n"
|
};
|
|
ClientProfile clientProfile = new ClientProfile();
|
HttpProfile httpProfile = new HttpProfile();
|
|
|
httpProfile.Endpoint = ("iai.tencentcloudapi.com");
|
clientProfile.HttpProfile = httpProfile;
|
|
var iaiClient = new IaiClient(cred, "ap-shanghai", clientProfile);
|
try
|
{
|
var retinfo = iaiClient.GetGroupInfoSync(new GetGroupInfoRequest
|
{
|
GroupId = App.Configuration["IAIGroupSet:ID"] ?? "",
|
});
|
}
|
catch (TencentCloudSDKException e)
|
{
|
if (e.ErrorCode == "InvalidParameterValue.GroupIdNotExist")
|
|
{
|
CreateGroupRequest request = new CreateGroupRequest()
|
{
|
GroupId = App.Configuration["IAIGroupSet:ID"],
|
GroupName= App.Configuration["IAIGroupSet:Name"],
|
|
|
};
|
var aia = iaiClient.CreateGroupSync(request);
|
}
|
}
|
|
|
|
}
|
|
|
public bool IaiAddPerso( string img64,string PersonNameId,string PersonName, int PersonGender)
|
{
|
|
Credential cred = new Credential
|
{
|
SecretId = App.Configuration["TencentCloud:SecretId"] ?? "AKIDIPFp9CyThfMmvoQlpeCl34pKYVBahY9T",
|
SecretKey = App.Configuration["TencentCloud:SecretKey"] ?? "4rNcaHhrkMhmb9QQ9bmgKipfFZcOt86n"
|
};
|
|
ClientProfile clientProfile = new ClientProfile();
|
HttpProfile httpProfile = new HttpProfile();
|
|
|
httpProfile.Endpoint = ("iai.tencentcloudapi.com");
|
clientProfile.HttpProfile = httpProfile;
|
|
var iaiClient = new IaiClient(cred, "ap-shanghai", clientProfile);
|
var ret= iaiClient.CreatePerson(new TencentCloud.Iai.V20200303.Models.CreatePersonRequest
|
{
|
GroupId = App.Configuration["IAIGroupSet:ID"],
|
Image = img64,
|
PersonId= PersonNameId,
|
PersonName= PersonName,
|
Gender=PersonGender,
|
|
});
|
|
if(ret.IsFaulted)
|
return false;
|
else
|
return true;
|
|
|
|
}
|
|
|
public bool IaiAddPersoImg(string img64, string PersonNameId, string PersonName, int PersonGender)
|
{
|
|
Credential cred = new Credential
|
{
|
SecretId = App.Configuration["TencentCloud:SecretId"] ?? "AKIDIPFp9CyThfMmvoQlpeCl34pKYVBahY9T",
|
SecretKey = App.Configuration["TencentCloud:SecretKey"] ?? "4rNcaHhrkMhmb9QQ9bmgKipfFZcOt86n"
|
};
|
|
ClientProfile clientProfile = new ClientProfile();
|
HttpProfile httpProfile = new HttpProfile();
|
|
|
httpProfile.Endpoint = ("iai.tencentcloudapi.com");
|
clientProfile.HttpProfile = httpProfile;
|
var iaiClient = new IaiClient(cred, "ap-shanghai", clientProfile);
|
var ret = iaiClient.CreateFace( new TencentCloud.Iai.V20200303.Models.CreateFaceRequest
|
{
|
|
PersonId = PersonNameId,
|
Images = [img64],
|
|
|
|
});
|
|
if (ret.IsFaulted)
|
return false;
|
else
|
return true;
|
|
|
|
}
|
|
|
public VerifyFaceResponse VerifyFace(string img64, string PersonNameId)
|
{
|
Credential cred = new Credential
|
{
|
SecretId = App.Configuration["TencentCloud:SecretId"] ?? "AKIDIPFp9CyThfMmvoQlpeCl34pKYVBahY9T",
|
SecretKey = App.Configuration["TencentCloud:SecretKey"] ?? "4rNcaHhrkMhmb9QQ9bmgKipfFZcOt86n"
|
};
|
|
ClientProfile clientProfile = new ClientProfile();
|
HttpProfile httpProfile = new HttpProfile();
|
|
|
httpProfile.Endpoint = ("iai.tencentcloudapi.com");
|
clientProfile.HttpProfile = httpProfile;
|
var iaiClient = new IaiClient(cred, "ap-shanghai", clientProfile);
|
var ret = iaiClient.VerifyFaceSync(new TencentCloud.Iai.V20200303.Models.VerifyFaceRequest
|
{
|
|
PersonId = PersonNameId,
|
Image= img64
|
|
|
|
});
|
return ret;
|
|
|
|
}
|
#endregion
|
|
}
|
|
//返回扩展参数
|
internal class AdvancedInfo
|
{
|
/// <summary>
|
/// idCard 裁剪后身份证照片的base64编码,请求 Config.CropIdCard 时返回;
|
/// </summary>
|
public string? IdCard { get; set; }
|
/// <summary>
|
/// 身份证头像照片的base64编码,请求 Config.CropPortrait 时返回;
|
/// </summary>
|
public string? Portrait { get; set; }
|
/// <summary>
|
/// 图片质量分数,请求 Config.Quality 时返回(取值范围:0 ~ 100,分数越低越模糊,建议阈值≥50);
|
/// </summary>
|
public int? Quality { get; set; }
|
/// <summary>
|
/// 身份证边框不完整告警阈值分数,请求 Config.BorderCheckWarn时返回(取值范围:0 ~ 100,分数越低边框遮挡可能性越低,建议阈值≤50);
|
/// </summary>
|
public int BorderCodeValue { get; set; }
|
/// <summary>
|
/// 告警信息,Code 告警码列表和释义:
|
///-9100 身份证有效日期不合法告警,
|
///-9101 身份证边框不完整告警,
|
///-9102 身份证复印件告警,
|
///-9103 身份证翻拍告警,
|
///-9105 身份证框内遮挡告警,
|
///-9104 临时身份证告警,
|
///-9106 身份证 PS 告警,
|
///-9107 身份证反光告警。
|
/// </summary>
|
public List<int> WarnInfos { get; set; }
|
}
|
|
}
|