轮胎外观检测添加思谋语义分割模型检测工具
C3204
2026-03-30 7ceaa09e4baefe84bad268b56bbf8b8f3f1d0f99
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
using HalconDotNet;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using SharpCompress.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
 
namespace LB_VisionProcesses.Processes
{
    public class BaseProcess : IProcess
    {
        public override bool Run()
        {
            DateTime StartTime = DateTime.Now;
 
            InitRunParams();
            HOperatorSet.GenEmptyObj(out HObject EmptyObj);
            OutputImage = EmptyObj;
 
            #region 运行逻辑
 
            #endregion
            Result = true;
            RunTime = (DateTime.Now - StartTime).TotalMilliseconds;
            return false;
        }
 
 
        /// <summary>
        /// 加载算法
        /// </summary>
        /// <param name="fullPath">完整路径带.json</param>
        /// <returns></returns>
        public override bool Load(string fullPath = null)
        {
            try
            {
                if (!fullPath.Contains(".json"))
                {
                    Debug.WriteLine("文件路径不完整");
                    return false;
                }
 
                if (fullPath.StartsWith(".\\"))
                {
                    // 判断原字符串长度是否大于等于2,避免越界
                    if (fullPath.Length >= 2)
                    {
                        // 替换开头两个字符
                        fullPath = Application.StartupPath + fullPath.Substring(2);
                        Debug.WriteLine($"修改后的字符串: {fullPath}");
                    }
                }
 
                if (string.IsNullOrEmpty(fullPath) || fullPath.Trim() == "")
                {
                    Debug.WriteLine("文件路径不完整");
                    return false;
                }
 
                // 获取不带文件名的目录路径
                string directoryPath = Path.GetDirectoryName(fullPath);
                strProcessName = Path.GetFileNameWithoutExtension(fullPath);
 
                if (!File.Exists(fullPath))
                {
                    Debug.WriteLine("文件不存在创建空文件");
                    Save(directoryPath);
                    return true;
                }
                string strJson = string.Empty;
                using (StreamReader streamReader = new StreamReader(fullPath, Encoding.UTF8))
                {
                    strJson = streamReader.ReadToEnd();
                    streamReader.Close();
                }
                Params = JsonConvert.DeserializeObject<ProcessParams>(strJson);
                if (Params == null)
                    return false;
 
                Params.FixDeserializedData();
                return true;
            }
            catch { return false; }
        }
 
        /// <summary>
        /// 保存算法
        /// </summary>
        /// <param name="filePath">不带.json</param>
        /// <returns></returns>
        public override bool Save(string filePath = null)
        {
            try
            {
                if (string.IsNullOrEmpty(filePath) || filePath.Trim() == "")
                {
                    Debug.WriteLine("文件路径不完整");
                    return false;
                }
 
                string strJson = string.Empty;
 
                var settings = new JsonSerializerSettings
                {
                    Formatting = Newtonsoft.Json.Formatting.Indented,
                    // 自定义缩进(4空格)
                    ContractResolver = new DefaultContractResolver
                    {
                        NamingStrategy = new CamelCaseNamingStrategy()
                    }
                };
 
                strJson = JsonConvert.SerializeObject(Params, settings);
                //判断文件夹是否存在,防呆输入为文件名称
                if (!Directory.Exists(filePath))
                {
                    try
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    catch (Exception)
                    { }
                }
                File.WriteAllText(filePath + "//" + strProcessName + ".json", strJson, Encoding.UTF8);
                return true;
            }
            catch { return false; }
        }
 
        public override void InitRunParams()
        {
            Result = true;
            Msg = string.Empty;
 
            if (Record != null)
                Record.Dispose();
        }
 
        public override void Dispose()
        {
            return;
        }
 
        public override object Clone()
        {
            return MemberwiseClone();
        }
    }
 
    public class TVInfo
    {
        public string ODF { get; set; }
        public string ProductName { get; set; }
        public string ProductCode { get; set; }
 
    }
 
    public class RootObject4
    {
        public string Guid { get; set; }
 
        public string IsSuccess { get; set; }
 
        public string Msg { get; set; }
 
        public string UpdateUser { get; set; }
    }
 
