Пример #1
0
        /// <summary>
        /// Insert a face element in FACES
        /// </summary>
        /// <author>GABRIEL</author>>
        /// <param name="face">Face object containing all the info</param>
        /// <returns>the inserted elements FaceId</returns>
        public int insert_face(Face face)
        {
            String bites = ImageToBinary(face.img.ToString());
            BCM encoder = new BCM();
            // encrypt image
            String encrypted = BinaryStringToHexString(encoder.crypt_binary(bites, 123, 456));

            /*
            // get from bits to bytes
            var byteArray = BinaryToByte(hex2binary(temp));
            // save encrypted image
            File.WriteAllBytes("nicusor2.bmp", byteArray);

            // decode entrypted text
            encoder.decrypt_text(encrypted, 123, 456);
            String decripted = encoder.getDecryptes_binary;
            // get from bits to bytes
            var byteArray2 = BinaryToByte(decripted);
            // save decripted image
            File.WriteAllBytes("nicusor3.bmp", byteArray2);
            //BitmapImage image = BinaryToImage(bites);
            */
            string query = "INSERT INTO faces (EntryId, Percentage, IsSample, Comment, Img)" +
                " VALUES (@EntryId, @Percentage, @IsSample, @Comment, @Img)";
            SqlDataReader reader = null;
            SqlCommand comm = new SqlCommand(query, db_conn);
            comm.Parameters.AddWithValue("@EntryId", face.entryId);
            comm.Parameters.AddWithValue("@Percentage", face.percentage);
            comm.Parameters.AddWithValue("@IsSample", face.isSample);
            comm.Parameters.AddWithValue("@Comment", face.comment);
            comm.Parameters.AddWithValue("@Img", encrypted);
            reader = comm.ExecuteReader();

            reader.Close();
            comm.Dispose();

            string select = "SELECT * FROM faces WHERE EntryId = @EntryId AND Percentage = @Percentage " +
                "AND IsSample = @IsSample AND Comment = @Comment AND Img = @Img";
            int FaceId = -1;
            comm = new SqlCommand(select, db_conn);
            comm.Parameters.AddWithValue("@EntryId", face.entryId);
            comm.Parameters.AddWithValue("@Percentage", face.percentage);
            comm.Parameters.AddWithValue("@IsSample", face.isSample);
            comm.Parameters.AddWithValue("@Comment", face.comment);
            comm.Parameters.AddWithValue("@Img", encrypted);
            try
            {
                FaceId = Int32.Parse(comm.ExecuteScalar().ToString());
                Console.WriteLine(FaceId);
            }
            catch (Exception e)
            {
                Console.WriteLine("Eroare SQL la {0} {1} in tabelul Entries", face.entryId, face.percentage);
                Console.WriteLine(e.Message);
            }
            reader.Close();
            comm.Dispose();
            return FaceId;
        }
Пример #2
0
        public void faceRecognitionAddEntries(string root)
        {
            Stack<string> dirs = new Stack<string>(20);
            if (!System.IO.Directory.Exists(root))
            {
                throw new ArgumentException();
            }
            dirs.Push(root);
            while (dirs.Count > 0)
            {
                string currentDir = dirs.Pop();
                string[] subDirs;
                try
                {
                    subDirs = System.IO.Directory.GetDirectories(currentDir);
                    foreach (string s in subDirs)
                    {

                        string[] files = null;
                        try
                        {
                            files = System.IO.Directory.GetFiles(s);
                        }catch (Exception e){
                            Console.WriteLine(e.Message);
                            continue;
                        }

                        //Insert entry with the name of the folder
                        String folderName = s.Substring(s.LastIndexOf('\\') + 1);
                        int EntryId = databaseConnection.insert_entry(folderName, folderName);
                        foreach (string file in files)
                        {
                            try
                            {
                                System.IO.FileInfo fi = new System.IO.FileInfo(file);
                                String imagePath = root + "\\" + folderName + "\\" + fi.Name;
                                BitmapImage image = new BitmapImage(new Uri(imagePath, UriKind.Relative));
                                Face f = new Face(-1, EntryId, 1f, false, "generic", image);

                                databaseConnection.insert_face(f);
                                Console.WriteLine("{0}: {1}, {2}", fi.Name, fi.Length, fi.CreationTime);
                            }
                            catch (System.IO.FileNotFoundException e)
                            {
                                Console.WriteLine(e.Message);
                                continue;
                            }
                        }
                        Console.WriteLine(s);
                    }
                }
                catch (UnauthorizedAccessException e)
                {
                    Console.WriteLine(e.Message);
                    continue;
                }
                catch (System.IO.DirectoryNotFoundException e)
                {
                    Console.WriteLine(e.Message);
                    continue;
                }

                dirs.Clear();
            }
        }