using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using OpenCvSharp;
using OpenCvSharp.UserInterface;
using OpenCvSharp.DebuggerVisualizers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace Diplom1._0._0
{
class Program
{
public static FileInfo file = new FileInfo("InfoFile.txt");
public static int facesFiles = 0;
public static int hs = CvContour.SizeOf;
public static CvMemStorage ms = new CvMemStorage(0);
public static CvContourTree Grusha = null;
public static int nc = 0;
static CvSeq<CvPoint> LoadSeq(int n)
{
CvSeq<CvPoint> LoadSeq =
Cv.Load<CvSeq<CvPoint>>("Face" + n + ".xml", ms);
return LoadSeq;
}
static IplImage CVRoi(IplImage img, CvRect[] rectface)
{
if (rectface.Length == 1) img.SetROI(rectface[0]);
else
{ img.SetROI(rectface[0]);
for (int i = 0; i < rectface.Length; i++)
img.SetROI(rectface[i]);
return img;
}
return img;
}
static CvSeq<CvPoint> Contour(IplImage img)
{
IplImage img_8uc1 = new IplImage(img.Size, BitDepth.U8, 1);
img.CvtColor(img_8uc1, ColorConversion.RgbToGray);
IplImage img_edge = Cv.CreateImage(img_8uc1.Size, BitDepth.U8, 1);
IplImage img_8uc3 = Cv.CreateImage(img_8uc1.Size, BitDepth.U8, 3);
Cv.Threshold(img_8uc1, img_edge, 128, 255, ThresholdType.Binary);
CvSeq<CvPoint> firstCont = null;
CvContour Cvcont = new CvContour();
nc = Cv.FindContours(img_edge, ms, out firstCont, hs, ContourRetrieval.List);
int n = 0;
CvSeq<CvPoint> d = null;
for (CvSeq<CvPoint> c = firstCont; c != null; c = c.HNext)
{
Cv.CvtColor(img_8uc1, img_8uc3, ColorConversion.GrayToBgr);
c.ApproxPoly(hs, ms, ApproxPolyMethod.DP, 1);
d = c;
System.Threading.Thread.Sleep(50);
n++;
}
return d;
}
static IplImage SomeDrow(string s, IplImage img)
{
CvPoint p = new CvPoint (30,30);
CvFont f = new CvFont (FontFace.HersheyComplex, 1,1);
CvScalar s1 = new CvScalar(255, 0, 0);
img.PutText(s, p, f, s1);
return img;
}
static double EqualsSeq(CvSeq<CvPoint> Cont1, CvSeq<CvPoint> Cont2)
{
CvContourTree TreeOne = Cv.CreateContourTree(Cont1, ms, 1);
CvContourTree TreeTwo = Cv.CreateContourTree(Cont1, ms, 1);
double mach = Cv.MatchContourTrees(TreeOne, TreeTwo, ContourTreesMatchMethod.I1, 3);
return mach;
}
static CvRect[] FindFace(IplImage img) {
double ScaleFactor = 1.0850;
int MinNeighbors = 2;
CvScalar col = new CvScalar(100, 100, 100);
IplImage smallImg = new IplImage(new CvSize(Cv.Round(img.Width), Cv.Round(img.Height)), BitDepth.U8, 1);
CvHaarClassifierCascade Hcc =
Cv.Load<CvHaarClassifierCascade>("haarcascade_frontalface_default.xml");
if (Hcc == null) { MessageBox.Show("Каскад не загружен"); return null; }
CvSeq<CvAvgComp> faces =
Cv.HaarDetectObjects(smallImg, Hcc, ms, ScaleFactor, MinNeighbors,
HaarDetectionType.Zero, new CvSize(0, 0));
if (faces.Total == 0) { MessageBox.Show("Лица не найдены"); return null; }
CvRect[] rect = null;
for (int i = 0; i < faces.Total; i++)
{
CvRect r = faces[i].Value.Rect;
CvPoint center = new CvPoint { X = Cv.Round(r.X + r.Width * 0.5) , Y = Cv.Round(r.Y + r.Height * 0.5) };
int radius = Cv.Round((r.Width + r.Height) * 0.25 );
img.Circle(center, radius, col, 3, LineType.AntiAlias, 0);
rect[i] = r;
}
return rect;
}
static void SaveSeq(CvSeq<CvPoint> SeqToSave)
{
StreamWriter sw = new StreamWriter("InfoFile.txt");
sw.WriteLine(facesFiles + " " + Console.ReadLine());
Cv.Save(SeqToSave, "Face"+facesFiles+".xml");
sw.Close();
facesFiles++;
}
static void Main(string[] args)
{
int delay = 0; string fstr= " "; if (file.Exists==false) file.Create() ;
StreamWriter
log = new StreamWriter
("log.txt"); IplImage frame = null;
CvCapture capture = null;
double proc = 10;
CvWindow windowCapture = new CvWindow("Capture");
try { capture = new CvCapture(0); }
catch
{ MessageBox.
Show("Camera not found"); log.
WriteLine("Camera not found at: " + DateTime.
Now.
ToString()); } if (capture != null)
{
while (true)
{
workpoint:
capture.GrabFrame();
frame = capture.RetrieveFrame();
windowCapture.ShowImage(frame);
int key = CvWindow.WaitKey(33); if (key == 27)
{ break; }
if (key == 32) {
CvRect[] rectface = FindFace(frame);
if (rectface == null)
{
log.
WriteLine("Error at: " + DateTime.
Now.
ToString()); goto workpoint;
}
IplImage temp = CVRoi(frame, rectface);
CvSeq<CvPoint> Contours = Contour(frame);
SaveSeq(Contours);
log.
WriteLine("Face add at: "+DateTime.
Now.
ToString()); }
if (key == 13) {
StreamReader sr = new StreamReader("InfoFile.txt");
CvRect[] rectface = FindFace(frame);
if (rectface
== null) { log.
WriteLine("Error at: " + DateTime.
Now.
ToString()); goto workpoint
; } IplImage temp = CVRoi(frame, rectface);
CvSeq<CvPoint> Contours = Contour(temp);
while (proc > 1)
{
fstr = sr.ReadLine();
string str2 = "";
char[] mas = fstr.ToCharArray();
foreach (char h in mas)
{
if (char.IsDigit(h) == true)
{
str2 += h;
}
}
int number = Convert.ToInt32(str2);
CvSeq<CvPoint> secondSeq = LoadSeq(number);
proc = EqualsSeq(Contours, secondSeq);
delay++;
if (delay > 50) {
log.
WriteLine("Worker not found at: " + DateTime.
Now.
ToString()); delay = 0;
goto workpoint;
}
}
IplImage finalImage = SomeDrow(fstr, frame);
CvWindow name = new CvWindow(fstr, finalImage);
log.
WriteLine("Worker {0} came at: {1}", fstr
, DateTime.
Now.
ToString()); delay = 0;
MessageBox.Show("Worker found");
System.Threading.Thread.Sleep(500);
name.Close();
sr.Close();
goto workpoint;
}
}
}
else MessageBox.Show("Cam not found ");
}
}
}