/// <summary>
		/// Обновляет свойства InputMatrix и OutputMatrix новыми значениями, полученными из colorMatrix. 
		/// </summary>
		public void UpdateImages(IImageMatrix colorMatrix)
		{
			if(colorMatrix != null)
			{
				InputMatrix = colorMatrix.ToMat();
				OutputMatrix = InputMatrix.Clone();
			}
		}
		/// <summary>
		/// Обновляет свойства InputMatrix и OutputMatrix новыми значениями, полученными из bitmap. 
		/// </summary>
		public void UpdateImages(System.Drawing.Bitmap bitmap)
		{
			if(bitmap != null)
			{
				Mat mat = BitmapConverter.ToMat(bitmap);

				InputMatrix = mat;
				OutputMatrix = InputMatrix.Clone();
			}
		}
Exemple #3
0
    public bool HW1()
    {
        string dbgout = "";

        DebugLog.Log("HW1 - Simple");
        InputMatrix  in1  = new InputMatrix(5);
        SubZone      sz1  = new SubZone(5);
        OutputMatrix out1 = new OutputMatrix(5);

        in1.setUpSubZone(sz1);
        sz1.setDownSubZones(in1);
        sz1.setUpSubZone(out1);
        out1.addInputs(sz1);
        DebugLog.Log("Learn");
        bool[][] in_p1 = new bool[][] {
            new bool[] { true, false, false, false, false },
            new bool[] { false, true, false, false, false },
            new bool[] { false, false, true, false, false },
            new bool[] { false, false, false, true, false },
            new bool[] { false, false, false, false, true },
        };
        for (int i = 0; i < in_p1.Length; i++)
        {
            in1.setBooleans(in_p1[i]);
            sz1.setColumnNeurons(i, in1.inputs);
        }
        for (int n = 0; n < in_p1.Length; n++)
        {
            out1.reset();
            DebugLog.Log("Analyse " + n + " pattern");
            in1.setBooleans(in_p1[n]);
            dbgout = "";
            for (int i = 0; i < in1.inputs.Length; i++)
            {
                dbgout += (in1.inputs[i].active > 0.8f ? "A" : "_") + " ";
            }
            DebugLog.Log("Pattern : " + dbgout);
            in1.sendSignals();
            sz1.analyze();
            dbgout = "";
            for (int i = 0; i < out1.inputs.Length; i++)
            {
                dbgout += (out1.inputsActives[i] > 0.8f ? "A" : "_") + " ";
            }
            DebugLog.Log("Columns : " + dbgout);
            out1.reset();
        }
        //sz1.teach();
        return(true);
    }
		//
		/**/

		/// <summary> 
		/// Создаёт объект <see cref="FaceDetector"/>
		/// </summary>
		/// <param name="colorMatrix"> Цветная матрица изображения. </param>
		public FaceDetector(ColorMatrix colorMatrix)
		{
			Classifier = new CascadeClassifier(
				Path.Combine(
					Directory.GetCurrentDirectory(),
					"Samples",
					"haarcascade_frontalface_default.xml"));

			//getting bitmap from colorMatrix
			System.Drawing.Bitmap bitmap = colorMatrix.CreateBitmap();

			//converting System.Drawing.Bitmap -> OpenCVSharp.CPlusPlus.Mat
			Mat mat = BitmapConverter.ToMat(bitmap);

			InputMatrix = mat;
			OutputMatrix = InputMatrix.Clone();

			FacesRepository = new List<Mat>();
		}
		/// <summary> 
		/// Создаёт объект <see cref="FaceDetector"/>
		/// </summary>
		/// <param name="videoImage"> Изображение. </param>
		public FaceDetector(VideoImage videoImage)
		{
			Classifier = new CascadeClassifier(
				Path.Combine(
					Directory.GetCurrentDirectory(),
					"Samples",
					"haarcascade_frontalface_default.xml"));

			//creating bitmap
			System.Drawing.Bitmap bitmap = videoImage.Matrix.CreateBitmap();

			//converting System.Drawing.Bitmap -> OpenCVSharp.CPlusPlus.Mat
			if(bitmap != null) { 
				Mat mat = BitmapConverter.ToMat(bitmap);
				InputMatrix = mat;
				OutputMatrix = InputMatrix.Clone();
			}

			FacesRepository = new List<Mat>();
		}
