轮胎外观检测添加思谋语义分割模型检测工具
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
using LB_SmartVision.SQL;
using LB_SmartVisionCommon;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;
namespace LB_SmartVision.CSV
{
    internal class CsvDataHelperRecordProductData
    {
        /// <summary>
        /// 将RecordProductDatas字典保存到CSV文件
        /// </summary>
        /// <param name="filePath">CSV文件路径</param>
        /// <param name="recordProductDatas">要保存的数据字典</param>
        public static void SaveToCsv(string filePath, List<RecordProductData> recordProductDatas)
        {
            try
            {
                using (var writer = new StreamWriter(filePath))
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    csv.Context.RegisterClassMap<CsvRecordProductDataMap>();
                    // 将字典转换为CsvRecord列表
                    var records = recordProductDatas.Select(kvp => new CsvRecordProductData
                    {
                        ProductName = kvp.ProductName,
                        ProductSN = kvp.ProductSN,
                        InspectionOperator = kvp.InspectionOperator,
                        NGType = kvp.NGType,
                        NGSize = kvp.NGSize,
                        DetectionTime = kvp.DetectionTime,
                        CameraInspection = kvp.CameraInspection,
 
                    }).ToList();
                    for (int i = 0; i < records.Count; i++)
                    {
                        AsyncLogHelper.Warn("物料号或产品名称:" + records[i].ProductName + "\r\n" +
                     ",产品SN号:" + records[i].ProductSN + "\r\n" +
                     ",检测作业员:" + records[i].InspectionOperator + "\r\n" +
                     ",NG类型:" + records[i].NGType + "\r\n" +
                     ",NG大小:" + records[i].NGSize + " mm^2" + "\r\n" +
                     ",检测时间:" + records[i].DetectionTime + "\r\n" +
                     ",检测相机:" + records[i].CameraInspection + "\r\n" );
 
                    }
                    csv.WriteRecords(records);
                }
                LogHelper.Info($"数据成功保存到: {filePath}");
            }
            catch (Exception ex)
            {
                LogHelper.Error($"保存CSV文件时出错: {ex.Message}");
                //throw;
            }
            //Task.Factory.StartNew(() =>
            //{
            //});
        }
        /// <summary>
        /// 从CSV文件读取数据到RecordProductDatas字典
        /// </summary>
        /// <param name="filePath">CSV文件路径</param>
        /// <returns>读取的数据字典</returns>
        public static List<RecordProductData> LoadFromCsv(string filePath)
        {
            var recordProductDatas = new List<RecordProductData>();
            try
            {
                if (!File.Exists(filePath))
                {
                    LogHelper.Info($"文件不存在: {filePath}");
                    return recordProductDatas;
                }
                using (var reader = new StreamReader(filePath))
                using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                {
                    csv.Context.RegisterClassMap<CsvRecordProductDataMap>();
                    var records = csv.GetRecords<CsvRecordProductData>();
                    foreach (var record in records)
                    {
                        var productData = new RecordProductData
                        {
                            ProductName = record.ProductName,
                            ProductSN = record.ProductSN,
                            InspectionOperator = record.InspectionOperator,
                            NGType = record.NGType,
                            NGSize = record.NGSize,
                            DetectionTime = record.DetectionTime,
                            CameraInspection = record.CameraInspection,
                        };
                        recordProductDatas.Add(productData);
                    }
                }
                LogHelper.Info($"从 {filePath} 成功读取 {recordProductDatas.Count} 条记录");
            }
            catch (Exception ex)
            {
                LogHelper.Error($"读取CSV文件时出错: {ex.Message}");
                //throw;
            }
            return recordProductDatas;
        }
 
    }
}