public ClsPrediction(ObjectType defaultObjType, Camera cam, ClsDeepstackDetection AiDetectionObject, ClsImageQueueItem curImg, ClsURLItem curURL) { this._defaultObjType = defaultObjType; this._cam = cam; this._curimg = curImg; this._cururl = curURL; //this._imageObject = AiDetectionObject; this.Camera = cam.Name; this.ImageHeight = curImg.Height; this.ImageWidth = curImg.Width; if (AiDetectionObject == null || cam == null || string.IsNullOrWhiteSpace(AiDetectionObject.label)) { Log("Error: Prediction or Camera was null?", "", this._cam.Name); this.Result = ResultType.Error; return; } //force first letter to always be capitalized this.Label = Global.UpperFirst(AiDetectionObject.label); this.XMax = AiDetectionObject.x_max; this.YMax = AiDetectionObject.y_max; this.XMin = AiDetectionObject.x_min; this.YMin = AiDetectionObject.y_min; this.Confidence = AiDetectionObject.confidence * 100; //store as whole number percent this.Filename = curImg.image_path; this.RectHeight = this.XMax - this.YMin; this.RectWidth = this.XMax - this.XMin; this.GetObjectType(); }
public ClsPrediction(ObjectType defaultObjType, Camera cam, ClsDoodsDetection AiDetectionObject, ClsImageQueueItem curImg, ClsURLItem curURL) { this._defaultObjType = defaultObjType; this._cam = cam; this._curimg = curImg; this._cururl = curURL; //this._imageObject = AiDetectionObject; this.Camera = cam.Name; this.ImageHeight = curImg.Height; this.ImageWidth = curImg.Width; if (AiDetectionObject == null || cam == null || string.IsNullOrWhiteSpace(AiDetectionObject.Label)) { Log("Error: Prediction or Camera was null?", "", this._cam.Name); this.Result = ResultType.Error; return; } //force first letter to always be capitalized this.Label = Global.UpperFirst(AiDetectionObject.Label); //{ // "top":0.09833781, // "left":0.62415826, // "bottom":0.14295554, // "right":0.7029755, // "label":"car", // "confidence":63.28125 // } // convert percentage values from doods to pixels //System.Drawing.Rectangle rect = new System.Drawing.Rectangle(xmin, ymin, xmax - xmin, ymax - ymin); //System.Drawing.Rectangle rect = new System.Drawing.Rectangle(x, y, width, height); //x, y = - coordinate of the UPPER LEFT corner of the rectangle //xmin, ymin double right = curImg.Width * AiDetectionObject.Right; double left = curImg.Width * AiDetectionObject.Left; double top = curImg.Height * AiDetectionObject.Top; double bottom = curImg.Height * AiDetectionObject.Bottom; this.XMin = Convert.ToInt32(Math.Round(left)); this.YMin = Convert.ToInt32(Math.Round(top)); this.XMax = Convert.ToInt32(Math.Round(right)); this.YMax = Convert.ToInt32(Math.Round(bottom)); this.RectHeight = this.XMax - this.YMin; this.RectWidth = this.XMax - this.XMin; this.Confidence = Convert.ToSingle(AiDetectionObject.Confidence); this.Filename = curImg.image_path; this.GetObjectType(); }
public ClsPrediction(ObjectType defaultObjType, Camera cam, ClsDeepstackDetection AiDetectionObject, ClsImageQueueItem curImg, ClsURLItem curURL) { this._defaultObjType = defaultObjType; this._cam = cam; this._curimg = curImg; this._cururl = curURL; this.Server = curURL.CurSrv; this.Time = DateTime.Now; //this._imageObject = AiDetectionObject; this.Camera = cam.Name; this.BICamName = cam.BICamName; this.ImageHeight = curImg.Height; this.ImageWidth = curImg.Width; this.Filename = curImg.image_path; if (AiDetectionObject == null || cam == null || (string.IsNullOrWhiteSpace(AiDetectionObject.label) && string.IsNullOrWhiteSpace(AiDetectionObject.UserID))) { Log("Error: Prediction or Camera was null?", "", this._cam.Name); this.Result = ResultType.Error; return; } //if running face detection: if (!string.IsNullOrWhiteSpace(AiDetectionObject.UserID)) { if (string.IsNullOrWhiteSpace(AiDetectionObject.label)) { AiDetectionObject.label = "Face"; } this.Label = AiDetectionObject.label.Trim(); this.Detail = Global.UpperFirst(AiDetectionObject.UserID); } else { //force first letter to always be capitalized this.Label = Global.UpperFirst(AiDetectionObject.label); } if (!string.IsNullOrEmpty(AiDetectionObject.Detail)) { this.Detail = Global.UpperFirst(AiDetectionObject.Detail); } this.XMax = AiDetectionObject.x_max; this.YMax = AiDetectionObject.y_max; this.XMin = AiDetectionObject.x_min; this.YMin = AiDetectionObject.y_min; this.Confidence = AiDetectionObject.confidence * 100; //store as whole number percent this.Filename = curImg.image_path; this.RectHeight = this.XMax - this.YMin; this.RectWidth = this.XMax - this.XMin; this.GetObjectType(); }
public ImageResItem(ClsImageQueueItem CurImg) { this.Count = 1; this.Height = CurImg.Height; this.Width = CurImg.Width; this.LastFileName = CurImg.image_path; this.LastSeenDate = CurImg.TimeCreated; this.LastFileSize = CurImg.FileSize; this.LastFileDPI = CurImg.DPI; }
public ClsPrediction(ObjectType defaultObjType, Camera cam, Amazon.Rekognition.Model.Label AiDetectionObject, int InstanceIdx, ClsImageQueueItem curImg, ClsURLItem curURL) { this._defaultObjType = defaultObjType; this._cam = cam; this._cururl = curURL; this._curimg = curImg; this.Camera = cam.Name; this.ImageHeight = curImg.Height; this.ImageWidth = curImg.Width; if (AiDetectionObject == null || cam == null || string.IsNullOrWhiteSpace(AiDetectionObject.Name)) { Log("Error: Prediction or Camera was null?", "", this._cam.Name); this.Result = ResultType.Error; return; } //"Name": "Car", // "Confidence": 98.87621307373047, // "Instances": [ // { // "BoundingBox": { // "Width": 0.10527367144823074, // "Height": 0.18472492694854736, // "Left": 0.0042892382480204105, // "Top": 0.5051581859588623 // }, // "Confidence": 98.87621307373047 // }, //Rectangle(xmin, ymin, xmax - xmin, ymax - ymin) // x, y Width, Height this.RectHeight = Convert.ToInt32(Math.Round(curImg.Height * AiDetectionObject.Instances[InstanceIdx].BoundingBox.Height)); this.RectWidth = Convert.ToInt32(Math.Round(curImg.Width * AiDetectionObject.Instances[InstanceIdx].BoundingBox.Width)); double right = (curImg.Width * AiDetectionObject.Instances[InstanceIdx].BoundingBox.Left) + this.RectWidth; double left = curImg.Width * AiDetectionObject.Instances[InstanceIdx].BoundingBox.Left; double top = curImg.Height * AiDetectionObject.Instances[InstanceIdx].BoundingBox.Top; double bottom = (curImg.Height * AiDetectionObject.Instances[InstanceIdx].BoundingBox.Top) + this.RectHeight; this.XMin = Convert.ToInt32(Math.Round(left)); this.YMin = Convert.ToInt32(Math.Round(top)); this.XMax = Convert.ToInt32(Math.Round(right)); this.YMax = Convert.ToInt32(Math.Round(bottom)); //force first letter to always be capitalized this.Label = Global.UpperFirst(AiDetectionObject.Name); this.Confidence = AiDetectionObject.Instances[InstanceIdx].Confidence; this.Filename = curImg.image_path; this.GetObjectType(); }
public bool TryAddFaceFile(ClsImageQueueItem CurImg, string face = "") { if (!CurImg.IsValid()) { return(false); } if (face.IsEmpty()) { face = "Unknown"; } if (face.EqualsIgnoreCase("face")) { face = "Unknown"; } if (face.EqualsIgnoreCase("unknown")) { if (!this.SaveUnknownFaces) { return(false); } } else { if (!this.SaveKnownFaces) { return(false); } } ClsFace FoundFace = this.TryAddFace(face); bool added = FoundFace.TryAddFaceFile(CurImg, out string Outfilename, this.MaxFilesPerFace, this.MaxFileAgeDays); //delete from unknown folder if it was originally from there and it moved if (added) { this.NeedsSaving.WriteFullFence(true); if (CurImg.image_path.Has("\\unknown\\") && !Outfilename.Has("\\unknown\\") && CurImg.image_path.Has("\\face\\") && !Outfilename.Has("\\face\\")) { Global.SafeFileDelete(CurImg.image_path, "TryAddFaceFile"); } } return(added); }
public bool UpdateImageResolutions(ClsImageQueueItem CurImg) { bool ret = false; if (!CurImg.IsValid()) { return(ret); } lock (CamLock) { ImageResItem newri = new ImageResItem(CurImg); int idx = this.ImageResolutions.IndexOf(newri); bool updated = false; if (idx > -1) { if (CurImg.TimeCreated > this.ImageResolutions[idx].LastSeenDate) { updated = true; this.ImageResolutions[idx].LastFileName = CurImg.image_path; this.ImageResolutions[idx].LastSeenDate = CurImg.TimeCreated; this.ImageResolutions[idx].LastFileSize = CurImg.FileSize; this.ImageResolutions[idx].LastFileDPI = CurImg.DPI; this.ImageResolutions[idx].Count++; } } else { ret = true; this.ImageResolutions.Add(newri); } //sort so most recent is at top of list if (ret || updated) { this.ImageResolutions = this.ImageResolutions.OrderByDescending(x => x.LastSeenDate).ToList(); } } return(ret); }
private void Frm_ObjectDetail_Load(object sender, EventArgs e) { Global_GUI.RestoreWindowState(this); this.Show(); try { Global_GUI.ConfigureFOLV(this.folv_ObjectDetail, typeof(ClsPrediction), null, null); Global_GUI.UpdateFOLV(this.folv_ObjectDetail, this.PredictionObjectDetails); if (!String.IsNullOrEmpty(this.ImageFileName) && this.ImageFileName.Contains("\\") && File.Exists(this.ImageFileName)) { OriginalBMP = new ClsImageQueueItem(this.ImageFileName, 0); this.pictureBox1.Image = Image.FromStream(OriginalBMP.ToStream()); //load actual image as background, so that an overlay can be added as the image } } catch (Exception) { throw; } }
public void ScanImages(int MaxFiles = 3000, int MaxTimeScanningMS = 60000, int MaxDaysOld = -4) { using var Trace = new Trace(); //This c# 8.0 using feature will auto dispose when the function is done. List <FileInfo> files = new List <FileInfo>(); Stopwatch fscansw = Stopwatch.StartNew(); if (!string.IsNullOrEmpty(this.input_path) && Directory.Exists(this.input_path)) { List <FileInfo> newfiles = Global.GetFiles(this.input_path, $"{this.Prefix}*.jpg", this.input_path_includesubfolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly, DateTime.Now.AddDays(MaxDaysOld), DateTime.Now, MaxFiles); files.AddRange(newfiles); AITOOL.Log($"Debug: Found {newfiles.Count} {this.Prefix}*.jpg files in {this.input_path}"); } if (files.Count < MaxFiles && !string.IsNullOrEmpty(AppSettings.Settings.input_path) && AppSettings.Settings.input_path != this.input_path && Directory.Exists(AppSettings.Settings.input_path)) { List <FileInfo> newfiles = Global.GetFiles(AppSettings.Settings.input_path, $"{this.Prefix}*.jpg", AppSettings.Settings.input_path_includesubfolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly, DateTime.Now.AddDays(MaxDaysOld), DateTime.Now, MaxFiles); files.AddRange(newfiles); AITOOL.Log($"Debug: Found {newfiles.Count} {this.Prefix}*.jpg files in {AppSettings.Settings.input_path}"); } if (files.Count < MaxFiles && !string.IsNullOrEmpty(this.Action_network_folder) && Directory.Exists(this.Action_network_folder)) { List <FileInfo> newfiles = Global.GetFiles(this.Action_network_folder, $"{this.Prefix}*.jpg", SearchOption.TopDirectoryOnly, DateTime.Now.AddDays(MaxDaysOld), DateTime.Now, MaxFiles); files.AddRange(newfiles); AITOOL.Log($"Debug: Found {newfiles.Count} {this.Prefix}*.jpg files in {this.Action_network_folder}"); } if (!string.IsNullOrEmpty(this.last_image_file) && File.Exists(this.last_image_file)) { if (!files.Any(x => string.Equals(x.FullName, this.last_image_file, StringComparison.OrdinalIgnoreCase))) { files.Add(new FileInfo(this.last_image_file)); } } files = files.OrderByDescending(t => t.CreationTime).ToList(); fscansw.Stop(); int cnt = 0; int updated = 0; int invalid = 0; AITOOL.Log($"Debug: Found {files.Count} images in {fscansw.ElapsedMilliseconds} ms. Scanning images..."); Stopwatch sw = Stopwatch.StartNew(); foreach (FileInfo fi in files) { try { ClsImageQueueItem img = new ClsImageQueueItem(fi.FullName, 0, true); if (img.IsValid()) { if (this.UpdateImageResolutions(img)) { updated++; } cnt++; } else { invalid++; } } catch (Exception ex) { invalid++; AITOOL.Log($"Debug: {fi.Name}: {ex.Message}"); } if (sw.ElapsedMilliseconds >= MaxTimeScanningMS) { AITOOL.Log($"Debug: Max search time exceeded: {sw.ElapsedMilliseconds} ms >= {MaxTimeScanningMS} ms"); break; } } sw.Stop(); string reseseses = ""; foreach (ImageResItem res in this.ImageResolutions) { reseseses += $"{res.ToString()};"; } if (string.IsNullOrEmpty(this.last_image_file) && files.Count > 0) { this.last_image_file = files[0].FullName; } AITOOL.Log($"Debug: {cnt} of {files.Count} image files processed, {updated} new resolutions found ({invalid} invalid) in {sw.ElapsedMilliseconds} ms (Max={MaxTimeScanningMS} ms), {this.ImageResolutions.Count} different image resolutions found: {reseseses}"); }
public ClsPrediction(ObjectType defaultObjType, Camera cam, SightHoundPersonObject AiDetectionObject, ClsImageQueueItem curImg, ClsURLItem curURL, SightHoundImage SHImg) { this._defaultObjType = defaultObjType; this._cam = cam; this._cururl = curURL; this._curimg = curImg; this.Camera = cam.Name; if (AiDetectionObject == null || cam == null || string.IsNullOrWhiteSpace(AiDetectionObject.Type)) { Log("Error: Prediction or Camera was null?", "", this._cam.Name); this.Result = ResultType.Error; return; } this.ImageHeight = SHImg.Height; //curImg.Height; this.ImageWidth = SHImg.Width; //curImg.Width; if (curImg.Height != SHImg.Height) { Log($"Debug: Original image Height does not match returned height: Original={curImg.Height}, Returned={SHImg.Height}"); } if (curImg.Width != SHImg.Width) { Log($"Debug: Original image Width does not match returned Width: Original={curImg.Width}, Returned={SHImg.Width}"); } //{ // "image": { // "width": 1280, // "height": 960, // "orientation": 1 // }, // "objects": [ // { // "type": "person", // "boundingBox": { // "x": 363, // "y": 182, // "height": 778, // "width": 723 // } // }, // { // "type": "face", // "boundingBox": { // "x": 508, // "y": 305, // "height": 406, // "width": 406 // }, // "attributes": { // "gender": "male", // "genderConfidence": 0.9883, // "age":25, // "ageConfidence": 0.2599, // "emotion": "happiness", // "emotionConfidence": 0.9943, // "emotionsAll": { // "neutral": 0.0018, // "sadness": 0.0009, // "disgust": 0.0002, // "anger": 0.0003, // "surprise": 0, // "fear": 0.0022, // "happiness": 0.9943 // }, // "pose":{ // "pitch":-18.4849, // "roll":0.854, // "yaw":-4.2123 // }, // "frontal": true // }, // "landmarks": { // "faceContour": [[515,447],[517,491]...[872,436]], // "noseBridge": [[710,419],[711,441]...[712,487]], // "noseBall": [[680,519],[696,522]...[742,518]], // "eyebrowRight": [[736,387],[768,376]...[854,394]], // "eyebrowLeft": [[555,413],[578,391]...[679,391]], // "eyeRight": [[753,428],[774,414]...[777,432]], // "eyeRightCenter": [[786,423]], // "eyeLeft": [[597,435],[617,423]...[619,442]], // "eyeLeftCenter": [[630,432]], // "mouthOuter": [[650,590],[674,572]...[675,600]], // "mouthInner": [[661,587],[697,580]...[697,584]] // } // } // ] //} //Rectangle(xmin, ymin, xmax - xmin, ymax - ymin) // x, y Width, Height //boundingBox (object): An object containing x, y, width, and height values //defining the location of the object in the image. The top left pixel of //the image represents coordinate (0,0). this.RectHeight = AiDetectionObject.BoundingBox.Height; this.RectWidth = AiDetectionObject.BoundingBox.Width; double right = AiDetectionObject.BoundingBox.X + this.RectWidth; double left = AiDetectionObject.BoundingBox.X; double top = AiDetectionObject.BoundingBox.Y; double bottom = AiDetectionObject.BoundingBox.Y + this.RectHeight; this.XMin = Convert.ToInt32(Math.Round(left)); this.YMin = Convert.ToInt32(Math.Round(top)); this.XMax = Convert.ToInt32(Math.Round(right)); this.YMax = Convert.ToInt32(Math.Round(bottom)); //force first letter to always be capitalized this.Label = Global.UpperFirst(AiDetectionObject.Type); if (AiDetectionObject.Attributes != null && !string.IsNullOrEmpty(AiDetectionObject.Attributes.Gender)) { this.Label += $" [{Global.UpperFirst(AiDetectionObject.Attributes.Gender)}, {AiDetectionObject.Attributes.Age}, {Global.UpperFirst(AiDetectionObject.Attributes.Emotion)}]"; //this isnt exactly right, but sighthound doesnt give confidence for person/face, only gender, age this.Confidence = AiDetectionObject.Attributes.GenderConfidence * 100; } else { this.Confidence = 100; } this.Filename = curImg.image_path; this.GetObjectType(); }
public ClsPrediction(ObjectType defaultObjType, Camera cam, SightHoundVehicleObject AiDetectionObject, ClsImageQueueItem curImg, ClsURLItem curURL, SightHoundImage SHImg) { //https://docs.sighthound.com/cloud/recognition/ this._defaultObjType = defaultObjType; this._cam = cam; this._cururl = curURL; this._curimg = curImg; this.Camera = cam.Name; if (AiDetectionObject == null || cam == null || string.IsNullOrWhiteSpace(AiDetectionObject.ObjectType)) { Log("Error: Prediction or Camera was null?", "", this._cam.Name); this.Result = ResultType.Error; return; } this.ImageHeight = SHImg.Height; //curImg.Height; this.ImageWidth = SHImg.Width; //curImg.Width; if (curImg.Height != SHImg.Height) { Log($"Debug: Original image Height does not match returned height: Original={curImg.Height}, Returned={SHImg.Height}"); } if (curImg.Width != SHImg.Width) { Log($"Debug: Original image Width does not match returned Width: Original={curImg.Width}, Returned={SHImg.Width}"); } //{ // "image": { // "width":2016, // "orientation":1, // "height":1512 //}, //"objects":[ // { // "vehicleAnnotation":{ // "bounding":{ // "vertices":[ // { "x":430, "y":286 }, // { "x":835, "y":286 }, // { "x":835, "y":559 }, // { "x":430, "y":559 } // ] // }, // "attributes":{ // "system":{ // "color":{ // "confidence":0.9968, // "name":"silver/grey" // }, // "make":{ // "confidence":0.8508, // "name":"BMW" // }, // "model":{ // "confidence":0.8508, // "name":"3 Series" // }, // "vehicleType":"car" // } // }, // "recognitionConfidence":0.8508 // "licenseplate":{ // "bounding":{ // "vertices":[ // { "x":617, "y":452 }, // { "x":743, "y":452 }, // { "x":743, "y":482 }, // { "x":617, "y":482 } // ] // }, // "attributes":{ // "system":{ // "region":{ // "name":"Florida", // "confidence":0.9994 // }, // "string":{ // "name":"RTB2", // "confidence":0.999 // }, // "characters":[ // { // "bounding":{ // "vertices":[ // { "y":455, "x":637 }, // { "y":455, "x":649 }, // { "y":473, "x":649 }, // { "y":473, "x":637 } // ] // }, // "index":0, // "confidence":0.9999, // "character":"R" // }, // { // "bounding":{ // "vertices":[ // { "y":455, "x":648 }, // { "y":455, "x":661 }, // { "y":473, "x":661 }, // { "y":473, "x":648 } // ] // }, // "index":1, // "confidence":0.9999, // "character":"T" // }, // { // "bounding":{ // "vertices":[ // { "y":455, "x":671 }, // { "y":455, "x":684 }, // { "y":474, "x":684 }, // { "y":474, "x":671 } // ] // }, // "index":2, // "confidence":0.9996, // "character":"B" // }, // { // "bounding":{ // "vertices":[ // { "y":456, "x":683 }, // { "y":456, "x":696 }, // { "y":474, "x":696 }, // { "y":474, "x":683 } // ] // }, // "index":3, // "confidence":0.9995, // "character":"2" // } // ] // } // } // }, // }, // "objectId":"_vehicle_f3c3d26b-c568-4d98-b6db-b96659fd7766", // "objectType":"vehicle" // } //], //"requestId":"d25b5e5d22f6431498065e9a25134d59" //} //Rectangle(xmin, ymin, xmax - xmin, ymax - ymin) // x, y Width, Height // // vertices (array): A list of objects that define coordinates of the vertices that surround the Object // x (integer): Horizontal pixel position of the vertex // y (integer): Vertical pixel position of the vertex // get the bounding box from vertices List <System.Drawing.Point> pts = new List <System.Drawing.Point>(); foreach (var pt in AiDetectionObject.VehicleAnnotation.Bounding.Vertices) { pts.Add(new System.Drawing.Point(pt.X, pt.Y)); } Rectangle rect = Global.RectFromVertices(pts); this.RectHeight = rect.Height; this.RectWidth = rect.Width; double right = rect.X + this.RectWidth; double left = rect.X; double top = rect.Y; double bottom = rect.Y + this.RectHeight; this.XMin = Convert.ToInt32(Math.Round(left)); this.YMin = Convert.ToInt32(Math.Round(top)); this.XMax = Convert.ToInt32(Math.Round(right)); this.YMax = Convert.ToInt32(Math.Round(bottom)); if (AiDetectionObject.VehicleAnnotation != null && !string.IsNullOrEmpty(AiDetectionObject.VehicleAnnotation.Attributes.System.VehicleType)) { string type = Global.UpperFirst(AiDetectionObject.VehicleAnnotation.Attributes.System.VehicleType); string color = Global.UpperFirst(AiDetectionObject.VehicleAnnotation.Attributes.System.Color.Name); string make = Global.UpperFirst(AiDetectionObject.VehicleAnnotation.Attributes.System.Make.Name); string model = Global.UpperFirst(AiDetectionObject.VehicleAnnotation.Attributes.System.Model.Name); string plate = "Unknown"; if (AiDetectionObject.VehicleAnnotation.Licenseplate != null && !string.IsNullOrEmpty(AiDetectionObject.VehicleAnnotation.Licenseplate.Attributes.System.String.Name)) { plate = $"{AiDetectionObject.VehicleAnnotation.Licenseplate.Attributes.System.Region.Name} {AiDetectionObject.VehicleAnnotation.Licenseplate.Attributes.System.String.Name}"; } this.Label += $"{type} [{color}, {make}, {model}, Plate={plate}]"; //this isnt exactly right, but sighthound doesnt give confidence for person/face, only gender, age this.Confidence = AiDetectionObject.VehicleAnnotation.RecognitionConfidence * 100; } else { this.Confidence = 100; } this.Filename = curImg.image_path; this.GetObjectType(); }
public ClsPrediction(ObjectType defaultObjType, Camera cam, Amazon.Rekognition.Model.FaceDetail AiDetectionObject, ClsImageQueueItem curImg, ClsURLItem curURL) { this._defaultObjType = defaultObjType; this._cam = cam; this._cururl = curURL; this._curimg = curImg; this.Camera = cam.Name; this.BICamName = cam.BICamName; this.ImageHeight = curImg.Height; this.ImageWidth = curImg.Width; this.Server = curURL.CurSrv; this.Time = DateTime.Now; this.Filename = curImg.image_path; if (AiDetectionObject == null || cam == null) { Log("Error: Prediction or Camera was null?", "", this._cam.Name); this.Result = ResultType.Error; return; } this.RectHeight = Convert.ToInt32(Math.Round(curImg.Height * AiDetectionObject.BoundingBox.Height)); this.RectWidth = Convert.ToInt32(Math.Round(curImg.Width * AiDetectionObject.BoundingBox.Width)); double right = (curImg.Width * AiDetectionObject.BoundingBox.Left) + this.RectWidth; double left = curImg.Width * AiDetectionObject.BoundingBox.Left; double top = curImg.Height * AiDetectionObject.BoundingBox.Top; double bottom = (curImg.Height * AiDetectionObject.BoundingBox.Top) + this.RectHeight; this.XMin = Convert.ToInt32(Math.Round(left)); this.YMin = Convert.ToInt32(Math.Round(top)); this.XMax = Convert.ToInt32(Math.Round(right)); this.YMax = Convert.ToInt32(Math.Round(bottom)); //[{Global.UpperFirst(AiDetectionObject.Attributes.Gender)}, {AiDetectionObject.Attributes.Age}, {Global.UpperFirst(AiDetectionObject.Attributes.Emotion)}] string emotions = ""; List <Emotion> emotionslist = new List <Emotion>(); if (AiDetectionObject.Emotions != null && AiDetectionObject.Emotions.Count > 0) { foreach (Emotion em in AiDetectionObject.Emotions) { if (em.Confidence >= 25) { emotionslist.Add(em); } } if (emotionslist.Count > 0) { //sort so highest conf is first emotionslist = emotionslist.OrderByDescending(a => a.Confidence).ToList(); emotions = ", "; int cnt = 0; foreach (Emotion em in emotionslist) { emotions += $"{Global.UpperFirst(em.Type.ToString().ToLower())};"; cnt++; if (cnt > 3) { break; } } emotions = emotions.Trim(" ;".ToCharArray()); } } string gender = ""; if (AiDetectionObject.Gender != null) { gender = $", {AiDetectionObject.Gender.Value}"; } string age = ""; if (AiDetectionObject.AgeRange != null) { age = $", {AiDetectionObject.AgeRange.Low}-{AiDetectionObject.AgeRange.High}"; } string smile = ""; if (AiDetectionObject.Smile.Value) { smile = ", Smile"; } string eyeglasses = ""; if (AiDetectionObject.Eyeglasses.Value) { eyeglasses = ", Eyeglasses"; } string sunglasses = ""; if (AiDetectionObject.Sunglasses.Value) { sunglasses = ", Sunglasses"; } string beard = ""; if (AiDetectionObject.Beard.Value) { beard = ", Beard"; } string mustache = ""; if (AiDetectionObject.Mustache.Value) { mustache = ", Mustache"; } string mouthopen = ""; if (AiDetectionObject.MouthOpen.Value) { mouthopen = ", MouthOpen"; } this.Label = "Face"; this.Detail = $"{gender}{age}{emotions}{smile}{eyeglasses}{sunglasses}{beard}{mustache}{mouthopen}".Trim(", ".ToCharArray()); this.Confidence = AiDetectionObject.Confidence; this.GetObjectType(); }
public bool TryAddFaceFile(ClsImageQueueItem CurImg, out string Newfilename, int MaxFilesPerFace, int MaxFileAgeDays) { Newfilename = ""; if (!CurImg.IsValid()) { return(false); } string fname = Path.GetFileName(CurImg.image_path).ToLower(); Newfilename = Path.Combine(this.FaceStoragePath, fname); if (!this.FilesDic.ContainsKey(fname)) { if (this.FilesDic.Count > 1) { //Get the oldest one ClsFaceFile oldest = this.FilesDic.Values.OrderBy(f => f.DateFileModified).First(); if (this.FilesDic.Count > MaxFilesPerFace) { string firstname = Path.GetFileName(oldest.FilePath).ToLower(); bool removed = this.FilesDic.TryRemove(firstname, out ClsFaceFile value); if (removed && oldest.Exists) { File.Delete(oldest.FilePath); } } if ((DateTime.Now - oldest.DateFileModified).TotalDays > MaxFileAgeDays) { //remove the first one string firstname = Path.GetFileName(oldest.FilePath).ToLower(); bool removed = this.FilesDic.TryRemove(firstname, out ClsFaceFile value); if (removed && oldest.Exists) { File.Delete(oldest.FilePath); } } } //if in different location, copy it in if (!CurImg.image_path.EqualsIgnoreCase(Newfilename)) { if (!CurImg.CopyFileTo(Newfilename)) { return(false); } } ClsFaceFile ff = new ClsFaceFile(Newfilename, this); ff.OriginalPath = CurImg.image_path; ff.OriginalCamera = GetCamera(ff.OriginalPath, true).Name; this.FilesDic.TryAdd(fname, ff); return(true); } return(false); }
private async void btTest_Click(object sender, EventArgs e) { AITOOL.UpdateAIURLs(); string pth = Global.GetSetting("TestImage", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestImage.jpg")); OpenFileDialog ofd = new OpenFileDialog { InitialDirectory = Path.GetDirectoryName(pth), FileName = Path.GetFileName(pth), Title = "Select test image", CheckFileExists = true, CheckPathExists = true, DefaultExt = "jpg", Filter = "jpg files (*.jpg)|*.jpg", FilterIndex = 2, RestoreDirectory = true, ShowReadOnly = true }; if (ofd.ShowDialog() == DialogResult.OK) { pth = ofd.FileName; Global.SaveSetting("TestImage", pth); if (File.Exists(pth)) { //must create a temp unique file every time because database key is the filename string tpth = Path.Combine(Path.GetTempPath(), $"TEST_CAM.{DateTime.Now:yyyy-MM-dd_HH-mm-ss-fff}"); File.Copy(pth, tpth, true); btTest.Enabled = false; bt_Save.Enabled = false; btTest.Text = "Working..."; this.Update(); ClsImageQueueItem CurImg = new ClsImageQueueItem(tpth, 0); Camera cam = new Camera("TEST_CAM"); bool ret = await AITOOL.DetectObjects(CurImg, this.CurURL, cam); btTest.Enabled = true; bt_Save.Enabled = true; btTest.Text = "Test"; if (ret) { this.CurURL.ErrCount.WriteUnfenced(0); this.CurURL.CurErrCount.WriteUnfenced(0); MessageBox.Show($"Success! {this.CurURL.LastResultMessage}", "Success"); } else { MessageBox.Show($"Error! {this.CurURL.LastResultMessage}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show($"Test file does not exist:\r\n{pth}"); } } }
private async void btTest_ClickAsync(object sender, EventArgs e) { this.btTest.Enabled = false; this.btnSave.Enabled = false; this.btnCancel.Enabled = false; try { AppSettings.Settings.mqtt_serverandport = this.tb_ServerPort.Text.Trim(); AppSettings.Settings.mqtt_password = this.tb_Password.Text.Trim(); AppSettings.Settings.mqtt_username = this.tb_Username.Text.Trim(); AppSettings.Settings.mqtt_UseTLS = this.cb_UseTLS.Checked; using (Global_GUI.CursorWait cw = new Global_GUI.CursorWait()) { Log("------ TESTING MQTT --------"); string topic = AITOOL.ReplaceParams(this.cam, null, null, this.tb_Topic.Text.Trim()); string payload = AITOOL.ReplaceParams(this.cam, null, null, this.tb_Payload.Text.Trim()); List <string> topics = Global.Split(topic, ";|"); List <string> payloads = Global.Split(payload, ";|"); MQTTClient mq = new MQTTClient(); MqttClientPublishResult pr = null; ClsImageQueueItem CurImg = null; for (int i = 0; i < topics.Count; i++) { if (this.cam.Action_mqtt_send_image) { if (topics[i].IndexOf("/image", StringComparison.OrdinalIgnoreCase) >= 0) { if (!string.IsNullOrEmpty(this.cam.last_image_file_with_detections) && File.Exists(this.cam.last_image_file_with_detections)) { CurImg = new ClsImageQueueItem(this.cam.last_image_file_with_detections, 0); } else if (!string.IsNullOrEmpty(this.cam.last_image_file) && File.Exists(this.cam.last_image_file)) { CurImg = new ClsImageQueueItem(this.cam.last_image_file, 0); } else { CurImg = null; } } else { CurImg = null; } } pr = await mq.PublishAsync(topics[i], payloads[i], this.cam.Action_mqtt_retain_message, CurImg); } Log("------ DONE TESTING MQTT --------"); if (pr != null && (pr.ReasonCode == MqttClientPublishReasonCode.Success)) { MessageBox.Show("Success! See Log for details."); } else if (pr != null) { MessageBox.Show($"Failed. See log for details. Reason={pr.ReasonCode}", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show($"Failed. See log for details.", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch { } finally { this.btTest.Enabled = true; this.btnSave.Enabled = true; this.btnCancel.Enabled = true; } }
public async Task <MqttClientPublishResult> PublishAsync(string topic, string payload, bool retain, ClsImageQueueItem CurImg) { using var Trace = new Trace(); //This c# 8.0 using feature will auto dispose when the function is done. MqttClientPublishResult res = null; Stopwatch sw = Stopwatch.StartNew(); this.LastRetain = retain; this.LastPayload = payload; this.LastTopic = topic; if (string.IsNullOrWhiteSpace(topic)) { this.LastTopic = Guid.NewGuid().ToString(); } if (!this.IsConnected) { await this.Connect(); } if (!this.IsConnected) { return(res); } await Task.Run(async() => { try { Log($"Debug: MQTT: Sending topic '{this.LastTopic}' with payload '{this.LastPayload}' to server '{this.server}:{this.portint}'..."); if (cres != null && mqttClient.IsConnected && cres.ResultCode == MqttClientConnectResultCode.Success) { IsConnected = true; MqttApplicationMessage ma; if (CurImg != null) { //using FileStream image_data = System.IO.File.OpenRead(CurImg.image_path); ma = new MqttApplicationMessageBuilder() .WithTopic(this.LastTopic) .WithPayload(CurImg.ToStream()) .WithAtLeastOnceQoS() .WithRetainFlag(this.LastRetain) .Build(); res = await mqttClient.PublishAsync(ma, CancellationToken.None); if (res.ReasonCode == MqttClientPublishReasonCode.Success) { Log($"Debug: MQTT: ...Sent image in {sw.ElapsedMilliseconds}ms, Reason: '{res.ReasonCode}' ({Convert.ToInt32(res.ReasonCode)} - '{res.ReasonString}')"); } else { Log($"Error: MQTT: sending image: ({sw.ElapsedMilliseconds}ms) Reason: '{res.ReasonCode}' ({Convert.ToInt32(res.ReasonCode)} - '{res.ReasonString}')"); } } else { ma = new MqttApplicationMessageBuilder() .WithTopic(this.LastTopic) .WithPayload(this.LastPayload) .WithAtLeastOnceQoS() .WithRetainFlag(this.LastRetain) .Build(); res = await mqttClient.PublishAsync(ma, CancellationToken.None); //Success = 0, // NoMatchingSubscribers = 0x10, // UnspecifiedError = 0x80, // ImplementationSpecificError = 0x83, // NotAuthorized = 0x87, // TopicNameInvalid = 0x90, // PacketIdentifierInUse = 0x91, // QuotaExceeded = 0x97, // PayloadFormatInvalid = 0x99 if (res.ReasonCode == MqttClientPublishReasonCode.Success) { Log($"Debug: MQTT: ...Sent in {sw.ElapsedMilliseconds}ms, Reason: '{res.ReasonCode}' ({Convert.ToInt32(res.ReasonCode)} - '{res.ReasonString}')"); } else { Log($"Error: MQTT: sending: ({sw.ElapsedMilliseconds}ms) Reason: '{res.ReasonCode}' ({Convert.ToInt32(res.ReasonCode)} - '{res.ReasonString}')"); } } } else if (cres != null) { IsConnected = false; Log($"Error: MQTT: connecting: ({sw.ElapsedMilliseconds}ms) Result: '{cres.ResultCode}' - '{cres.ReasonString}'"); } else { IsConnected = false; Log($"Error: MQTT: Error connecting: ({sw.ElapsedMilliseconds}ms) cres=null"); } } catch (Exception ex) { Log($"Error: MQTT: Unexpected Problem: Topic '{this.LastTopic}' Payload '{this.LastPayload}': " + ex.Msg()); } finally { } }); return(res); }
private async void btTest_Click(object sender, EventArgs e) { this.UpdateURL(); AITOOL.UpdateAIURLs(); string pth = Global.GetRegSetting("TestImage", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestImage.jpg")); OpenFileDialog ofd = new OpenFileDialog { InitialDirectory = Path.GetDirectoryName(pth), FileName = Path.GetFileName(pth), Title = "Select test image", CheckFileExists = true, CheckPathExists = true, DefaultExt = "jpg", Filter = "jpg files (*.jpg)|*.jpg", FilterIndex = 2, RestoreDirectory = true, ShowReadOnly = true }; if (ofd.ShowDialog() == DialogResult.OK) { pth = ofd.FileName; Global.SaveRegSetting("TestImage", pth); if (File.Exists(pth)) { //must create a temp unique file every time because database key is the filename Camera cam = AITOOL.GetCamera("default", true); if (cam == null) { cam = new Camera("TEST_CAM"); } string ext = Path.GetExtension(pth); string tpth = Path.Combine(Path.GetTempPath(), $"{cam.Name}.URLTEST.{DateTime.Now:yyyy-MM-dd_HH-mm-ss-fff}{ext}"); File.Copy(pth, tpth, true); btTest.Enabled = false; bt_Save.Enabled = false; btTest.Text = "Working..."; this.UpdateURL(); ClsImageQueueItem CurImg = new ClsImageQueueItem(tpth, 0); List <ClsURLItem> linked = new List <ClsURLItem> { this.CurURL }; if (this.CurURL.LinkServerResults && !string.IsNullOrEmpty(this.CurURL.LinkedResultsServerList)) { linked.AddRange(await AITOOL.WaitForNextURL(cam, false, null, this.CurURL.LinkedResultsServerList)); } if (linked.Count > 1) { AITOOL.Log($"Debug: ---- Found '{linked.Count}' linked AI URL's."); } AITOOL.DetectObjectsResult result = await AITOOL.DetectObjects(CurImg, linked, cam); //make sure not stuck in use for the test: foreach (var url in result.OutURLs) { url.InUse.WriteFullFence(false); } btTest.Enabled = true; bt_Save.Enabled = true; btTest.Text = "Test"; if (result.Success) { Frm_ObjectDetail frm = new Frm_ObjectDetail(); frm.PredictionObjectDetails = result.OutPredictions; frm.ImageFileName = tpth; frm.Show(); MessageBox.Show($"Success! {this.CurURL.LastResultMessage}", "Success"); } else { MessageBox.Show($"Error! {this.CurURL.LastResultMessage}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } this.CurURL.ErrCount.WriteUnfenced(0); this.CurURL.CurErrCount.WriteUnfenced(0); } else { MessageBox.Show($"Test file does not exist:\r\n{pth}"); } } }
public string GetMaskFile(bool MustExist, ClsImageQueueItem CurImg = null, ImageResItem ir = null) { string ret = ""; lock (CamLock) { try { String resstr = ""; if (CurImg != null) { resstr = $"_{CurImg.Width}x{CurImg.Height}"; } else if (ir == null && this.ImageResolutions.Count > 0) { ir = this.ImageResolutions[0]; //the first one should be the most recent image processed because of the sort. resstr = $"_{ir.Width}x{ir.Height}"; } else if (ir != null) { resstr = $"_{ir.Width}x{ir.Height}"; } string CamMaskFile = ""; if (!string.IsNullOrEmpty(this.MaskFileName)) { if (this.MaskFileName.Contains("\\") && this.MaskFileName.Contains(".")) { CamMaskFile = this.MaskFileName; } else if (this.MaskFileName.Contains(".")) { CamMaskFile = Path.Combine(Path.GetDirectoryName(AppSettings.Settings.SettingsFileName), $"{this.MaskFileName}"); } else { CamMaskFile = Path.Combine(Path.GetDirectoryName(AppSettings.Settings.SettingsFileName), $"{this.MaskFileName}.bmp"); } //Add WidthxHeight to filename string ResFile = Path.Combine(Path.GetDirectoryName(CamMaskFile), $"{Path.GetFileNameWithoutExtension(CamMaskFile)}{resstr}.bmp"); bool resempty = string.IsNullOrEmpty(resstr); bool cammaskexist = File.Exists(CamMaskFile); bool resexist = File.Exists(ResFile); if (!resempty && cammaskexist && !Path.GetFileName(CamMaskFile).Contains("_") && !resexist) { //lets rename it to appropriate ResFile name string tmpresstr = ""; using (FileStream fileStream = new FileStream(CamMaskFile, FileMode.Open, FileAccess.Read)) { using Image img = Image.FromStream(fileStream, false, false); tmpresstr = $"_{img.Width}x{img.Height}"; } if (tmpresstr == resstr) { AITOOL.Log($"Debug: Renaming mask file from '{CamMaskFile}' to '{ResFile}'..."); File.Move(CamMaskFile, ResFile); } else { AITOOL.Log($"Debug: Cannot rename mask file because it does not match the current image resolution of '{resstr}' (!={tmpresstr}): MaskFile='{CamMaskFile}'..."); } } else { //AITOOL.Log($"Debug: ResEmpty={resempty}, CamMaskExist={cammaskexist}, ResMaskFileExist={resexist}, CurRes={resstr}, MaskRes={resstr}, CamMaskFile='{CamMaskFile}', ResMaskFile={ResFile}..."); } if (MustExist) { if (File.Exists(ResFile)) { return(ResFile); } else { return(""); } } else { return(ResFile); } } } catch (Exception ex) { AITOOL.Log("Error: " + ex.Msg()); } } return(ret); }
public async Task <MqttClientPublishResult> PublishAsync(string topic, string payload, bool retain, ClsImageQueueItem CurImg) { using var Trace = new Trace(); //This c# 8.0 using feature will auto dispose when the function is done. MqttClientPublishResult res = null; Stopwatch sw = Stopwatch.StartNew(); await Task.Run(async() => { MqttFactory factory = new MqttFactory(); IMqttClient mqttClient = null; bool subscribed = false; bool IsConnected = false; using (mqttClient = factory.CreateMqttClient()) { try { string server = Global.GetWordBetween(AppSettings.Settings.mqtt_serverandport, "", ":"); string port = Global.GetWordBetween(AppSettings.Settings.mqtt_serverandport, ":", "/"); int portint = 0; if (!string.IsNullOrEmpty(port)) { portint = Convert.ToInt32(port); } if (portint == 0 && AppSettings.Settings.mqtt_UseTLS) { portint = 8883; } else if (portint == 0) { portint = 1883; } bool IsWebSocket = (AppSettings.Settings.mqtt_serverandport.IndexOf("ws://", StringComparison.OrdinalIgnoreCase) >= 0 || AppSettings.Settings.mqtt_serverandport.IndexOf("/mqtt", StringComparison.OrdinalIgnoreCase) >= 0 || AppSettings.Settings.mqtt_serverandport.IndexOf("wss://", StringComparison.OrdinalIgnoreCase) >= 0); bool UseTLS = (AppSettings.Settings.mqtt_UseTLS || portint == 8883 || AppSettings.Settings.mqtt_serverandport.ToLower().Contains("wss://")); bool UseCreds = (!string.IsNullOrWhiteSpace(AppSettings.Settings.mqtt_username)); IMqttClientOptions options; //===================================================================== //Seems like there should be a better way here with this Options builder... //I dont see an obvious way to directly modify options without the builder //and I cant seem to put IF statements around each part of the option builder //parameters. //===================================================================== if (UseTLS) { if (UseCreds) { if (IsWebSocket) { options = new MqttClientOptionsBuilder() .WithClientId(AppSettings.Settings.mqtt_clientid) .WithWebSocketServer(AppSettings.Settings.mqtt_serverandport) .WithCredentials(AppSettings.Settings.mqtt_username, AppSettings.Settings.mqtt_password) .WithTls() .WithCleanSession() .Build(); } else { options = new MqttClientOptionsBuilder() .WithClientId(AppSettings.Settings.mqtt_clientid) .WithTcpServer(server, portint) .WithCredentials(AppSettings.Settings.mqtt_username, AppSettings.Settings.mqtt_password) .WithTls() .WithCleanSession() .Build(); } } else { if (IsWebSocket) { options = new MqttClientOptionsBuilder() .WithClientId(AppSettings.Settings.mqtt_clientid) .WithWebSocketServer(AppSettings.Settings.mqtt_serverandport) .WithTls() .WithCleanSession() .Build(); } else { options = new MqttClientOptionsBuilder() .WithClientId(AppSettings.Settings.mqtt_clientid) .WithTcpServer(server, portint) .WithTls() .WithCleanSession() .Build(); } } } else { if (UseCreds) { if (IsWebSocket) { options = new MqttClientOptionsBuilder() .WithClientId(AppSettings.Settings.mqtt_clientid) .WithWebSocketServer(AppSettings.Settings.mqtt_serverandport) .WithCredentials(AppSettings.Settings.mqtt_username, AppSettings.Settings.mqtt_password) .WithCleanSession() .Build(); } else { options = new MqttClientOptionsBuilder() .WithClientId(AppSettings.Settings.mqtt_clientid) .WithTcpServer(server, portint) .WithCredentials(AppSettings.Settings.mqtt_username, AppSettings.Settings.mqtt_password) .WithCleanSession() .Build(); } } else { if (IsWebSocket) { options = new MqttClientOptionsBuilder() .WithClientId(AppSettings.Settings.mqtt_clientid) .WithTcpServer(server, portint) .WithCleanSession() .Build(); } else { options = new MqttClientOptionsBuilder() .WithClientId(AppSettings.Settings.mqtt_clientid) .WithWebSocketServer(AppSettings.Settings.mqtt_serverandport) .WithCleanSession() .Build(); } } } if (string.IsNullOrWhiteSpace(topic)) { topic = Guid.NewGuid().ToString(); } mqttClient.UseDisconnectedHandler(async e => { IsConnected = false; string excp = ""; if (e.Exception != null) { excp = e.Exception.Message; } Log($"Debug: MQTT: ### DISCONNECTED FROM SERVER ### - Reason: {e.ReasonCode}, ClientWasDisconnected: {e.ClientWasConnected}, {excp}"); //reconnect here if needed? }); mqttClient.UseApplicationMessageReceivedHandler(async e => { Log($"Debug: MQTT: ### RECEIVED APPLICATION MESSAGE ###"); Log($"Debug: MQTT: + Topic = {e.ApplicationMessage.Topic}"); if (e.ApplicationMessage.Payload.Length < 64) { Log($"Debug: MQTT: + Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}"); } Log($"Debug: MQTT: + QoS = {e.ApplicationMessage.QualityOfServiceLevel}"); Log($"Debug: MQTT: + Retain = {e.ApplicationMessage.Retain}"); Log(""); }); mqttClient.UseConnectedHandler(async e => { IsConnected = true; Log($"Debug: MQTT: ### CONNECTED WITH SERVER '{AppSettings.Settings.mqtt_serverandport}' ### - Result: {e.AuthenticateResult.ResultCode}, '{e.AuthenticateResult.ReasonString}'"); // Subscribe to the topic await mqttClient.SubscribeAsync(topic, MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce); subscribed = true; Log($"Debug: MQTT: ### SUBSCRIBED to topic '{topic}'"); }); Log($"Debug: MQTT: Sending topic '{topic}' with payload '{payload}' to server '{server}:{portint}'..."); MqttClientAuthenticateResult cres = await mqttClient.ConnectAsync(options, CancellationToken.None); if (cres != null && mqttClient.IsConnected && cres.ResultCode == MqttClientConnectResultCode.Success) { IsConnected = true; MqttApplicationMessage ma; if (CurImg != null) { using FileStream image_data = System.IO.File.OpenRead(CurImg.image_path); ma = new MqttApplicationMessageBuilder() .WithTopic(topic) .WithPayload(image_data) .WithAtLeastOnceQoS() .WithRetainFlag(retain) .Build(); res = await mqttClient.PublishAsync(ma, CancellationToken.None); if (res.ReasonCode == MqttClientPublishReasonCode.Success) { Log($"Debug: MQTT: ...Sent image in {sw.ElapsedMilliseconds}ms, Reason: '{res.ReasonCode}' ({Convert.ToInt32(res.ReasonCode)} - '{res.ReasonString}')"); } else { Log($"Error: MQTT: sending image: ({sw.ElapsedMilliseconds}ms) Reason: '{res.ReasonCode}' ({Convert.ToInt32(res.ReasonCode)} - '{res.ReasonString}')"); } } else { ma = new MqttApplicationMessageBuilder() .WithTopic(topic) .WithPayload(payload) .WithAtLeastOnceQoS() .WithRetainFlag(retain) .Build(); res = await mqttClient.PublishAsync(ma, CancellationToken.None); //Success = 0, // NoMatchingSubscribers = 0x10, // UnspecifiedError = 0x80, // ImplementationSpecificError = 0x83, // NotAuthorized = 0x87, // TopicNameInvalid = 0x90, // PacketIdentifierInUse = 0x91, // QuotaExceeded = 0x97, // PayloadFormatInvalid = 0x99 if (res.ReasonCode == MqttClientPublishReasonCode.Success) { Log($"Debug: MQTT: ...Sent in {sw.ElapsedMilliseconds}ms, Reason: '{res.ReasonCode}' ({Convert.ToInt32(res.ReasonCode)} - '{res.ReasonString}')"); } else { Log($"Error: MQTT: sending: ({sw.ElapsedMilliseconds}ms) Reason: '{res.ReasonCode}' ({Convert.ToInt32(res.ReasonCode)} - '{res.ReasonString}')"); } } } else if (cres != null) { IsConnected = false; Log($"Error: MQTT: connecting: ({sw.ElapsedMilliseconds}ms) Result: '{cres.ResultCode}' - '{cres.ReasonString}'"); } else { IsConnected = false; Log($"Error: MQTT: Error connecting: ({sw.ElapsedMilliseconds}ms) cres=null"); } if (mqttClient != null && mqttClient.IsConnected && IsConnected) { if (subscribed) { Log($"Debug: MQTT: Unsubscribing from topic '{topic}'"); await mqttClient.UnsubscribeAsync(topic); } if (mqttClient.IsConnected && IsConnected) { Log($"Debug: MQTT: Disconnecting from server."); try { await mqttClient.DisconnectAsync(); } catch (Exception ex) { //dont throw ERROR in the log if fail to disconnect Log($"Debug: MQTT: Could not disconnect from server, got: {Global.ExMsg(ex)}"); } } else { Log($"Debug: MQTT: Already disconnected from server, no need to disconnect."); } IsConnected = false; } } catch (Exception ex) { Log($"Error: MQTT: Unexpected Problem: Topic '{topic}' Payload '{payload}': " + Global.ExMsg(ex)); } finally { if (mqttClient != null && mqttClient.IsConnected) { if (subscribed) { Log($"Debug: MQTT: Un-Subscribing from topic '{topic}'"); await mqttClient.UnsubscribeAsync(topic); } Log($"Debug: MQTT: Disconnecting from server."); await mqttClient.DisconnectAsync(); mqttClient.Dispose(); //using should dispose anyway } } } }); return(res); }
public async void MergeImageAnnotations(string OutputImageFile, ClsImageQueueItem CurImg = null) { int countr = 0; string detections = ""; string lasttext = ""; string lastposition = ""; try { string InputImageFile = ""; if (CurImg == null) { InputImageFile = this.last_image_file_with_detections; } else { InputImageFile = CurImg.image_path; } if (File.Exists(InputImageFile)) { Stopwatch sw = Stopwatch.StartNew(); using (Bitmap img = new Bitmap(InputImageFile)) { using (Graphics g = Graphics.FromImage(img)) { g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; //http://csharphelper.com/blog/2014/09/understand-font-aliasing-issues-in-c/ g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; System.Drawing.Color color = new System.Drawing.Color(); detections = this.last_detections_summary; if (string.IsNullOrEmpty(detections)) { detections = ""; } if (detections.Contains("irrelevant") || detections.Contains("masked") || detections.Contains("confidence")) { color = System.Drawing.Color.FromArgb(AppSettings.Settings.RectIrrelevantColorAlpha, AppSettings.Settings.RectIrrelevantColor); detections = detections.Split(':')[1]; //removes the "1x masked, 3x irrelevant:" before the actual detection, otherwise this would be displayed in the detection tags } else { color = System.Drawing.Color.FromArgb(AppSettings.Settings.RectRelevantColorAlpha, AppSettings.Settings.RectRelevantColor); } //List<string> detectlist = Global.Split(detections, "|;"); countr = this.last_detections.Count(); //display a rectangle around each relevant object for (int i = 0; i < countr; i++) { //({ Math.Round((user.confidence * 100), 2).ToString() }%) lasttext = $"{this.last_detections[i]} {String.Format(AppSettings.Settings.DisplayPercentageFormat, this.last_confidences[i] * 100)}"; lastposition = this.last_positions[i]; //load 'xmin,ymin,xmax,ymax' from third column into a string //store xmin, ymin, xmax, ymax in separate variables Int32.TryParse(lastposition.Split(',')[0], out int xmin); Int32.TryParse(lastposition.Split(',')[1], out int ymin); Int32.TryParse(lastposition.Split(',')[2], out int xmax); Int32.TryParse(lastposition.Split(',')[3], out int ymax); xmin = xmin + this.XOffset; ymin = ymin + this.YOffset; System.Drawing.Rectangle rect = new System.Drawing.Rectangle(xmin, ymin, xmax - xmin, ymax - ymin); using (Pen pen = new Pen(color, 2)) { g.DrawRectangle(pen, rect); //draw rectangle } //object name text below rectangle rect = new System.Drawing.Rectangle(xmin - 1, ymax, img.Width, img.Height); //sets bounding box for drawn text Brush brush = new SolidBrush(color); //sets background rectangle color System.Drawing.SizeF size = g.MeasureString(lasttext, new Font(AppSettings.Settings.RectDetectionTextFont, AppSettings.Settings.RectDetectionTextSize)); //finds size of text to draw the background rectangle g.FillRectangle(brush, xmin - 1, ymax, size.Width, size.Height); //draw grey background rectangle for detection text g.DrawString(lasttext, new Font(AppSettings.Settings.RectDetectionTextFont, AppSettings.Settings.RectDetectionTextSize), Brushes.Black, rect); //draw detection text g.Flush(); //Global.Log($"...{i}, LastText='{lasttext}' - LastPosition='{lastposition}'"); } if (countr > 0) { GraphicsState gs = g.Save(); ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg); // Create an Encoder object based on the GUID // for the Quality parameter category. System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; // Create an EncoderParameters object. // An EncoderParameters object has an array of EncoderParameter // objects. In this case, there is only one // EncoderParameter object in the array. EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, this.Action_image_merge_jpegquality); //100=least compression, largest file size, best quality myEncoderParameters.Param[0] = myEncoderParameter; bool Success = true; if (File.Exists(OutputImageFile)) { Success = await Global.WaitForFileAccessAsync(OutputImageFile, FileSystemRights.FullControl, FileShare.ReadWrite); } if (Success) { img.Save(OutputImageFile, jpgEncoder, myEncoderParameters); Global.Log($"Merged {countr} detections in {sw.ElapsedMilliseconds}ms into image {OutputImageFile}"); } else { Global.Log($"Error: Could not gain access to write merged file {OutputImageFile}"); } } else { Global.Log($"No detections to merge. Time={sw.ElapsedMilliseconds}ms, {OutputImageFile}"); } } } } else { Global.Log("Error: could not find last image with detections: " + this.last_image_file_with_detections); } } catch (Exception ex) { Global.Log($"Error: Detections='{detections}', LastText='{lasttext}', LastPostions='{lastposition}' - " + Global.ExMsg(ex)); } }