MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
Database.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Drawing;
6using System.Data.SqlClient;
7using System.Diagnostics;
8using System.Net;
9using System.Runtime.InteropServices;
10using System.Threading;
12using MyCaffe.basecode;
13using System.Data.Entity.Infrastructure;
14using System.IO;
15using System.Threading.Tasks;
16
18{
22 public class Database : IDisposable
23 {
27 protected Source m_src = null;
28 DNNEntities m_entities = null;
29 List<Label> m_rgLabelCache;
33 protected string m_strPrimaryImgPath = null;
37 protected string m_strSecondaryImgPath = null;
41 protected bool m_bEnableFileBasedData = false;
45 protected int m_nLastIndex = -1;
46 object m_objSync = new object();
47 Dictionary<int, string> m_rgSecondarySourcePath = new Dictionary<int, string>();
48 object m_objRawImgSync = new object();
49 Dictionary<string, string> m_rgstrDatabaseFilePath = new Dictionary<string, string>();
50 object m_objRemoteSync = new object();
51 FileWriter m_fileWriter = null;
52
56 public enum FORCE_LOAD
57 {
61 NONE,
65 FROM_FILE,
69 FROM_DB
70 }
71
75 public Database()
76 {
77 }
78
82 public void Dispose()
83 {
84 if (m_fileWriter != null)
85 m_fileWriter.Cancel();
86 }
87
94 public bool VerifyDataConnection(string strSrc, ConnectInfo ci)
95 {
96 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
97 {
98 string strPrimaryPath = m_strPrimaryImgPath;
99
100 try
101 {
102 int nSrcId = GetSourceID(strSrc, ci);
103
104 List<RawImage> rgImg = entities.RawImages.Where(p => p.SourceID == nSrcId).Take(1).ToList();
105 if (rgImg.Count == 0)
106 throw new Exception("No images found for data source = '" + strSrc + "' on connection: " + ci.ToString("signalpop."));
107
108 byte[] rgRawData = rgImg[0].Data;
109 int nVirtualId = rgImg[0].VirtualID.GetValueOrDefault(0);
110 if (rgRawData == null && nVirtualId > 0)
111 {
112 int nSrcId1 = rgImg[0].OriginalSourceID.GetValueOrDefault(rgImg[0].SourceID.GetValueOrDefault(0));
113 rgImg = entities.RawImages.Where(p => p.ID == nVirtualId).Take(1).ToList();
114 if (rgImg.Count == 0)
115 throw new Exception("No images found for data source = '" + strSrc + "' on connection: " + ci.ToString("signalpop."));
116
117 rgRawData = rgImg[0].Data;
118 strSrc = GetSourceName(nSrcId1);
119 }
120
121 m_strPrimaryImgPath = getImagePathBase(strSrc, entities);
122 byte[] rgData = getRawImage(rgRawData, null, ci, entities, true);
123 if (rgData == null || rgData.Length == 0)
124 throw new Exception("The image at image ID = " + rgImg[0].ID.ToString() + " for data source = '" + strSrc + "' on connection: " + ci.ToString("signalpop.") + " is empty!");
125 }
126 catch (Exception excpt)
127 {
128 throw new Exception("Failed to get raw image data for data source = '" + strSrc + "' on connection: " + ci.ToString("signalpop.") + " with error = '" + excpt.Message + "'", excpt);
129 }
130 finally
131 {
132 m_strPrimaryImgPath = strPrimaryPath;
133 }
134
135 return true;
136 }
137 }
138
142 public int LastIndex
143 {
144 get { return m_nLastIndex; }
145 }
146
147 private string convertWs(string str, char chReplacement)
148 {
149 string strOut = "";
150
151 if (str == null)
152 return strOut;
153
154 foreach (char ch in str)
155 {
156 if (char.IsWhiteSpace(ch))
157 strOut += chReplacement;
158 else
159 strOut += ch;
160 }
161
162 return strOut;
163 }
164
168 public void SaveChanges()
169 {
170 if (m_entities != null)
171 m_entities.SaveChanges();
172 }
173
177 public Source CurrentSource
178 {
179 get { return m_src; }
180 }
181
188 public virtual void Open(int nSrcId, FORCE_LOAD nForceLoad = FORCE_LOAD.NONE, ConnectInfo ci = null)
189 {
190 m_src = GetSource(nSrcId, ci);
191 if (m_src == null)
192 throw new Exception("Could not find the source with ID = " + nSrcId.ToString());
193
194 m_entities = EntitiesConnection.CreateEntities(ci);
195 m_rgLabelCache = loadLabelCache(m_src.ID);
196
197 if (ci == null)
199
200 if (ci.Location == ConnectInfo.TYPE.AZURE)
201 nForceLoad = FORCE_LOAD.FROM_DB;
202
203 setImagePath(nForceLoad);
204 }
205
211 public void Open(string strSrc, bool bForceLoadImageFilePath = false)
212 {
213 m_src = GetSource(strSrc);
214 m_entities = EntitiesConnection.CreateEntities();
215 m_rgLabelCache = loadLabelCache(m_src.ID);
216
217 setImagePath((bForceLoadImageFilePath) ? FORCE_LOAD.FROM_FILE : FORCE_LOAD.NONE);
218 }
219
224 protected virtual void setImagePath(FORCE_LOAD nForceLoad)
225 {
226 if (nForceLoad == FORCE_LOAD.FROM_DB)
227 {
229 return;
230 }
231
233
234 if (m_src.SaveImagesToFile.GetValueOrDefault(false) || nForceLoad == FORCE_LOAD.FROM_FILE)
235 {
237
238 if (string.IsNullOrEmpty(EntitiesConnection.GlobalDatabaseConnectInfo.Password))
239 {
240 if (!Directory.Exists(m_strPrimaryImgPath))
241 Directory.CreateDirectory(m_strPrimaryImgPath);
242 }
243 }
244 }
245
250 protected virtual string getImagePath(string strSrcName = null)
251 {
252 return getImagePathBase(strSrcName);
253 }
254
255 string getImagePathBase(string strSrcName = null, DNNEntities entities = null)
256 {
257 if (strSrcName == null)
258 {
259 if (m_src == null)
260 return null;
261
262 strSrcName = m_src.Name;
263
264 if (m_src.CopyOfSourceID > 0)
265 strSrcName = GetSourceName(m_src.CopyOfSourceID.GetValueOrDefault());
266 }
267
268 if (entities == null)
269 entities = m_entities;
270
271 return GetDatabaseImagePath(entities.Database.Connection.Database) + strSrcName + "\\";
272 }
273
277 public virtual void Close()
278 {
279 m_src = null;
280
281 if (m_entities != null)
282 {
283 m_entities.Dispose();
284 m_entities = null;
285 }
286
287 m_strPrimaryImgPath = null;
289 }
290
294 public void Refresh()
295 {
296 int nSrcId = m_src.ID;
297 Close();
298 Open(nSrcId);
299 }
300
306 public string GetDatabaseFilePath(string strName)
307 {
309 return "";
310
311 if (m_rgstrDatabaseFilePath.ContainsKey(strName))
312 return m_rgstrDatabaseFilePath[strName];
313
315 {
316 string strCmd = "SELECT physical_name FROM sys.master_files WHERE name = '" + strName + "'";
317 DbRawSqlQuery<string> qry = entities.Database.SqlQuery<string>(strCmd);
318 List<string> rgStr = qry.ToList();
319
320 if (rgStr.Count == 0)
321 return null;
322
323 FileInfo fi = new FileInfo(rgStr[0]);
324 string strDir = fi.DirectoryName;
325
326 string strPath = strDir + "\\";
327
328 m_rgstrDatabaseFilePath.Add(strName, strPath);
329
330 return strPath;
331 }
332 }
333
339 public string GetDatabaseImagePath(string strName)
340 {
341 string strDir = GetDatabaseFilePath(strName);
342 string strPath = strDir + "Images\\" + strName + "\\";
343 return strPath;
344 }
345
346
347 //---------------------------------------------------------------------
348 // Labels
349 //---------------------------------------------------------------------
350 #region Labels
351
352 private List<Label> loadLabelCache(int nSrcId)
353 {
354 return m_entities.Labels.Where(p => p.SourceID == nSrcId).ToList();
355 }
356
363 public void UpdateLabelName(int nLabel, string strName, int nSrcId = 0)
364 {
365 if (nSrcId == 0)
366 nSrcId = m_src.ID;
367
369 {
370 List<Label> rg = entities.Labels.Where(p => p.SourceID == nSrcId && p.ActiveLabel == nLabel).ToList();
371 Label l;
372
373 if (rg.Count == 0)
374 {
375 l = new Label();
376 l.Label1 = nLabel;
377 l.ActiveLabel = nLabel;
378 l.ImageCount = 0;
379 l.SourceID = nSrcId;
380
381 entities.Labels.Add(l);
382 }
383 else
384 {
385 l = rg[0];
386 }
387
388 l.Name = strName;
389
390 entities.SaveChanges();
391 }
392 }
393
399 public Label GetLabel(int nID)
400 {
401 foreach (Label l in m_rgLabelCache)
402 {
403 if (l.ID == nID)
404 return l;
405 }
406
407 return null;
408 }
409
416 public string GetLabelName(int nLabel, int nSrcId = 0)
417 {
418 if (nSrcId == 0)
419 {
420 foreach (Label l in m_rgLabelCache)
421 {
422 if (l.Label1 == nLabel)
423 return l.Name;
424 }
425
426 return null;
427 }
428 else
429 {
431 {
432 List<Label> rg = entities.Labels.AsNoTracking().Where(p => p.SourceID == nSrcId && p.Label1 == nLabel).ToList();
433
434 if (rg.Count == 0)
435 return null;
436
437 return rg[0].Name;
438 }
439 }
440 }
441
447 public Label FindLabelInCache(int nLabel)
448 {
449 foreach (Label l in m_rgLabelCache)
450 {
451 if (l.ActiveLabel == nLabel)
452 return l;
453 }
454
455 return null;
456 }
457
463 public int GetLabelID(int nLabel)
464 {
465 foreach (Label l in m_rgLabelCache)
466 {
467 if (l.ActiveLabel == nLabel)
468 return l.ID;
469 }
470
471 return 0;
472 }
473
479 public int GetLabelCount(int nLabel)
480 {
481 foreach (Label l in m_rgLabelCache)
482 {
483 if (l.ActiveLabel == nLabel)
484 return l.ImageCount.GetValueOrDefault(0);
485 }
486
487 return 0;
488 }
489
494 public void AddLabelToCache(int nLabel)
495 {
496 Label l = FindLabelInCache(nLabel);
497
498 if (l == null)
499 {
500 l = new Label();
501 l.ImageCount = 1;
502 l.Name = "";
503 l.SourceID = m_src.ID;
504 l.Label1 = nLabel;
505 l.ActiveLabel = nLabel;
506 m_rgLabelCache.Add(l);
507 }
508 else
509 {
510 l.ImageCount++;
511 }
512 }
513
517 public void SaveLabelCache()
518 {
520 {
521 foreach (Label l in m_rgLabelCache)
522 {
523 List<Label> rgLabels = entities.Labels.Where(p => p.SourceID == m_src.ID && p.Label1 == l.Label1).ToList();
524
525 if (rgLabels.Count == 0)
526 {
527 if (!l.ActiveLabel.HasValue)
528 l.ActiveLabel = l.Label1;
529
530 entities.Labels.Add(l);
531 }
532 }
533
534 entities.SaveChanges();
535 }
536 }
537
543 public void UpdateLabelCounts(Dictionary<int, int> rgCounts, DNNEntities entities)
544 {
545 if (entities == null)
546 entities = m_entities;
547
548 foreach (Label l in m_rgLabelCache)
549 {
550 l.ImageCount = 0;
551 }
552
553 foreach (KeyValuePair<int, int> kv in rgCounts)
554 {
555 List<Label> rgLabel = m_rgLabelCache.Where(p => p.ActiveLabel == kv.Key).ToList();
556
557 if (rgLabel.Count > 0)
558 rgLabel[0].ImageCount = kv.Value;
559 }
560
561 entities.SaveChanges();
562 }
563
569 public void LoadLabelCounts(Dictionary<int, int> rgCounts, int nSrcId = 0)
570 {
571 if (nSrcId == 0)
572 nSrcId = m_src.ID;
573
575 {
576 List<Label> rgLabels = entities.Labels.AsNoTracking().Where(p => p.SourceID == nSrcId).ToList();
577
578 foreach (Label l in rgLabels)
579 {
580 int nCount = entities.RawImages.Where(p => p.SourceID == nSrcId && p.ActiveLabel == l.ActiveLabel && p.Active == true).Count();
581 rgCounts.Add(l.ActiveLabel.GetValueOrDefault(), nCount);
582 }
583 }
584 }
585
592 public string GetLabelCountsAsText(int nSrcId = 0, ConnectInfo ci = null)
593 {
594 if (nSrcId == 0)
595 nSrcId = m_src.ID;
596
597 if (ci == null)
599
600 if (ci.Location == ConnectInfo.TYPE.AZURE || !string.IsNullOrEmpty(ci.Password))
601 return "n\a";
602
603 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
604 {
605 List<Label> rgLabels = entities.Labels.AsNoTracking().Where(p => p.SourceID == nSrcId).OrderBy(p => p.Label1).ToList();
606
607 string strOut = "(" + rgLabels.Count.ToString() + ") {";
608
609 foreach (Label l in rgLabels)
610 {
611 strOut += l.ImageCount.GetValueOrDefault().ToString();
612 strOut += ",";
613 }
614
615 strOut = strOut.TrimEnd(',');
616 strOut += "}";
617
618 return strOut;
619 }
620 }
621
627 public void UpdateLabelCounts(Dictionary<int, int> rgCounts, int nSrcId = 0)
628 {
629 if (nSrcId == 0)
630 nSrcId = m_src.ID;
631
633 {
634 List<Label> rgLabels = entities.Labels.Where(p => p.SourceID == nSrcId).ToList();
635
636 foreach (Label l in rgLabels)
637 {
638 l.ImageCount = 0;
639 }
640
641 foreach (KeyValuePair<int, int> kv in rgCounts)
642 {
643 List<Label> rgLabel = rgLabels.Where(p => p.ActiveLabel == kv.Key).ToList();
644
645 if (rgLabel.Count > 0)
646 rgLabel[0].ImageCount = kv.Value;
647 }
648
649 entities.SaveChanges();
650 }
651 }
652
659 public void UpdateLabelCounts(int nSrcId = 0, int nProjectId = 0, ConnectInfo ci = null)
660 {
661 Dictionary<int, double> rgLabelBoosts = null;
662 double dfTotal = 0;
663
664 if (nSrcId == 0)
665 nSrcId = m_src.ID;
666
667 if (nProjectId > 0)
668 rgLabelBoosts = new Dictionary<int, double>();
669
670 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
671 {
672 List<Label> rgLabels = entities.Labels.Where(p => p.SourceID == nSrcId).ToList();
673
674 foreach (Label l in rgLabels)
675 {
676 int nCount = entities.RawImages.Where(p => p.SourceID == nSrcId && p.ActiveLabel == l.ActiveLabel && p.Active == true).Count();
677 l.ImageCount = nCount;
678
679 if (nProjectId > 0)
680 {
681 rgLabelBoosts.Add(l.ActiveLabel.GetValueOrDefault(0), nCount);
682 dfTotal += nCount;
683 }
684 }
685
686 entities.SaveChanges();
687 }
688
689 if (nProjectId > 0)
690 {
691 if (dfTotal == 0)
692 throw new Exception("There are no images for label boost!");
693
694 foreach (int nKey in rgLabelBoosts.Keys)
695 {
696 AddLabelBoost(nProjectId, nKey, rgLabelBoosts[nKey] / dfTotal, nSrcId);
697 }
698 }
699 }
700
701
710 public List<Label> GetLabels(bool bSort = true, bool bWithImagesOnly = false, int nSrcId = 0, ConnectInfo ci = null)
711 {
712 if (nSrcId == 0)
713 nSrcId = m_src.ID;
714
715 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
716 {
717 List<Label> rgLabels = entities.Labels.AsNoTracking().Where(p => p.SourceID == nSrcId).ToList();
718
719 if (bWithImagesOnly)
720 rgLabels = rgLabels.Where(p => p.ImageCount > 0).ToList();
721
722 if (bSort)
723 rgLabels = rgLabels.OrderBy(p => p.Label1).ToList();
724
725 return rgLabels;
726 }
727 }
728
733 public void DeleteLabels(int nSrcId = 0)
734 {
735 if (nSrcId == 0)
736 nSrcId = m_src.ID;
737
739 {
740 string strCmd = "DELETE FROM [DNN].[dbo].[Labels] WHERE (SourceID = " + nSrcId.ToString() + ")";
741 entities.Database.ExecuteSqlCommand(strCmd);
742 }
743 }
744
753 public int AddLabel(int nLabel, string strName = "", int nSrcId = 0, ConnectInfo ci = null)
754 {
755 if (nSrcId == 0)
756 nSrcId = m_src.ID;
757
758 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
759 {
760 List<Label> rgLabel = entities.Labels.Where(p => p.SourceID == nSrcId && p.Label1 == nLabel).ToList();
761 Label l;
762
763 if (rgLabel.Count > 0)
764 {
765 l = rgLabel[0];
766 }
767 else
768 {
769 l = new Label();
770 l.Label1 = nLabel;
771 l.SourceID = nSrcId;
772 }
773
774 l.ActiveLabel = nLabel;
775 l.ImageCount = 0;
776 l.Name = strName;
777
778 if (rgLabel.Count == 0)
779 entities.Labels.Add(l);
780
781 entities.SaveChanges();
782
783 return l.ID;
784 }
785 }
786
794 public void AddLabelBoost(int nProjectId, int nLabel, double dfBoost, int nSrcId = 0)
795 {
796 if (nSrcId == 0)
797 nSrcId = m_src.ID;
798
800 {
801 List<LabelBoost> rgLabelBoosts = entities.LabelBoosts.Where(p => p.ProjectID == nProjectId && p.SourceID == nSrcId && p.ActiveLabel == nLabel).ToList();
802 LabelBoost lb;
803
804 if (rgLabelBoosts.Count > 0)
805 {
806 lb = rgLabelBoosts[0];
807 }
808 else
809 {
810 lb = new LabelBoost();
811 lb.ActiveLabel = nLabel;
812 lb.ProjectID = nProjectId;
813 lb.SourceID = nSrcId;
814 }
815
816 lb.Boost = (decimal)dfBoost;
817
818 if (rgLabelBoosts.Count == 0)
819 entities.LabelBoosts.Add(lb);
820
821 entities.SaveChanges();
822 }
823 }
824
830 public void SetLabelMapping(LabelMapping map, int nSrcId = 0)
831 {
832 if (nSrcId == 0)
833 nSrcId = m_src.ID;
834
836 {
837 entities.Database.CommandTimeout = 180;
838 string strCmd = "UPDATE [DNN].[dbo].[RawImages] SET [ActiveLabel] = " + map.NewLabel.ToString() + " WHERE (SourceID = " + nSrcId.ToString() + ")";
839 strCmd += " AND (OriginalLabel = " + map.OriginalLabel.ToString() + ")";
840
841 if (map.ConditionBoostEquals.HasValue)
842 strCmd += " AND (ActiveBoost = " + map.ConditionBoostEquals.Value.ToString() + ")";
843
844 entities.Database.ExecuteSqlCommand(strCmd);
845
846 if (map.ConditionBoostEquals.HasValue)
847 {
848 if (map.NewLabelConditionFalse.HasValue)
849 {
850 strCmd = "UPDATE [DNN].[dbo].[RawImages] SET [ActiveLabel] = " + map.NewLabelConditionFalse.Value.ToString() + " WHERE (SourceID = " + nSrcId.ToString() + ")";
851
852 strCmd += " AND (OriginalLabel = " + map.OriginalLabel.ToString() + ")";
853 strCmd += " AND (ActiveBoost != " + map.ConditionBoostEquals.Value.ToString() + ")";
854 entities.Database.ExecuteSqlCommand(strCmd);
855 }
856 }
857 }
858 }
859
866 public void UpdateLabelMapping(int nNewLabel, List<int> rgOriginalLabels, int nSrcId = 0)
867 {
868 if (nSrcId == 0)
869 nSrcId = m_src.ID;
870
872 {
873 string strCmd = "UPDATE [DNN].[dbo].[RawImages] SET [ActiveLabel] = " + nNewLabel.ToString() + " WHERE (SourceID = " + nSrcId.ToString() + ") AND (";
874
875 for (int i = 0; i < rgOriginalLabels.Count; i++)
876 {
877 strCmd += "OriginalLabel = " + rgOriginalLabels[i].ToString();
878
879 if (i < rgOriginalLabels.Count - 1)
880 strCmd += " OR ";
881 }
882
883 strCmd += ")";
884
885 entities.Database.ExecuteSqlCommand(strCmd);
886 }
887 }
888
894 public void ResetLabels(int nProjectId = 0, int nSrcId = 0)
895 {
896 if (nSrcId == 0)
897 nSrcId = m_src.ID;
898
900 {
901 entities.Database.CommandTimeout = 180;
902
903 string strResetCmd = "UPDATE [DNN].[dbo].[RawImages] SET [ActiveLabel] = [OriginalLabel] WHERE (SourceID = " + nSrcId.ToString() + ")";
904 entities.Database.ExecuteSqlCommand(strResetCmd);
905
906 if (nProjectId > 0)
907 DeleteLabelBoosts(nProjectId, nSrcId);
908 }
909 }
910
916 public void DeleteLabelBoosts(int nProjectId, int nSrcId = 0)
917 {
918 if (nSrcId == 0)
919 nSrcId = m_src.ID;
920
922 {
923 string strCmd = "DELETE FROM [DNN].[dbo].[LabelBoosts] WHERE (ProjectID = " + nProjectId.ToString() + ") AND (SourceID = " + nSrcId.ToString() + ")";
924 entities.Database.ExecuteSqlCommand(strCmd);
925 }
926 }
927
932 public void DeleteLabelBoosts(int nProjectId)
933 {
935 {
936 string strCmd = "DELETE FROM [DNN].[dbo].[LabelBoosts] WHERE (ProjectID = " + nProjectId.ToString() + ")";
937 entities.Database.ExecuteSqlCommand(strCmd);
938 }
939 }
940
945 public void ResetLabelBoosts(int nProjectId)
946 {
948 {
949 List<LabelBoost> rgLabels = entities.LabelBoosts.Where(p => p.ProjectID == nProjectId).ToList();
950
951 foreach (LabelBoost l in rgLabels)
952 {
953 l.Boost = 1;
954 }
955
956 entities.SaveChanges();
957 }
958 }
959
967 public List<LabelBoost> GetLabelBoosts(int nProjectId, bool bSort = true, int nSrcId = 0)
968 {
969 if (nSrcId == 0)
970 nSrcId = m_src.ID;
971
973 {
974 List<LabelBoost> rgBoosts = entities.LabelBoosts.AsNoTracking().Where(p => p.ProjectID == nProjectId && p.SourceID == nSrcId).ToList();
975
976 if (bSort)
977 rgBoosts = rgBoosts.OrderBy(p => p.ActiveLabel).ToList();
978
979 return rgBoosts;
980 }
981 }
982
990 public string GetLabelBoostsAsText(int nProjectId, int nSrcId = 0, bool bSort = true)
991 {
992 if (nSrcId == 0)
993 nSrcId = m_src.ID;
994
995 List<LabelBoost> rgLb = GetLabelBoosts(nProjectId, bSort, nSrcId);
996 string strOut = "";
997
998 foreach (LabelBoost lb in rgLb)
999 {
1000 strOut += lb.Boost.GetValueOrDefault().ToString("N2");
1001 strOut += ",";
1002 }
1003
1004 return strOut.TrimEnd(',');
1005 }
1006
1013 public void ActivateLabels(List<int> rgLabels, bool bActive, params int[] rgSrcId)
1014 {
1015 string strSQL = "UPDATE [dbo].[RawImages] SET [Active] = " + ((bActive) ? "1" : "0") + " WHERE (";
1016
1017 for (int i = 0; i < rgSrcId.Length; i++)
1018 {
1019 strSQL += "(SourceID = " + rgSrcId[i].ToString() + ")";
1020
1021 if (i < rgSrcId.Length - 1)
1022 strSQL += " OR ";
1023 }
1024
1025 strSQL += ") AND (";
1026
1027 for (int i = 0; i < rgLabels.Count; i++)
1028 {
1029 strSQL += "(ActiveLabel = " + rgLabels[i].ToString() + ")";
1030
1031 if (i < rgLabels.Count - 1)
1032 strSQL += " OR ";
1033 }
1034
1035 strSQL += ")";
1036
1037 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1038 {
1039 entities.Database.ExecuteSqlCommand(strSQL);
1040 }
1041 }
1042
1053 public void UpdateLabelBoost(int? nTgtLbl, bool bTgtLblExact, int? nTgtBst, bool bTgtBstExact, int? nNewLbl, int? nNewBst, params int[] rgSrcId)
1054 {
1055 if (rgSrcId.Length == 0)
1056 throw new Exception("You must specify at least one source ID!");
1057
1058 string strSQL = "UPDATE [dbo].[RawImages] SET [Active] = 1";
1059
1060 if (nNewLbl.HasValue)
1061 strSQL += ", [ActiveLabel] = " + nNewLbl.Value.ToString();
1062
1063 if (nNewBst.HasValue)
1064 strSQL += ", [ActiveBoost] = " + nNewBst.Value.ToString();
1065
1066 strSQL += " WHERE (";
1067
1068 for (int i = 0; i < rgSrcId.Length; i++)
1069 {
1070 strSQL += "([SourceID] = " + rgSrcId[i].ToString() + ")";
1071
1072 if (i < rgSrcId.Length - 1)
1073 strSQL += " OR ";
1074 }
1075
1076 if (nTgtBst.HasValue || nTgtLbl.HasValue)
1077 strSQL += ") AND (";
1078
1079 if (nTgtLbl.HasValue)
1080 {
1081 strSQL += "(";
1082 strSQL += "[ActiveLabel] ";
1083 strSQL += (bTgtLblExact) ? "=" : ">=";
1084 strSQL += nTgtLbl.Value.ToString();
1085 strSQL += ")";
1086 }
1087
1088 if (nTgtBst.HasValue)
1089 {
1090 if (nTgtLbl.HasValue)
1091 strSQL += " OR ";
1092
1093 strSQL += "(";
1094 strSQL += "[ActiveBoost] ";
1095 strSQL += (bTgtBstExact) ? "=" : ">=";
1096 strSQL += nTgtBst.Value.ToString();
1097 strSQL += ")";
1098 }
1099
1100 strSQL += ")";
1101
1102 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1103 {
1104 entities.Database.ExecuteSqlCommand(strSQL);
1105 }
1106 }
1107
1108 #endregion
1109
1110
1111 //---------------------------------------------------------------------
1112 // Images
1113 //---------------------------------------------------------------------
1114 #region RawImages
1115
1120 public int GetImageCount()
1121 {
1122 return m_entities.RawImages.AsNoTracking().Where(p => p.SourceID == m_src.ID).Count();
1123 }
1124
1132 public List<DbItem> GetAllRawImageIndexes(bool bBoostedOnly, bool bIncludeActive = true, bool bIncludeInactive = false)
1133 {
1134 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1135 {
1136 int nSrcID = m_src.ID;
1137 IQueryable<RawImage> iQuery = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcID);
1138
1139 if (bIncludeActive && !bIncludeInactive)
1140 iQuery = iQuery.Where(p => p.Active == true);
1141 else if (!bIncludeActive && bIncludeInactive)
1142 iQuery = iQuery.Where(p => p.Active == false);
1143
1144 if (bBoostedOnly)
1145 iQuery = iQuery.Where(p => p.ActiveBoost > 0);
1146
1147 iQuery = iQuery.OrderBy(p => p.Idx);
1148
1149 return iQuery.Select(p => new DbItem { id = p.ID, virtualid = p.VirtualID, index = p.Idx, label = p.ActiveLabel, boost = p.ActiveBoost, time = p.TimeStamp, desc = p.Description, originalsrcid = p.OriginalSourceID, active = p.Active }).ToList();
1150 }
1151 }
1152
1158 public List<RawImage> QueryRawImages(params int[] rgSrcId)
1159 {
1160 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1161 {
1162 List<int?> rgSrcId1 = new List<int?>();
1163
1164 foreach (int id in rgSrcId)
1165 {
1166 rgSrcId1.Add(id);
1167 }
1168
1169 return entities.RawImages.AsNoTracking().Where(p => rgSrcId1.Contains(p.SourceID)).ToList();
1170 }
1171 }
1172
1179 public List<RawImageParameter> QueryRawImageParameters(int nSrcId, string strName)
1180 {
1181 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1182 {
1183 IQueryable<RawImageParameter> iQuery = entities.RawImageParameters.AsNoTracking().Where(p => p.SourceID == nSrcId && p.Name == strName);
1184 return iQuery.ToList();
1185 }
1186 }
1187
1193 public List<RawImageParameter> QueryRawImageParameters(int nImageID)
1194 {
1195 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1196 {
1197 IQueryable<RawImageParameter> iQuery = entities.RawImageParameters.AsNoTracking().Where(p => p.RawImageID == nImageID);
1198 return iQuery.ToList();
1199 }
1200 }
1201
1202
1211 public List<RawImage> QueryRawImages(int nSrcId, bool? bActive = null, int nBoostVal = 0, bool bExactBoostVal = false)
1212 {
1213 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1214 {
1215 IQueryable<RawImage> iQuery = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcId);
1216
1217 if (bActive.HasValue)
1218 {
1219 bool bVal = bActive.Value;
1220 iQuery = iQuery.Where(p => p.Active == bVal);
1221 }
1222
1223 if (nBoostVal > 0)
1224 {
1225 if (bExactBoostVal)
1226 iQuery = iQuery.Where(p => p.ActiveBoost == nBoostVal);
1227 else
1228 iQuery = iQuery.Where(p => p.ActiveBoost >= nBoostVal);
1229 }
1230
1231 return iQuery.ToList();
1232 }
1233 }
1234
1243 public List<RawImage> GetRawImagesAt(int nIdx, int nCount, int nSrcId = 0, string strDescription = null)
1244 {
1245 if (nSrcId == 0)
1246 nSrcId = m_src.ID;
1247
1248 string strCmd = "SELECT TOP " + nCount.ToString() + " * FROM [DNN].[dbo].[RawImages] WHERE (SourceID = " + nSrcId.ToString() + ") AND (Idx >= " + nIdx.ToString() + ") AND (Active = 1)";
1249
1250 if (!String.IsNullOrEmpty(strDescription))
1251 strCmd += " AND (Description = " + strDescription + ")";
1252
1253 strCmd += " ORDER BY Idx";
1254
1255 return m_entities.Database.SqlQuery<RawImage>(strCmd).ToList();
1256 }
1257
1265 public List<RawImage> GetRawImagesAt(List<int> rgImageIdx, int nSrcId = 0, string strDescription = null)
1266 {
1267 if (nSrcId == 0)
1268 nSrcId = m_src.ID;
1269
1270 if (rgImageIdx.Count > 100)
1271 throw new Exception("You can only query up to 100 images at a time when using the list of image indexes.");
1272
1273 string strCmd = "SELECT * FROM [DNN].[dbo].[RawImages] WHERE (SourceID = " + nSrcId.ToString() + ") AND (";
1274
1275 for (int i = 0; i < rgImageIdx.Count; i++)
1276 {
1277 strCmd += "Idx = " + rgImageIdx[i].ToString();
1278
1279 if (i < rgImageIdx.Count-1)
1280 strCmd += " OR ";
1281 }
1282
1283 strCmd += ") AND (Active = 1)";
1284
1285 if (!String.IsNullOrEmpty(strDescription))
1286 strCmd += " AND (Description = " + strDescription + ")";
1287
1288 return m_entities.Database.SqlQuery<RawImage>(strCmd).ToList();
1289 }
1290
1291
1299 public List<RawImage> GetRawImagesAtID(List<int> rgImageID, int nSrcId = 0, string strDescription = null)
1300 {
1301 if (nSrcId == 0)
1302 nSrcId = m_src.ID;
1303
1304 if (rgImageID.Count > 100)
1305 throw new Exception("You can only query up to 100 images at a time when using the list of image indexes.");
1306
1307 string strCmd = "SELECT * FROM [DNN].[dbo].[RawImages] WHERE (SourceID = " + nSrcId.ToString() + ") AND (";
1308
1309 for (int i = 0; i < rgImageID.Count; i++)
1310 {
1311 strCmd += "ID = " + rgImageID[i].ToString();
1312
1313 if (i < rgImageID.Count - 1)
1314 strCmd += " OR ";
1315 }
1316
1317 strCmd += ") AND (Active = 1)";
1318
1319 if (!String.IsNullOrEmpty(strDescription))
1320 strCmd += " AND (Description = " + strDescription + ")";
1321
1322 lock (m_objRemoteSync)
1323 {
1324 return m_entities.Database.SqlQuery<RawImage>(strCmd).ToList();
1325 }
1326 }
1327
1334 public RawImage GetRawImageAt(int nIdx, int nSrcId = 0)
1335 {
1336 if (nSrcId == 0)
1337 nSrcId = m_src.ID;
1338
1339 string strCmd = "SELECT TOP 1 * FROM [DNN].[dbo].[RawImages] WHERE (SourceID = " + nSrcId.ToString() + ") AND (Idx = " + nIdx.ToString() + ") AND (Active = 1)";
1340 List<RawImage> rgImg = null;
1341
1342 lock (m_objSync)
1343 {
1344 rgImg = m_entities.Database.SqlQuery<RawImage>(strCmd).ToList();
1345 }
1346
1347 if (rgImg == null || rgImg.Count == 0)
1348 return null;
1349
1350 return rgImg[0];
1351 }
1352
1359 public int GetRawImageID(DateTime dt, int nSrcId = 0)
1360 {
1361 if (nSrcId == 0)
1362 nSrcId = m_src.ID;
1363
1364 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1365 {
1366 List<RawImage> rgImg = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcId && p.TimeStamp == dt && p.Active == true).ToList();
1367
1368 if (rgImg.Count == 0)
1369 return 0;
1370
1371 return rgImg[0].ID;
1372 }
1373 }
1374
1389 public byte[] GetRawImageData(RawImage img, bool bLoadDataCriteria, bool bLoadDebugData, out byte[] rgDataCriteria, out int? nDataCriteriaFmtId, out byte[] rgDebugData, out int? nDebugDataFmtId)
1390 {
1391 if (img.VirtualID == 0)
1392 {
1393 rgDataCriteria = (bLoadDataCriteria) ? getRawImage(img.DataCriteria, img.OriginalSourceID) : img.DataCriteria;
1394 nDataCriteriaFmtId = img.DataCriteriaFormatID;
1395 rgDebugData = (bLoadDebugData) ? getRawImage(img.DebugData, img.OriginalSourceID) : img.DebugData;
1396 nDebugDataFmtId = img.DebugDataFormatID;
1397 return getRawImage(img.Data, img.OriginalSourceID);
1398 }
1399
1400 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1401 {
1402 List<RawImage> rgImg = entities.RawImages.AsNoTracking().Where(p => p.ID == img.VirtualID).ToList();
1403
1404 if (rgImg.Count == 0)
1405 {
1406 rgDataCriteria = null;
1407 nDataCriteriaFmtId = null;
1408 rgDebugData = null;
1409 nDebugDataFmtId = null;
1410 return null;
1411 }
1412
1413 rgDataCriteria = (bLoadDataCriteria) ? getRawImage(rgImg[0].DataCriteria, img.OriginalSourceID, null, entities) : rgImg[0].DataCriteria;
1414 nDataCriteriaFmtId = rgImg[0].DataCriteriaFormatID;
1415 rgDebugData = (bLoadDebugData) ? getRawImage(rgImg[0].DebugData, img.OriginalSourceID, null, entities) : rgImg[0].DebugData;
1416 nDebugDataFmtId = rgImg[0].DebugDataFormatID;
1417
1418 return getRawImage(rgImg[0].Data, img.OriginalSourceID, null, entities);
1419 }
1420 }
1421
1431 public byte[] GetRawImageDataCriteria(RawImage img, out int? nDataCriteriaFmtId)
1432 {
1433 if (img.VirtualID == 0)
1434 {
1435 nDataCriteriaFmtId = img.DataCriteriaFormatID;
1436 return getRawImage(img.DataCriteria, img.OriginalSourceID);
1437 }
1438
1439 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1440 {
1441 List<RawImage> rgImg = entities.RawImages.AsNoTracking().Where(p => p.ID == img.VirtualID).ToList();
1442
1443 if (rgImg.Count == 0)
1444 {
1445 nDataCriteriaFmtId = null;
1446 return null;
1447 }
1448
1449 nDataCriteriaFmtId = rgImg[0].DataCriteriaFormatID;
1450 return getRawImage(rgImg[0].DataCriteria, img.OriginalSourceID, null, entities);
1451 }
1452 }
1453
1463 public byte[] GetRawImageDebugData(RawImage img, out int? nDebugDataFormatId)
1464 {
1465 if (img.VirtualID == 0)
1466 {
1467 nDebugDataFormatId = img.DebugDataFormatID;
1468 return getRawImage(img.DebugData, img.OriginalSourceID);
1469 }
1470
1471 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1472 {
1473 List<RawImage> rgImg = entities.RawImages.AsNoTracking().Where(p => p.ID == img.VirtualID).ToList();
1474
1475 if (rgImg.Count == 0)
1476 {
1477 nDebugDataFormatId = null;
1478 return null;
1479 }
1480
1481 nDebugDataFormatId = rgImg[0].DebugDataFormatID;
1482 return getRawImage(rgImg[0].DebugData, img.OriginalSourceID, null, entities);
1483 }
1484 }
1485
1495 public byte[] GetRawImageDebugData(byte[] rgData, int? nOriginalSourceID = null)
1496 {
1497 return getRawImage(rgData, nOriginalSourceID);
1498 }
1499
1506 public byte[] GetRawImageDebugData(int nImgID, int? nOriginalSourceID = null)
1507 {
1508 byte[] rgData = null;
1509
1510 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1511 {
1512 var data = entities.RawImages.AsNoTracking().Where(p => p.ID == nImgID).Select(p => new { p.DebugData, p.VirtualID }).FirstOrDefault();
1513 if (data.DebugData == null)
1514 {
1515 nImgID = data.VirtualID.GetValueOrDefault(0);
1516 if (nImgID == 0)
1517 return null;
1518
1519 rgData = entities.RawImages.AsNoTracking().Where(p => p.ID == nImgID).Select(p => p.DebugData).FirstOrDefault();
1520 if (rgData == null)
1521 return null;
1522 }
1523 else
1524 {
1525 rgData = data.DebugData;
1526 }
1527 }
1528
1529 return getRawImage(rgData, nOriginalSourceID);
1530 }
1531
1538 public byte[] GetRawImageDataCriteria(byte[] rgData, int? nOriginalSourceID = null)
1539 {
1540 return getRawImage(rgData, nOriginalSourceID);
1541 }
1542
1552 public byte[] GetRawImageDataCriteria(int nImgID, int? nOriginalSourceID = null)
1553 {
1554 byte[] rgData = null;
1555
1556 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1557 {
1558 var data = entities.RawImages.AsNoTracking().Where(p => p.ID == nImgID).Select(p => new { p.DataCriteria, p.VirtualID }).FirstOrDefault();
1559 if (data.DataCriteria == null)
1560 {
1561 nImgID = data.VirtualID.GetValueOrDefault(0);
1562 if (nImgID == 0)
1563 return null;
1564
1565 rgData = entities.RawImages.AsNoTracking().Where(p => p.ID == nImgID).Select(p => p.DataCriteria).FirstOrDefault();
1566 if (rgData == null)
1567 return null;
1568 }
1569 else
1570 {
1571 rgData = data.DataCriteria;
1572 }
1573 }
1574
1575 return getRawImage(rgData, nOriginalSourceID);
1576 }
1577
1588 protected byte[] getRawImage(byte[] rgData, int? nSecondarySrcId = null, ConnectInfo ci = null, DNNEntities entities = null, bool bTestConnection = false)
1589 {
1590 if (rgData == null || rgData.Length < 5)
1591 return rgData;
1592
1593 string strFile = getImagePath(rgData);
1594 if (strFile == null)
1595 return rgData;
1596
1597 if (m_strPrimaryImgPath == null)
1598 throw new Exception("You must open the database on a datasource.");
1599
1600 // Get the file.
1601 if (nSecondarySrcId == null)
1602 {
1603 if (isRemote(ci))
1604 {
1605 string strFile1 = m_strPrimaryImgPath + strFile;
1606
1607 if (bTestConnection)
1608 {
1609 string strDbPath = GetDatabaseFilePath("DNN");
1610 int nIdx = strFile1.IndexOf(strDbPath);
1611 if (nIdx == 0)
1612 strFile1 = strFile1.Substring(strDbPath.Length);
1613 }
1614
1615 return getRemoteData(strFile1, entities);
1616 }
1617 else
1618 {
1619 return File.ReadAllBytes(m_strPrimaryImgPath + strFile);
1620 }
1621 }
1622
1623 string strPath = m_strPrimaryImgPath;
1624
1625 if (!File.Exists(strPath + strFile))
1626 {
1627 lock (m_objRawImgSync)
1628 {
1629 int nSecondarySrcId1 = nSecondarySrcId.Value;
1630
1631 if (m_rgSecondarySourcePath.ContainsKey(nSecondarySrcId1))
1632 {
1633 m_strSecondaryImgPath = m_rgSecondarySourcePath[nSecondarySrcId1];
1634 }
1635 else
1636 {
1638 m_rgSecondarySourcePath.Add(nSecondarySrcId1, m_strSecondaryImgPath);
1639 }
1640 }
1641
1642 strPath = m_strSecondaryImgPath;
1643 }
1644
1645 try
1646 {
1647 if (isRemote(ci))
1648 return getRemoteData(strPath + strFile, entities);
1649 else
1650 return File.ReadAllBytes(strPath + strFile);
1651 }
1652 catch (Exception excpt)
1653 {
1654 throw excpt;
1655 }
1656 }
1657
1658 private bool isRemote(ConnectInfo ci)
1659 {
1660 if (ci == null)
1662
1663 if (string.IsNullOrEmpty(ci.Password))
1664 return false;
1665 else
1666 return true;
1667 }
1668
1669 private byte[] getRemoteData(string strInfo, DNNEntities entities = null)
1670 {
1671 if (entities == null)
1672 entities = m_entities;
1673
1674 string strCmd = "EXEC [dbo].[GetRawData] @strInfo = N'" + strInfo + "'";
1675 DbRawSqlQuery<byte[]> qry = entities.Database.SqlQuery<byte[]>(strCmd);
1676
1677 lock (m_objRemoteSync)
1678 {
1679 return qry.Single();
1680 }
1681 }
1682
1688 protected string getImagePath(byte[] rgData)
1689 {
1690 if (rgData == null)
1691 return null;
1692
1693 if (rgData.Length < 5)
1694 return null;
1695
1696 if (rgData[0] != 'F' ||
1697 rgData[1] != 'I' ||
1698 rgData[2] != 'L' ||
1699 rgData[3] != 'E' ||
1700 rgData[4] != ':')
1701 return null;
1702
1703 return Encoding.ASCII.GetString(rgData, 5, rgData.Length - 5);
1704 }
1705
1713 public bool ChangeRawImageSourceID(int nID, int nNewSrcID, bool bSave = true)
1714 {
1715 List<RawImage> rg = m_entities.RawImages.Where(p => p.ID == nID).ToList();
1716
1717 if (rg.Count == 0)
1718 return false;
1719
1720 if (rg[0].VirtualID == 0)
1721 return false;
1722
1723 rg[0].SourceID = nNewSrcID;
1724
1725 if (bSave)
1726 m_entities.SaveChanges();
1727
1728 return true;
1729 }
1730
1739 public bool UpdateActiveLabel(int nID, int nLabel, bool bActivate = true, bool bSaveChanges = true)
1740 {
1741 List<RawImage> rg = m_entities.RawImages.Where(p => p.ID == nID).ToList();
1742
1743 if (rg.Count == 0)
1744 return false;
1745
1746 rg[0].ActiveLabel = nLabel;
1747 rg[0].Active = bActivate;
1748
1749 if (bSaveChanges)
1750 m_entities.SaveChanges();
1751
1752 return true;
1753 }
1754
1760 public void UpdateActiveLabelByID(int nID, int nLabel)
1761 {
1762 string strCmd = "UPDATE [dbo].[RawImages] SET [ActiveLabel] = " + nLabel.ToString() + ",[Active] = 1";
1763 strCmd += " WHERE ([ID] = " + nID.ToString() + ")";
1764
1765 m_entities.Database.ExecuteSqlCommand(strCmd);
1766 }
1767
1774 public void UpdateActiveLabelByIndex(int nSrcId, int nIdx, int nLabel)
1775 {
1776 string strCmd = "UPDATE [dbo].[RawImages] SET [ActiveLabel] = " + nLabel.ToString() + ",[Active] = 1";
1777 strCmd += " WHERE ([SourceID] = " + nSrcId.ToString() + ") AND ([Idx] = " + nIdx.ToString() + ")";
1778
1779 m_entities.Database.ExecuteSqlCommand(strCmd);
1780 }
1781
1788 public void UpdateAllActiveLabels(int nSrcId, int nLabel, int? nOriginalLabel)
1789 {
1790 string strCmd = "UPDATE [dbo].[RawImages] SET [ActiveLabel] = " + nLabel.ToString() + ",[Active] = 1";
1791 strCmd += " WHERE ([SourceID] = " + nSrcId.ToString() + ")";
1792
1793 if (nOriginalLabel.HasValue)
1794 strCmd += " AND ([OriginalLabel] = " + nOriginalLabel.Value.ToString() + ")";
1795
1796 m_entities.Database.ExecuteSqlCommand(strCmd);
1797 }
1798
1803 public void ResetAllActiveLabels(int nSrcId)
1804 {
1805 string strCmd = "UPDATE [dbo].[RawImages] SET [ActiveLabel] = [OriginalLabel], [Active] = 1";
1806 strCmd += " WHERE ([SourceID] = " + nSrcId.ToString() + ")";
1807 m_entities.Database.ExecuteSqlCommand(strCmd);
1808 }
1809
1816 public void DisableLabel(int nSrcId, int nLabel, bool bOriginalLabel = false)
1817 {
1818 string strCmd = "UPDATE [dbo].[RawImages] SET [Active] = 0";
1819 strCmd += " WHERE ([SourceID] = " + nSrcId.ToString() + ")";
1820
1821 if (bOriginalLabel)
1822 strCmd += " AND ([OriginalLabel] = " + nLabel.ToString() + ")";
1823 else
1824 strCmd += " AND ([ActiveLabel] = " + nLabel.ToString() + ")";
1825
1826 m_entities.Database.ExecuteSqlCommand(strCmd);
1827 }
1828
1833 public void DisableAllLabels(int nSrcId)
1834 {
1835 string strCmd = "UPDATE [dbo].[RawImages] SET [Active] = 0";
1836 strCmd += " WHERE ([SourceID] = " + nSrcId.ToString() + ")";
1837 m_entities.Database.ExecuteSqlCommand(strCmd);
1838 }
1839
1847 public int DisableAllNonMatchingImages(int nSrcId, int nWidth, int nHeight)
1848 {
1849 string strCmd = "SELECT COUNT([ID]) FROM[DNN].[dbo].[RawImages] WHERE(SourceID = " + nSrcId.ToString() + ") AND (Width != " + nWidth.ToString() + " OR Height != " + nHeight.ToString() + ")";
1850 DbRawSqlQuery<int> qry = m_entities.Database.SqlQuery<int>(strCmd);
1851 List<int> rgVal = qry.ToList();
1852 int nCount = rgVal[0];
1853
1854 strCmd = "UPDATE [dbo].[RawImages] SET [Active] = 0 WHERE(SourceID = " + nSrcId.ToString() + ") AND (Width != " + nWidth.ToString() + " OR Height != " + nHeight.ToString() + ")";
1855 m_entities.Database.ExecuteSqlCommand(strCmd);
1856
1857 return nCount;
1858 }
1859
1866 public void ActivateRawImageByIndex(int nSrcId, int nIdx, bool bActive)
1867 {
1868 string strCmd = "UPDATE [dbo].[RawImages] SET [Active] = " + ((bActive) ? "1" : "0");
1869 strCmd += " WHERE ([SourceID] = " + nSrcId.ToString() + ") AND ([Idx] = " + nIdx.ToString() + ")";
1870
1871 m_entities.Database.ExecuteSqlCommand(strCmd);
1872 }
1873
1874
1880 public void UpdateActiveLabelDirect(int nID, int nLabel)
1881 {
1882 string strSql = "UPDATE [dbo].[RawImages] SET [ActiveLabel] = " + nLabel.ToString() + ",[Active] = 1 WHERE (ID = " + nID.ToString() + ")";
1883
1884 if (m_entities != null)
1885 {
1886 m_entities.Database.ExecuteSqlCommand(strSql);
1887 return;
1888 }
1889
1890 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1891 {
1892 entities.Database.ExecuteSqlCommand(strSql);
1893 }
1894 }
1895
1902 public bool UpdateRawImageDescription(int nID, string strDescription)
1903 {
1904 List<RawImage> rg = m_entities.RawImages.Where(p => p.ID == nID).ToList();
1905
1906 if (rg.Count == 0)
1907 return false;
1908
1909 rg[0].Description = strDescription;
1910 m_entities.SaveChanges();
1911
1912 return true;
1913 }
1914
1921 public bool UpdateRawImageDescriptionFromParameter(int nID, string strParamName)
1922 {
1923 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1924 {
1925 List<RawImage> rg = entities.RawImages.Where(p => p.ID == nID).ToList();
1926
1927 if (rg.Count == 0)
1928 return false;
1929
1930 List<RawImageParameter> rgP = entities.RawImageParameters.Where(p => p.RawImageID == nID && p.Name == strParamName).ToList();
1931
1932 if (rgP.Count == 0)
1933 return false;
1934
1935 if (rgP[0].TextValue == null || rgP[0].TextValue.Length == 0)
1936 return false;
1937
1938 if (rg[0].Description == rgP[0].TextValue)
1939 return false;
1940
1941 rg[0].Description = rgP[0].TextValue;
1942 entities.SaveChanges();
1943
1944 return true;
1945 }
1946 }
1947
1959 public List<int> QueryAllRawImageIDs(int nSrcId = 0, int nMax = int.MaxValue, int nLabel = -1, int nBoost = -1, bool bBoostIsExact = false, bool bAnnotatedOnly = false, bool bActiveOnly = true)
1960 {
1961 if (nSrcId == 0)
1962 nSrcId = m_src.ID;
1963
1964 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1965 {
1966 string strTop = (nMax == int.MaxValue) ? "" : "TOP " + nMax.ToString();
1967 string strCmd = "SELECT " + strTop + " ID FROM RawImages WHERE (SourceID = " + nSrcId.ToString() + ")";
1968
1969 if (nLabel != -1)
1970 strCmd += " AND (ActiveLabel = " + nLabel.ToString() + ")";
1971
1972 if (nBoost != -1)
1973 {
1974 string strCompare = (bBoostIsExact) ? "=" : ">=";
1975 strCmd += " AND (ActiveBoost " + strCompare + " " + nBoost.ToString() + ")";
1976 }
1977
1978 if (bAnnotatedOnly)
1979 strCmd += " AND (DataCriteriaFormatID = " + ((int)SimpleDatum.DATA_FORMAT.ANNOTATION_DATA).ToString() + ")";
1980
1981 if (bActiveOnly)
1982 strCmd += " AND (Active = 1)";
1983
1984 return entities.Database.SqlQuery<int>(strCmd).ToList();
1985 }
1986 }
1987
1998 public RawImage CreateRawImage(int nIdx, SimpleDatum d, int nBackgroundWritingThreadCount, string strDescription = null, int? nOriginalSourceID = null, bool bActive = true)
1999 {
2000 if (string.IsNullOrEmpty(strDescription))
2001 strDescription = d.Description;
2002
2003 DateTime dtMin = new DateTime(1980, 1, 1);
2004 RawImage img = new RawImage();
2005 bool bEncoded = false;
2006 img.Channels = d.Channels;
2007 img.Height = d.Height;
2008 img.Width = d.Width;
2009 img.SourceID = m_src.ID;
2010 img.TimeStamp = (d.TimeStamp < dtMin) ? dtMin : d.TimeStamp;
2011 img.Idx = nIdx;
2012 img.OriginalBoost = (short)d.Boost;
2013 img.ActiveBoost = (short)d.Boost;
2014 img.GroupID = d.GroupID;
2015 img.ActiveLabel = d.Label;
2016 img.OriginalLabel = d.Label;
2017 img.SourceID = m_src.ID;
2018 img.Active = bActive;
2019 img.AutoLabel = d.AutoLabeled;
2020 img.Description = strDescription;
2021
2022 if (nOriginalSourceID.HasValue)
2023 img.OriginalSourceID = nOriginalSourceID.Value;
2024
2025 string strGuid = Guid.NewGuid().ToString();
2026
2027 if (d.VirtualID > 0)
2028 {
2029 img.VirtualID = d.VirtualID;
2030 img.Encoded = d.IsRealData;
2031 }
2032 else
2033 {
2034 img.VirtualID = 0;
2035 img.Data = setImageByteData(d.GetByteData(out bEncoded), null, strGuid, nBackgroundWritingThreadCount);
2036 img.Encoded = bEncoded;
2037 }
2038
2039 if (d.DebugData != null)
2040 {
2041 img.DebugData = setImageByteData(d.DebugData, "dbg", strGuid);
2042 img.DebugDataFormatID = (byte)d.DebugDataFormat;
2043 }
2044
2047
2048 if (d.DataCriteria != null)
2049 {
2050 img.DataCriteria = setImageByteData(d.DataCriteria, "criteria", strGuid);
2051 img.DataCriteriaFormatID = (byte)d.DataCriteriaFormat;
2052 }
2053
2054 return img;
2055 }
2056
2062 public bool WaitForFileWriter(int nWait = int.MaxValue)
2063 {
2064 if (m_fileWriter == null)
2065 return true;
2066
2067 return m_fileWriter.WaitUntilCompletion(nWait);
2068 }
2069
2082 protected byte[] setImageByteData(byte[] rgImg, string strType = null, string strGuid = null, int nBackgroundWritingThreadCount = 0)
2083 {
2084 if (rgImg == null)
2085 return null;
2086
2087 if (!m_bEnableFileBasedData || rgImg.Length < 100)
2088 return rgImg;
2089
2090 if (strGuid == null)
2091 strGuid = Guid.NewGuid().ToString();
2092
2093 if (!Directory.Exists(m_strPrimaryImgPath))
2094 Directory.CreateDirectory(m_strPrimaryImgPath);
2095
2096 string strTypeExt = (strType == null) ? "" : "." + strType;
2097 string strFile = strGuid + strTypeExt + ".bin";
2098
2099 if (nBackgroundWritingThreadCount > 0)
2100 {
2101 if (m_fileWriter == null)
2102 m_fileWriter = new FileWriter(nBackgroundWritingThreadCount);
2103 m_fileWriter.Add(m_strPrimaryImgPath + strFile, rgImg);
2104 }
2105 else
2106 {
2107 File.WriteAllBytes(m_strPrimaryImgPath + strFile, rgImg);
2108 }
2109
2110 string strTag = "FILE:" + strFile;
2111 return Encoding.ASCII.GetBytes(strTag);
2112 }
2113
2114 private void fileWriterThread()
2115 {
2116 }
2117
2126 public bool ConvertRawImagesSaveToFile(int nIdx, int nCount, CancelEvent evtCancel = null)
2127 {
2128 if (m_strPrimaryImgPath == null)
2130
2131 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2132 {
2133 List<RawImage> rgImg = entities.RawImages.Where(p => p.SourceID == m_src.ID && p.Idx >= nIdx && p.Active == true).OrderBy(p => p.Idx).Take(nCount).ToList();
2134 string strPath;
2135 string strImgPath;
2136 string strTag;
2137 string strName;
2138 byte[] rgData;
2139 List<int?> rgId = new List<int?>();
2140 Dictionary<int, string> rgNames = new Dictionary<int, string>();
2141
2142 for (int i=0; i<rgImg.Count; i++)
2143 {
2144 if (evtCancel != null && evtCancel.WaitOne(0))
2145 return false;
2146
2147 strName = Guid.NewGuid().ToString();
2148 rgNames.Add(rgImg[i].ID, strName);
2149
2150 rgId.Add(rgImg[i].ID);
2151 rgData = rgImg[i].Data;
2152 if (rgData != null)
2153 {
2154 strPath = getImagePath(rgData);
2155 if (strPath == null)
2156 {
2157 strImgPath = strName + ".bin";
2158 File.WriteAllBytes(m_strPrimaryImgPath + strImgPath, rgData);
2159 strTag = "FILE:" + strImgPath;
2160 rgImg[i].Data = Encoding.ASCII.GetBytes(strTag);
2161 }
2162 }
2163
2164 rgData = rgImg[i].DebugData;
2165 if (rgData != null)
2166 {
2167 strPath = getImagePath(rgData);
2168 if (strPath == null)
2169 {
2170 strImgPath = strName + ".dbg.bin";
2171 File.WriteAllBytes(m_strPrimaryImgPath + strImgPath, rgData);
2172 strTag = "FILE:" + strImgPath;
2173 rgImg[i].DebugData = Encoding.ASCII.GetBytes(strTag);
2174 }
2175 }
2176
2177 rgData = rgImg[i].DataCriteria;
2178 if (rgData != null)
2179 {
2180 strPath = getImagePath(rgData);
2181 if (strPath == null)
2182 {
2183 strImgPath = strName + ".criteria.bin";
2184 File.WriteAllBytes(m_strPrimaryImgPath + strImgPath, rgData);
2185 strTag = "FILE:" + strImgPath;
2186 rgImg[i].DataCriteria = Encoding.ASCII.GetBytes(strTag);
2187 }
2188 }
2189 }
2190
2191 entities.SaveChanges();
2192
2193 List<RawImageParameter> rgParam = entities.RawImageParameters.Where(p => rgId.Contains(p.RawImageID)).ToList();
2194 for (int i = 0; i < rgParam.Count; i++)
2195 {
2196 rgData = rgParam[i].Value;
2197 if (rgData != null)
2198 {
2199 strPath = getImagePath(rgData);
2200 if (strPath == null)
2201 {
2202 int nRawImgId = rgParam[i].RawImageID.GetValueOrDefault(0);
2203 string strName1 = rgNames[nRawImgId];
2204 strImgPath = strName1 + ".param_" + rgParam[i].Name + ".bin";
2205 File.WriteAllBytes(m_strPrimaryImgPath + strImgPath, rgData);
2206 strTag = "FILE:" + strImgPath;
2207 rgParam[i].Value = Encoding.ASCII.GetBytes(strTag);
2208 }
2209 }
2210 }
2211
2212 entities.SaveChanges();
2213 }
2214
2215 return true;
2216 }
2217
2226 public bool ConvertRawImagesSaveToDatabase(int nIdx, int nCount, CancelEvent evtCancel = null)
2227 {
2228 if (m_strPrimaryImgPath == null)
2230
2231 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2232 {
2233 List<RawImage> rgImg = entities.RawImages.Where(p => p.SourceID == m_src.ID && p.Idx >= nIdx && p.Active == true).OrderBy(p => p.Idx).Take(nCount).ToList();
2234 List<string> rgstrFiles = new List<string>();
2235 string strPath;
2236 byte[] rgData;
2237 List<int?> rgId = new List<int?>();
2238
2239 for (int i = 0; i < rgImg.Count; i++)
2240 {
2241 if (evtCancel != null && evtCancel.WaitOne(0))
2242 return false;
2243
2244 rgData = rgImg[i].Data;
2245 if (rgData != null)
2246 {
2247 strPath = getImagePath(rgData);
2248 if (strPath != null)
2249 {
2250 rgImg[i].Data = File.ReadAllBytes(m_strPrimaryImgPath + strPath);
2251 rgstrFiles.Add(m_strPrimaryImgPath + strPath);
2252 }
2253 }
2254
2255 rgData = rgImg[i].DebugData;
2256 if (rgData != null)
2257 {
2258 strPath = getImagePath(rgData);
2259 if (strPath != null)
2260 {
2261 rgImg[i].DebugData = File.ReadAllBytes(m_strPrimaryImgPath + strPath);
2262 rgstrFiles.Add(m_strPrimaryImgPath + strPath);
2263 }
2264 }
2265
2266 rgData = rgImg[i].DataCriteria;
2267 if (rgData != null)
2268 {
2269 strPath = getImagePath(rgData);
2270 if (strPath != null)
2271 {
2272 rgImg[i].DataCriteria = File.ReadAllBytes(m_strPrimaryImgPath + strPath);
2273 rgstrFiles.Add(m_strPrimaryImgPath + strPath);
2274 }
2275 }
2276 }
2277
2278 entities.SaveChanges();
2279
2280 List<RawImageParameter> rgParam = entities.RawImageParameters.Where(p => rgId.Contains(p.RawImageID)).ToList();
2281 for (int i = 0; i < rgParam.Count; i++)
2282 {
2283 rgData = rgParam[i].Value;
2284 if (rgData != null)
2285 {
2286 strPath = getImagePath(rgData);
2287 if (strPath != null)
2288 {
2289 rgParam[i].Value = File.ReadAllBytes(m_strPrimaryImgPath + strPath);
2290 rgstrFiles.Add(m_strPrimaryImgPath + strPath);
2291 }
2292 }
2293 }
2294
2295 entities.SaveChanges();
2296
2297 foreach (string strFile in rgstrFiles)
2298 {
2299 File.Delete(strFile);
2300 }
2301
2302 if (Directory.Exists(m_strPrimaryImgPath))
2303 {
2304 if (Directory.GetFiles(m_strPrimaryImgPath).Length == 0)
2305 Directory.Delete(m_strPrimaryImgPath);
2306 }
2307 }
2308
2309 return true;
2310 }
2311
2324 public void FixupRawImageCopy(int nImageID, int nSecondarySrcId)
2325 {
2326 if (m_strPrimaryImgPath == null)
2328
2329 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2330 {
2331 List<RawImage> rgImg = entities.RawImages.Where(p => p.ID == nImageID).ToList();
2332 if (rgImg.Count > 0)
2333 {
2334 string strPath;
2335 int nVirtId = rgImg[0].VirtualID.GetValueOrDefault(0);
2336
2337 if (nVirtId > 0)
2338 {
2339 List<RawImage> rgImg2 = entities.RawImages.Where(p => p.ID == nVirtId).ToList();
2340 if (rgImg2.Count > 0)
2341 {
2342 strPath = getImagePath(rgImg2[0].Data);
2343 if (!File.Exists(m_strPrimaryImgPath + strPath))
2344 {
2345 rgImg[0].OriginalSourceID = nSecondarySrcId;
2346 entities.SaveChanges();
2347 }
2348 }
2349 }
2350 else if (rgImg[0].Data != null)
2351 {
2352 strPath = getImagePath(rgImg[0].Data);
2353 if (!File.Exists(m_strPrimaryImgPath + strPath))
2354 {
2355 rgImg[0].OriginalSourceID = nSecondarySrcId;
2356 entities.SaveChanges();
2357 }
2358 }
2359 }
2360 }
2361 }
2362
2368 public void PutRawImageParameters(List<ParameterData> rgParam, ConnectInfo ci = null)
2369 {
2370 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
2371 {
2372 entities.Configuration.AutoDetectChangesEnabled = false;
2373 entities.Configuration.ValidateOnSaveEnabled = false;
2374
2375 foreach (ParameterData param in rgParam)
2376 {
2377 IQueryable<RawImageParameter> iquery = entities.RawImageParameters.Where(p => p.SourceID == param.SourceID && p.RawImageID == param.ImageID && p.Name == param.Name);
2378
2379 int nCount = iquery.Count();
2380 if (nCount == 0)
2381 {
2382 RawImageParameter rip = new RawImageParameter();
2383 rip.RawImageID = param.ImageID;
2384 rip.Name = param.Name;
2385 rip.SourceID = param.SourceID;
2386 rip.TextValue = param.Value;
2387 rip.NumericValue2 = (float)param.NumericValue;
2388 rip.Value = param.Data;
2389 entities.RawImageParameters.Add(rip);
2390 }
2391 else
2392 {
2393 if (!param.OnlyAddNew)
2394 {
2395 List<RawImageParameter> rg = iquery.ToList();
2396 rg[0].TextValue = param.Value;
2397 rg[0].NumericValue2 = (float)param.NumericValue;
2398 rg[0].Value = param.Data;
2399 }
2400 }
2401 }
2402
2403 entities.SaveChanges();
2404 }
2405 }
2406
2413 public void PutRawImages(List<RawImage> rgImg, List<List<ParameterData>> rgrgParam = null, ConnectInfo ci = null)
2414 {
2415 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
2416 {
2417 entities.Configuration.AutoDetectChangesEnabled = false;
2418 entities.Configuration.ValidateOnSaveEnabled = false;
2419
2420 foreach (RawImage img in rgImg)
2421 {
2422 m_nLastIndex = Math.Max(m_nLastIndex, img.Idx.GetValueOrDefault(0));
2423 entities.RawImages.Add(img);
2424 AddLabelToCache(img.ActiveLabel.GetValueOrDefault());
2425 }
2426
2427 entities.SaveChanges();
2428
2429 if (rgrgParam != null && rgrgParam.Count == rgImg.Count)
2430 {
2431 for (int i = 0; i < rgImg.Count; i++)
2432 {
2433 for (int j = 0; j < rgrgParam[i].Count; j++)
2434 {
2435 ParameterData p = rgrgParam[i][j];
2436 string strName = p.Name;
2437 string strVal = p.Value;
2438 double? dfVal = p.NumericValue;
2439 byte[] rgData = p.Data;
2440
2441 if (p.ImageID != 0)
2442 {
2443 RawImageParameter imgParam = GetRawImageParameterEx(p.ImageID, strName);
2444
2445 if (imgParam != null)
2446 {
2447 strVal = imgParam.TextValue;
2448 rgData = imgParam.Value;
2449 }
2450 }
2451
2452 if (!String.IsNullOrEmpty(strVal) || dfVal.HasValue || rgData != null)
2453 SetRawImageParameter(rgImg[i].ID, strName, strVal, dfVal, rgData, false, false, entities);
2454 }
2455 }
2456
2457 entities.SaveChanges();
2458 }
2459 }
2460 }
2461
2469 public int PutRawImage(int nIdx, SimpleDatum d, string strDescription = null)
2470 {
2471 RawImage img = CreateRawImage(nIdx, d, 0, strDescription);
2472 m_entities.RawImages.Add(img);
2473 m_entities.SaveChanges();
2474
2475 if (d is Datum)
2476 {
2477 string str = ((Datum)d).Tag as string;
2478 string strName = ((Datum)d).TagName;
2479
2480 if (str != null && str.Length > 0 && strName != null && strName.Length > 0)
2481 SetRawImageParameter(m_src.ID, img.ID, strName, str);
2482 }
2483
2485
2486 return img.ID;
2487 }
2488
2494 public RawImage GetRawImage(int nID)
2495 {
2496 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2497 {
2498 List<RawImage> rgItems = entities.RawImages.AsNoTracking().Where(p => p.ID == nID).ToList();
2499
2500 if (rgItems.Count == 0)
2501 return null;
2502
2503 return rgItems[0];
2504 }
2505 }
2506
2512 public int QueryRawImageCount(int nSrcId = 0)
2513 {
2514 if (nSrcId == 0)
2515 nSrcId = m_src.ID;
2516
2517 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2518 {
2519 return entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcId).Count();
2520 }
2521 }
2522
2527 public void DeleteRawImages(int nSrcId = 0)
2528 {
2529 if (nSrcId == 0)
2530 nSrcId = m_src.ID;
2531
2532 string strCmd = "DELETE FROM RawImages WHERE (SourceID = " + nSrcId.ToString() + ")";
2533
2534 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2535 {
2536 entities.Database.ExecuteSqlCommand(strCmd);
2537
2538 string strImgPath = getImagePath();
2539 if (strImgPath != null)
2540 deleteImages(strImgPath);
2541 }
2542 }
2543
2544 private bool deleteImages(string strPath)
2545 {
2546 if (!Directory.Exists(strPath))
2547 return true;
2548
2549 string[] rgstrFiles = Directory.GetFiles(strPath);
2550 foreach (string strFile in rgstrFiles)
2551 {
2552 File.Delete(strFile);
2553 }
2554
2555 Directory.Delete(strPath);
2556
2557 return true;
2558 }
2559
2564 public void DeleteRawImageResults(int nSrcId = 0)
2565 {
2566 if (nSrcId == 0)
2567 nSrcId = m_src.ID;
2568
2569 string strCmd = "DELETE FROM RawImageResults WHERE (SourceID = " + nSrcId.ToString() + ")";
2570
2571 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2572 {
2573 entities.Database.ExecuteSqlCommand(strCmd);
2574 }
2575 }
2576
2583 public RawImageMean GetRawImageMean(int nSrcId = 0, ConnectInfo ci = null)
2584 {
2585 if (nSrcId == 0)
2586 nSrcId = m_src.ID;
2587
2588 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
2589 {
2590 List<RawImageMean> rgImg = entities.RawImageMeans.AsNoTracking().Where(p => p.SourceID == nSrcId).ToList();
2591
2592 if (rgImg.Count == 0)
2593 return null;
2594
2595 return rgImg[0];
2596 }
2597 }
2598
2607 public int PutRawImageMean(SimpleDatum sd, bool bUpdate, int nSrcId = 0, ConnectInfo ci = null)
2608 {
2609 if (sd == null)
2610 return 0;
2611
2612 if (nSrcId == 0)
2613 nSrcId = m_src.ID;
2614
2615 if (isRemote(null))
2616 return 0;
2617
2618 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
2619 {
2620 IQueryable<RawImageMean> iQuery = entities.RawImageMeans.Where(p => p.SourceID == nSrcId);
2621 if (iQuery != null)
2622 {
2623 List<RawImageMean> rgMean = iQuery.ToList();
2624 RawImageMean im = null;
2625
2626 if (rgMean.Count == 0)
2627 im = new RawImageMean();
2628 else
2629 im = rgMean[0];
2630
2631 if (bUpdate || rgMean.Count == 0)
2632 {
2633 bool bEncoded = false;
2634 im.Channels = sd.Channels;
2635 im.Height = sd.Height;
2636 im.Width = sd.Width;
2637 im.SourceID = nSrcId;
2638 im.Encoded = sd.IsRealData;
2639 im.Data = sd.GetByteData(out bEncoded);
2640 }
2641
2642 if (rgMean.Count == 0)
2643 entities.RawImageMeans.Add(im);
2644
2645 if (rgMean.Count == 0 || bUpdate)
2646 entities.SaveChanges();
2647
2648 return im.ID;
2649 }
2650
2651 return 0;
2652 }
2653 }
2654
2662 public bool CopyImageMean(int nSrcIdSrc, int nSrcIdDst, ConnectInfo ci = null)
2663 {
2664 RawImageMean src = GetRawImageMean(nSrcIdSrc);
2665 if (src == null)
2666 return false;
2667
2668 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
2669 {
2670 List<RawImageMean> rgDst = entities.RawImageMeans.Where(p => p.SourceID == nSrcIdDst).ToList();
2671 RawImageMean dst;
2672
2673 if (rgDst.Count == 0)
2674 {
2675 dst = new RawImageMean();
2676 dst.SourceID = nSrcIdDst;
2677 }
2678 else
2679 {
2680 dst = rgDst[0];
2681 }
2682
2683 dst.Encoded = src.Encoded;
2684 dst.Channels = src.Channels;
2685 dst.Height = src.Height;
2686 dst.Width = src.Width;
2687 dst.Data = src.Data;
2688
2689 if (rgDst.Count == 0)
2690 entities.RawImageMeans.Add(dst);
2691
2692 entities.SaveChanges();
2693 }
2694
2695 return true;
2696 }
2697
2702 public void DeleteRawImageMeans(int nSrcId = 0)
2703 {
2704 if (nSrcId == 0)
2705 nSrcId = m_src.ID;
2706
2707 string strCmd = "DELETE FROM RawImageMeans WHERE (SourceID = " + nSrcId.ToString() + ")";
2708
2709 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2710 {
2711 entities.Database.ExecuteSqlCommand(strCmd);
2712 }
2713 }
2714
2715 private IQueryable<RawImage> getQuery(IQueryable<RawImage> iQuery, string strFilterVal, int? nBoostVal)
2716 {
2717 if (!string.IsNullOrEmpty(strFilterVal))
2718 iQuery = iQuery.Where(p => p.Description == strFilterVal);
2719
2720 if (nBoostVal.HasValue)
2721 {
2722 if (nBoostVal.Value < 0)
2723 {
2724 int nVal = Math.Abs(nBoostVal.Value);
2725 iQuery = iQuery.Where(p => p.ActiveBoost == nVal);
2726 }
2727 else
2728 {
2729 int nVal = nBoostVal.Value;
2730 iQuery = iQuery.Where(p => p.ActiveBoost >= nVal);
2731 }
2732 }
2733 else
2734 {
2735 iQuery = iQuery.Where(p => p.ActiveBoost > 0);
2736 }
2737
2738 return iQuery;
2739 }
2740
2748 public int GetBoostCount(int nSrcId = 0, string strFilterVal = null, int? nBoostVal = null)
2749 {
2750 if (nSrcId == 0)
2751 nSrcId = m_src.ID;
2752
2753 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2754 {
2755 IQueryable<RawImage> iQuery = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcId && p.Active == true);
2756 iQuery = getQuery(iQuery, strFilterVal, nBoostVal);
2757 return iQuery.Count();
2758 }
2759 }
2760
2768 public int ActivateFiltered(int nSrcId = 0, string strFilterVal = null, int? nBoostVal = null)
2769 {
2770 if (nSrcId == 0)
2771 nSrcId = m_src.ID;
2772
2773 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2774 {
2775 if (!string.IsNullOrEmpty(strFilterVal) || nBoostVal.HasValue)
2776 {
2777 string strCmd1 = "UPDATE[dbo].[RawImages] SET[Active] = 0 WHERE SourceID = " + nSrcId.ToString();
2778 entities.Database.ExecuteSqlCommand(strCmd1);
2779
2780 string strCmd2 = "UPDATE[dbo].[RawImages] SET[Active] = 1 WHERE SourceID = " + nSrcId.ToString();
2781
2782 if (strFilterVal != null)
2783 strCmd2 += " AND Description = '" + strFilterVal + "'";
2784
2785 if (nBoostVal.HasValue)
2786 {
2787 int nVal = nBoostVal.Value;
2788
2789 if (nVal < 0)
2790 {
2791 nVal = Math.Abs(nVal);
2792 strCmd2 += " AND ActiveBoost = " + nVal.ToString();
2793 }
2794 else
2795 {
2796 strCmd2 += " AND ActiveBoost >= " + nVal.ToString();
2797 }
2798 }
2799
2800 entities.Database.ExecuteSqlCommand(strCmd2);
2801 }
2802 else
2803 {
2804 string strCmd2 = "UPDATE[dbo].[RawImages] SET[Active] = 1 WHERE SourceID = " + nSrcId.ToString();
2805 entities.Database.ExecuteSqlCommand(strCmd2);
2806 }
2807
2808 return entities.RawImages.Where(p => p.Active == true).Count();
2809 }
2810 }
2811
2820 public int GetImageCount(int nSrcId = 0, bool bActive = true, bool bInactive = true, ConnectInfo ci = null)
2821 {
2822 if (nSrcId == 0)
2823 nSrcId = m_src.ID;
2824
2825 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
2826 {
2827 IQueryable<RawImage> iQuery = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcId);
2828
2829 if (bActive && bInactive)
2830 return iQuery.Count();
2831
2832 if (bActive)
2833 iQuery = iQuery.Where(p => p.Active == true);
2834 else
2835 iQuery = iQuery.Where(p => p.Active == false);
2836
2837 return iQuery.Count();
2838 }
2839 }
2840
2848 public void ResetAllBoosts(int nSrcId = 0, int nMinBoost = 0, bool bExactVal = false, int nDesiredBoostVal = -1)
2849 {
2850 if (nSrcId == 0)
2851 nSrcId = m_src.ID;
2852
2853 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2854 {
2855 string strCmd = "UPDATE RawImages SET ActiveBoost = ";
2856
2857 if (nDesiredBoostVal >= 0)
2858 strCmd += nDesiredBoostVal.ToString();
2859 else
2860 strCmd += "OriginalBoost";
2861
2862 strCmd += " WHERE(SourceID = " + nSrcId.ToString();
2863
2864 if (nMinBoost >= 1)
2865 {
2866 if (bExactVal)
2867 strCmd += " AND ActiveBoost = " + nMinBoost.ToString();
2868 else
2869 strCmd += " AND ActiveBoost >= " + nMinBoost.ToString();
2870 }
2871
2872 strCmd += ")";
2873
2874 entities.Database.ExecuteSqlCommand(strCmd);
2875 }
2876 }
2877
2883 public void UpdateBoost(long nImageID, int nBoost)
2884 {
2885 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2886 {
2887 string strCmd = "UPDATE RawImages SET ActiveBoost = " + nBoost.ToString() + " WHERE (ID = " + nImageID.ToString() + ")";
2888 entities.Database.ExecuteSqlCommand(strCmd);
2889 }
2890 }
2891
2899 public void UpdateBoosts(int nSrcId, DateTime dtStart, DateTime dtEnd, int nBoost)
2900 {
2901 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2902 {
2903 string strCmd = "UPDATE RawImages SET ActiveBoost = " + nBoost.ToString() + " WHERE (SourceID = " + nSrcId.ToString() + " AND TimeStamp >= '" + dtStart.ToString() + "' AND TimeStamp <= '" + dtEnd.ToString() + "')";
2904 entities.Database.ExecuteSqlCommand(strCmd);
2905 }
2906 }
2907
2915 public List<RawImage> ReindexRawImages(Log log, CancelEvent evtCancel, int nSrcId = 0)
2916 {
2917 if (nSrcId == 0 && m_src != null)
2918 nSrcId = m_src.ID;
2919
2920 if (nSrcId == 0)
2921 return null;
2922
2923 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2924 {
2925 log.WriteLine("Resetting inactive indexes...");
2926 string strCmd = "UPDATE [dbo].[RawImages] SET [Idx] = -1 WHERE SourceID = " + nSrcId.ToString() + " AND Active = 0";
2927 entities.Database.ExecuteSqlCommand(strCmd);
2928
2929 log.WriteLine("Querying active images...");
2930
2931 Stopwatch sw = new Stopwatch();
2932 List<RawImage> rgImg = entities.RawImages.Where(p => p.SourceID == nSrcId && p.Active == true).OrderBy(p => p.TimeStamp).ThenBy(p => p.ID).ToList();
2933
2934 sw.Start();
2935 int nIdx = 0;
2936
2937 for (int i = 0; i < rgImg.Count; i++)
2938 {
2939 rgImg[i].Idx = nIdx;
2940 nIdx++;
2941
2942 if (i % 1000 == 0)
2943 entities.SaveChanges();
2944
2945 if (sw.Elapsed.TotalMilliseconds > 1000)
2946 {
2947 log.Progress = ((double)i / (double)rgImg.Count);
2948 log.WriteLine("reindexing at " + log.Progress.ToString("P"));
2949 sw.Restart();
2950 }
2951
2952 if (evtCancel.WaitOne(0))
2953 return null;
2954 }
2955
2956 entities.SaveChanges();
2957
2958 return rgImg;
2959 }
2960 }
2961
2968 public bool UpdateRawImageSourceID(int nImageID, int nSrcID)
2969 {
2970 using (DNNEntities entities = EntitiesConnection.CreateEntities())
2971 {
2972 List<RawImage> rgImg = entities.RawImages.Where(p => p.ID == nImageID).ToList();
2973 if (rgImg.Count > 0)
2974 {
2975 if (rgImg[0].SourceID != nSrcID)
2976 {
2977 if (rgImg[0].OriginalSourceID == null)
2978 rgImg[0].OriginalSourceID = rgImg[0].SourceID;
2979
2980 rgImg[0].SourceID = nSrcID;
2981
2982 entities.SaveChanges();
2983 return true;
2984 }
2985 }
2986 }
2987
2988 return false;
2989 }
2990
2998 public bool ActivateRawImage(int nImageID, bool bActivate, bool bSave = true)
2999 {
3000 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3001 {
3002 List<RawImage> rgImg = entities.RawImages.Where(p => p.ID == nImageID).ToList();
3003 if (rgImg.Count == 0)
3004 return false;
3005
3006 if (rgImg[0].Active == bActivate)
3007 return false;
3008
3009 rgImg[0].Active = bActivate;
3010
3011 if (bSave)
3012 entities.SaveChanges();
3013 }
3014
3015 return true;
3016 }
3017
3024 public void ActivateAllRawImages(bool bActive, bool bAnnotatedOnly, params int[] rgSrcId)
3025 {
3026 if (rgSrcId.Length == 0)
3027 throw new Exception("You must specify at least one source iD.");
3028
3029 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3030 {
3031 string strActive = (bActive) ? "1" : "0";
3032 string strCmd = "UPDATE RawImages SET [Active] = " + strActive + " WHERE (";
3033
3034 for (int i=0; i<rgSrcId.Length; i++)
3035 {
3036 strCmd += "SourceID = " + rgSrcId[i].ToString();
3037
3038 if (i < rgSrcId.Length - 1)
3039 strCmd += " OR ";
3040 }
3041
3042 strCmd += ")";
3043
3044 if (bAnnotatedOnly)
3045 {
3046 strCmd += " AND ([DataCriteriaFormatID] = 9)";
3047 strCmd += " AND (Convert(varbinary(8), [DataCriteria]) != 0x00000000)";
3048 }
3049
3050 entities.Database.ExecuteSqlCommand(strCmd);
3051 }
3052 }
3053
3064 public void ActivateAllRawImages(bool bActive, bool bAnnotatedOnly, int? nTgtLabel, bool bTargetLabelExact, int? nTgtBoost, bool bTargetBoostExact, params int[] rgSrcId)
3065 {
3066 if (rgSrcId.Length == 0)
3067 throw new Exception("You must specify at least one source iD.");
3068
3069 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3070 {
3071 string strActive = (bActive) ? "1" : "0";
3072 string strCmd = "UPDATE RawImages SET [Active] = " + strActive + " WHERE (";
3073
3074 for (int i = 0; i < rgSrcId.Length; i++)
3075 {
3076 strCmd += "SourceID = " + rgSrcId[i].ToString();
3077
3078 if (i < rgSrcId.Length - 1)
3079 strCmd += " OR ";
3080 }
3081
3082 strCmd += ")";
3083
3084 if (bAnnotatedOnly)
3085 {
3086 strCmd += " AND ([DataCriteriaFormatID] = 9)";
3087 strCmd += " AND (Convert(varbinary(8), [DataCriteria]) != 0x00000000)";
3088 }
3089
3090 if (nTgtLabel.HasValue)
3091 {
3092 strCmd += " AND ([ActiveLabel] ";
3093
3094 if (bTargetLabelExact)
3095 strCmd += "=";
3096 else
3097 strCmd += ">=";
3098
3099 strCmd += nTgtLabel.Value.ToString() + ")";
3100 }
3101
3102 if (nTgtBoost.HasValue)
3103 {
3104 strCmd += " AND ([ActiveBoost] ";
3105
3106 if (bTargetBoostExact)
3107 strCmd += "=";
3108 else
3109 strCmd += ">=";
3110
3111 strCmd += nTgtBoost.Value.ToString() + ")";
3112 }
3113
3114 entities.Database.ExecuteSqlCommand(strCmd);
3115 }
3116 }
3117
3125 public void UpdateDatasetImageAnnotations(int nSrcId, int nImageId, AnnotationGroupCollection annotations, bool bSetLabelOnly)
3126 {
3128 {
3129 List<RawImage> rgImg = entities.RawImages.Where(p => p.ID == nImageId).ToList();
3130 if (rgImg.Count > 0)
3131 {
3132 if (bSetLabelOnly)
3133 {
3134 if (annotations.Count > 0)
3135 rgImg[0].ActiveLabel = annotations[0].group_label;
3136 else
3137 rgImg[0].ActiveLabel = 0;
3138 }
3139 else
3140 {
3141 if (annotations.Count > 0)
3142 {
3143 rgImg[0].DataCriteria = SimpleDatum.SaveAnnotationDataToDataCriteriaByteArray(SimpleDatum.ANNOTATION_TYPE.BBOX, annotations);
3144 rgImg[0].DataCriteriaFormatID = (int)SimpleDatum.DATA_FORMAT.ANNOTATION_DATA;
3145 }
3146 else
3147 {
3148 rgImg[0].DataCriteria = null;
3149 rgImg[0].DataCriteriaFormatID = (int)SimpleDatum.DATA_FORMAT.NONE;
3150 }
3151 }
3152
3153 entities.SaveChanges();
3154 }
3155
3156 foreach (KeyValuePair<int, string> kv in annotations.Labels)
3157 {
3158 List<Label> rgLabel = entities.Labels.Where(p => p.SourceID == nSrcId && p.Label1 == kv.Key).ToList();
3159
3160 if (rgLabel.Count == 0)
3161 {
3162 Label label = new Label();
3163 label.Label1 = kv.Key;
3164 label.ActiveLabel = kv.Key;
3165 label.ImageCount = 0;
3166 label.SourceID = nSrcId;
3167 label.Name = kv.Value;
3168 entities.Labels.Add(label);
3169 }
3170 else
3171 {
3172 rgLabel[0].Name = kv.Value;
3173 }
3174
3175 entities.SaveChanges();
3176 }
3177 }
3178 }
3179
3180 #endregion
3181
3182
3183 //---------------------------------------------------------------------
3184 // RawImage Results
3185 //---------------------------------------------------------------------
3186 #region RawImage Results
3187
3199 public int PutRawImageResults(int nSrcId, int nIdx, int nLabel, DateTime dt, List<Result> rgResults, bool bInvert, List<Tuple<DateTime, int>> rgExtra = null)
3200 {
3201 if (rgResults.Count == 0)
3202 throw new Exception("You must have at least one result!");
3203
3204 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3205 {
3206 List<RawImageResult> rg = entities.RawImageResults.Where(p => p.SourceID == nSrcId && p.Idx == nIdx).ToList();
3207 RawImageResult r;
3208
3209 if (rg.Count == 0)
3210 {
3211 r = new RawImageResult();
3212 r.Idx = nIdx;
3213 r.SourceID = nSrcId;
3214 }
3215 else
3216 {
3217 r = rg[0];
3218 }
3219
3220 r.Label = nLabel;
3221 r.ResultCount = rgResults.Count;
3222 r.Results = ResultDescriptor.CreateResults(rgResults, bInvert);
3223 r.TimeStamp = dt;
3224 r.BatchCount = 0;
3225
3226 if (rgExtra != null && rgExtra.Count > 0)
3227 r.ExtraData = PackExtraData(rgExtra);
3228
3229 if (rg.Count == 0)
3230 entities.RawImageResults.Add(r);
3231
3232 entities.SaveChanges();
3233
3234 return r.ID;
3235 }
3236 }
3237
3248 public int PutRawImageResults(int nSrcId, int nIdx, int nLabel, DateTime dt, List<Tuple<SimpleDatum, List<Result>>> rgrgResults, List<Tuple<DateTime, int>> rgExtra = null)
3249 {
3250 if (rgrgResults.Count == 0 || rgrgResults[0].Item2.Count == 0)
3251 throw new Exception("You must have at least one result!");
3252
3253 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3254 {
3255 List<RawImageResult> rg = entities.RawImageResults.Where(p => p.SourceID == nSrcId && p.Idx == nIdx).ToList();
3256 RawImageResult r;
3257
3258 if (rg.Count == 0)
3259 {
3260 r = new RawImageResult();
3261 r.Idx = nIdx;
3262 r.SourceID = nSrcId;
3263 }
3264 else
3265 {
3266 r = rg[0];
3267 }
3268
3269 r.Label = nLabel;
3270 r.ResultCount = rgrgResults[0].Item2.Count;
3271 r.Results = ResultDescriptor.CreateResults(rgrgResults);
3272 r.TimeStamp = dt;
3273 r.BatchCount = rgrgResults.Count;
3274
3275 if (rgExtra != null && rgExtra.Count > 0)
3276 r.ExtraData = PackExtraData(rgExtra);
3277
3278 if (rg.Count == 0)
3279 entities.RawImageResults.Add(r);
3280
3281 entities.SaveChanges();
3282
3283 return r.ID;
3284 }
3285 }
3286
3293 public List<Tuple<SimpleDatum, List<Result>>> GetRawImageResultBatch(int nBatchCount, byte[] rgResults)
3294 {
3295 if (nBatchCount <= 0)
3296 throw new Exception("The batch count must be 1 or greater!");
3297
3298 return ResultDescriptor.GetResults(nBatchCount, rgResults);
3299 }
3300
3307 public int PutRawImageResultExtraData(int nRawImageResultID, byte[] rgExtraData)
3308 {
3309 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3310 {
3311 List<RawImageResult> rg = entities.RawImageResults.Where(p => p.ID == nRawImageResultID).ToList();
3312 if (rg.Count == 0)
3313 return 0;
3314
3315 rg[0].ExtraData = rgExtraData;
3316 entities.SaveChanges();
3317
3318 return rg[0].ID;
3319 }
3320 }
3321
3329 public List<RawImageResult> GetRawImageResults(int nSrcId = 0, bool bRequireExtraData = false, int nMax = -1)
3330 {
3331 if (nSrcId == 0)
3332 nSrcId = m_src.ID;
3333
3335 {
3336 IQueryable<RawImageResult> iQry = entities.RawImageResults.AsNoTracking().Where(p => p.SourceID == nSrcId).OrderBy(p => p.TimeStamp);
3337
3338 if (bRequireExtraData)
3339 iQry = iQry.Where(p => p.ExtraData != null);
3340
3341 if (nMax > 0)
3342 iQry = iQry.Take(nMax);
3343
3344 return iQry.ToList();
3345 }
3346 }
3347
3353 public static byte[] PackExtraData(List<Tuple<DateTime, int>> rg)
3354 {
3355 using (MemoryStream ms = new MemoryStream())
3356 using (BinaryWriter bw = new BinaryWriter(ms))
3357 {
3358 bw.Write(rg.Count);
3359
3360 for (int i = 0; i < rg.Count; i++)
3361 {
3362 bw.Write(rg[i].Item1.ToFileTimeUtc());
3363 bw.Write(rg[i].Item2);
3364 }
3365
3366 ms.Flush();
3367 return ms.ToArray();
3368 }
3369 }
3370
3376 public static List<Tuple<DateTime, int>> UnpackExtraData(byte[] rg)
3377 {
3378 List<Tuple<DateTime, int>> rgExtra = new List<Tuple<DateTime, int>>();
3379
3380 using (MemoryStream ms = new MemoryStream(rg))
3381 using (BinaryReader br = new BinaryReader(ms))
3382 {
3383 int nCount = br.ReadInt32();
3384
3385 for (int i = 0; i < nCount; i++)
3386 {
3387 long lTime = br.ReadInt64();
3388 int nLabel = br.ReadInt32();
3389 DateTime dt = DateTime.FromFileTimeUtc(lTime);
3390
3391 rgExtra.Add(new Tuple<DateTime, int>(dt, nLabel));
3392 }
3393 }
3394
3395 return rgExtra;
3396 }
3397
3398 #endregion
3399
3400
3401 //---------------------------------------------------------------------
3402 // RawImage Parameters
3403 //---------------------------------------------------------------------
3404 #region RawImage Parameters
3405
3413 public string GetRawImageParameter(int nRawImageID, string strName, string strDefault)
3414 {
3415 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3416 {
3417 List<RawImageParameter> rgP = entities.RawImageParameters.AsNoTracking().Where(p => p.RawImageID == nRawImageID && p.Name == strName).ToList();
3418
3419 if (rgP.Count == 0)
3420 return strDefault;
3421
3422 return rgP[0].TextValue;
3423 }
3424 }
3425
3433 public int GetRawImageParameter(int nRawImageID, string strName, int nDefault)
3434 {
3435 string str = GetRawImageParameter(nRawImageID, strName, null);
3436
3437 if (str == null)
3438 return nDefault;
3439
3440 return int.Parse(str);
3441 }
3442
3450 public double GetRawImageParameter(int nRawImageID, string strName, double dfDefault)
3451 {
3452 string str = GetRawImageParameter(nRawImageID, strName, null);
3453
3454 if (str == null)
3455 return dfDefault;
3456
3457 return BaseParameter.ParseDouble(str);
3458 }
3459
3467 public bool GetRawImageParameter(int nRawImageID, string strName, bool bDefault)
3468 {
3469 string str = GetRawImageParameter(nRawImageID, strName, null);
3470
3471 if (str == null)
3472 return bDefault;
3473
3474 return bool.Parse(str);
3475 }
3476
3483 public byte[] GetRawImageParameterData(int nRawImageID, string strName)
3484 {
3485 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3486 {
3487 List<RawImageParameter> rgP = entities.RawImageParameters.AsNoTracking().Where(p => p.RawImageID == nRawImageID && p.Name == strName).ToList();
3488
3489 if (rgP.Count == 0)
3490 return null;
3491
3492 return getRawImage(rgP[0].Value, null, null, entities);
3493 }
3494 }
3495
3502 public RawImageParameter GetRawImageParameterEx(int nRawImageID, string strName)
3503 {
3504 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3505 {
3506 List<RawImageParameter> rgP = entities.RawImageParameters.AsNoTracking().Where(p => p.RawImageID == nRawImageID && p.Name == strName).ToList();
3507
3508 if (rgP.Count == 0)
3509 return null;
3510
3511 return rgP[0];
3512 }
3513 }
3514
3527 public int SetRawImageParameter(int nRawImageID, string strName, string strValue, double? dfVal = null, byte[] rgData = null, bool bSave = true, bool bOnlyAddNew = false, DNNEntities entities = null)
3528 {
3529 if (entities == null)
3530 entities = m_entities;
3531
3532 List<RawImageParameter> rgP = entities.RawImageParameters.Where(p => p.RawImageID == nRawImageID && p.Name == strName).ToList();
3533 RawImageParameter riP = null;
3534
3535 if (rgP.Count == 0)
3536 {
3537 riP = new RawImageParameter();
3538 riP.RawImageID = nRawImageID;
3539 riP.SourceID = m_src.ID;
3540 riP.Name = strName;
3541 }
3542 else
3543 {
3544 if (bOnlyAddNew)
3545 return rgP[0].ID;
3546
3547 riP = rgP[0];
3548 }
3549
3550 float fVal = 0;
3551 if (dfVal < -float.MaxValue)
3552 fVal = -float.MaxValue;
3553 else if (dfVal > float.MaxValue)
3554 fVal = float.MaxValue;
3555 else
3556 fVal = (float)dfVal;
3557
3558 riP.TextValue = strValue;
3559 riP.NumericValue2 = fVal;
3560 riP.NumericValue = null;
3561 riP.Value = setImageByteData(rgData, "param_" + strName);
3562
3563 if (rgP.Count == 0)
3564 entities.RawImageParameters.Add(riP);
3565
3566 if (bSave)
3567 entities.SaveChanges();
3568
3569 return riP.ID;
3570 }
3571
3582 public int SetRawImageParameter(int nSrcId, int nRawImageID, string strName, string strValue, double? dfVal = null, byte[] rgData = null)
3583 {
3584 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3585 {
3586 List<RawImageParameter> rgP = entities.RawImageParameters.Where(p => p.RawImageID == nRawImageID && p.Name == strName).ToList();
3587 RawImageParameter riP = null;
3588
3589 if (rgP.Count == 0)
3590 {
3591 riP = new RawImageParameter();
3592 riP.RawImageID = nRawImageID;
3593 riP.SourceID = nSrcId;
3594 riP.Name = strName;
3595 }
3596 else
3597 {
3598 riP = rgP[0];
3599 }
3600
3601 riP.TextValue = strValue;
3602 riP.NumericValue2 = (float)dfVal;
3603 riP.Value = setImageByteData(rgData, "param_" + strName);
3604
3605 if (rgP.Count == 0)
3606 entities.RawImageParameters.Add(riP);
3607
3608 entities.SaveChanges();
3609
3610 return riP.ID;
3611 }
3612 }
3613
3623 public int SetRawImageParameterAt(DateTime dt, string strName, string strValue, double? dfVal, byte[] rgData)
3624 {
3625 List<RawImage> rg = m_entities.RawImages.Where(p => p.SourceID == m_src.ID && p.TimeStamp == dt).ToList();
3626
3627 if (rg.Count == 0)
3628 return 0;
3629
3630 return SetRawImageParameter(m_src.ID, rg[0].ID, strName, strValue, dfVal, rgData);
3631 }
3632
3637 public void DeleteRawImageParameters(int nSrcId)
3638 {
3639 string strCmd = "DELETE FROM RawImageParameters WHERE (SourceID = " + nSrcId.ToString() + ")";
3640
3641 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3642 {
3643 entities.Database.ExecuteSqlCommand(strCmd);
3644 }
3645 }
3646
3654 public int GetRawImageParameterCount(string strName, int nSrcId = 0, string strType = "TEXT")
3655 {
3656 if (nSrcId == 0)
3657 nSrcId = m_src.ID;
3658
3659 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3660 {
3661 if (strType.ToLower() == "text")
3662 return entities.RawImageParameters.AsNoTracking().Where(p => p.SourceID == nSrcId && p.Name == strName && p.TextValue != null).Count();
3663
3664 if (strType.ToLower() == "numeric")
3665 return entities.RawImageParameters.AsNoTracking().Where(p => p.SourceID == nSrcId && p.Name == strName && p.NumericValue != null).Count();
3666
3667 if (strType.ToLower() == "value")
3668 return entities.RawImageParameters.AsNoTracking().Where(p => p.SourceID == nSrcId && p.Name == strName && p.Value != null).Count();
3669 }
3670
3671 return 0;
3672 }
3673
3681 public bool GetRawImageParameterExist(string strName, int nSrcId = 0, string strType = "TEXT")
3682 {
3683 if (nSrcId == 0)
3684 nSrcId = m_src.ID;
3685
3686 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3687 {
3688 if (strType.ToLower() == "text")
3689 {
3690 int nCount = entities.RawImageParameters.AsNoTracking().Where(p => p.SourceID == nSrcId && p.Name == strName && p.TextValue != null).Take(1).Count();
3691 return (nCount > 0) ? true : false;
3692 }
3693
3694 if (strType.ToLower() == "numeric")
3695 {
3696 int nCount = entities.RawImageParameters.AsNoTracking().Where(p => p.SourceID == nSrcId && p.Name == strName && p.NumericValue != null).Take(1).Count();
3697 return (nCount > 0) ? true : false;
3698 }
3699
3700 if (strType.ToLower() == "value")
3701 {
3702 int nCount = entities.RawImageParameters.AsNoTracking().Where(p => p.SourceID == nSrcId && p.Name == strName && p.Value != null).Take(1).Count();
3703 return (nCount > 0) ? true : false;
3704 }
3705 }
3706
3707 return false;
3708 }
3709
3715 public List<string> GetRawImageDistinctParameterDescriptions(int nSrcId = 0)
3716 {
3717 if (nSrcId == 0)
3718 nSrcId = m_src.ID;
3719
3720 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3721 {
3722 string strCmd = "SELECT DISTINCT [TextValue] FROM RawImageParameters WHERE SourceID = " + nSrcId.ToString();
3723 DbRawSqlQuery<string> res = entities.Database.SqlQuery<string>(strCmd);
3724 List<string> rgstr = res.ToList<string>();
3725
3726 List<string> rgstr1 = new List<string>();
3727
3728 foreach (string str in rgstr)
3729 {
3730 if (str.Trim().Length > 0)
3731 rgstr1.Add(str);
3732 }
3733
3734 return rgstr1;
3735 }
3736 }
3737
3738 #endregion
3739
3740
3741 //---------------------------------------------------------------------
3742 // RawImage Groups
3743 //---------------------------------------------------------------------
3744 #region RawImage Groups
3745
3755 public int AddRawImageGroup(Image img, int nIdx, DateTime dtStart, DateTime dtEnd, List<double> rgProperties)
3756 {
3757 RawImageGroup g = new RawImageGroup();
3758
3759 g.RawData = ImageTools.ImageToByteArray(img);
3760 g.Idx = nIdx;
3761 g.StartDate = dtStart;
3762 g.EndDate = dtEnd;
3763
3764 if (rgProperties != null)
3765 {
3766 if (rgProperties.Count > 0)
3767 g.Property1 = (decimal)rgProperties[0];
3768
3769 if (rgProperties.Count > 1)
3770 g.Property2 = (decimal)rgProperties[1];
3771
3772 if (rgProperties.Count > 2)
3773 g.Property3 = (decimal)rgProperties[2];
3774
3775 if (rgProperties.Count > 3)
3776 g.Property4 = (decimal)rgProperties[3];
3777
3778 if (rgProperties.Count > 4)
3779 g.Property5 = (decimal)rgProperties[4];
3780 }
3781
3782 return PutRawImageGroup(g);
3783 }
3784
3790 public int PutRawImageGroup(RawImageGroup g)
3791 {
3792 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3793 {
3794 List<RawImageGroup> rgG = null;
3795
3796 if (g.ID != 0)
3797 rgG = entities.RawImageGroups.Where(p => p.ID == g.ID).ToList();
3798
3799 if (rgG != null && rgG.Count > 0)
3800 {
3801 rgG[0].EndDate = g.EndDate;
3802 rgG[0].Idx = g.Idx;
3803 rgG[0].Property1 = g.Property1;
3804 rgG[0].Property2 = g.Property2;
3805 rgG[0].Property3 = g.Property3;
3806 rgG[0].Property4 = g.Property4;
3807 rgG[0].Property5 = g.Property5;
3808 rgG[0].Rating = g.Rating;
3809 rgG[0].RawData = g.RawData;
3810 rgG[0].StartDate = g.StartDate;
3811 }
3812 else
3813 {
3814 entities.RawImageGroups.Add(g);
3815 }
3816
3817 entities.SaveChanges();
3818
3819 return g.ID;
3820 }
3821 }
3822
3830 public RawImageGroup FindRawImageGroup(int nIdx, DateTime dtStart, DateTime dtEnd)
3831 {
3832 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3833 {
3834 List<RawImageGroup> rgG = entities.RawImageGroups.AsNoTracking().Where(p => p.Idx == nIdx && p.StartDate == dtStart && p.EndDate == dtEnd).ToList();
3835
3836 if (rgG.Count == 0)
3837 return null;
3838
3839 return rgG[0];
3840 }
3841 }
3842
3850 public int FindRawImageGroupID(int nIdx, DateTime dtStart, DateTime dtEnd)
3851 {
3852 RawImageGroup g = FindRawImageGroup(nIdx, dtStart, dtEnd);
3853
3854 if (g == null)
3855 return 0;
3856
3857 return g.ID;
3858 }
3859
3864 {
3865 string strCmd = "DELETE FROM RawImageGroups";
3866
3867 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3868 {
3869 entities.Database.ExecuteSqlCommand(strCmd);
3870 }
3871 }
3872
3873 #endregion
3874
3875
3876 //---------------------------------------------------------------------
3877 // Sources
3878 //---------------------------------------------------------------------
3879 #region Sources
3880
3881#pragma warning disable 1591
3882
3883 public List<int> GetAllDataSourcesIDs()
3884 {
3885 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3886 {
3887 return entities.Sources.AsNoTracking().Select(p => p.ID).ToList();
3888 }
3889 }
3890
3891#pragma warning restore 1591
3892
3896 public void DeleteSourceData()
3897 {
3899 }
3900
3906 public void UpdateSaveImagesToFile(bool bSaveToFile, int nSrcId = 0)
3907 {
3908 if (nSrcId == 0)
3909 nSrcId = m_src.ID;
3910
3911 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3912 {
3913 List<Source> rg = entities.Sources.Where(p => p.ID == nSrcId).ToList();
3914
3915 if (rg.Count > 0)
3916 {
3917 rg[0].SaveImagesToFile = bSaveToFile;
3918 entities.SaveChanges();
3919 }
3920 }
3921
3922 if (m_src != null)
3923 m_src.SaveImagesToFile = bSaveToFile;
3924 }
3925
3934 public void UpdateSource(int nChannels, int nWidth, int nHeight, bool bDataIsReal, int nSrcId = 0)
3935 {
3936 if (nSrcId == 0)
3937 nSrcId = m_src.ID;
3938
3939 using (DNNEntities entities = EntitiesConnection.CreateEntities())
3940 {
3941 List<Source> rg = entities.Sources.Where(p => p.ID == nSrcId).ToList();
3942
3943 if (rg.Count > 0)
3944 {
3945 rg[0].ImageChannels = nChannels;
3946 rg[0].ImageWidth = nWidth;
3947 rg[0].ImageHeight = nHeight;
3948 rg[0].ImageEncoded = bDataIsReal;
3949 entities.SaveChanges();
3950 }
3951 }
3952
3953 if (m_src != null)
3954 {
3955 m_src.ImageChannels = nChannels;
3956 m_src.ImageWidth = nWidth;
3957 m_src.ImageHeight = nHeight;
3958 m_src.ImageEncoded = bDataIsReal;
3959 }
3960 }
3961
3967 public void UpdateSourceCounts(int nImageCount, ConnectInfo ci = null)
3968 {
3969 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
3970 {
3971 int nSrcId = m_src.ID;
3972 List<Source> rg = entities.Sources.Where(p => p.ID == nSrcId).ToList();
3973
3974 if (rg.Count > 0)
3975 {
3976 rg[0].ImageCount = nImageCount;
3977 entities.SaveChanges();
3978 }
3979 }
3980
3981 m_src.ImageCount = nImageCount;
3982 }
3983
3988 public void UpdateSourceCounts(ConnectInfo ci = null)
3989 {
3990 string strCmd = "SELECT COUNT(ID) FROM RawImages WHERE (SourceID = " + m_src.ID.ToString() + " AND Active=1)";
3991 DbRawSqlQuery<int> result = m_entities.Database.SqlQuery<int>(strCmd);
3992 List<int> rgResult = result.ToList();
3993 int nCount = 0;
3994
3995 if (rgResult.Count > 0)
3996 nCount = rgResult[0];
3997
3998 if (nCount == 0)
3999 {
4000 strCmd = "SELECT COUNT(ID) FROM RawValues WHERE (SourceID = " + m_src.ID.ToString() + " AND Active=1)";
4001 result = m_entities.Database.SqlQuery<int>(strCmd);
4002 rgResult = result.ToList();
4003 nCount = 0;
4004
4005 if (rgResult.Count > 0)
4006 nCount = rgResult[0];
4007 }
4008
4009 UpdateSourceCounts(nCount, ci);
4010 }
4011
4018 public int GetSourceID(string strName, ConnectInfo ci = null)
4019 {
4020 strName = convertWs(strName, '_');
4021 Source src = GetSource(strName, ci);
4022
4023 if (src == null)
4024 return 0;
4025
4026 return src.ID;
4027 }
4028
4035 public string GetSourceName(int nID, ConnectInfo ci = null)
4036 {
4037 Source src = GetSource(nID);
4038
4039 if (src == null)
4040 return null;
4041
4042 return src.Name;
4043 }
4044
4051 public Source GetSource(string strName, ConnectInfo ci = null)
4052 {
4053 strName = convertWs(strName, '_');
4054 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
4055 {
4056 List<Source> rgSrc = entities.Sources.AsNoTracking().Where(p => p.Name == strName).ToList();
4057
4058 if (rgSrc.Count == 0)
4059 return null;
4060
4061 return rgSrc[0];
4062 }
4063 }
4064
4071 public Source GetSource(int nID, ConnectInfo ci = null)
4072 {
4073 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
4074 {
4075 List<Source> rgSrc = entities.Sources.AsNoTracking().Where(p => p.ID == nID).ToList();
4076
4077 if (rgSrc.Count == 0)
4078 return null;
4079
4080 return rgSrc[0];
4081 }
4082 }
4083
4090 public int PutSource(Source src, ConnectInfo ci = null)
4091 {
4092 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
4093 {
4094 List<Source> rgSrc = null;
4095
4096 if (src.ID != 0)
4097 rgSrc = entities.Sources.Where(p => p.ID == src.ID).ToList();
4098 else
4099 rgSrc = entities.Sources.Where(p => p.Name == src.Name).ToList();
4100
4101 if (rgSrc != null && rgSrc.Count > 0)
4102 {
4103 rgSrc[0].ImageChannels = src.ImageChannels;
4104 rgSrc[0].ImageCount = src.ImageCount;
4105 rgSrc[0].ImageEncoded = src.ImageEncoded;
4106 rgSrc[0].ImageHeight = src.ImageHeight;
4107 rgSrc[0].ImageWidth = src.ImageWidth;
4108 rgSrc[0].SaveImagesToFile = src.SaveImagesToFile;
4109 src.ID = rgSrc[0].ID;
4110 }
4111 else
4112 {
4113 entities.Sources.Add(src);
4114 }
4115
4116 entities.SaveChanges();
4117
4118 return src.ID;
4119 }
4120 }
4121
4134 public int AddSource(string strName, int nChannels, int nWidth, int nHeight, bool bDataIsReal, int nCopyOfSourceID = 0, bool bSaveImagesToFile = true, ConnectInfo ci = null)
4135 {
4136 Source src = new Source();
4137
4138 src.Name = convertWs(strName, '_');
4139 src.ImageChannels = nChannels;
4140 src.ImageHeight = nHeight;
4141 src.ImageWidth = nWidth;
4142 src.ImageEncoded = bDataIsReal;
4143 src.ImageCount = 0;
4144 src.SaveImagesToFile = bSaveImagesToFile;
4145 src.CopyOfSourceID = nCopyOfSourceID;
4146
4147 return PutSource(src, ci);
4148 }
4149
4155 public virtual bool DeleteSource(int nSrcId = 0)
4156 {
4157 string strCmd;
4158
4159 if (nSrcId == 0)
4160 {
4161 if (m_src == null)
4162 return false;
4163
4164 nSrcId = m_src.ID;
4165 }
4166
4167 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4168 {
4169 strCmd = "DELETE FROM Sources WHERE (ID = " + nSrcId.ToString() + ")";
4170 entities.Database.ExecuteSqlCommand(strCmd);
4171
4172 strCmd = "DELETE FROM SourceParameters WHERE (SourceID = " + nSrcId.ToString() + ")";
4173 entities.Database.ExecuteSqlCommand(strCmd);
4174
4175 strCmd = "DELETE FROM Labels WHERE (SourceID = " + nSrcId.ToString() + ")";
4176 entities.Database.ExecuteSqlCommand(strCmd);
4177
4178 strCmd = "IF OBJECT_ID (N'dbo.ValueItems', N'U') IS NOT NULL DELETE FROM ValueItems WHERE (SourceID = " + nSrcId.ToString() + ")";
4179 entities.Database.ExecuteSqlCommand(strCmd);
4180
4181 strCmd = "IF OBJECT_ID (N'dbo.ValueStreams', N'U') IS NOT NULL DELETE FROM ValueStreams WHERE (SourceID = " + nSrcId.ToString() + ")";
4182 entities.Database.ExecuteSqlCommand(strCmd);
4183
4184 strCmd = "IF OBJECT_ID (N'dbo.RawValues', N'U') IS NOT NULL DELETE FROM RawValues WHERE (SourceID = " + nSrcId.ToString() + ")";
4185 entities.Database.ExecuteSqlCommand(strCmd);
4186 }
4187
4188 DeleteSourceData(nSrcId);
4189
4190 return true;
4191 }
4192
4197 public void DeleteSources(params string[] rgstrSrc)
4198 {
4199 List<string> rgstrSrc1 = new List<string>();
4200
4201 foreach (string str in rgstrSrc)
4202 {
4203 rgstrSrc1.Add(convertWs(str, '_'));
4204 }
4205
4206 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4207 {
4208 foreach (string str in rgstrSrc1)
4209 {
4210 List<Source> rgSrc = entities.Sources.Where(p => rgstrSrc.Contains(p.Name)).ToList();
4211
4212 foreach (Source src in rgSrc)
4213 {
4214 DeleteSource(src.ID);
4215 }
4216 }
4217 }
4218 }
4219
4225 public virtual bool DeleteSourceData(int nSrcId = 0)
4226 {
4227 if (nSrcId == 0)
4228 {
4229 if (m_src == null)
4230 return false;
4231
4232 nSrcId = m_src.ID;
4233 }
4234
4235 DeleteRawImageMeans(nSrcId);
4236 DeleteRawImageResults(nSrcId);
4238 DeleteRawImages(nSrcId);
4239
4240 return true;
4241 }
4242
4247 public void DeleteSourceData(string strSrc)
4248 {
4249 DeleteSourceData(GetSourceID(convertWs(strSrc, '_')));
4250 }
4251
4258 public Dictionary<string, string> GetSourceParameters(int nSrcId = 0, ConnectInfo ci = null)
4259 {
4260 if (nSrcId == 0)
4261 nSrcId = m_src.ID;
4262
4263 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
4264 {
4265 List<SourceParameter> rgP = entities.SourceParameters.AsNoTracking().Where(p => p.SourceID == nSrcId).ToList();
4266 Dictionary<string, string> rgPval = new Dictionary<string, string>();
4267
4268 foreach (SourceParameter p in rgP)
4269 {
4270 rgPval.Add(p.Name, p.Value);
4271 }
4272
4273 return rgPval;
4274 }
4275 }
4276
4283 public string GetSourceParameter(string strName, int nSrcId = 0)
4284 {
4285 strName = convertWs(strName, '_');
4286
4287 if (nSrcId == 0)
4288 nSrcId = m_src.ID;
4289
4290 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4291 {
4292 List<SourceParameter> rgP = entities.SourceParameters.AsNoTracking().Where(p => p.SourceID == nSrcId && p.Name == strName).ToList();
4293
4294 if (rgP.Count == 0)
4295 return null;
4296
4297 return rgP[0].Value;
4298 }
4299 }
4300
4308 public int GetSourceParameter(string strName, int nDefault, int nSrcId = 0)
4309 {
4310 strName = convertWs(strName, '_');
4311 string strVal = GetSourceParameter(strName, nSrcId);
4312
4313 if (strVal == null)
4314 return nDefault;
4315
4316 return int.Parse(strVal);
4317 }
4318
4326 public double GetSourceParameter(string strName, double dfDefault, int nSrcId = 0)
4327 {
4328 strName = convertWs(strName, '_');
4329 string strVal = GetSourceParameter(strName, nSrcId);
4330
4331 if (strVal == null)
4332 return dfDefault;
4333
4334 return BaseParameter.ParseDouble(strVal);
4335 }
4336
4344 public bool GetSourceParameter(string strName, bool bDefault, int nSrcId = 0)
4345 {
4346 strName = convertWs(strName, '_');
4347 string strVal = GetSourceParameter(strName, nSrcId);
4348
4349 if (strVal == null)
4350 return bDefault;
4351
4352 return bool.Parse(strVal);
4353 }
4354
4361 public void SetSourceParameter(string strName, string strValue, int nSrcId = 0)
4362 {
4363 strName = convertWs(strName, '_');
4364
4365 if (nSrcId == 0)
4366 nSrcId = m_src.ID;
4367
4368 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4369 {
4370 List<SourceParameter> rgP = entities.SourceParameters.Where(p => p.SourceID == nSrcId && p.Name == strName).ToList();
4371 SourceParameter sP = null;
4372
4373 if (rgP.Count == 0)
4374 {
4375 sP = new SourceParameter();
4376 sP.Name = strName;
4377 sP.SourceID = nSrcId;
4378 }
4379 else
4380 {
4381 sP = rgP[0];
4382 }
4383
4384 sP.Value = strValue;
4385
4386 if (rgP.Count == 0)
4387 entities.SourceParameters.Add(sP);
4388
4389 entities.SaveChanges();
4390 }
4391 }
4392
4399 public DateTime GetFirstTimeStamp(int nSrcId = 0, string strDesc = null)
4400 {
4401 if (nSrcId == 0)
4402 nSrcId = m_src.ID;
4403
4404 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4405 {
4406 IQueryable<RawImage> iquery = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcId).OrderBy(p => p.TimeStamp);
4407 if (strDesc != null)
4408 iquery = iquery.Where(p => p.Description == strDesc);
4409
4410 List<RawImage> rgImages = iquery.Take(1).ToList();
4411 if (rgImages.Count == 0)
4412 return DateTime.MinValue;
4413
4414 return rgImages[0].TimeStamp.GetValueOrDefault();
4415 }
4416 }
4417
4424 public DateTime GetLastTimeStamp(int nSrcId = 0, string strDesc = null)
4425 {
4426 if (nSrcId == 0)
4427 nSrcId = m_src.ID;
4428
4429 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4430 {
4431 IQueryable<RawImage> iquery = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcId).OrderByDescending(p => p.TimeStamp);
4432 if (strDesc != null)
4433 iquery = iquery.Where(p => p.Description == strDesc);
4434
4435 List<RawImage> rgImages = iquery.Take(1).ToList();
4436 if (rgImages.Count == 0)
4437 return DateTime.MinValue;
4438
4439 return rgImages[0].TimeStamp.GetValueOrDefault();
4440 }
4441 }
4442
4452 public DateTime GetLastTimeStamp(DateTime dtStart, DateTime dtEnd, bool bEndInclusive, int nSrcId = 0, string strDesc = null)
4453 {
4454 if (nSrcId == 0)
4455 nSrcId = m_src.ID;
4456
4457 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4458 {
4459 IQueryable<RawImage> iquery;
4460
4461 if (bEndInclusive)
4462 iquery = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcId && p.TimeStamp >= dtStart && p.TimeStamp <= dtEnd).OrderByDescending(p => p.TimeStamp);
4463 else
4464 iquery = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcId && p.TimeStamp >= dtStart && p.TimeStamp < dtEnd).OrderByDescending(p => p.TimeStamp);
4465
4466 if (strDesc != null)
4467 iquery = iquery.Where(p => p.Description == strDesc);
4468
4469 List<RawImage> rgImages = iquery.Take(1).ToList();
4470 if (rgImages.Count == 0)
4471 return DateTime.MinValue;
4472
4473 return rgImages[0].TimeStamp.GetValueOrDefault();
4474 }
4475 }
4476
4484 public DateTime GetLastTimeStamp(out int nIndex, int nSrcId = 0, string strDesc = null)
4485 {
4486 nIndex = -1;
4487
4488 if (nSrcId == 0)
4489 nSrcId = m_src.ID;
4490
4491 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4492 {
4493 IQueryable<RawImage> iquery = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcId).OrderByDescending(p => p.TimeStamp);
4494 if (strDesc != null)
4495 iquery = iquery.Where(p => p.Description == strDesc);
4496
4497 List<RawImage> rgImages = iquery.Take(1).ToList();
4498 if (rgImages.Count == 0)
4499 return DateTime.MinValue;
4500
4501 nIndex = rgImages[0].Idx.GetValueOrDefault(-1);
4502
4503 return rgImages[0].TimeStamp.GetValueOrDefault();
4504 }
4505 }
4506
4517 public DateTime GetLastTimeStamp(DateTime dtStart, DateTime dtEnd, bool bEndInclusive, out int nIndex, int nSrcId = 0, string strDesc = null)
4518 {
4519 nIndex = -1;
4520
4521 if (nSrcId == 0)
4522 nSrcId = m_src.ID;
4523
4524 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4525 {
4526 IQueryable<RawImage> iquery;
4527
4528 if (bEndInclusive)
4529 iquery = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcId && p.TimeStamp >= dtStart && p.TimeStamp <= dtEnd).OrderByDescending(p => p.TimeStamp);
4530 else
4531 iquery = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcId && p.TimeStamp >= dtStart && p.TimeStamp < dtEnd).OrderByDescending(p => p.TimeStamp);
4532
4533 if (strDesc != null)
4534 iquery = iquery.Where(p => p.Description == strDesc);
4535
4536 List<RawImage> rgImages = iquery.Take(1).ToList();
4537 if (rgImages.Count == 0)
4538 return DateTime.MinValue;
4539
4540 nIndex = rgImages[0].Idx.GetValueOrDefault(-1);
4541
4542 return rgImages[0].TimeStamp.GetValueOrDefault();
4543 }
4544 }
4545
4546 #endregion
4547
4548
4549 //---------------------------------------------------------------------
4550 // Datasets
4551 //---------------------------------------------------------------------
4552 #region Datasets
4553
4560 public string FindDatasetNameFromSourceName(string strTrainSrc, string strTestSrc)
4561 {
4562 int nTrainSrcID = 0;
4563 int nTestSrcID = 0;
4564
4565 if (!String.IsNullOrEmpty(strTestSrc))
4566 nTestSrcID = GetSourceID(strTestSrc);
4567
4568 if (!String.IsNullOrEmpty(strTrainSrc))
4569 nTrainSrcID = GetSourceID(strTrainSrc);
4570
4571 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4572 {
4573 List<Dataset> rgDs = null;
4574
4575 if (nTestSrcID != 0 && nTrainSrcID != 0)
4576 rgDs = entities.Datasets.AsNoTracking().Where(p => p.TrainingSourceID == nTrainSrcID && p.TestingSourceID == nTestSrcID).ToList();
4577 else if (nTestSrcID != 0)
4578 rgDs = entities.Datasets.AsNoTracking().Where(p => p.TestingSourceID == nTestSrcID).ToList();
4579 else if (nTrainSrcID != 0)
4580 rgDs = entities.Datasets.AsNoTracking().Where(p => p.TrainingSourceID == nTrainSrcID).ToList();
4581
4582 if (rgDs != null && rgDs.Count > 0)
4583 return rgDs[0].Name;
4584 }
4585
4586 return null;
4587 }
4588
4594 public int FindDatasetFromSourceId(int nSourceId)
4595 {
4596 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4597 {
4598 List<Dataset> rgDs = entities.Datasets.AsNoTracking().Where(p => p.TrainingSourceID == nSourceId || p.TestingSourceID == nSourceId).ToList();
4599 if (rgDs.Count == 0)
4600 return 0;
4601
4602 return rgDs[0].ID;
4603 }
4604 }
4605
4612 public int GetDatasetID(string strName, ConnectInfo ci = null)
4613 {
4614 strName = convertWs(strName, '_');
4615 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
4616 {
4617 List<Dataset> rgDs = entities.Datasets.AsNoTracking().Where(p => p.Name == strName).ToList();
4618
4619 if (rgDs.Count == 0)
4620 return 0;
4621
4622 return rgDs[0].ID;
4623 }
4624 }
4625
4632 public string GetDatasetName(int nID, ConnectInfo ci = null)
4633 {
4634 Dataset ds = GetDataset(nID, ci);
4635
4636 if (ds == null)
4637 return null;
4638
4639 return ds.Name;
4640 }
4641
4648 public Dataset GetDataset(int nID, ConnectInfo ci = null)
4649 {
4650 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
4651 {
4652 IQueryable<Dataset> iQuery = entities.Datasets.AsNoTracking().Where(p => p.ID == nID);
4653
4654 if (iQuery.Count() == 0)
4655 return null;
4656
4657 List<Dataset> rgDs = iQuery.ToList();
4658
4659 if (rgDs.Count == 0)
4660 return null;
4661
4662 return rgDs[0];
4663 }
4664 }
4665
4671 public Dataset GetDataset(string strName)
4672 {
4673 strName = convertWs(strName, '_');
4674 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4675 {
4676 List<Dataset> rgDs = entities.Datasets.AsNoTracking().Where(p => p.Name == strName).ToList();
4677
4678 if (rgDs.Count == 0)
4679 return null;
4680
4681 return rgDs[0];
4682 }
4683 }
4684
4691 public Dataset GetDataset(string strTestingSrc, string strTrainingSrc)
4692 {
4693 int nTestingSrcId = GetSourceID(strTestingSrc);
4694 int nTrainingSrcId = GetSourceID(strTrainingSrc);
4695
4696 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4697 {
4698 List<Dataset> rgDs = entities.Datasets.AsNoTracking().Where(p => p.TestingSourceID == nTestingSrcId && p.TrainingSourceID == nTrainingSrcId).ToList();
4699
4700 if (rgDs.Count == 0)
4701 return null;
4702
4703 return rgDs[0];
4704 }
4705 }
4706
4719 public int AddDataset(int nDsCreatorID, string strName, int nTestSrcId, int nTrainSrcId, int nDsGroupID = 0, int nModelGroupID = 0, ConnectInfo ci = null, bool bVerify = true)
4720 {
4721 strName = convertWs(strName, '_');
4722
4723 Source srcTest = GetSource(nTestSrcId, ci);
4724 if (srcTest == null)
4725 throw new Exception("Could not find either the test source with ID = " + nTestSrcId.ToString() + "!");
4726
4727 Source srcTrain = GetSource(nTrainSrcId, ci);
4728 if (srcTrain == null)
4729 throw new Exception("Could not find either the train source with ID = " + nTrainSrcId.ToString() + "!");
4730
4731 if (bVerify)
4732 {
4733 if (srcTest.ImageChannels.GetValueOrDefault() != srcTrain.ImageChannels.GetValueOrDefault())
4734 throw new Exception("The test and train sources have different image channels!");
4735
4736 if (srcTest.ImageHeight.GetValueOrDefault() != srcTrain.ImageHeight.GetValueOrDefault())
4737 throw new Exception("The test and train sources have different image heights!");
4738
4739 if (srcTest.ImageWidth.GetValueOrDefault() != srcTrain.ImageWidth.GetValueOrDefault())
4740 throw new Exception("The test and train sources have different image widths!");
4741
4742 if (srcTest.ImageEncoded.GetValueOrDefault() != srcTrain.ImageEncoded.GetValueOrDefault())
4743 throw new Exception("The test and train sources have different image encodings!");
4744 }
4745
4746 Dataset ds = null;
4747
4748 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
4749 {
4750 List<Dataset> rgDs = entities.Datasets.Where(p => p.Name == strName && p.DatasetCreatorID == nDsCreatorID).ToList();
4751
4752 if (rgDs.Count > 0)
4753 {
4754 ds = rgDs[0];
4755 }
4756 else
4757 {
4758 ds = new Dataset();
4759 ds.Name = strName;
4760 ds.DatasetCreatorID = nDsCreatorID;
4761 }
4762
4763 double dfPct = 0;
4764 double dfTotal = srcTest.ImageCount.GetValueOrDefault() + srcTrain.ImageCount.GetValueOrDefault();
4765
4766 if (dfTotal > 0)
4767 dfPct = srcTest.ImageCount.GetValueOrDefault() / dfTotal;
4768
4769 ds.ImageChannels = srcTrain.ImageChannels;
4770 ds.ImageHeight = srcTrain.ImageHeight;
4771 ds.ImageWidth = srcTrain.ImageWidth;
4772 ds.ImageEncoded = srcTrain.ImageEncoded;
4773 ds.DatasetGroupID = nDsGroupID;
4774 ds.ModelGroupID = nModelGroupID;
4775 ds.TestingTotal = srcTest.ImageCount;
4776 ds.TrainingTotal = srcTrain.ImageCount;
4777 ds.TestingPercent = (decimal)dfPct;
4778 ds.Relabeled = false;
4779 ds.TestingSourceID = srcTest.ID;
4780 ds.TrainingSourceID = srcTrain.ID;
4781
4782 if (rgDs.Count == 0)
4783 entities.Datasets.Add(ds);
4784
4785 entities.SaveChanges();
4786 }
4787
4788 UpdateLabelCounts(srcTest.ID, 0, ci);
4789 UpdateLabelCounts(srcTrain.ID, 0, ci);
4790
4791 if (ds == null)
4792 return 0;
4793
4794 return ds.ID;
4795 }
4796
4802 public void UpdateDatasetDescription(int nDsId, string strDesc)
4803 {
4804 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4805 {
4806 List<Dataset> rgDs = entities.Datasets.Where(p => p.ID == nDsId).ToList();
4807
4808 if (rgDs.Count == 0)
4809 return;
4810
4811 rgDs[0].Description = strDesc;
4812 entities.SaveChanges();
4813 }
4814 }
4815
4821 public void UpdateDatasetCounts(int nDsId, ConnectInfo ci = null)
4822 {
4823 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
4824 {
4825 List<Dataset> rgDs = entities.Datasets.Where(p => p.ID == nDsId).ToList();
4826
4827 if (rgDs.Count == 0)
4828 return;
4829
4830 int nTestSrcId = rgDs[0].TestingSourceID.GetValueOrDefault();
4831 List<Source> rgSrcTest = entities.Sources.Where(p => p.ID == nTestSrcId).ToList();
4832
4833 int nTrainSrcId = rgDs[0].TrainingSourceID.GetValueOrDefault();
4834 List<Source> rgSrcTrain = entities.Sources.Where(p => p.ID == nTrainSrcId).ToList();
4835
4836 int nTestingTotal = rgSrcTest[0].ImageCount.GetValueOrDefault(0);
4837 int nTrainingTotal = rgSrcTrain[0].ImageCount.GetValueOrDefault(0);
4838
4839 rgDs[0].TestingTotal = nTestingTotal;
4840 rgDs[0].TrainingTotal = nTrainingTotal;
4841 rgDs[0].TestingPercent = 0;
4842
4843 if (nTrainingTotal + nTrainingTotal > 0)
4844 rgDs[0].TestingPercent = (decimal)nTestingTotal / (decimal)(nTestingTotal + nTrainingTotal);
4845
4846 entities.SaveChanges();
4847 }
4848 }
4849
4855 public int PutDataset(Dataset ds)
4856 {
4857 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4858 {
4859 List<Dataset> rgDs = null;
4860
4861 if (ds.ID != 0)
4862 rgDs = entities.Datasets.Where(p => p.ID == ds.ID).ToList();
4863
4864 if (rgDs != null && rgDs.Count > 0)
4865 {
4866 rgDs[0].DatasetGroupID = ds.DatasetGroupID;
4867 rgDs[0].Name = ds.Name;
4868 rgDs[0].TestingSourceID = ds.TestingSourceID;
4869 rgDs[0].TrainingSourceID = ds.TrainingSourceID;
4870 rgDs[0].DatasetCreatorID = ds.DatasetCreatorID;
4871 rgDs[0].ImageChannels = ds.ImageChannels;
4872 rgDs[0].ImageEncoded = ds.ImageEncoded;
4873 rgDs[0].ImageHeight = ds.ImageHeight;
4874 rgDs[0].ImageWidth = ds.ImageWidth;
4875 rgDs[0].ModelGroupID = ds.ModelGroupID;
4876 rgDs[0].TestingPercent = ds.TestingPercent;
4877 rgDs[0].TrainingTotal = ds.TrainingTotal;
4878 rgDs[0].TestingTotal = ds.TestingTotal;
4879 }
4880 else
4881 {
4882 entities.Datasets.Add(ds);
4883 }
4884
4885 entities.SaveChanges();
4886 }
4887
4888 return ds.ID;
4889 }
4890
4897 public DatasetGroup GetDatasetGroup(int nGroupID, ConnectInfo ci = null)
4898 {
4899 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
4900 {
4901 List<DatasetGroup> rgGroups = entities.DatasetGroups.AsNoTracking().Where(p => p.ID == nGroupID).ToList();
4902
4903 if (rgGroups.Count == 0)
4904 return null;
4905
4906 return rgGroups[0];
4907 }
4908 }
4909
4915 public string GetDatasetGroupName(int nGroupID)
4916 {
4917 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4918 {
4919 List<DatasetGroup> rgGroups = entities.DatasetGroups.AsNoTracking().Where(p => p.ID == nGroupID).ToList();
4920
4921 if (rgGroups.Count == 0)
4922 return null;
4923
4924 return rgGroups[0].Name;
4925 }
4926 }
4927
4934 public Dictionary<string, string> GetDatasetParameters(int nDsId, ConnectInfo ci = null)
4935 {
4936 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
4937 {
4938 List<DatasetParameter> rgP = entities.DatasetParameters.AsNoTracking().Where(p => p.DatasetID == nDsId).ToList();
4939 Dictionary<string, string> rgDsP = new Dictionary<string, string>();
4940
4941 foreach (DatasetParameter p in rgP)
4942 {
4943 if (!rgDsP.ContainsKey(p.Name))
4944 rgDsP.Add(p.Name, p.Value);
4945 }
4946
4947 return rgDsP;
4948 }
4949 }
4950
4957 public string GetDatasetParameter(int nDsId, string strName)
4958 {
4959 using (DNNEntities entities = EntitiesConnection.CreateEntities())
4960 {
4961 List<DatasetParameter> rgP = entities.DatasetParameters.AsNoTracking().Where(p => p.DatasetID == nDsId && p.Name == strName).ToList();
4962
4963 if (rgP.Count == 0)
4964 return null;
4965
4966 return rgP[0].Value;
4967 }
4968 }
4969
4977 public int GetDatasetParameter(int nDsId, string strName, int nDefault)
4978 {
4979 string strVal = GetDatasetParameter(nDsId, strName);
4980
4981 if (strVal == null)
4982 return nDefault;
4983
4984 return int.Parse(strVal);
4985 }
4986
4994 public double GetDatasetParameter(int nDsId, string strName, double dfDefault)
4995 {
4996 string strVal = GetDatasetParameter(nDsId, strName);
4997
4998 if (strVal == null)
4999 return dfDefault;
5000
5001 return BaseParameter.ParseDouble(strVal);
5002 }
5003
5011 public bool GetDatasetParameter(int nDsId, string strName, bool bDefault)
5012 {
5013 string strVal = GetDatasetParameter(nDsId, strName);
5014
5015 if (strVal == null)
5016 return bDefault;
5017
5018 return bool.Parse(strVal);
5019 }
5020
5027 public void SetDatasetParameter(int nDsId, string strName, string strValue)
5028 {
5029 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5030 {
5031 List<DatasetParameter> rgP = entities.DatasetParameters.Where(p => p.DatasetID == nDsId && p.Name == strName).ToList();
5032 DatasetParameter dsP = null;
5033
5034 if (rgP.Count == 0)
5035 {
5036 dsP = new DatasetParameter();
5037 dsP.Name = strName;
5038 dsP.DatasetID = nDsId;
5039 }
5040 else
5041 {
5042 dsP = rgP[0];
5043 }
5044
5045 dsP.Value = strValue;
5046
5047 if (rgP.Count == 0)
5048 entities.DatasetParameters.Add(dsP);
5049
5050 entities.SaveChanges();
5051 }
5052 }
5053
5059 public void SetDatasetParameters(int nDsId, Dictionary<string, string> rgP)
5060 {
5062
5063 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5064 {
5065 entities.Configuration.AutoDetectChangesEnabled = false;
5066 entities.Configuration.ValidateOnSaveEnabled = false;
5067
5068 foreach (KeyValuePair<string, string> kv in rgP)
5069 {
5070 DatasetParameter dsp = new DatasetParameter();
5071 dsp.Name = kv.Key;
5072 dsp.Value = kv.Value;
5073 dsp.DatasetID = nDsId;
5074 entities.DatasetParameters.Add(dsp);
5075 }
5076
5077 entities.SaveChanges();
5078 }
5079 }
5080
5088 public virtual void DeleteDataset(string strDsName, bool bDeleteRelatedProjects, Log log, CancelEvent evtCancel)
5089 {
5090 Dataset ds = GetDataset(strDsName);
5091 if (ds == null)
5092 return;
5093
5094 Source srcTraining = GetSource(ds.TrainingSourceID.GetValueOrDefault());
5095 Source srcTesting = GetSource(ds.TestingSourceID.GetValueOrDefault());
5096 string strCmd;
5097
5098 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5099 {
5100 entities.Database.CommandTimeout = 180;
5101
5102 strCmd = "DELETE RawImageParameters WHERE (SourceID = " + ds.TestingSourceID.GetValueOrDefault().ToString() + ") OR (SourceID = " + ds.TrainingSourceID.GetValueOrDefault().ToString() + ")";
5103 entities.Database.ExecuteSqlCommand(strCmd);
5104
5105 strCmd = "DELETE Labels WHERE (SourceID = " + ds.TestingSourceID.GetValueOrDefault().ToString() + ") OR (SourceID = " + ds.TrainingSourceID.GetValueOrDefault().ToString() + ")";
5106 entities.Database.ExecuteSqlCommand(strCmd);
5107
5108 strCmd = "DELETE RawImageMeans WHERE (SourceID = " + ds.TestingSourceID.GetValueOrDefault().ToString() + ") OR (SourceID = " + ds.TrainingSourceID.GetValueOrDefault().ToString() + ")";
5109 entities.Database.ExecuteSqlCommand(strCmd);
5110
5111 strCmd = "DELETE RawImageResults WHERE (SourceID = " + ds.TestingSourceID.GetValueOrDefault().ToString() + ") OR (SourceID = " + ds.TrainingSourceID.GetValueOrDefault().ToString() + ")";
5112 entities.Database.ExecuteSqlCommand(strCmd);
5113
5114 strCmd = "DELETE RawImages WHERE (SourceID = " + ds.TestingSourceID.GetValueOrDefault().ToString() + ") OR (SourceID = " + ds.TrainingSourceID.GetValueOrDefault().ToString() + ")";
5115 entities.Database.ExecuteSqlCommand(strCmd);
5116
5117 strCmd = "DELETE SourceParameters WHERE (SourceID = " + ds.TestingSourceID.GetValueOrDefault().ToString() + ") OR (SourceID = " + ds.TrainingSourceID.GetValueOrDefault().ToString() + ")";
5118 entities.Database.ExecuteSqlCommand(strCmd);
5119
5120 strCmd = "DELETE Sources WHERE (ID = " + ds.TestingSourceID.GetValueOrDefault().ToString() + ") OR (ID = " + ds.TrainingSourceID.GetValueOrDefault().ToString() + ")";
5121 entities.Database.ExecuteSqlCommand(strCmd);
5122
5123 strCmd = "DELETE DatasetParameters WHERE (DatasetID = " + ds.ID.ToString() + ")";
5124 entities.Database.ExecuteSqlCommand(strCmd);
5125
5126 strCmd = "DELETE Datasets WHERE (ID = " + ds.ID.ToString() + ")";
5127 entities.Database.ExecuteSqlCommand(strCmd);
5128
5129 DeleteFiles del = new DeleteFiles(log, evtCancel);
5130
5131 if (srcTesting != null && !string.IsNullOrEmpty(srcTesting.Name))
5132 del.DeleteDirectory(getImagePathBase(srcTesting.Name, entities));
5133
5134 if (srcTraining != null && !string.IsNullOrEmpty(srcTraining.Name))
5135 del.DeleteDirectory(getImagePathBase(srcTraining.Name, entities));
5136 }
5137 }
5138
5144 public List<Dataset> GetAllDatasets(int nDatasetGroupID)
5145 {
5146 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5147 {
5148 if (nDatasetGroupID > 0)
5149 return entities.Datasets.AsNoTracking().Where(p => p.DatasetGroupID == nDatasetGroupID).ToList();
5150
5151 return entities.Datasets.ToList();
5152 }
5153 }
5154
5160 public List<Dataset> GetAllDatasetsWithCreators(int nDatasetGroupID)
5161 {
5162 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5163 {
5164 List<Dataset> rgDs = new List<Dataset>();
5165 IQueryable<Dataset> iQuery = entities.Datasets.AsNoTracking().Where(p => p.DatasetCreatorID > 0);
5166
5167 if (iQuery.Count() > 0)
5168 {
5169 rgDs = iQuery.ToList();
5170
5171 if (nDatasetGroupID > 0)
5172 return rgDs.Where(p => p.DatasetGroupID == nDatasetGroupID).ToList();
5173 }
5174
5175 return rgDs;
5176 }
5177 }
5178
5185 public List<Dataset> GetAllDatasetsWithCreator(int nDsCreatorID, bool? bRelabeled = null)
5186 {
5187 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5188 {
5189 if (bRelabeled.HasValue)
5190 {
5191 bool bRelabeledValue = bRelabeled.Value;
5192 return entities.Datasets.AsNoTracking().Where(p => p.DatasetCreatorID == nDsCreatorID && p.Relabeled == bRelabeled).ToList();
5193 }
5194 else
5195 {
5196 return entities.Datasets.Where(p => p.DatasetCreatorID == nDsCreatorID).ToList();
5197 }
5198 }
5199 }
5200
5206 public int GetDatasetGroupID(string strName)
5207 {
5208 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5209 {
5210 List<DatasetGroup> rgGroups = entities.DatasetGroups.AsNoTracking().Where(p => p.Name == strName).ToList();
5211
5212 if (rgGroups.Count == 0)
5213 return 0;
5214
5215 return rgGroups[0].ID;
5216 }
5217 }
5218
5225 public string GetDatasetCreatorName(int nDatasetCreatorID, ConnectInfo ci = null)
5226 {
5227 if (ci == null)
5229
5230 if (ci != null && !string.IsNullOrEmpty(ci.Password))
5231 return "";
5232
5233 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
5234 {
5235 List<DatasetCreator> rgDsc = entities.DatasetCreators.AsNoTracking().Where(p => p.ID == nDatasetCreatorID).ToList();
5236
5237 if (rgDsc.Count == 0)
5238 return "";
5239
5240 return rgDsc[0].Name;
5241 }
5242 }
5243
5249 public int GetDatasetCreatorID(string strName)
5250 {
5251 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5252 {
5253 List<DatasetCreator> rgDsc = entities.DatasetCreators.AsNoTracking().Where(p => p.Name == strName).ToList();
5254
5255 if (rgDsc.Count == 0)
5256 return 0;
5257
5258 return rgDsc[0].ID;
5259 }
5260 }
5261
5266 public void ResetAllDatasetRelabelWithCreator(int nDsCreatorID)
5267 {
5268 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5269 {
5270 List<Dataset> rgDs = entities.Datasets.Where(p => p.DatasetCreatorID == nDsCreatorID && p.Relabeled == true).ToList();
5271
5272 foreach (Dataset ds in rgDs)
5273 {
5274 ds.Relabeled = false;
5275 }
5276
5277 entities.SaveChanges();
5278 }
5279 }
5280
5286 public void UpdateDatasetRelabel(int nDsID, bool bRelabel)
5287 {
5288 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5289 {
5290 List<Dataset> rgDs = entities.Datasets.Where(p => p.ID == nDsID).ToList();
5291
5292 foreach (Dataset ds in rgDs)
5293 {
5294 ds.Relabeled = bRelabel;
5295 }
5296
5297 entities.SaveChanges();
5298 }
5299 }
5300
5306 public DateTime GetDatasetMinimumTimestamp(int nDsID)
5307 {
5308 Dataset ds = GetDataset(nDsID);
5309
5310 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5311 {
5312 int nSrcTestId = ds.TestingSourceID.GetValueOrDefault();
5313 int nSrcTrainId = ds.TrainingSourceID.GetValueOrDefault();
5314
5315 List<RawImage> rgImg = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcTestId || p.SourceID == nSrcTrainId).OrderBy(p => p.TimeStamp).Take(1).ToList();
5316 if (rgImg.Count == 0)
5317 return DateTime.MinValue;
5318
5319 return rgImg[0].TimeStamp.GetValueOrDefault(DateTime.MinValue);
5320 }
5321 }
5322
5328 public DateTime GetDatasetMaximumTimestamp(int nDsID)
5329 {
5330 Dataset ds = GetDataset(nDsID);
5331
5332 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5333 {
5334 int nSrcTestId = ds.TestingSourceID.GetValueOrDefault();
5335 int nSrcTrainId = ds.TrainingSourceID.GetValueOrDefault();
5336
5337 List<RawImage> rgImg = entities.RawImages.AsNoTracking().Where(p => p.SourceID == nSrcTestId || p.SourceID == nSrcTrainId).OrderByDescending(p => p.TimeStamp).Take(1).ToList();
5338 if (rgImg.Count == 0)
5339 return DateTime.MinValue;
5340
5341 return rgImg[0].TimeStamp.GetValueOrDefault(DateTime.MinValue);
5342 }
5343 }
5344
5353 public void UpdateDatasetCounts(CancelEvent evtCancel, Log log, int nDatasetCreatorID, List<string> rgstrDs, string strParamNameForDescription)
5354 {
5355 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5356 {
5357 List<Dataset> rgDs = entities.Datasets.Where(p => p.DatasetCreatorID == nDatasetCreatorID).ToList();
5358
5359 foreach (Dataset ds in rgDs)
5360 {
5361 bool bContainsDs = false;
5362 int nIdx;
5363 Stopwatch sw = new Stopwatch();
5364
5365 if (evtCancel.WaitOne(0))
5366 {
5367 log.WriteLine("Aborting dataset count update.");
5368 return;
5369 }
5370
5371 if (rgstrDs != null)
5372 {
5373 foreach (string str in rgstrDs)
5374 {
5375 if (ds.Name.Contains(str))
5376 {
5377 bContainsDs = true;
5378 break;
5379 }
5380 }
5381
5382 if (!bContainsDs)
5383 continue;
5384 }
5385
5386 log.WriteLine("Updating '" + ds.Name + "'...");
5387
5388 int nSrcIdTraining = ds.TrainingSourceID.GetValueOrDefault();
5389 if (nSrcIdTraining> 0)
5390 {
5391 List<Source> rgSrc = entities.Sources.Where(p => p.ID == nSrcIdTraining).ToList();
5392 if (rgSrc.Count > 0)
5393 {
5394 Source src = rgSrc[0];
5395 int nImageCount = entities.RawImages.Where(p => p.SourceID == nSrcIdTraining).Count();
5396
5397 if (src.ImageCount != nImageCount)
5398 src.ImageCount = nImageCount;
5399
5400 if (ds.TrainingTotal != nImageCount)
5401 ds.TrainingTotal = nImageCount;
5402
5403 UpdateLabelCounts(src.ID);
5404
5405 if (bContainsDs)
5406 {
5407 List<int> rgId = QueryAllRawImageIDs(src.ID);
5408
5409 nIdx = 0;
5410 sw.Restart();
5411
5412 foreach (int nID in rgId)
5413 {
5414 if (evtCancel.WaitOne(0))
5415 return;
5416
5417 UpdateRawImageDescriptionFromParameter(nID, strParamNameForDescription);
5418 nIdx++;
5419
5420 if (sw.Elapsed.TotalMilliseconds > 1000)
5421 {
5422 log.Progress = ((double)nIdx / (double)rgId.Count);
5423 sw.Restart();
5424 }
5425 }
5426 }
5427 }
5428 }
5429
5430 int nSrcIdTesting = ds.TestingSourceID.GetValueOrDefault();
5431 if (nSrcIdTesting > 0)
5432 {
5433 List<Source> rgSrc = entities.Sources.Where(p => p.ID == nSrcIdTesting).ToList();
5434 if (rgSrc.Count > 0)
5435 {
5436 Source src = rgSrc[0];
5437 int nImageCount = entities.RawImages.Where(p => p.SourceID == nSrcIdTesting).Count();
5438
5439 if (src.ImageCount != nImageCount)
5440 src.ImageCount = nImageCount;
5441
5442 if (ds.TestingTotal != nImageCount)
5443 ds.TestingTotal = nImageCount;
5444
5445 UpdateLabelCounts(src.ID);
5446
5447 if (bContainsDs)
5448 {
5449 List<int> rgId = QueryAllRawImageIDs(src.ID);
5450
5451 nIdx = 0;
5452 sw.Restart();
5453
5454 foreach (int nID in rgId)
5455 {
5456 if (evtCancel.WaitOne(0))
5457 return;
5458
5459 UpdateRawImageDescriptionFromParameter(nID, strParamNameForDescription);
5460 nIdx++;
5461
5462 if (sw.Elapsed.TotalMilliseconds > 1000)
5463 {
5464 log.Progress = ((double)nIdx / (double)rgId.Count);
5465 sw.Restart();
5466 }
5467 }
5468 }
5469 }
5470 }
5471
5472 double dfTestingPct = (double)ds.TestingTotal / (double)(ds.TestingTotal + ds.TrainingTotal);
5473 if (ds.TestingPercent != (decimal)dfTestingPct)
5474 ds.TestingPercent = (decimal)dfTestingPct;
5475
5476 entities.SaveChanges();
5477 }
5478 }
5479 }
5480
5487 public ModelGroup GetModelGroup(int nGroupID, ConnectInfo ci = null)
5488 {
5489 using (DNNEntities entities = EntitiesConnection.CreateEntities(ci))
5490 {
5491 List<ModelGroup> rgGroups = entities.ModelGroups.AsNoTracking().Where(p => p.ID == nGroupID).ToList();
5492
5493 if (rgGroups.Count == 0)
5494 return null;
5495
5496 return rgGroups[0];
5497 }
5498 }
5499
5505 public string GetModelGroupName(int nGroupID)
5506 {
5507 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5508 {
5509 List<ModelGroup> rgGroups = entities.ModelGroups.AsNoTracking().Where(p => p.ID == nGroupID).ToList();
5510
5511 if (rgGroups.Count == 0)
5512 return null;
5513
5514 return rgGroups[0].Name;
5515 }
5516 }
5517
5523 public int GetModelGroupID(string strGroup)
5524 {
5525 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5526 {
5527 List<ModelGroup> rgGroup = entities.ModelGroups.AsNoTracking().Where(p => p.Name == strGroup).ToList();
5528 if (rgGroup.Count == 0)
5529 return 0;
5530
5531 return rgGroup[0].ID;
5532 }
5533 }
5534
5540 public List<Dataset> GetAllDatasetsInModelGroup(int nModelGroupId)
5541 {
5543 {
5544 return entities.Datasets.AsNoTracking().Where(p => p.ModelGroupID == nModelGroupId).ToList();
5545 }
5546 }
5547
5554 public void DeleteModelGroup(string strGroup, Log log, CancelEvent evtCancel)
5555 {
5556 int nModelGroupId = GetModelGroupID(strGroup);
5557 List<Dataset> rgDs = GetAllDatasetsInModelGroup(nModelGroupId);
5558
5559 foreach (Dataset ds in rgDs)
5560 {
5561 DeleteDataset(ds.Name, false, log, evtCancel);
5562 }
5563
5564 using (DNNEntities entities = EntitiesConnection.CreateEntities())
5565 {
5566 string strCmd = "DELETE FROM ModelGroups WHERE ([Name] = '" + strGroup + "')";
5567 entities.Database.ExecuteSqlCommand(strCmd);
5568 }
5569
5570 }
5571
5572 #endregion
5573 }
5574
5575#pragma warning disable 1591
5576
5577 class FileDataCollection
5578 {
5579 List<Tuple<string, byte[]>> m_rgData = new List<Tuple<string, byte[]>>();
5580 object m_syncObj = new object();
5581
5582 public FileDataCollection()
5583 {
5584 }
5585
5586 public int Count
5587 {
5588 get { return m_rgData.Count; }
5589 }
5590
5591 public void Add(string strFile, byte[] rgData)
5592 {
5593 lock (m_syncObj)
5594 {
5595 m_rgData.Add(new Tuple<string, byte[]>(strFile, rgData));
5596 }
5597 }
5598
5599 public Tuple<string, byte[]> Remove()
5600 {
5601 lock (m_syncObj)
5602 {
5603 if (m_rgData.Count == 0)
5604 return null;
5605
5606 Tuple<string, byte[]> item = m_rgData[0];
5607 m_rgData.RemoveAt(0);
5608 return item;
5609 }
5610 }
5611 }
5612
5613 class FileWriter : IDisposable
5614 {
5615 FileDataCollection m_rgData = new FileDataCollection();
5616 Task[] m_rgFileWriterThreads = null;
5617 ManualResetEvent m_evtCancelFileWriterThread = new ManualResetEvent(false);
5618 ManualResetEvent m_evtFileWriterDone = new ManualResetEvent(false);
5619 ManualResetEvent m_evtFileWriterEmpty = new ManualResetEvent(true);
5620 int m_nPeriod = 1000;
5621 int m_nThreads = 50;
5622
5623 public FileWriter(int nThreads = 50, int nPeriod = 1000)
5624 {
5625 m_nPeriod = nPeriod;
5626 m_nThreads = nThreads;
5627 }
5628
5629 public void Dispose()
5630 {
5631 Cancel();
5632 WaitUntilCompletion();
5633
5634 if (m_rgFileWriterThreads != null)
5635 {
5636 foreach (Task t in m_rgFileWriterThreads)
5637 {
5638 t.Dispose();
5639 }
5640 }
5641 }
5642
5643 public void Cancel()
5644 {
5645 m_evtCancelFileWriterThread.Set();
5646 }
5647
5648 public void Add(string strPath, byte[] rgData)
5649 {
5650 m_evtFileWriterEmpty.Reset();
5651 m_rgData.Add(strPath, rgData);
5652
5653 if (m_rgFileWriterThreads == null)
5654 {
5655 m_evtCancelFileWriterThread.Reset();
5656 m_evtFileWriterDone.Reset();
5657 m_rgFileWriterThreads = new Task[m_nThreads];
5658
5659 for (int i = 0; i < m_nThreads; i++)
5660 {
5661 m_rgFileWriterThreads[i] = Task.Factory.StartNew(new Action(fileWriterThread));
5662 }
5663 }
5664 }
5665
5666 public bool WaitUntilCompletion(int nWait = int.MaxValue)
5667 {
5668 if (m_rgFileWriterThreads == null)
5669 return true;
5670
5671 if (m_evtFileWriterEmpty.WaitOne(nWait))
5672 return true;
5673
5674 return false;
5675 }
5676
5677 private void fileWriterThread()
5678 {
5679 try
5680 {
5681 int nWait = m_nPeriod;
5682
5683 while (!m_evtCancelFileWriterThread.WaitOne(nWait))
5684 {
5685 Tuple<string, byte[]> item = m_rgData.Remove();
5686 if (item != null)
5687 {
5688 File.WriteAllBytes(item.Item1, item.Item2);
5689 Thread.Sleep(0);
5690 nWait = 5;
5691 }
5692 else
5693 {
5694 m_evtFileWriterEmpty.Set();
5695 nWait = m_nPeriod;
5696 }
5697 }
5698 }
5699 finally
5700 {
5701 m_evtFileWriterDone.Set();
5702 }
5703 }
5704 }
5705
5706#pragma warning restore 1591
5707
5711 public class ParameterData
5712 {
5713 string m_strName;
5714 string m_strValue = null;
5715 double? m_dfValue = null;
5716 byte[] m_rgValue = null;
5717 int m_nImageID = 0;
5718 bool m_bOnlyAddNew = false;
5719 int m_nSrcId = 0;
5720
5731 public ParameterData(string strName, string strValue, double? dfVal = null, byte[] rgData = null, int nImageID = 0, bool bOnlyAddNew = false, int nSrcId = 0)
5732 {
5733 m_strName = strName;
5734 m_strValue = strValue;
5735 m_dfValue = dfVal;
5736 m_rgValue = rgData;
5737 m_nImageID = nImageID;
5738 m_bOnlyAddNew = bOnlyAddNew;
5739 m_nSrcId = nSrcId;
5740 }
5741
5747 public ParameterData(string strName, int nImageID)
5748 {
5749 m_strName = strName;
5750 m_nImageID = nImageID;
5751 }
5752
5756 public string Name
5757 {
5758 get { return m_strName; }
5759 }
5760
5764 public int SourceID
5765 {
5766 get { return m_nSrcId; }
5767 set { m_nSrcId = value; }
5768 }
5769
5773 public bool OnlyAddNew
5774 {
5775 get { return m_bOnlyAddNew; }
5776 }
5777
5781 public string Value
5782 {
5783 get { return m_strValue; }
5784 }
5785
5789 public double? NumericValue
5790 {
5791 get { return m_dfValue; }
5792 }
5793
5797 public byte[] Data
5798 {
5799 get { return m_rgValue; }
5800 }
5801
5805 public int ImageID
5806 {
5807 get { return m_nImageID; }
5808 set { m_nImageID = value; }
5809 }
5810 }
5811
5815 public class DbItem
5816 {
5817 object m_tag;
5818
5822 public DbItem()
5823 {
5824 }
5825
5830 public DbItem Clone()
5831 {
5832 DbItem item = new DbItem();
5833 item.id = id;
5834 item.virtualid = virtualid;
5835 item.index = index;
5836 item.label = label;
5837 item.boost = boost;
5838 item.time = time;
5839 item.desc = desc;
5841 item.active = active;
5842 return item;
5843 }
5844
5848 public object Tag
5849 {
5850 get { return m_tag; }
5851 set { m_tag = value; }
5852 }
5853
5857 public int ID
5858 {
5859 get { return id; }
5860 }
5861
5865 public int id { get; set; }
5866
5870 public int VirtualID
5871 {
5872 get { return virtualid.GetValueOrDefault(); }
5873 }
5874
5878 public int? virtualid { get; set; }
5879
5883 public int Index
5884 {
5885 get { return index.GetValueOrDefault(); }
5886 }
5887
5891 public int? index { get; set; }
5892
5896 public int Label
5897 {
5898 get { return label.GetValueOrDefault(); }
5899 }
5900
5904 public int? label { get; set; }
5905
5909 public int Boost
5910 {
5911 get { return boost.GetValueOrDefault(); }
5912 }
5913
5917 public int? boost { get; set; }
5918
5922 public DateTime Time
5923 {
5924 get { return time.GetValueOrDefault(); }
5925 }
5926
5930 public DateTime? time { get; set; }
5931
5935 public string Desc
5936 {
5937 get { return desc; }
5938 }
5939
5943 public string desc { get; set; }
5944
5949 {
5950 get { return originalsrcid; }
5951 }
5952
5956 public int? originalsrcid { get; set; }
5957
5961 public bool Active
5962 {
5963 get { return active.GetValueOrDefault(false); }
5964 }
5965
5969 public bool? active { get; set; }
5970
5975 public override string ToString()
5976 {
5977 return "Active=" + Active.ToString() + "; ID=" + ID.ToString() + "; Idx=" + Index.ToString() + "; Label=" + Label.ToString() + "; Time=" + Time.ToString() + "; Desc=" + Desc + "; Boost=" + Boost.ToString();
5978 }
5979 }
5980}
Defines a collection of AnnotationGroups.
Definition: Annotation.cs:256
Dictionary< int, string > Labels
Get/set the label name mappings.
Definition: Annotation.cs:341
int Count
Specifies the number of items in the collection.
Definition: Annotation.cs:350
The BaseParameter class is the base class for all other parameter classes.
static double ParseDouble(string strVal)
Parse double values using the US culture if the decimal separator = '.', then using the native cultur...
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
bool WaitOne(int nMs=int.MaxValue)
Waits for the signal state to occur.
Definition: CancelEvent.cs:290
The ConnectInfo class specifies the server, database and username/password used to connect to a datab...
Definition: ConnectInfo.cs:14
string Server
Get/set the server.
Definition: ConnectInfo.cs:127
TYPE
Defines the generic connection location
Definition: ConnectInfo.cs:25
override string ToString()
Returns a string representation of the connection.
Definition: ConnectInfo.cs:90
string Password
Returns the password.
Definition: ConnectInfo.cs:153
The Datum class is a simple wrapper to the SimpleDatum class to ensure compatibility with the origina...
Definition: Datum.cs:12
The ImageTools class is a helper class used to manipulate image data.
Definition: ImageTools.cs:16
static byte[] ImageToByteArray(Image imageIn)
Converts an Image into an array of byte.
Definition: ImageTools.cs:236
The LabelMapping class represents a single label mapping.
int? ConditionBoostEquals
Get/set the boost condition to test which if met, the new label is set, otherwise it is not.
int NewLabel
Get/set the new label.
int? NewLabelConditionFalse
Get/set the label to use if the boost condition fails.
int OriginalLabel
Get/set the original label.
The Log class provides general output in text form.
Definition: Log.cs:13
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
Definition: Log.cs:80
double Progress
Get/set the progress associated with the Log.
Definition: Log.cs:147
The SimpleDatum class holds a data input within host memory.
Definition: SimpleDatum.cs:161
static byte[] SaveAnnotationDataToDataCriteriaByteArray(ANNOTATION_TYPE type, AnnotationGroupCollection annotations)
Save the annotation data to a byte array.
ANNOTATION_TYPE
Specifies the annotation type when using annotations.
Definition: SimpleDatum.cs:204
byte[] DebugData
Get/set debug data associated with the data.
int VirtualID
Returns the virtual ID of the SimpleDatum.
void SaveAnnotationDataToDataCriteria()
Save the annotation data and type to the data criteria.
DateTime TimeStamp
Get/set the Timestamp.
string Description
Get/set a description of the data.
static byte[] GetByteData(byte[] rgData, int nImagePadX, int nImagePadY, int nHeight, int nWidth, int nChannels)
Return the non-real data as a byte array after padding the data.
int Channels
Return the number of channels of the data.
bool IsRealData
Returns whether or not the data contains real numbers or byte data.
AnnotationGroupCollection annotation_group
When using annoations, each annotation group contains an annotation for a particular class used with ...
byte[] DataCriteria
Get/set data criteria associated with the data.
ANNOTATION_TYPE annotation_type
When using annotations, the annotation type specifies the type of annotation. Currently,...
DATA_FORMAT
Defines the data format of the DebugData and DataCriteria when specified.
Definition: SimpleDatum.cs:223
int GroupID
Get/set the group ID of the SimpleDatum.
int Boost
Get/set the boost for this data.
int Width
Return the width of the data.
bool AutoLabeled
Get/set whether or not the label was auto generated.
DATA_FORMAT DebugDataFormat
Get/set the data format of the debug data.
int Height
Return the height of the data.
DATA_FORMAT DataCriteriaFormat
Get/set the data format of the data criteria.
int Label
Return the known label of the data.
The ResultDescriptor class describes the results of a run.
static byte[] CreateResults(List< Result > rgResults, bool bInvert)
The CreateResults function converts the list of (int nLabel, double dfResult) pairs into a array of b...
static List< Result > GetResults(byte[] rgData)
Extract the results from the binary data.
The DNNEntities class defines the entities used to connecto the database via Entity Frameworks.
The Database class manages the actual connection to the physical database using Entity Framworks from...
Definition: Database.cs:23
Dataset GetDataset(int nID, ConnectInfo ci=null)
Returns the Dataset entity for a dataset ID.
Definition: Database.cs:4648
int GetLabelCount(int nLabel)
Returns the number of images under a given label.
Definition: Database.cs:479
void ActivateRawImageByIndex(int nSrcId, int nIdx, bool bActive)
Activate/deactivate a raw image based on its index.
Definition: Database.cs:1866
bool ChangeRawImageSourceID(int nID, int nNewSrcID, bool bSave=true)
Change the data source ID on a raw image - currently only allowed on virtual raw images.
Definition: Database.cs:1713
double GetDatasetParameter(int nDsId, string strName, double dfDefault)
Returns the value of a dataset parameter as a double.
Definition: Database.cs:4994
void ActivateAllRawImages(bool bActive, bool bAnnotatedOnly, params int[] rgSrcId)
Activate all raw images associated with a set of source ID's.
Definition: Database.cs:3024
int GetBoostCount(int nSrcId=0, string strFilterVal=null, int? nBoostVal=null)
Return the number of boosted images for a data source.
Definition: Database.cs:2748
DateTime GetDatasetMaximumTimestamp(int nDsID)
Returns the maximum time-stamp for a dataset.
Definition: Database.cs:5328
void SetSourceParameter(string strName, string strValue, int nSrcId=0)
Set the value of a data source parameter.
Definition: Database.cs:4361
FORCE_LOAD
Defines the force load type.
Definition: Database.cs:57
string GetDatasetCreatorName(int nDatasetCreatorID, ConnectInfo ci=null)
Returns the name of a dataset creator given its ID.
Definition: Database.cs:5225
List< RawImage > GetRawImagesAt(int nIdx, int nCount, int nSrcId=0, string strDescription=null)
Returns a list of RawImages from the database for a data source.
Definition: Database.cs:1243
void UpdateSourceCounts(int nImageCount, ConnectInfo ci=null)
Updates the source counts for the open data source.
Definition: Database.cs:3967
string GetDatabaseImagePath(string strName)
Query the physical database file path for Images.
Definition: Database.cs:339
Source m_src
Specifies the default data source.
Definition: Database.cs:27
List< Tuple< SimpleDatum, List< Result > > > GetRawImageResultBatch(int nBatchCount, byte[] rgResults)
Extracts the raw image result batch from the result binary data.
Definition: Database.cs:3293
static byte[] PackExtraData(List< Tuple< DateTime, int > > rg)
Pack the extra data into a byte array.
Definition: Database.cs:3353
void ResetLabelBoosts(int nProjectId)
Reset all label boosts to their orignal settings for a project.
Definition: Database.cs:945
byte[] GetRawImageDebugData(byte[] rgData, int? nOriginalSourceID=null)
Converts the raw image debug data which may be stored as a path to the underlying data file,...
Definition: Database.cs:1495
byte[] GetRawImageDataCriteria(byte[] rgData, int? nOriginalSourceID=null)
Converts the raw image data criteria data which may be stored as a path to the underlying data file,...
Definition: Database.cs:1538
void AddLabelToCache(int nLabel)
Adds a label to the label cache.
Definition: Database.cs:494
void UpdateActiveLabelByIndex(int nSrcId, int nIdx, int nLabel)
Update the label value of a label.
Definition: Database.cs:1774
virtual void DeleteDataset(string strDsName, bool bDeleteRelatedProjects, Log log, CancelEvent evtCancel)
Delete a dataset.
Definition: Database.cs:5088
void DisableLabel(int nSrcId, int nLabel, bool bOriginalLabel=false)
Disable a set of labels within the source specified by the ID.
Definition: Database.cs:1816
virtual void Open(int nSrcId, FORCE_LOAD nForceLoad=FORCE_LOAD.NONE, ConnectInfo ci=null)
Opens a data source.
Definition: Database.cs:188
List< Dataset > GetAllDatasetsWithCreators(int nDatasetGroupID)
Returns a list of all datasets within a group with dataset creators.
Definition: Database.cs:5160
List< RawImage > QueryRawImages(int nSrcId, bool? bActive=null, int nBoostVal=0, bool bExactBoostVal=false)
Returns the list of raw images that have a source ID from a selected list.
Definition: Database.cs:1211
void SetLabelMapping(LabelMapping map, int nSrcId=0)
Saves a label mapping in the database for a data source.
Definition: Database.cs:830
bool ConvertRawImagesSaveToFile(int nIdx, int nCount, CancelEvent evtCancel=null)
The ConvertRawImagesSaveToFile method saves the image in the database to the file system and replaces...
Definition: Database.cs:2126
void UpdateLabelCounts(Dictionary< int, int > rgCounts, int nSrcId=0)
Update the label counts for a given data source.
Definition: Database.cs:627
List< RawImageParameter > QueryRawImageParameters(int nImageID)
Query all image parameters for a given image.
Definition: Database.cs:1193
void Open(string strSrc, bool bForceLoadImageFilePath=false)
Opens a data source.
Definition: Database.cs:211
void ActivateLabels(List< int > rgLabels, bool bActive, params int[] rgSrcId)
Activate (or deactivate) the labels specified for each of the source ID's specified.
Definition: Database.cs:1013
bool UpdateRawImageDescriptionFromParameter(int nID, string strParamName)
Update the RawImage description from a RawImage parameter.
Definition: Database.cs:1921
bool GetRawImageParameterExist(string strName, int nSrcId=0, string strType="TEXT")
Returns whether or not a given RawImage parameter exists.
Definition: Database.cs:3681
RawImageGroup FindRawImageGroup(int nIdx, DateTime dtStart, DateTime dtEnd)
Searches for a RawImageGroup by index, start time-stamp and end time-stamp.
Definition: Database.cs:3830
bool UpdateActiveLabel(int nID, int nLabel, bool bActivate=true, bool bSaveChanges=true)
Update the label value of a label.
Definition: Database.cs:1739
int SetRawImageParameter(int nRawImageID, string strName, string strValue, double? dfVal=null, byte[] rgData=null, bool bSave=true, bool bOnlyAddNew=false, DNNEntities entities=null)
Add a new RawImage parameter (or update an existing if found).
Definition: Database.cs:3527
int GetDatasetCreatorID(string strName)
Returns the ID of a dataset creator given its name.
Definition: Database.cs:5249
bool ConvertRawImagesSaveToDatabase(int nIdx, int nCount, CancelEvent evtCancel=null)
The ConvertRawImagesSaveToDatabase method saves the image in the file system to the database and dele...
Definition: Database.cs:2226
List< Label > GetLabels(bool bSort=true, bool bWithImagesOnly=false, int nSrcId=0, ConnectInfo ci=null)
Returns a list of all labels used by a data source.
Definition: Database.cs:710
Dataset GetDataset(string strName)
Returns the Dataset entity for a dataset name.
Definition: Database.cs:4671
virtual void Close()
Close the previously opened data source.
Definition: Database.cs:277
void UpdateLabelCounts(int nSrcId=0, int nProjectId=0, ConnectInfo ci=null)
Update the label counts for a given data source and project (optionally) by querying the database for...
Definition: Database.cs:659
int PutSource(Source src, ConnectInfo ci=null)
Adds or updates (if exists) a data source to the database.
Definition: Database.cs:4090
Source CurrentSource
Returns the current entity framwork Source object set during the previous call to Open().
Definition: Database.cs:178
int AddSource(string strName, int nChannels, int nWidth, int nHeight, bool bDataIsReal, int nCopyOfSourceID=0, bool bSaveImagesToFile=true, ConnectInfo ci=null)
Adds a new data source to the database.
Definition: Database.cs:4134
int PutDataset(Dataset ds)
Save the Dataset entity to the database.
Definition: Database.cs:4855
bool GetSourceParameter(string strName, bool bDefault, int nSrcId=0)
Return the data source parameter as a bool.
Definition: Database.cs:4344
void DeleteModelGroup(string strGroup, Log log, CancelEvent evtCancel)
Deletes a model group from the database.
Definition: Database.cs:5554
int SetRawImageParameter(int nSrcId, int nRawImageID, string strName, string strValue, double? dfVal=null, byte[] rgData=null)
Add a new RawImage parameter (or update an existing if found).
Definition: Database.cs:3582
byte[] getRawImage(byte[] rgData, int? nSecondarySrcId=null, ConnectInfo ci=null, DNNEntities entities=null, bool bTestConnection=false)
Converts a set of bytes from a file path-name by loading its bytes and returning them,...
Definition: Database.cs:1588
DateTime GetLastTimeStamp(int nSrcId=0, string strDesc=null)
Returns the last time-stamp in the data source.
Definition: Database.cs:4424
RawImageParameter GetRawImageParameterEx(int nRawImageID, string strName)
Returns the RawImageParameter entity given the image ID and parameter name..
Definition: Database.cs:3502
int GetRawImageParameterCount(string strName, int nSrcId=0, string strType="TEXT")
Returns the RawImage parameter count for a data source.
Definition: Database.cs:3654
int m_nLastIndex
Specifies the last index added to the data source.
Definition: Database.cs:45
string GetDatasetGroupName(int nGroupID)
Returns the name of a dataset group given its ID.
Definition: Database.cs:4915
List< RawImageResult > GetRawImageResults(int nSrcId=0, bool bRequireExtraData=false, int nMax=-1)
Returns the RawImageResults for a data source.
Definition: Database.cs:3329
void DeleteSourceData(string strSrc)
Delete the data source data (images, means, results and parameters) from the database.
Definition: Database.cs:4247
bool VerifyDataConnection(string strSrc, ConnectInfo ci)
Verify the data connection information.
Definition: Database.cs:94
Label GetLabel(int nID)
Return the Label with the given ID.
Definition: Database.cs:399
void SetDatasetParameters(int nDsId, Dictionary< string, string > rgP)
Adds a batch of new parametes.
Definition: Database.cs:5059
List< DbItem > GetAllRawImageIndexes(bool bBoostedOnly, bool bIncludeActive=true, bool bIncludeInactive=false)
Returns the list of the image indexes of all images.
Definition: Database.cs:1132
void UpdateLabelName(int nLabel, string strName, int nSrcId=0)
Update the name of a label.
Definition: Database.cs:363
void SaveLabelCache()
Saves the label cache to the database.
Definition: Database.cs:517
byte[] GetRawImageParameterData(int nRawImageID, string strName)
Return the byte array data of a RawImage parameter.
Definition: Database.cs:3483
bool m_bEnableFileBasedData
Specifies whether or not file based data is enabled.
Definition: Database.cs:41
bool UpdateRawImageSourceID(int nImageID, int nSrcID)
Updates a given image's source ID.
Definition: Database.cs:2968
double GetRawImageParameter(int nRawImageID, string strName, double dfDefault)
Return the double value of a RawImage parameter.
Definition: Database.cs:3450
string m_strSecondaryImgPath
Specifies the secondary base path to the file based data (used when copying a data source)
Definition: Database.cs:37
double GetSourceParameter(string strName, double dfDefault, int nSrcId=0)
Return the data source parameter as a double.
Definition: Database.cs:4326
string GetDatasetName(int nID, ConnectInfo ci=null)
Returns the name of a dataset given its ID.
Definition: Database.cs:4632
void Refresh()
Close and re Open with the current data source.
Definition: Database.cs:294
void UpdateSaveImagesToFile(bool bSaveToFile, int nSrcId=0)
Update the SaveImagesToFile flag in a given Data Source.
Definition: Database.cs:3906
DateTime GetLastTimeStamp(DateTime dtStart, DateTime dtEnd, bool bEndInclusive, int nSrcId=0, string strDesc=null)
Returns the last time-stamp in the data source.
Definition: Database.cs:4452
void DeleteRawImages(int nSrcId=0)
Delete all RawImages in a data source.
Definition: Database.cs:2527
void ResetAllBoosts(int nSrcId=0, int nMinBoost=0, bool bExactVal=false, int nDesiredBoostVal=-1)
Reset all image boosts for a data set.
Definition: Database.cs:2848
RawImage GetRawImage(int nID)
Returns the RawImage with a given ID.
Definition: Database.cs:2494
List< string > GetRawImageDistinctParameterDescriptions(int nSrcId=0)
Returns a list of distinct RawImage parameter descriptions for a data source.
Definition: Database.cs:3715
virtual bool DeleteSourceData(int nSrcId=0)
Delete the data source data (images, means, results and parameters) from the database.
Definition: Database.cs:4225
virtual bool DeleteSource(int nSrcId=0)
Delete a data source from the database.
Definition: Database.cs:4155
static List< Tuple< DateTime, int > > UnpackExtraData(byte[] rg)
Unpack the extra data from a byte array.
Definition: Database.cs:3376
int GetRawImageParameter(int nRawImageID, string strName, int nDefault)
Return the int value of a RawImage parameter.
Definition: Database.cs:3433
RawImageMean GetRawImageMean(int nSrcId=0, ConnectInfo ci=null)
Return the RawImageMean for the image mean from the open data source.
Definition: Database.cs:2583
string GetSourceParameter(string strName, int nSrcId=0)
Return the data source parameter as a string.
Definition: Database.cs:4283
void ResetLabels(int nProjectId=0, int nSrcId=0)
Resets all labels back to their original labels for a project.
Definition: Database.cs:894
void DeleteLabelBoosts(int nProjectId, int nSrcId=0)
Delete all label boosts for a project.
Definition: Database.cs:916
void ActivateAllRawImages(bool bActive, bool bAnnotatedOnly, int? nTgtLabel, bool bTargetLabelExact, int? nTgtBoost, bool bTargetBoostExact, params int[] rgSrcId)
Activate all raw images associated with a set of source ID's.
Definition: Database.cs:3064
int AddRawImageGroup(Image img, int nIdx, DateTime dtStart, DateTime dtEnd, List< double > rgProperties)
Adds a new RawImage group to the database.
Definition: Database.cs:3755
void ResetAllActiveLabels(int nSrcId)
Reset the all active labels to their original label within a source.
Definition: Database.cs:1803
int GetSourceID(string strName, ConnectInfo ci=null)
Returns the ID of a data source given its name.
Definition: Database.cs:4018
void UpdateLabelMapping(int nNewLabel, List< int > rgOriginalLabels, int nSrcId=0)
Update a label mapping in the database for a data source.
Definition: Database.cs:866
Dictionary< string, string > GetDatasetParameters(int nDsId, ConnectInfo ci=null)
Returns all dataset parameters for a given dataset.
Definition: Database.cs:4934
void SetDatasetParameter(int nDsId, string strName, string strValue)
Adds a new parameter or Sets the value of an existing dataset parameter.
Definition: Database.cs:5027
void FixupRawImageCopy(int nImageID, int nSecondarySrcId)
The FixupRawImageCopy method is used to fixup the OriginalSourceId by setting it to a secondary sourc...
Definition: Database.cs:2324
int GetDatasetParameter(int nDsId, string strName, int nDefault)
Returns the value of a dataset parameter as an int.
Definition: Database.cs:4977
int DisableAllNonMatchingImages(int nSrcId, int nWidth, int nHeight)
Disable all images that do not have a matching widxht size.
Definition: Database.cs:1847
Dictionary< string, string > GetSourceParameters(int nSrcId=0, ConnectInfo ci=null)
Returns a dictionary of the data source parameters.
Definition: Database.cs:4258
int FindDatasetFromSourceId(int nSourceId)
Searches for the dataset containing the sourceId.
Definition: Database.cs:4594
List< int > QueryAllRawImageIDs(int nSrcId=0, int nMax=int.MaxValue, int nLabel=-1, int nBoost=-1, bool bBoostIsExact=false, bool bAnnotatedOnly=false, bool bActiveOnly=true)
Returns the ID's of all RawImages within a data source.
Definition: Database.cs:1959
string GetModelGroupName(int nGroupID)
Returns the name of a model group given its ID.
Definition: Database.cs:5505
void DeleteSourceData()
Deletes the data source data for the open data source.
Definition: Database.cs:3896
int AddDataset(int nDsCreatorID, string strName, int nTestSrcId, int nTrainSrcId, int nDsGroupID=0, int nModelGroupID=0, ConnectInfo ci=null, bool bVerify=true)
Add a new (or update an existing if exists) dataset to the database.
Definition: Database.cs:4719
int FindRawImageGroupID(int nIdx, DateTime dtStart, DateTime dtEnd)
Searches fro the RawImageGroup ID.
Definition: Database.cs:3850
List< RawImageParameter > QueryRawImageParameters(int nSrcId, string strName)
Query a list of all raw image parameters of a give name stored with a given source ID.
Definition: Database.cs:1179
int GetDatasetID(string strName, ConnectInfo ci=null)
Returns a datasets ID given its name.
Definition: Database.cs:4612
void AddLabelBoost(int nProjectId, int nLabel, double dfBoost, int nSrcId=0)
Add a label boost to the database for a given project.
Definition: Database.cs:794
void DisableAllLabels(int nSrcId)
Disable all labels within the source specified by the ID.
Definition: Database.cs:1833
int QueryRawImageCount(int nSrcId=0)
Returns the number of RawImages in a data source.
Definition: Database.cs:2512
ModelGroup GetModelGroup(int nGroupID, ConnectInfo ci=null)
Returns the ModelGroup entity given the ID of a model group.
Definition: Database.cs:5487
DateTime GetLastTimeStamp(DateTime dtStart, DateTime dtEnd, bool bEndInclusive, out int nIndex, int nSrcId=0, string strDesc=null)
Returns the last time-stamp and index in the data source falling within a time range.
Definition: Database.cs:4517
void UpdateAllActiveLabels(int nSrcId, int nLabel, int? nOriginalLabel)
Update the all items to a label value for the given nSrcId.
Definition: Database.cs:1788
void DeleteLabelBoosts(int nProjectId)
Delete all label boosts for a project.
Definition: Database.cs:932
byte[] GetRawImageDataCriteria(RawImage img, out int? nDataCriteriaFmtId)
Returns the raw data criteria data of the RawImage.
Definition: Database.cs:1431
int GetDatasetGroupID(string strName)
Returns the ID of a dataset group given its name.
Definition: Database.cs:5206
void UpdateDatasetImageAnnotations(int nSrcId, int nImageId, AnnotationGroupCollection annotations, bool bSetLabelOnly)
Update the annotations of a given raw image.
Definition: Database.cs:3125
Source GetSource(string strName, ConnectInfo ci=null)
Returns the Source entity given a data source name.
Definition: Database.cs:4051
List< RawImage > GetRawImagesAtID(List< int > rgImageID, int nSrcId=0, string strDescription=null)
Returns a list of RawImages from the database for a data source.
Definition: Database.cs:1299
void UpdateActiveLabelDirect(int nID, int nLabel)
Directly update the active label and activate the image with the specified ID.
Definition: Database.cs:1880
RawImage CreateRawImage(int nIdx, SimpleDatum d, int nBackgroundWritingThreadCount, string strDescription=null, int? nOriginalSourceID=null, bool bActive=true)
Create a new RawImage but do not add it to the database.
Definition: Database.cs:1998
string m_strPrimaryImgPath
Specifies the base path to the file based data.
Definition: Database.cs:33
void UpdateDatasetDescription(int nDsId, string strDesc)
Update the description of a given dataset.
Definition: Database.cs:4802
void PutRawImages(List< RawImage > rgImg, List< List< ParameterData > > rgrgParam=null, ConnectInfo ci=null)
Saves a List of RawImages to the database.
Definition: Database.cs:2413
void UpdateSource(int nChannels, int nWidth, int nHeight, bool bDataIsReal, int nSrcId=0)
Updates a data source.
Definition: Database.cs:3934
int GetLabelID(int nLabel)
Returns the label ID associated with a label value.
Definition: Database.cs:463
byte[] GetRawImageDebugData(int nImgID, int? nOriginalSourceID=null)
Queries the Debug Data for an image an Converts the raw image data criteria data which may be stored ...
Definition: Database.cs:1506
int PutRawImageResults(int nSrcId, int nIdx, int nLabel, DateTime dt, List< Tuple< SimpleDatum, List< Result > > > rgrgResults, List< Tuple< DateTime, int > > rgExtra=null)
Save the results of a Run as a RawImageResult.
Definition: Database.cs:3248
RawImage GetRawImageAt(int nIdx, int nSrcId=0)
Returns the RawImage at a given image index.
Definition: Database.cs:1334
bool UpdateRawImageDescription(int nID, string strDescription)
Update the description of a RawImage.
Definition: Database.cs:1902
void DeleteRawImageParameters(int nSrcId)
Delete all RawImage parameters within a data source.
Definition: Database.cs:3637
bool GetRawImageParameter(int nRawImageID, string strName, bool bDefault)
Return the bool value of a RawImage parameter.
Definition: Database.cs:3467
int PutRawImageResults(int nSrcId, int nIdx, int nLabel, DateTime dt, List< Result > rgResults, bool bInvert, List< Tuple< DateTime, int > > rgExtra=null)
Save the results of a Run as a RawImageResult.
Definition: Database.cs:3199
List< RawImage > GetRawImagesAt(List< int > rgImageIdx, int nSrcId=0, string strDescription=null)
Returns a list of RawImages from the database for a data source.
Definition: Database.cs:1265
string GetLabelName(int nLabel, int nSrcId=0)
Get the Label name of a label within a data source.
Definition: Database.cs:416
void UpdateDatasetCounts(int nDsId, ConnectInfo ci=null)
Update the dataset counts.
Definition: Database.cs:4821
int GetRawImageID(DateTime dt, int nSrcId=0)
Returns the RawImage ID for the image with the given time-stamp.
Definition: Database.cs:1359
void ResetAllDatasetRelabelWithCreator(int nDsCreatorID)
Reset all dataset relabel flags with a given creator.
Definition: Database.cs:5266
int GetImageCount()
Returns the number of raw images in the database for the open data source.
Definition: Database.cs:1120
string getImagePath(byte[] rgData)
Returns the file path contained within a byte array or null if no path is found.
Definition: Database.cs:1688
void Dispose()
Release any resources used.
Definition: Database.cs:82
int AddLabel(int nLabel, string strName="", int nSrcId=0, ConnectInfo ci=null)
Add a label to the database for a data source.
Definition: Database.cs:753
int LastIndex
Returns the last image index added to the database.
Definition: Database.cs:143
void DeleteSources(params string[] rgstrSrc)
Delete the list of data sources, listed by name, from the database.
Definition: Database.cs:4197
bool GetDatasetParameter(int nDsId, string strName, bool bDefault)
Returns the value of a dataset parameter as a bool.
Definition: Database.cs:5011
int ActivateFiltered(int nSrcId=0, string strFilterVal=null, int? nBoostVal=null)
Activate the images that meet the filtering criteria in the Data Source. If no filtering criteria is ...
Definition: Database.cs:2768
List< Dataset > GetAllDatasetsInModelGroup(int nModelGroupId)
Returns all Dataset entities within a given model group.
Definition: Database.cs:5540
byte[] setImageByteData(byte[] rgImg, string strType=null, string strGuid=null, int nBackgroundWritingThreadCount=0)
When enabled, saves the bytes to file and returns the file name of the binary file saved as an array ...
Definition: Database.cs:2082
bool ActivateRawImage(int nImageID, bool bActivate, bool bSave=true)
Activate/Deactivate a given image.
Definition: Database.cs:2998
void UpdateDatasetCounts(CancelEvent evtCancel, Log log, int nDatasetCreatorID, List< string > rgstrDs, string strParamNameForDescription)
Updates the dataset counts for a set of datasets.
Definition: Database.cs:5353
void LoadLabelCounts(Dictionary< int, int > rgCounts, int nSrcId=0)
Load the label counts from the database for a data source.
Definition: Database.cs:569
virtual void setImagePath(FORCE_LOAD nForceLoad)
Sets the image path member to the path used when saving binary data to the file system.
Definition: Database.cs:224
void UpdateSourceCounts(ConnectInfo ci=null)
Updates the source counts for the open data source by querying the database for the counts.
Definition: Database.cs:3988
byte[] GetRawImageDebugData(RawImage img, out int? nDebugDataFormatId)
Returns the raw debug data data of the RawImage.
Definition: Database.cs:1463
string GetLabelBoostsAsText(int nProjectId, int nSrcId=0, bool bSort=true)
Returns the Label boosts as a string.
Definition: Database.cs:990
int SetRawImageParameterAt(DateTime dt, string strName, string strValue, double? dfVal, byte[] rgData)
Set the RawImage parameter for all RawImages with the given time-stamp in the data source.
Definition: Database.cs:3623
List< LabelBoost > GetLabelBoosts(int nProjectId, bool bSort=true, int nSrcId=0)
Returns a list of all label boosts set on a project.
Definition: Database.cs:967
Database()
The Database constructor.
Definition: Database.cs:75
string GetSourceName(int nID, ConnectInfo ci=null)
Returns the name of a data source given its ID.
Definition: Database.cs:4035
int PutRawImageMean(SimpleDatum sd, bool bUpdate, int nSrcId=0, ConnectInfo ci=null)
Save the SimpleDatum as a RawImageMean in the database.
Definition: Database.cs:2607
string GetLabelCountsAsText(int nSrcId=0, ConnectInfo ci=null)
Returns the label counts for a given data source.
Definition: Database.cs:592
List< RawImage > ReindexRawImages(Log log, CancelEvent evtCancel, int nSrcId=0)
Reindex the RawImages of a data source.
Definition: Database.cs:2915
void SaveChanges()
Saves any changes on the open satabase.
Definition: Database.cs:168
List< Dataset > GetAllDatasets(int nDatasetGroupID)
Returns a list of all datasets within a group.
Definition: Database.cs:5144
void DeleteLabels(int nSrcId=0)
Delete the labels of a data source from the database.
Definition: Database.cs:733
void DeleteRawImageGroups()
Deletes all RawImage groups
Definition: Database.cs:3863
void UpdateLabelCounts(Dictionary< int, int > rgCounts, DNNEntities entities)
Updates the label counts in the database for the open data source.
Definition: Database.cs:543
void UpdateBoost(long nImageID, int nBoost)
Update the image boost of a given image.
Definition: Database.cs:2883
int PutRawImage(int nIdx, SimpleDatum d, string strDescription=null)
Save a SimpleDatum as a RawImage in the database.
Definition: Database.cs:2469
int GetImageCount(int nSrcId=0, bool bActive=true, bool bInactive=true, ConnectInfo ci=null)
Get the number of images in the source.
Definition: Database.cs:2820
void UpdateBoosts(int nSrcId, DateTime dtStart, DateTime dtEnd, int nBoost)
Update the boost of all images between a date range.
Definition: Database.cs:2899
Label FindLabelInCache(int nLabel)
Search for a Label in the label cache.
Definition: Database.cs:447
bool CopyImageMean(int nSrcIdSrc, int nSrcIdDst, ConnectInfo ci=null)
Copy the raw image mean from one source to another.
Definition: Database.cs:2662
Source GetSource(int nID, ConnectInfo ci=null)
Returns the Source entity given a data source ID.
Definition: Database.cs:4071
int PutRawImageResultExtraData(int nRawImageResultID, byte[] rgExtraData)
Set the raw image result extra data field.
Definition: Database.cs:3307
List< RawImage > QueryRawImages(params int[] rgSrcId)
Returns the list of raw images that have a source ID from a selected list.
Definition: Database.cs:1158
void PutRawImageParameters(List< ParameterData > rgParam, ConnectInfo ci=null)
Save a list of raw image parameters.
Definition: Database.cs:2368
void UpdateDatasetRelabel(int nDsID, bool bRelabel)
Update the dataset relabel flag for a dataset.
Definition: Database.cs:5286
DateTime GetFirstTimeStamp(int nSrcId=0, string strDesc=null)
Returns the first time-stamp in the data source.
Definition: Database.cs:4399
DateTime GetLastTimeStamp(out int nIndex, int nSrcId=0, string strDesc=null)
Returns the last time-stamp and index in the data source.
Definition: Database.cs:4484
byte[] GetRawImageDataCriteria(int nImgID, int? nOriginalSourceID=null)
Queries the Data Criteria for an image an Converts the raw image data criteria data which may be stor...
Definition: Database.cs:1552
string GetRawImageParameter(int nRawImageID, string strName, string strDefault)
Return the string value of a RawImage parameter.
Definition: Database.cs:3413
int GetModelGroupID(string strGroup)
Retruns the ID of a model group given its name.
Definition: Database.cs:5523
byte[] GetRawImageData(RawImage img, bool bLoadDataCriteria, bool bLoadDebugData, out byte[] rgDataCriteria, out int? nDataCriteriaFmtId, out byte[] rgDebugData, out int? nDebugDataFmtId)
Returns the raw data of the RawImage.
Definition: Database.cs:1389
void DeleteRawImageMeans(int nSrcId=0)
Delete all RawImageMeans for a data source.
Definition: Database.cs:2702
void UpdateActiveLabelByID(int nID, int nLabel)
Update the label value of a label.
Definition: Database.cs:1760
Dataset GetDataset(string strTestingSrc, string strTrainingSrc)
Returns the Dataset entity containing the training and testing source names.
Definition: Database.cs:4691
string FindDatasetNameFromSourceName(string strTrainSrc, string strTestSrc)
Searches for the data set name based on the training and testing source names.
Definition: Database.cs:4560
string GetDatabaseFilePath(string strName)
Query the physical database file path.
Definition: Database.cs:306
DatasetGroup GetDatasetGroup(int nGroupID, ConnectInfo ci=null)
Returns the DatasetGroup entity given a group ID.
Definition: Database.cs:4897
int GetSourceParameter(string strName, int nDefault, int nSrcId=0)
Return the data source parameter as an int.
Definition: Database.cs:4308
List< Dataset > GetAllDatasetsWithCreator(int nDsCreatorID, bool? bRelabeled=null)
Returns a list of all datasets within a group with dataset creators.
Definition: Database.cs:5185
string GetDatasetParameter(int nDsId, string strName)
Returns the value of a dataset parameter as a string.
Definition: Database.cs:4957
void UpdateLabelBoost(int? nTgtLbl, bool bTgtLblExact, int? nTgtBst, bool bTgtBstExact, int? nNewLbl, int? nNewBst, params int[] rgSrcId)
Update the label and boost for a given search target criteria.
Definition: Database.cs:1053
virtual string getImagePath(string strSrcName=null)
Returns the base image path used when saving binary data to the file system.
Definition: Database.cs:250
bool WaitForFileWriter(int nWait=int.MaxValue)
Wait for the file writer to complete writing all files.
Definition: Database.cs:2062
DateTime GetDatasetMinimumTimestamp(int nDsID)
Returns the minimum time-stamp for a dataset.
Definition: Database.cs:5306
void DeleteRawImageResults(int nSrcId=0)
Delete all RawImageResults for a data source.
Definition: Database.cs:2564
int PutRawImageGroup(RawImageGroup g)
Adds a RawImageGroup to the database.
Definition: Database.cs:3790
Specifies a database item used when querying boosted items.
Definition: Database.cs:5816
int ID
Specifies the image ID.
Definition: Database.cs:5858
int Boost
Specifies the image boost.
Definition: Database.cs:5910
int id
Specifies the image ID used within the lambda statement.
Definition: Database.cs:5865
int VirtualID
Specifies the image VirtualID (if any).
Definition: Database.cs:5871
DateTime? time
Specifies the image time within the lambda statement.
Definition: Database.cs:5930
string Desc
Specifies the image description.
Definition: Database.cs:5936
int Index
Specifies the image index.
Definition: Database.cs:5884
DateTime Time
Specifies the image time.
Definition: Database.cs:5923
bool Active
Specifies whether or not the item is active.
Definition: Database.cs:5962
int? OriginalSourceID
Specifies the original source ID.
Definition: Database.cs:5949
object Tag
Get/set a user defined item.
Definition: Database.cs:5849
bool? active
Specifies the active state used within the lambda statement.
Definition: Database.cs:5969
int? index
Specifies the image index used within the lambda statement.
Definition: Database.cs:5891
DbItem Clone()
Create a copy of the DbItem.
Definition: Database.cs:5830
int? virtualid
Specifies the image VirtualID used within the lambda statement.
Definition: Database.cs:5878
int? originalsrcid
Specifies the original source id used within the lambda statement.
Definition: Database.cs:5956
DbItem()
The constructor.
Definition: Database.cs:5822
int? label
Specifies the image label used within the lambda statement.
Definition: Database.cs:5904
int? boost
Specifies the image boost used within the lambda statement.
Definition: Database.cs:5917
string desc
Specifies the image description used within the lambda statement.
Definition: Database.cs:5943
int Label
Specifies the image label.
Definition: Database.cs:5897
override string ToString()
Returns the string representation of the DbItem.
Definition: Database.cs:5975
The EntitiesConnection class defines how to connect to the database via Entity Frameworks.
static DNNEntities CreateEntities(ConnectInfo ci=null)
Returns the DNNEntities to use.
static ConnectInfo GlobalDatabaseConnectInfo
Get/set the global database connection info.
The ParameterData class is used to save and retrieve parameter data.
Definition: Database.cs:5712
int SourceID
Specifies the source ID that the images belong to.
Definition: Database.cs:5765
bool OnlyAddNew
Returns whether or not to only add the parameter if it does not exist.
Definition: Database.cs:5774
int ImageID
When specified, returns the RawImage ID from which the parameter is to be queried or is associated,...
Definition: Database.cs:5806
string Value
Returns the parameter value.
Definition: Database.cs:5782
ParameterData(string strName, int nImageID)
The ParameterData constructor.
Definition: Database.cs:5747
string Name
Returns the parameter name.
Definition: Database.cs:5757
double? NumericValue
Returns the parameter numeric value.
Definition: Database.cs:5790
byte[] Data
Returns the raw data associated with the parameter.
Definition: Database.cs:5798
ParameterData(string strName, string strValue, double? dfVal=null, byte[] rgData=null, int nImageID=0, bool bOnlyAddNew=false, int nSrcId=0)
The ParameterData constructor.
Definition: Database.cs:5731
The descriptors namespace contains all descriptor used to describe various items stored within the da...
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
@ NONE
No training category specified.
The MyCaffe.db.image namespace contains all image database related classes.
Definition: Database.cs:18
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12