fork(2) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using OpenCvSharp;
  7. using OpenCvSharp.UserInterface;
  8. using OpenCvSharp.DebuggerVisualizers;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Input;
  12.  
  13. namespace Diplom1._0._0
  14. {
  15. class Program
  16. {
  17. public static FileInfo file = new FileInfo("InfoFile.txt");
  18. public static int facesFiles = 0;
  19. public static int hs = CvContour.SizeOf;
  20. public static CvMemStorage ms = new CvMemStorage(0);
  21. public static CvContourTree Grusha = null;
  22. public static int nc = 0;
  23.  
  24.  
  25. static CvSeq<CvPoint> LoadSeq(int n)
  26. {
  27. CvSeq<CvPoint> LoadSeq =
  28. Cv.Load<CvSeq<CvPoint>>("Face" + n + ".xml", ms);
  29. return LoadSeq;
  30. }
  31. static IplImage CVRoi(IplImage img, CvRect[] rectface)
  32. {
  33. if (rectface.Length == 1) img.SetROI(rectface[0]);
  34. else
  35. { img.SetROI(rectface[0]);
  36. for (int i = 0; i < rectface.Length; i++)
  37. img.SetROI(rectface[i]);
  38. return img;
  39. }
  40. return img;
  41. }
  42. static CvSeq<CvPoint> Contour(IplImage img)
  43. {
  44. IplImage img_8uc1 = new IplImage(img.Size, BitDepth.U8, 1);
  45. img.CvtColor(img_8uc1, ColorConversion.RgbToGray);
  46. IplImage img_edge = Cv.CreateImage(img_8uc1.Size, BitDepth.U8, 1);
  47. IplImage img_8uc3 = Cv.CreateImage(img_8uc1.Size, BitDepth.U8, 3);
  48. Cv.Threshold(img_8uc1, img_edge, 128, 255, ThresholdType.Binary);
  49. CvSeq<CvPoint> firstCont = null;
  50. CvContour Cvcont = new CvContour();
  51. nc = Cv.FindContours(img_edge, ms, out firstCont, hs, ContourRetrieval.List);
  52. int n = 0;
  53. CvSeq<CvPoint> d = null;
  54. for (CvSeq<CvPoint> c = firstCont; c != null; c = c.HNext)
  55. {
  56. Cv.CvtColor(img_8uc1, img_8uc3, ColorConversion.GrayToBgr);
  57. c.ApproxPoly(hs, ms, ApproxPolyMethod.DP, 1);
  58. d = c;
  59. System.Threading.Thread.Sleep(50);
  60. n++;
  61. }
  62. return d;
  63. }
  64. static IplImage SomeDrow(string s, IplImage img)
  65. {
  66. CvPoint p = new CvPoint (30,30);
  67. CvFont f = new CvFont (FontFace.HersheyComplex, 1,1);
  68. CvScalar s1 = new CvScalar(255, 0, 0);
  69. img.PutText(s, p, f, s1);
  70. return img;
  71. }
  72. static double EqualsSeq(CvSeq<CvPoint> Cont1, CvSeq<CvPoint> Cont2)
  73. {
  74.  
  75. CvContourTree TreeOne = Cv.CreateContourTree(Cont1, ms, 1);
  76. CvContourTree TreeTwo = Cv.CreateContourTree(Cont1, ms, 1);
  77. double mach = Cv.MatchContourTrees(TreeOne, TreeTwo, ContourTreesMatchMethod.I1, 3);
  78. return mach;
  79. }
  80. static CvRect[] FindFace(IplImage img) {
  81. double ScaleFactor = 1.0850;
  82. int MinNeighbors = 2;
  83. CvScalar col = new CvScalar(100, 100, 100);
  84. IplImage smallImg = new IplImage(new CvSize(Cv.Round(img.Width), Cv.Round(img.Height)), BitDepth.U8, 1);
  85. CvHaarClassifierCascade Hcc =
  86. Cv.Load<CvHaarClassifierCascade>("haarcascade_frontalface_default.xml");
  87. if (Hcc == null) { MessageBox.Show("Каскад не загружен"); return null; }
  88. CvSeq<CvAvgComp> faces =
  89. Cv.HaarDetectObjects(smallImg, Hcc, ms, ScaleFactor, MinNeighbors,
  90. HaarDetectionType.Zero, new CvSize(0, 0));
  91. if (faces.Total == 0) { MessageBox.Show("Лица не найдены"); return null; }
  92. CvRect[] rect = null;
  93. for (int i = 0; i < faces.Total; i++)
  94. {
  95. CvRect r = faces[i].Value.Rect;
  96. CvPoint center = new CvPoint { X = Cv.Round(r.X + r.Width * 0.5) , Y = Cv.Round(r.Y + r.Height * 0.5) };
  97. int radius = Cv.Round((r.Width + r.Height) * 0.25 );
  98. img.Circle(center, radius, col, 3, LineType.AntiAlias, 0);
  99. rect[i] = r;
  100. }
  101. return rect;
  102. }
  103.  
  104. static void SaveSeq(CvSeq<CvPoint> SeqToSave)
  105. {
  106. StreamWriter sw = new StreamWriter("InfoFile.txt");
  107. sw.WriteLine(facesFiles + " " + Console.ReadLine());
  108. Cv.Save(SeqToSave, "Face"+facesFiles+".xml");
  109. sw.Close();
  110. facesFiles++;
  111. }
  112. static void Main(string[] args)
  113. {
  114. int delay = 0; string fstr= " "; if (file.Exists==false) file.Create() ;
  115. StreamWriter log = new StreamWriter("log.txt");
  116. IplImage frame = null;
  117. CvCapture capture = null;
  118. double proc = 10;
  119. CvWindow windowCapture = new CvWindow("Capture");
  120. try { capture = new CvCapture(0); }
  121. catch { MessageBox.Show("Camera not found"); log.WriteLine("Camera not found at: " + DateTime.Now.ToString()); }
  122. if (capture != null)
  123. {
  124. while (true)
  125. {
  126. workpoint:
  127. capture.GrabFrame();
  128. frame = capture.RetrieveFrame();
  129. windowCapture.ShowImage(frame);
  130. int key = CvWindow.WaitKey(33); if (key == 27)
  131. { break; }
  132. if (key == 32) {
  133. CvRect[] rectface = FindFace(frame);
  134. if (rectface == null)
  135. {
  136. log.WriteLine("Error at: " + DateTime.Now.ToString());
  137. goto workpoint;
  138. }
  139. IplImage temp = CVRoi(frame, rectface);
  140. CvSeq<CvPoint> Contours = Contour(frame);
  141. SaveSeq(Contours);
  142. log.WriteLine("Face add at: "+DateTime.Now.ToString());
  143. }
  144. if (key == 13) {
  145. StreamReader sr = new StreamReader("InfoFile.txt");
  146. CvRect[] rectface = FindFace(frame);
  147. if (rectface == null) { log.WriteLine("Error at: " + DateTime.Now.ToString()); goto workpoint; }
  148. IplImage temp = CVRoi(frame, rectface);
  149. CvSeq<CvPoint> Contours = Contour(temp);
  150.  
  151. while (proc > 1)
  152. {
  153. fstr = sr.ReadLine();
  154. string str2 = "";
  155. char[] mas = fstr.ToCharArray();
  156. foreach (char h in mas)
  157. {
  158. if (char.IsDigit(h) == true)
  159. {
  160. str2 += h;
  161. }
  162. }
  163. int number = Convert.ToInt32(str2);
  164. CvSeq<CvPoint> secondSeq = LoadSeq(number);
  165. proc = EqualsSeq(Contours, secondSeq);
  166. delay++;
  167. if (delay > 50) {
  168. log.WriteLine("Worker not found at: " + DateTime.Now.ToString());
  169. delay = 0;
  170. goto workpoint;
  171. }
  172. }
  173. IplImage finalImage = SomeDrow(fstr, frame);
  174. CvWindow name = new CvWindow(fstr, finalImage);
  175. log.WriteLine("Worker {0} came at: {1}", fstr, DateTime.Now.ToString());
  176. delay = 0;
  177. MessageBox.Show("Worker found");
  178. System.Threading.Thread.Sleep(500);
  179. name.Close();
  180. sr.Close();
  181. goto workpoint;
  182.  
  183. }
  184. }
  185. }
  186. else MessageBox.Show("Cam not found ");
  187. log.Close();
  188.  
  189. }
  190. }
  191. }
  192.  
  193.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(6,7): error CS0246: The type or namespace name `OpenCvSharp' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(7,7): error CS0246: The type or namespace name `OpenCvSharp' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(8,7): error CS0246: The type or namespace name `OpenCvSharp' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(9,14): error CS0234: The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(10,14): error CS0234: The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(11,14): error CS0234: The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(20,23): error CS0246: The type or namespace name `CvMemStorage' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(21,23): error CS0246: The type or namespace name `CvContourTree' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(25,16): error CS0246: The type or namespace name `CvSeq' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(31,16): error CS0246: The type or namespace name `IplImage' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(42,16): error CS0246: The type or namespace name `CvSeq' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(64,16): error CS0246: The type or namespace name `IplImage' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(72,33): error CS0246: The type or namespace name `CvSeq' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(72,55): error CS0246: The type or namespace name `CvSeq' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(80,16): error CS0246: The type or namespace name `CvRect' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(104,29): error CS0246: The type or namespace name `CvSeq' could not be found. Are you missing a using directive or an assembly reference?
Compilation failed: 16 error(s), 0 warnings
stdout
Standard output is empty