    public class MaInfo
    {
        public string ODF { get; set; }
        public string BOM { get; set; }
        public string SCREEN { get; set; }
        public string SHIPPING { get; set; }
        public string SIZE { get; set; }
        public string Model { get; set; }
    }
 
    public class GetWebServer
    {
        public static string QueryGetWebService(String URL, String MethodName, Hashtable Pars)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + ParsToString(Pars));
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            SetWebRequest(request);
            return ReadXmlResponse(request.GetResponse());
        }
 
        /// <summary>
        /// 获得产品信息
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="MethodName"></param>
        /// <param name="SN"></param>
        /// <returns></returns>
        public static string GetTVInfo(String URL, String MethodName, string SN)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + "SN=" + Uri.EscapeDataString(SN));
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            SetWebRequest(request);
            return ReadXmlResponse(request.GetResponse());
        }
 
 
        /// <summary>
        /// 获得下一工序代码
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="MethodName"></param>
        /// <param name="SN"></param>
        /// <returns></returns>
        public static string GetNextProcessCode(String URL, String MethodName, string SN)
        {
 
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + "TVSN=" + Uri.EscapeDataString(SN));
 
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            SetWebRequest(request);
            return ReadXmlResponse(request.GetResponse());
        }
 
        /// <summary>
        /// 上传条码扫描路线结果
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="MethodName"></param>
        /// <param name="TVSN"></param>
        /// <param name="LineCode"></param>
        /// <param name="PcProcessCode"></param>
        /// <param name="Host"></param>
        /// <param name="IP"></param>
        /// <param name="User"></param>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public static string Scan(String URL, String MethodName, string SN, string LineCode, string PcProcessCode, string Host, string IP, string User, string UserName)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + "TVSN=" + Uri.EscapeDataString(SN) + "&" + "LineCode=" + LineCode + "&" + "PcProcessCode=" + PcProcessCode + "&" + "Host=" + Host + "&" + "IP=" + IP + "&" + "User=" + User + "&" + "UserName=" + UserName + "&" + "PartStrs=" + "");
                request.Method = "GET";
                request.ContentType = "application/x-www-form-urlencoded";
                SetWebRequest(request);
                return ReadXmlResponse(request.GetResponse());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return "";
            }
        }
 
        private static void SetWebRequest(HttpWebRequest request)
        {
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Timeout = 50000;
        }
 
        private static string ReadXmlResponse(WebResponse response)
        {
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            String retXml = sr.ReadToEnd();
            sr.Close();
            XmlDocument doc = new XmlDocument();
            return retXml;
        }
 
        private static String ParsToString(Hashtable Pars)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string k in Pars.Keys)
            {
                if (sb.Length > 0)
                {
                    sb.Append("&");
                }
                sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(Pars[k].ToString()));
            }
            return sb.ToString();
 
        }
 
        public static string PostXml(string url, string SN)
        {
            string text = "";
            Stream stream = null;
            string text2 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><GetMaInfo xmlns=\"http://tempuri.org/\"><sn>" + SN + "</sn></GetMaInfo></soap:Body></soap:Envelope>";
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentLength = text2.Length;
            httpWebRequest.ContentType = "text/xml";
            httpWebRequest.KeepAlive = false;
            httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
            httpWebRequest.Timeout = 7000;
            Encoding encoding = Encoding.GetEncoding("UTF-8");
            byte[] bytes = encoding.GetBytes(text2);
            try
            {
                stream = httpWebRequest.GetRequestStream();
            }
            catch (Exception)
            {
                return "INT ERR";
            }
 
            stream.Write(bytes, 0, text2.Length);
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
            {
                text = streamReader.ReadToEnd();
                streamReader.Close();
            }
 
            text = text.Substring(0, text.LastIndexOf("}</GetMaInfoResult>") + 1);
            text = text.Substring(text.LastIndexOf("<GetMaInfoResult>{") + 17);
            RootObject4 rootObject = JsonConvert.DeserializeObject<RootObject4>(text);
            if (rootObject.IsSuccess == "true")
            {
                return rootObject.Msg;
            }
 
            return "error" + rootObject.Msg;
        }
 
    }
}