移动系统liao
2024-08-16 4c3ac42be64eae9c92d9b876b16312d5de46338e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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-chengdu", 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;
            }
 
 
        }
        #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-chengdu", 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-chengdu", 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-chengdu", clientProfile);
            var ret = iaiClient.CreateFace( new TencentCloud.Iai.V20200303.Models.CreateFaceRequest
            {
              
                PersonId = PersonNameId,
                Images = [img64],
                 
        
 
            });
 
            if (ret.IsFaulted)
                return false;
            else
                return true;
 
 
 
        }
        #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; }
    }
 
}