Exemple #6
0
        public static void DrawMatrix(List <Cart> carts)
        {
            for (var x = 0; x < InputMatrix.GetLength(0); x++)
            {
                for (var y = 0; y < InputMatrix.GetLength(1); y++)
                {
                    var cart = carts.FirstOrDefault(c => c.Point.X == x && c.Point.Y == y);
                    if (cart is null)
                    {
                        Console.Write(InputMatrix[x, y]);
                    }
                    else
                    {
                        Console.Write(cart.Orientation);
                    }
                }

                Console.WriteLine();
            }
        }
Exemple #7
0
        private void вводМатрицыToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (вводМатрицыToolStripMenuItem.Checked)
            {
                InputMatrix inputMatrix = new InputMatrix();
                inputMatrix.MdiParent = this;

                inputMatrix.Show();
            }
            else
            {
                bool fl = true;
                for (int i = 0; fl && i < this.MdiChildren.Length; i++)
                {
                    if (MdiChildren[i] is InputMatrix)
                    {
                        MdiChildren[i].Close();
                        fl = false;
                    }
                }
            }
        }
		/// <summary>
		/// Обнаружение лиц на изображении.
		/// </summary>
		/// <returns>
		/// Возвращает массивы прямоугольников <seealso cref="Rect"/> обнаруженных лиц. 
		/// </returns>
		public Rect[] DetectFaces()
		{
			// Detect face rectangles
			DetectedFaces = Classifier.DetectMultiScale(
				image: InputMatrix,
				scaleFactor: 1.35,
				minNeighbors: 5,
				flags: HaarDetectionType.ScaleImage,
				minSize: new Size(30, 30),
				maxSize: InputMatrix.Size());


			foreach(var faceRect in DetectedFaces)
			{
				Mat face = new Mat(InputMatrix, roi: faceRect)
					.ResizeImage(IMG_WIDTH, IMG_HEIGHT)
					.ConvertToGray();

				FacesRepository.Add(face);
			}			

			return DetectedFaces;
		}
