Files
ShaftControlCodes/src/BRS.UI.Main/SampleHelper.cs
2023-02-23 14:52:43 +08:00

238 lines
8.1 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using BRS.Common.Model.Helper;
using System.Windows.Forms;
using BRS.UI.Main.Properties;
using System.Reflection;
using System.Resources;
namespace BRS.UI.Main
{
/// <summary>
/// 配置文件夹以及示例数据处理
/// </summary>
public static class SampleHelper
{
private static readonly List<string> Folders = new List<string> {
"Configs",
//"DataBase",
//"Images",
"Logs",
//"Models",
//"Result",
//"Samples",
//"Vision",
};
/// <summary>
/// 初始化文件夹
/// </summary>
/// <param name="path"></param>
private static void InitFolder(string path)
{
Folders.ForEach(folder =>
{
Directory.CreateDirectory(Path.Combine(path, folder));
});
}
/// <summary>
/// 解压缩Sample数据
/// </summary>
/// <param name="path"></param>
private static void Extract(string path)
{
// 7z.exe x FN_Inference.7z -pxkrs0425 -o"J:\test data"
var currentDir = Environment.CurrentDirectory;
var _7z = Path.Combine(currentDir, "7z.exe");
var sampleFile = Path.Combine(currentDir, "data", "Sample.7z");
ProcessStartInfo psi = new ProcessStartInfo(_7z);
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.Arguments = $"x {sampleFile} -pxkrs0425 -o\"{path}\"";
System.Diagnostics.Process.Start(psi);
}
private static void GenerateConfig(string basePath)
{
// 初始化配置文件
Assembly assem = Assembly.GetEntryAssembly();
ResourceManager rm = new ResourceManager("BRS.UI.Main.Properties.Resources", assem);
string defaultConfigStr = rm.GetString("DefaultConfigStr"); //资源文件名称不带扩展名
object defaultConfig = rm.GetObject("DefaultConfig");
// 离线图片目录
string strOfflineImgDir = Path.Combine(basePath, "SampleImages").Replace("\\", "\\\\");
defaultConfigStr = defaultConfigStr.Replace("%SAMPLE_OFFLINE_IMAGE_DIR%", strOfflineImgDir);
// 模型
string strModelPath = Path.Combine(basePath, "Models", "FN_Model.onnx").Replace("\\", "\\\\");
defaultConfigStr = defaultConfigStr.Replace("%SAMPLE_MODEL_PATH%", strModelPath);
//标签
string strLabelPath = Path.Combine(basePath, "Models", "FN_Class.txt").Replace("\\", "\\\\");
defaultConfigStr = defaultConfigStr.Replace("%SAMPLE_LABEL_PATH%", strLabelPath);
// 图片保存路径
string strImgSaveDir = Path.Combine(basePath, "Images").Replace("\\", "\\\\");
defaultConfigStr = defaultConfigStr.Replace("%SAMPLE_IMAGE_SAVE_DIR%", strImgSaveDir);
// 日志
string strLogPath = Path.Combine(basePath, "Logs").Replace("\\", "\\\\");
defaultConfigStr = defaultConfigStr.Replace("%SAMPLE_LOG_PATH%", strLogPath);
// 配置文件保存路径
string strConfigPath = Path.Combine(basePath, "Configs", "Config.json");
using (StreamWriter writer = new StreamWriter(strConfigPath, false, Encoding.UTF8))
{
writer.WriteLine(defaultConfigStr);
writer.Flush();
}
}
/// <summary>
/// 初始化配置数据
/// </summary>
public static void Init()
{
bool settingChecked = false;
// 配置文件不存在
while (!settingChecked)
{
if (settingChecked)
{
Environment.Exit(1);
}
// 设置目录
string myDoc = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var retPath = Path.Combine(myDoc, "A020");
// var retPath = Path.Combine(dialog.SelectedPath, "Configs");
// 将配置写入文件
string defaultSetting = "{\"ProcessCodes\":[],\"ProductionCodes\":[\"Default\"],\"DefaultProcess\":\"A020\",\"DefaultProduction\":\"\",\"ConfigPath\":\""
+ Path.Combine(retPath, "Configs").Replace("\\", "\\\\")
+ "\",\"Description\":\"振动盘供料缺陷检测系统\",\"IconPath\":null,\"CurrLayout\":\"Default\",\"IsMinimumWhenClose\":false}";
SettingHelper.InitWithContent(defaultSetting);
settingChecked = true;
// 创建文件夹
InitFolder(retPath);
// 解压缩示例数据
// Extract(retPath);
// 生成配置文件
GenerateConfig(retPath);
}
}
///// <summary>
///// 初始化配置数据
///// </summary>
//public static void Init()
//{
// bool settingChecked = false;
// // 配置文件不存在
// while (!settingChecked)
// {
// if (settingChecked)
// {
// Environment.Exit(1);
// }
// // 设置目录
// MessageBox.Show("首次使用软件,请选择一个空文件夹作为数据目录,路径中请勿包含中文", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
// bool dirOk = false;
// FolderBrowserDialog dialog = new FolderBrowserDialog();
// dialog.ShowNewFolderButton = true;
// dialog.Description = "请选择数据目录(不允许中文路径)";
// while (!dirOk) // 检查是否选择好了目录
// {
// if (dialog.ShowDialog() == DialogResult.OK)
// {
// bool haveChinese = dialog.SelectedPath.ToCharArray().Any(c => c > 0xff);
// if (haveChinese)
// {
// MessageBox.Show("路径中包含中文,请重新选择", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
// continue;
// }
// var subEntities = Directory.GetFileSystemEntries(dialog.SelectedPath).Length;
// if (subEntities > 0)
// {
// MessageBox.Show("目录非空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
// continue;
// }
// dirOk = true;
// }
// else
// {
// MessageBox.Show("请选择数据目录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
// }
// }
// var retPath = dialog.SelectedPath; //Path.Combine(dialog.SelectedPath, "DefectDetectionData");
// // var retPath = Path.Combine(dialog.SelectedPath, "Configs");
// // 将配置写入文件
// string defaultSetting = "{\"ProcessCodes\":[],\"ProductionCodes\":[\"Default\"],\"DefaultProcess\":\"A020\",\"DefaultProduction\":\"\",\"ConfigPath\":\""
// + Path.Combine(dialog.SelectedPath, "Configs").Replace("\\", "\\\\")
// + "\",\"Description\":\"振动盘供料缺陷检测系统\",\"IconPath\":null,\"CurrLayout\":\"Default\",\"IsMinimumWhenClose\":false}";
// SettingHelper.InitWithContent(defaultSetting);
// settingChecked = true;
// // 创建文件夹
// InitFolder(retPath);
// // 解压缩示例数据
// Extract(retPath);
// // 生成配置文件
// GenerateConfig(retPath);
// }
//}
}
}