123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- using MeterVision.db;
- using MeterVision.model;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using static MeterVision.FreeAi.FaImport;
- namespace MeterVision.FreeAi
- {
- public class FaBefore
- {
- //给beforeAI设置默认值
- //private static void setDefaultValue_BeforeAI(ref BeforeAI beforeAI)
- //{
- // beforeAI.AI_result = FaConstant.IMPOSSIBLE_MAX_VALUE;
- // beforeAI.result_of_1st_part = FaConstant.IMPOSSIBLE_MAX_VALUE;
- // beforeAI.result_of_2st_part = FaConstant.IMPOSSIBLE_MAX_VALUE;
- // beforeAI.big_jpg_max_size = 12 * 1024;
- // beforeAI.bright_factor = 1.2f;
- // beforeAI.center_x = FaConstant.IMPOSSIBLE_VALUE;
- // beforeAI.center_y = FaConstant.IMPOSSIBLE_VALUE;
- // beforeAI.lean_angle = FaConstant.IMPOSSIBLE_VALUE;
- // beforeAI.is_nums_in_upper = 1;
- // beforeAI.is_OK_preValue = 0;
- // beforeAI.is_upload_red_ind_part = 1;
- // beforeAI.manual_parameter_enable = 0;
- // beforeAI.MAX_FLOW_PER_HOUR = FaConstant.IMPOSSIBLE_MAX_VALUE;
- // beforeAI.meter_type = 0;
- // beforeAI.NUM_BLACK_NUMBERS = FaConstant.IMPOSSIBLE_NUM_NUMBER;
- // beforeAI.NUM_NUMBERS = FaConstant.IMPOSSIBLE_NUM_NUMBER;
- // beforeAI.NUM_INDICATORS = FaConstant.IMPOSSIBLE_NUM_NUMBER;
- // beforeAI.UNIT_OF_THE_LAST_NUMBER = FaConstant.ONE_STERE;
- // beforeAI.compress_jpg_index = 0;
- // beforeAI.number_image_zoomrate = 1.0f;
- // beforeAI.indicator_image_zommrate = 1.0f;
- // beforeAI.use_test_jpg_flag = 1;
- // beforeAI.is_get_whole_image = 1;
- // beforeAI.num_region = new ushort[4];
- // beforeAI.config_num_region = new ushort[4];
- // beforeAI.indicators_regions = new ushort[10 * 4];
- //}
- public static BeforeAI getBeforeAI()
- {
- BeforeAI beforeAI = new BeforeAI();
- //setDefaultValue_BeforeAI(ref beforeAI);
- return beforeAI;
- }
- private static void SetDefaultValue(ref BeforeAI beforeAI)
- {
- beforeAI.METER_TYPE = FaConstant.IMPOSSIBLE_NUM_NUMBER;
- beforeAI.NUM_NUMBERS = FaConstant.IMPOSSIBLE_NUM_NUMBER;
- beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)FaConstant.IMPOSSIBLE_VALUE;
- beforeAI.NUM_INDICATORS = FaConstant.IMPOSSIBLE_NUM_NUMBER;
- beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)FaConstant.IMPOSSIBLE_VALUE;
- beforeAI.MAX_FLOW_THRESHOLD = FaConstant.IMPOSSIBLE_MAX_VALUE;
- beforeAI.AI_RESULT = FaConstant.IMPOSSIBLE_MAX_VALUE;
- beforeAI.AI_COMPLETE_RESULT = FaConstant.IMPOSSIBLE_MAX_VALUE;
- beforeAI.CONFIG_NUM_REGION = new ushort[8];
- for(int i = 0; i < 8; i++)
- {
- beforeAI.CONFIG_NUM_REGION[i] = (ushort)FaConstant.IMPOSSIBLE_VALUE;
- }
- beforeAI.CONFIG_TWO_IND = new ushort[4];
- for(int i = 0; i < 4; i++)
- {
- beforeAI.CONFIG_TWO_IND[i] = (ushort)FaConstant.IMPOSSIBLE_VALUE;
- }
- beforeAI.CONFIG_METER_REGION = new ushort[4];
- for(int i = 0; i < 4; i++)
- {
- beforeAI.CONFIG_METER_REGION[i] = (ushort)FaConstant.IMPOSSIBLE_VALUE;
- }
- beforeAI.BRIGHT_FACTOR = 1.20f;
- beforeAI.BIG_JPG_SIZE = 12 * 1024;
- beforeAI.USE_TEST_JPG_FLAG = 1;
- beforeAI.COMPRESS_JPG_INDEX = 100;
- beforeAI.IS_UPLOAD_RED_IND_PART = 1;
- }
- public static BeforeAI getBeforeAI(SingleDetailItem detailItem)
- {
- BeforeAI beforeAI = new BeforeAI();
- SetDefaultValue(ref beforeAI);
- beforeAI.METER_TYPE = (byte)detailItem.MeterType;
-
- if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 3)
- {
- beforeAI.NUM_NUMBERS = (byte)detailItem.DigitCount;
- beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
- beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
-
- TryParseCoordinates(detailItem.FeatureRegion, 4, ref beforeAI.CONFIG_NUM_REGION);
- }
- else if(beforeAI.METER_TYPE == 2)
- {
- beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
- beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
- TryParseCoordinates(detailItem.FeatureRegion, 2, ref beforeAI.CONFIG_TWO_IND);
- }
- if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 2 || beforeAI.METER_TYPE == 3)
- {
- TryParseCoordinates(detailItem.MeterRegion, 2, ref beforeAI.CONFIG_METER_REGION);
- //单张图计算最大流量,意义不大,也不太好确定时间,就按1小时计算吧
- beforeAI.MAX_FLOW_THRESHOLD = (ulong)detailItem.FlowRate * FaConstant.CUBE_VALUE;
- beforeAI.AI_RESULT = (ulong)detailItem.LastValue * FaConstant.CUBE_VALUE;
- }
- beforeAI.BRIGHT_FACTOR = (float)detailItem.BrightVal;
- beforeAI.USE_TEST_JPG_FLAG = 1; //(float)detailItem.use
- beforeAI.COMPRESS_JPG_INDEX = 100;
- beforeAI.IS_UPLOAD_RED_IND_PART = 1; //这个应该价格配置项
- return beforeAI;
- }
- public static BeforeAI getBeforeAI(PatchDetailItem detailItem)
- {
- BeforeAI beforeAI = new BeforeAI();
- SetDefaultValue(ref beforeAI);
- beforeAI.METER_TYPE = (byte)detailItem.MeterType;
- if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 3)
- {
- beforeAI.NUM_NUMBERS = (byte)detailItem.DigitCount;
- beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
- beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
- TryParseCoordinates(detailItem.FeatureRegion, 4, ref beforeAI.CONFIG_NUM_REGION);
- }
- else if (beforeAI.METER_TYPE == 2)
- {
- beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
- beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
- TryParseCoordinates(detailItem.FeatureRegion, 2, ref beforeAI.CONFIG_TWO_IND);
- }
- if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 2 || beforeAI.METER_TYPE == 3)
- {
- TryParseCoordinates(detailItem.MeterRegion, 2, ref beforeAI.CONFIG_METER_REGION);
- //单张图计算最大流量,意义不大,也不太好确定时间,就按1小时计算吧
- double hourDiffs = CalculateHourDifference(detailItem.LatestTime, detailItem.SampleTime);
- beforeAI.MAX_FLOW_THRESHOLD = (ulong)(detailItem.FlowRate * hourDiffs * FaConstant.CUBE_VALUE);
- //上次的值
- beforeAI.AI_RESULT = (ulong)detailItem.LatestValue; // * FaConstant.CUBE_VALUE;
- if(detailItem.LatestComplete >= 0)
- {
- beforeAI.AI_COMPLETE_RESULT = (ulong)detailItem.LatestComplete;
- }
- }
- beforeAI.BRIGHT_FACTOR = (float)detailItem.BrightVal;
- beforeAI.USE_TEST_JPG_FLAG = 1; //(float)detailItem.use
- if (detailItem.LastCompress > 0 && detailItem.LastCompress <= 100)
- {
- beforeAI.COMPRESS_JPG_INDEX = (byte)detailItem.LastCompress;
- }
- beforeAI.IS_UPLOAD_RED_IND_PART = 1; //这个应该价格配置项
- return beforeAI;
- }
- public static BeforeAI getBeforeAI(TPatchDetail detailItem)
- {
- BeforeAI beforeAI = new BeforeAI();
- SetDefaultValue(ref beforeAI);
- beforeAI.METER_TYPE = (byte)detailItem.MeterType;
- if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 3)
- {
- beforeAI.NUM_NUMBERS = (byte)detailItem.DigitCount;
- beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
- beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
- TryParseCoordinates(detailItem.FeatureRegion, 4, ref beforeAI.CONFIG_NUM_REGION);
- }
- else if (beforeAI.METER_TYPE == 2)
- {
- beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
- beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
- TryParseCoordinates(detailItem.FeatureRegion, 2, ref beforeAI.CONFIG_TWO_IND);
- }
- if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 2 || beforeAI.METER_TYPE == 3)
- {
- TryParseCoordinates(detailItem.MeterRegion, 2, ref beforeAI.CONFIG_METER_REGION);
- double hourDiffs = CalculateHourDifference(detailItem.LatestTime, detailItem.SampleTime);
- beforeAI.MAX_FLOW_THRESHOLD = (ulong)(detailItem.FlowRate * hourDiffs * FaConstant.CUBE_VALUE);
- //上次的值
- beforeAI.AI_RESULT = (ulong)detailItem.LatestValue; // * FaConstant.CUBE_VALUE;
- if (detailItem.LatestComplete>=0)
- {
- beforeAI.AI_COMPLETE_RESULT = (ulong)detailItem.CompleteValue;
- }
- }
- beforeAI.BRIGHT_FACTOR = (float)detailItem.BrightVal;
- beforeAI.USE_TEST_JPG_FLAG = 1; //(float)detailItem.use
- if (detailItem.LastCompress > 0 && detailItem.LastCompress <= 100)
- {
- beforeAI.COMPRESS_JPG_INDEX = 100;
- }
- beforeAI.IS_UPLOAD_RED_IND_PART = 1; //这个应该价格配置项
- return beforeAI;
- }
- static double CalculateHourDifference(string time1, string time2)
- {
- // 解析字符串为 DateTime
- DateTime dateTime1 = DateTime.ParseExact(time1, "yyyy-MM-dd HH:mm:ss", null);
- DateTime dateTime2 = DateTime.ParseExact(time2, "yyyy-MM-dd HH:mm:ss", null);
- // 计算小时差(带小数)
- return (dateTime2 - dateTime1).TotalHours;
- }
- static bool TryParseCoordinates(string input, int numPairs, ref ushort[] numbers)
- {
- // 确保传入的数组足够大
- if (numbers.Length < numPairs * 2)
- {
- Console.WriteLine("错误: 数组长度不足");
- return false;
- }
- // 生成正则表达式,例如匹配 4 组时:"(\d+),(\d+) (\d+),(\d+) (\d+),(\d+) (\d+),(\d+)"
- string pattern = "^" + string.Join(" ", Enumerable.Repeat(@"(\d+),(\d+)", numPairs)) + "$";
- Match match = Regex.Match(input, pattern);
- if (match.Success)
- {
- for (int i = 0; i < numPairs * 2; i++)
- {
- numbers[i] = ushort.Parse(match.Groups[i + 1].Value);
- }
- return true;
- }
- return false;
- }
- //static bool TryParseCoordinates(string input, int numPairs, out int[] numbers)
- //{
- // numbers = new int[numPairs * 2];
- // // 生成正则表达式,例如匹配 4 组时:"(\d+),(\d+) (\d+),(\d+) (\d+),(\d+) (\d+),(\d+)"
- // string pattern = "^" + string.Join(" ", new string[numPairs].Select(_ => @"(\d+),(\d+)")) + "$";
- // Match match = Regex.Match(input, pattern);
- // if (match.Success)
- // {
- // for (int i = 0; i < numPairs * 2; i++)
- // {
- // numbers[i] = int.Parse(match.Groups[i + 1].Value);
- // }
- // return true;
- // }
- // return false;
- //}
- //static bool TryParseCoordinates4(string input, out int[] numbers)
- //{
- // numbers = new int[8];
- // // 正则表达式匹配 4 组 "数字,数字",中间用空格分隔
- // string pattern = @"^(\d+),(\d+) (\d+),(\d+) (\d+),(\d+) (\d+),(\d+)$";
- // Match match = Regex.Match(input, pattern);
- // if (match.Success)
- // {
- // // 提取匹配到的 8 个数值
- // for (int i = 0; i < 8; i++)
- // {
- // numbers[i] = int.Parse(match.Groups[i + 1].Value);
- // }
- // return true;
- // }
- // return false;
- //}
- //--------------------------------------------------------
- }
- }
|