Exemple #9
0
        public static void Main(string[] args)
        {
            /*
             * XorShift160 lol = new XorShift160();
             * double runningSum = 0;
             *
             * while (!Console.KeyAvailable)
             * {
             *  Console.WriteLine(lol.NextDouble());
             * }
             * return;
             */

            IStateMatrix A = new StateMatrix(2, 2);

            A.SetValue(0, 0, 0);
            A.SetValue(0, 1, 1);
            A.SetValue(1, 0, 0);
            A.SetValue(1, 0, 0);

            /*
             * var c = new CompiledMatrix<IStateMatrix>(A);
             * c.Compile();
             */

            IInputMatrix B = new InputMatrix(2, 1);

            B.SetValue(0, 0, 0);
            B.SetValue(1, 0, 1);

            IOutputMatrix C = new OutputMatrix(2, 2);

            C.SetValue(0, 0, 1);
            C.SetValue(0, 1, 0);
            C.SetValue(1, 0, 0);
            C.SetValue(1, 1, 1);

            IFeedthroughMatrix D = new FeedthroughMatrix(2, 1);

            D.SetValue(0, 0, 0);
            D.SetValue(1, 0, 0);

            ControlVector u = new ControlVector(1);

            u.SetValue(0, 1);
            // TODO: Construct vectors and matrices by factory and let the simulation driver compile them after every external change

            IStateVector x = new StateVector(2);

            /*
             * IStateVector dx = new StateVector(x.Length);
             * IStateVector dxu = new StateVector(x.Length);
             * IOutputVector y = new OutputVector(C.Rows);
             * IOutputVector yu = new OutputVector(C.Rows);
             */

            CancellationTokenSource cts = new CancellationTokenSource();
            CancellationToken       ct  = cts.Token;

            Semaphore startCalculation = new Semaphore(0, 2);
            Semaphore calculationDone  = new Semaphore(0, 2);

            SimulationTime simulationTime = new SimulationTime();

            IStateVector dx           = new StateVector(x.Length);
            Task         inputToState = new Task(() =>
            {
                IStateVector dxu = new StateVector(x.Length);
                while (!ct.IsCancellationRequested)
                {
                    startCalculation.WaitOne();
                    Thread.MemoryBarrier();

                    A.Transform(x, ref dx);
                    B.Transform(u, ref dxu);                                  // TODO: TransformAndAdd()
                    dx.AddInPlace(dxu);

                    Thread.MemoryBarrier();
                    calculationDone.Release();
                }
            });

            IOutputVector y             = new OutputVector(C.Rows);
            Task          stateToOutput = new Task(() =>
            {
                IOutputVector yu = new OutputVector(C.Rows);
                while (!ct.IsCancellationRequested)
                {
                    startCalculation.WaitOne();
                    Thread.MemoryBarrier();

                    C.Transform(x, ref y);
                    D.Transform(u, ref yu);                                   // TODO: TransformAndAdd()
                    y.AddInPlace(yu);

                    Thread.MemoryBarrier();
                    calculationDone.Release();
                }
            });

            Task control = new Task(() =>
            {
                Stopwatch watch = Stopwatch.StartNew();
                int steps       = 0;

                while (!ct.IsCancellationRequested)
                {
                    // wait for a new u to be applied
                    // TODO: apply control vector
                    startCalculation.Release(2);

                    // wait for y
                    calculationDone.WaitOne();
                    calculationDone.WaitOne();
                    Thread.MemoryBarrier();

                    // wait for state vector to be changeable
                    // TODO: perform real transformation
                    x.AddInPlace(dx);                             // discrete integration, T=1

                    // video killed the radio star
                    if (steps % 1000 == 0)
                    {
                        var localY    = y;
                        double thingy = steps / watch.Elapsed.TotalSeconds;
                        Trace.WriteLine(simulationTime.Time + " Position: " + localY.GetValue(0) + ", Velocity: " + localY.GetValue(1) + ", Acceleration: " + u.GetValue(0) + ", throughput: " + thingy);
                    }

                    // cancel out acceleration
                    if (steps++ == 10)
                    {
                        u.SetValue(0, 0);
                    }

                    // advance simulation time
                    simulationTime.Add(TimeSpan.FromSeconds(1));
                }
            });

            inputToState.Start();
            stateToOutput.Start();
            control.Start();

            Console.ReadKey(true);
            cts.Cancel();

            /*
             * while (!Console.KeyAvailable)
             * {
             *  A.Transform(x, ref dx);
             *  B.Transform(u, ref dxu); // TODO: TransformAndAdd()
             *  dx.AddInPlace(dxu);
             *
             *  // TODO: perform transformation
             *  x.AddInPlace(dx); // discrete integration, T=1
             *
             *  C.Transform(x, ref y);
             *  D.Transform(u, ref yu); // TODO: TransformAndAdd()
             *  y.AddInPlace(yu);
             *
             *  // video killed the radio star
             *  if (steps%1000 == 0)
             *  {
             *      Trace.WriteLine("Position: " + y[0] + ", Velocity: " + y[1] + ", Acceleration: " + u[0] + ", throughput: " + steps/watch.Elapsed.TotalSeconds);
             *  }
             *
             *  // cancel out acceleration
             *  if (steps++ == 10)
             *  {
             *      u[0] = 0;
             *  }
             * }
             */
        }
Exemple #10
0
 public void InputMatrix(string key, InputMatrix inputMatrix)
 {
     InputMatrices[key] = inputMatrix;
 }
