You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
377 lines
13 KiB
377 lines
13 KiB
using Newtonsoft.Json; |
|
using SparkClient.Model.Entity.ApiEntity; |
|
using SparkClient.Model.Helper; |
|
using SparkClient.ViewModel.BaseWindow; |
|
using SparkClient.Views.Dialog; |
|
using System; |
|
using System.Diagnostics.Metrics; |
|
using System.DirectoryServices.ActiveDirectory; |
|
using System.Reflection.Metadata; |
|
using System.Windows; |
|
using System.Windows.Input; |
|
using System.Windows.Media.Imaging; |
|
using System.Windows.Threading; |
|
using SparkClient.Model.Services; |
|
using MessageBox = System.Windows.MessageBox; |
|
|
|
namespace SparkClient.ViewModel.Grading; |
|
|
|
public class DiamondSelectVM : BaseViewModel |
|
{ |
|
|
|
private SOCClientService _socClientService; |
|
private AlgorithmServer _algorithmServer; |
|
private string DiamondCode { get; set; } |
|
private List<ButtonViewModel> _buttons; |
|
private List<ButtonViewModel> _buttons2; |
|
private ICommand ChangeDiamondTypeCommand; |
|
private ICommand StartGradingCommand; |
|
public List<ButtonViewModel> Buttons { |
|
get { return _buttons; } |
|
set |
|
{ |
|
_buttons = value; |
|
OnPropertyChanged(nameof(Buttons)); |
|
} |
|
} |
|
public List<ButtonViewModel> Buttons2 |
|
{ |
|
get { return _buttons2; } |
|
set |
|
{ |
|
_buttons2 = value; |
|
OnPropertyChanged(nameof(Buttons2)); |
|
} |
|
} |
|
|
|
public DiamondSelectVM(string DiamondCode) |
|
{ |
|
ChangeDiamondTypeCommand = new RelayCommand(ChangeDiamondType); |
|
StartGradingCommand = new RelayCommand(StartGrading); |
|
this.DiamondCode = DiamondCode; |
|
List<ButtonViewModel> tempButtons = new List<ButtonViewModel>(); |
|
List<ButtonInfo> buttonInfos = GetButtonInfos(); |
|
foreach (var buttonInfo in buttonInfos) |
|
{ |
|
var bitmap = new BitmapImage(new Uri("pack://application:,,,/Resource/Images/" + buttonInfo.ImageName, UriKind.RelativeOrAbsolute)); |
|
ButtonViewModel button = new ButtonViewModel() { Text = buttonInfo.ButtonName, ImageSource = bitmap, Type = buttonInfo.Type, Command = ChangeDiamondTypeCommand ,IsEnabled= buttonInfo.IsEnabled, IsHighlighted = false}; |
|
tempButtons.Add(button); |
|
} |
|
Buttons = tempButtons; |
|
|
|
} |
|
private List<ButtonInfo> GetButtonInfos() |
|
{ |
|
List<ButtonInfo> buttonInfos = new List<ButtonInfo>(); |
|
buttonInfos.Add(new ButtonInfo() { Type = "round", IsEnabled = true, ImageName = "round_3x.png", ButtonName = MultilingualHelper.getString("圆形") }); |
|
buttonInfos.Add(new ButtonInfo() { Type = "Heart", IsEnabled = false, ImageName = "Heart-shaped.png", ButtonName = MultilingualHelper.getString("心形") }); |
|
buttonInfos.Add(new ButtonInfo() { Type = "cushion", IsEnabled = false, ImageName = "Cushion-shaped.png", ButtonName = MultilingualHelper.getString("枕形") }); |
|
buttonInfos.Add(new ButtonInfo() { Type = "Pear", IsEnabled = false, ImageName = "Pear-shaped.png", ButtonName = MultilingualHelper.getString("梨形") }); |
|
buttonInfos.Add(new ButtonInfo() { Type = "Princess", IsEnabled = false, ImageName = "Princess-shaped.png", ButtonName = MultilingualHelper.getString("公主方形") }); |
|
buttonInfos.Add(new ButtonInfo() { Type = "oval", IsEnabled = false, ImageName = "oval.png", ButtonName = MultilingualHelper.getString("椭圆形") }); |
|
buttonInfos.Add(new ButtonInfo() { Type = "Emerald", IsEnabled = false, ImageName = "Emerald-shape.png", ButtonName = MultilingualHelper.getString("祖母绿形") }); |
|
buttonInfos.Add(new ButtonInfo() { Type = "Odd", IsEnabled = false, ImageName = "Odd-shaped.png", ButtonName = MultilingualHelper.getString("异形钻") }); |
|
return buttonInfos; |
|
} |
|
/// <summary> |
|
/// 获取钻石列表 |
|
/// </summary> |
|
/// <param name="param"></param> |
|
public void InitDiamondList(object param) |
|
{ |
|
|
|
} |
|
|
|
/// <summary> |
|
/// 切换钻石类型(右侧列表) |
|
/// </summary> |
|
/// <param name="type">钻石类型</param> |
|
public void ChangeDiamondType(object type) |
|
{ |
|
if (type!= null) |
|
{ |
|
foreach (var item in Buttons.Where(x => x.Type == type)) |
|
{ |
|
item.IsHighlighted = true; |
|
}; |
|
List<ButtonViewModel> tempButtons2 = new List<ButtonViewModel>(); |
|
var bitmap = new BitmapImage(new Uri("pack://application:,,,/Resource/Images/round_P8-P8.png", UriKind.RelativeOrAbsolute)); |
|
ButtonViewModel button = new ButtonViewModel() { Text = "P8-P8", ImageSource = bitmap, Type = "round P8 P8", Command = StartGradingCommand }; |
|
tempButtons2.Add(button); |
|
Buttons2 = tempButtons2; |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 开始检测(对soc和算法开始通讯) |
|
/// </summary> |
|
/// <param name="param"></param> |
|
public async void StartGrading(object param) |
|
{ |
|
if (param != null) |
|
{ |
|
LoadingDialog loading = new LoadingDialog(); |
|
var progressTask = Task.Run(() => Application.Current.Dispatcher.Invoke(() => loading.ShowDialog())); |
|
await Task.Run(async () => |
|
{ |
|
for (int i = 0; i <= 100; i++) |
|
{ |
|
// 模拟耗时操作 |
|
//System.Threading.Thread.Sleep(50); // 休眠50毫秒 |
|
await Task.Delay(5); |
|
// 更新进度条的值(需要在UI线程上执行) |
|
loading.Dispatcher.Invoke(() => |
|
{ |
|
loading.setValue(i); |
|
}); |
|
|
|
} |
|
SocResultEntity socResolt = new SocResultEntity(); |
|
AlgorithmResultEntity parameter = new AlgorithmResultEntity(); |
|
parameter.Standard = "IGI 2024"; |
|
string value = param.ToString()??""; |
|
if (value!= null && value.Split(" ").Length==3) |
|
{ |
|
parameter.Shape = value.Split(" ")[0]; |
|
parameter.CrownType = value.Split(" ")[1]; |
|
parameter.PavType = value.Split(" ")[2]; |
|
} |
|
|
|
// 启动soc |
|
socResolt = await DoSoc(); |
|
// 启动算法 |
|
parameter = await DoAlgorithm(socResolt, parameter.Shape, parameter.CrownType); |
|
|
|
|
|
parameter.DiamondCode = DiamondCode; |
|
GradingResult(parameter); |
|
}); |
|
await Task.Delay(5); |
|
loading.Close(); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 跳转至检测结果 |
|
/// </summary> |
|
/// <param name="param"></param> |
|
public void GradingResult(AlgorithmResultEntity param) |
|
{ |
|
|
|
WindowManager.mainViewModel.Content = WindowManager.PreviousVM(); |
|
BaseControlVM vm = new BaseControlVM(new GradingResultVM(param), MultilingualHelper.getString("DetectionResult")); |
|
vm.ShowFunctionButton = System.Windows.Visibility.Hidden; |
|
WindowManager.mainViewModel.Content = vm; |
|
WindowManager.openContent.Add(vm); |
|
} |
|
|
|
|
|
/// <summary> |
|
/// 启动切工仪接口。 |
|
/// </summary> |
|
/// <param name="index">图片索引</param> |
|
/// <param name="index">图片索引</param> |
|
/// <param name="index">图片索引</param> |
|
/// <returns>图片的字节数组</returns> |
|
private async Task<SocResultEntity> DoSoc() |
|
{ |
|
// 光照度 |
|
int lightLevel = 100; |
|
// 初始化SOC客户端服务,传入SOC端的地址和认证Token |
|
_socClientService = new SOCClientService("http://localhost:5000/api/SoC", "your_basic_auth_token"); |
|
// SOC接口 |
|
string savePath = @"d:\\diamond_images"; |
|
SocResultEntity resultEntity = await _socClientService.ProcessImageCollectionAsync(lightLevel, savePath); |
|
// 转换成json |
|
string jsonResult = JsonConvert.SerializeObject(resultEntity, Formatting.Indented); |
|
ShowMessage(jsonResult); |
|
|
|
return resultEntity; |
|
} |
|
|
|
/// <summary> |
|
/// 启动算法接口。 |
|
/// </summary> |
|
/// <param name="socResolt">切工仪接口返回值</param> |
|
/// <param name="crownType"></param> |
|
/// <param name="shape"></param> |
|
/// <returns>定级参数,3D模型参数</returns> |
|
private Task<AlgorithmResultEntity> DoAlgorithm(SocResultEntity socResolt, String shape, String crownType) |
|
{ |
|
_algorithmServer = new AlgorithmServer(); |
|
//钻石形状:shape |
|
//钻石子形状 |
|
string shape_mode = crownType; |
|
//图片根目录 |
|
string image_file_base_path = "d:\\diamond_images"; |
|
//图片集合 |
|
string image_files = JsonConvert.SerializeObject(socResolt.Images, Formatting.Indented); |
|
|
|
//算法配置参数 |
|
string algo_config = @" |
|
{ |
|
""edgel_detector"": { |
|
""kernel_size"": 15, |
|
""num_filters"": 16, |
|
""non_max_suppression_winsize"": 17 |
|
}, |
|
""edge_matcher"": { |
|
""grid_size"": 32, |
|
""ransac_iterations"": 10, |
|
""inliner_pts_threshold"": 25, |
|
""inliner_dist_threshold"": 1.0, |
|
""inliner_angle_threshold"": 10.0, |
|
""line_gap_threshold"": 10.0, |
|
""line_length_threshold"": 30.0 |
|
}, |
|
""base_detector"": { |
|
""inliner_dist_threshold"": 30.0, |
|
""inliner_theta_threshold"": 5.0, |
|
""inliner_num_threshold"": 50, |
|
""sample_dist"": 25.0, |
|
""iterations"": 100, |
|
""learning_rate"": 0.0002, |
|
""gradient_clip"": 5.0, |
|
""gradient_momentum"": 0.1, |
|
""base_center_x"": 1296.0 |
|
}, |
|
""girdle_detector"": { |
|
""pavilion_edge_threshold"": 300.0, |
|
""pavilion_angle_min"": 30.0, |
|
""pavilion_angle_max"": 50.0, |
|
""gridle_angle_threshold"": 80.0, |
|
""gridle_to_axis_threshold"": 300.0, |
|
""gridle_y_ratio_min"": 0.2, |
|
""gridle_y_ratio_max"": 0.7, |
|
""gridle_num_candid_threshold"": 30, |
|
""gridle_center_dist_threshold"": 120.0, |
|
""gridle_search_radius"": 80, |
|
""iterations"": 2000, |
|
""learning_rate"": 0.002, |
|
""gradient_clip"": 5.0, |
|
""gradient_momentum"": 0.1 |
|
}, |
|
""pavilion_detector"": { |
|
""pavilion_edge_threshold"": 300.0, |
|
""pavilion_angle_min"": 30.0, |
|
""pavilion_angle_max"": 50.0, |
|
""silhouette_match_samples"": 10, |
|
""silhouette_match_padding"": 0.1, |
|
""iterations"": 10000, |
|
""learning_rate"": 0.02, |
|
""gradient_momentum"": 0.1, |
|
""loss_func_tasks"": 16 |
|
}, |
|
""crown_detector"": { |
|
""sample_point_rate"": 0.1, |
|
""iterations"": 2500, |
|
""learning_rate"": 0.1, |
|
""gradient_momentum"": 0.1, |
|
""loss_func_tasks"": 16 |
|
}, |
|
""output"": { |
|
""pixel_length"": 0.00678, |
|
""girdle_interpolate_pts"": 3 |
|
} |
|
}"; |
|
|
|
AlgorithmResultEntity algoResult = _algorithmServer.CallParseJsonAndReturnActions(shape, shape_mode, image_file_base_path, image_files, algo_config); |
|
|
|
|
|
// 将 algoResult 序列化为格式化的 JSON 字符串 |
|
string algoJsonResult = JsonConvert.SerializeObject(algoResult, Formatting.Indented); |
|
ShowMessage(algoJsonResult); |
|
return Task.FromResult(algoResult); |
|
} |
|
|
|
|
|
private void ShowMessage(string message) |
|
{ |
|
MessageBox.Show(message); |
|
} |
|
|
|
} |
|
public class ButtonInfo |
|
{ |
|
public string Type { get; set; } |
|
public string ImageName { get; set; } |
|
public string ButtonName { get; set; } |
|
public bool IsEnabled { get; set; } |
|
} |
|
public class ButtonViewModel : BaseViewModel |
|
{ |
|
private string _text; |
|
private BitmapImage _imageSource; |
|
private ICommand _command; |
|
private string _type; |
|
private int _col; |
|
private bool _isEnabled; |
|
private bool _isHighlighted; |
|
|
|
|
|
public string Text |
|
{ |
|
get { return _text; } |
|
set |
|
{ |
|
_text = value; |
|
OnPropertyChanged(nameof(Text)); |
|
} |
|
} |
|
|
|
public BitmapImage ImageSource |
|
{ |
|
get { return _imageSource; } |
|
set |
|
{ |
|
_imageSource = value; |
|
OnPropertyChanged(nameof(ImageSource)); |
|
} |
|
} |
|
|
|
public ICommand Command |
|
{ |
|
get { return _command; } |
|
set |
|
{ |
|
_command = value; |
|
OnPropertyChanged(nameof(Command)); |
|
} |
|
} |
|
public string Type |
|
{ |
|
get { return _type; } |
|
set |
|
{ |
|
_type = value; |
|
OnPropertyChanged(nameof(Type)); |
|
} |
|
} |
|
public int Column |
|
{ |
|
get { return _col; } |
|
set |
|
{ |
|
_col = value; |
|
OnPropertyChanged(nameof(Column)); |
|
} |
|
} |
|
public bool IsEnabled |
|
{ |
|
get { return _isEnabled; } |
|
set |
|
{ |
|
_isEnabled = value; |
|
OnPropertyChanged(nameof(IsEnabled)); |
|
} |
|
} |
|
public bool IsHighlighted |
|
{ |
|
get { return _isHighlighted; } |
|
set |
|
{ |
|
_isHighlighted = value; |
|
OnPropertyChanged(nameof(IsHighlighted)); |
|
} |
|
} |
|
} |