Exemple #11
0
    public void HW2()
    {
        string dbgout = "";

        DebugLog.Log("HW2 - Sequence");
        InputMatrix  in1  = new InputMatrix(5);
        SubZone      sz1  = new SubZone(5);
        OutputMatrix out1 = new OutputMatrix(5);

        in1.setUpSubZone(sz1);
        sz1.setDownSubZones(in1);
        sz1.setUpSubZone(out1);
        out1.addInputs(sz1);
        DebugLog.Log("Learn");
        bool[][] in_p1 = new bool[][] {
            new bool[] { true, true, false, false, false },
            new bool[] { false, true, true, false, false },
        };
        for (int i = 0; i < in_p1.Length; i++)
        {
            in1.setBooleans(in_p1[i]);
            sz1.setColumnNeurons(0, in1.inputs);
        }
        bool[][] in_p2 = new bool[][] {
            new bool[] { false, false, true, true, false },
            new bool[] { false, false, false, true, true },
        };
        for (int i = 0; i < in_p2.Length; i++)
        {
            in1.setBooleans(in_p2[i]);
            sz1.setColumnNeurons(1, in1.inputs);
        }
        bool[][] in_p3 = new bool[][] {
            new bool[] { false, false, true, true, false },
            new bool[] { false, true, true, false, false },
            new bool[] { true, true, false, false, false },
        };
        for (int i = 0; i < in_p3.Length; i++)
        {
            in1.setBooleans(in_p3[i]);
            sz1.setColumnNeurons(2, in1.inputs);
        }
        bool[][] in_p4 = new bool[][] {
            new bool[] { true, false, false, false, true },
            new bool[] { false, true, false, true, false },
            new bool[] { false, false, true, false, false },
        };
        for (int i = 0; i < in_p4.Length; i++)
        {
            in1.setBooleans(in_p4[i]);
            sz1.setColumnNeurons(3, in1.inputs);
        }
        bool[][] in_p5 = new bool[][] {
            new bool[] { true, false, false, false, false },
            new bool[] { false, true, false, false, false },
            new bool[] { false, false, true, false, false },
            new bool[] { false, false, false, true, false },
            new bool[] { false, false, false, false, true },
        };
        for (int i = 0; i < in_p5.Length; i++)
        {
            in1.setBooleans(in_p5[i]);
            sz1.setColumnNeurons(4, in1.inputs);
        }
        bool[][] in_p = new bool[][] {
            new bool[] { true, false, false, false, false }, // 5
            new bool[] { false, true, false, false, false }, // 5
            new bool[] { false, false, true, false, false }, // 5
            new bool[] { false, false, false, true, false }, // 5
            new bool[] { false, false, false, false, true }, // 5
            new bool[] { false, false, false, true, true },  // _
            new bool[] { false, false, true, true, false },  // 2, 3
            new bool[] { false, true, true, false, false },  // 3
            new bool[] { true, true, false, false, false },  // 3, 1
            new bool[] { false, true, true, false, false },  // 1
            new bool[] { false, false, true, true, false },  // 2
            new bool[] { false, false, false, true, true },  // 2
            new bool[] { true, false, false, false, true },  // 4
            new bool[] { false, true, false, true, false },  // 4
            new bool[] { false, false, true, false, false }, // 4
            new bool[] { false, false, false, true, false }, // _
            new bool[] { false, false, false, false, true }, // _
            new bool[] { true, false, false, false, false }, // 5
            new bool[] { false, true, false, false, false }, // 5
            new bool[] { false, false, true, false, false }, // 5
            new bool[] { true, false, false, false, true },  // 4
            new bool[] { false, true, false, true, false },  // 4
            new bool[] { false, false, true, true, false },  // 3
            new bool[] { false, true, true, false, false },  // 3
            new bool[] { true, true, false, false, false },  // 3
            new bool[] { true, false, false, false, true },  // 4
            new bool[] { true, true, false, false, false },  // 1
            new bool[] { false, true, true, false, false },  // 1
            new bool[] { false, false, true, true, false },  // 3
            new bool[] { false, false, false, true, true },  // _
        };
        bool[][] in_e = new bool[][] {
            new bool[] { false, false, false, false, true },  // 5
            new bool[] { false, false, false, false, true },  // 5
            new bool[] { false, false, false, false, true },  // 5
            new bool[] { false, false, false, false, true },  // 5
            new bool[] { false, false, false, false, true },  // 5
            new bool[] { false, false, false, false, false }, // _
            new bool[] { false, true, true, false, false },   // 2, 3
            new bool[] { false, false, true, false, false },  // 3
            new bool[] { true, false, true, false, false },   // 3, 1
            new bool[] { true, false, false, false, false },  // 1
            new bool[] { false, true, false, false, true },   // 2, 5
            new bool[] { false, true, false, false, false },  // 2
            new bool[] { false, false, false, true, false },  // 4
            new bool[] { false, false, false, true, false },  // 4
            new bool[] { false, false, false, true, false },  // 4
            new bool[] { false, false, false, false, false }, // _
            new bool[] { false, false, false, false, false }, // _
            new bool[] { false, false, false, false, true },  // 5
            new bool[] { false, false, false, false, true },  // 5
            new bool[] { false, false, false, false, true },  // 5
            new bool[] { false, false, false, true, false },  // 4
            new bool[] { false, false, false, true, false },  // 4
            new bool[] { false, true, true, false, false },   // 3, 2
            new bool[] { false, false, true, false, false },  // 3
            new bool[] { false, false, true, false, false },  // 3
            new bool[] { false, false, false, true, false },  // 4
            new bool[] { true, false, false, false, false },  // 1
            new bool[] { true, false, false, false, false },  // 1
            new bool[] { false, true, false, false, false },  // 2
            new bool[] { false, true, false, false, false },  // 2
        };
        for (int n = 0; n < in_p.Length; n++)
        {
            DebugLog.Log("Analyse " + n + " pattern");
            in1.setBooleans(in_p[n]);
            dbgout = "";
            for (int i = 0; i < in1.inputs.Length; i++)
            {
                dbgout += (in1.inputs[i].active > 0.8f ? "A" : "_") + " ";
            }
            DebugLog.Log("Pattern : " + dbgout);
            dbgout = "";
            for (int i = 0; i < in1.inputs.Length; i++)
            {
                dbgout += (in_e[n][i] ? "A" : "_") + " ";
            }
            DebugLog.Log("Etalon : " + dbgout);
            in1.sendSignals();
            sz1.analyze();
            dbgout = "";
            for (int i = 0; i < out1.inputs.Length; i++)
            {
                dbgout += (out1.inputsActives[i] > 0.8f ? "A" : "_") + " ";
            }
            DebugLog.Log("Columns : " + dbgout);
            dbgout = "";
            for (int i = 0; i < out1.inputs.Length; i++)
            {
                dbgout += (in1.inputs[i].prediction >= 0.5f ? "A" : "_") + " ";
            }
            DebugLog.Log("Prediction : " + dbgout);
            out1.reset();
        }
        //sz1.teach();
    }
Exemple #12
0
    public void HW4()
    {
        string dbgout = "";

        DebugLog.Log("HW4 - Motor");
        InputMatrix  in1  = new InputMatrix(5);
        SubZone      sz1  = new SubZone(5);
        OutputMatrix out1 = new OutputMatrix(5);

        in1.setUpSubZone(sz1);
        sz1.setDownSubZones(in1);
        sz1.setUpSubZone(out1);
        out1.setDownSubZones(sz1);
        out1.addInputs(sz1);
        DebugLog.Log("Learn");

        bool[][] in_p2 = new bool[][] {
            new bool[] { false, false, true, true, false },
            new bool[] { false, false, false, true, true },
        };
        for (int i = 0; i < in_p2.Length; i++)
        {
            in1.setBooleans(in_p2[i]);
            sz1.setColumnNeurons(1, in1.inputs);
        }
        bool[][] in_p3 = new bool[][] {
            new bool[] { false, false, true, true, false },
            new bool[] { false, true, true, false, false },
            new bool[] { true, true, false, false, false },
        };
        for (int i = 0; i < in_p3.Length; i++)
        {
            in1.setBooleans(in_p3[i]);
            sz1.setColumnNeurons(2, in1.inputs);
        }

        bool[][] in_p = new bool[][] {
            new bool[] { false, false, false, true, false }, // _
            new bool[] { false, false, true, true, false },  // 2,3
        };

        for (int n = 0; n < in_p.Length; n++)
        {
            DebugLog.Log("Analyse " + n + " pattern");
            in1.setBooleans(in_p[n]);
            dbgout = "";
            for (int i = 0; i < in1.inputs.Length; i++)
            {
                dbgout += (in1.inputs[i].active > 0.8f ? "A" : "_") + " ";
            }
            DebugLog.Log("Pattern : " + dbgout);
            in1.sendSignals();
            sz1.analyze();
            dbgout = "";
            for (int i = 0; i < out1.inputs.Length; i++)
            {
                dbgout += (out1.inputsActives[i] > 0.8f ? "A" : "_") + " ";
            }
            DebugLog.Log("Columns : " + dbgout);
            dbgout = "";
            for (int i = 0; i < out1.inputs.Length; i++)
            {
                dbgout += (in1.inputs[i].prediction >= 0.5f ? "A" : "_") + " ";
            }
            DebugLog.Log("Prediction : " + dbgout);
            for (int p = 0; p < 5; p++)
            {
                DebugLog.Log("Motor " + p + " pattern");
                out1.outSignalMotor(p);
                dbgout = "";
                for (int i = 0; i < in1.inputs.Length; i++)
                {
                    dbgout += (in1.getMotorValues()[i] >= 0.5f ? "A" : "_") + " ";
                }
                DebugLog.Log("Motor : " + dbgout);
                in1.reset();
            }
            out1.reset();
        }
        sz1.onDeactive();
    }