public static COMimageObject GetObjectById(int id)
 {
     using (var connection = new MySqlConnection(csb.ConnectionString))
     {
         COMimageObject obj = new COMimageObject();
         connection.Open();
         MySqlCommand get_object_by_id = new MySqlCommand("SELECT * FROM Objects_tbl WHERE objectId=@objId;", connection);
         get_object_by_id.Parameters.AddWithValue("objId", id);
         using (var reader = get_object_by_id.ExecuteReader())
         {
             if (reader.Read())
             {
                 obj.ObjectId = Convert.ToInt32(reader["objectId"]);
                 obj.Name     = Convert.ToString(reader["objectName"]);
                 obj.ImageID  = Convert.ToInt32(reader["imageId"]);
                 obj.VoiceURL = Convert.ToString(reader["voiceURL"]);
                 obj.X1       = Convert.ToDouble(reader["X1"]);
                 obj.X2       = Convert.ToDouble(reader["X2"]);
                 obj.X3       = Convert.ToDouble(reader["X3"]);
                 obj.X4       = Convert.ToDouble(reader["X4"]);
                 obj.Y1       = Convert.ToDouble(reader["Y1"]);
                 obj.Y2       = Convert.ToDouble(reader["Y2"]);
                 obj.Y3       = Convert.ToDouble(reader["Y3"]);
                 obj.Y4       = Convert.ToDouble(reader["Y4"]);
             }
         }
         return(obj);
     }
 }
 public static List <COMimageObject> Getobjects()
 {
     using (var connection = new MySqlConnection(csb.ConnectionString))
     {
         List <COMimageObject> list = new List <COMimageObject>();
         connection.Open();
         MySqlCommand select_all_objects = new MySqlCommand("SELECT * FROM Objects_tbl", connection);
         using (var reader = select_all_objects.ExecuteReader())
         {
             while (reader.Read())
             {
                 COMimageObject obj = new COMimageObject()
                 {
                     ImageID  = Convert.ToInt32(reader["imageId"]),
                     ObjectId = Convert.ToInt32(reader["objectId"]),
                     Name     = Convert.ToString(reader["objectName"]),
                     VoiceURL = Convert.ToString(reader["voiceURL"]),
                     X1       = Convert.ToDouble(reader["X1"]),
                     X2       = Convert.ToDouble(reader["X2"]),
                     X3       = Convert.ToDouble(reader["X3"]),
                     X4       = Convert.ToDouble(reader["X4"]),
                     Y1       = Convert.ToDouble(reader["Y1"]),
                     Y2       = Convert.ToDouble(reader["Y2"]),
                     Y3       = Convert.ToDouble(reader["Y3"]),
                     Y4       = Convert.ToDouble(reader["Y4"]),
                 };
                 list.Add(obj);
             }
         }
         return(list);
     }
 }
 public static void AddObject(COMimageObject obj)
 {
     using (var connection = new MySqlConnection(csb.ConnectionString))
     {
         Int64 objectId = nextObjectId();
         try
         {
             connection.Open();
         }
         catch (Exception)
         {
             throw;
         }
         MySqlCommand insert_table = new MySqlCommand("INSERT INTO Objects_tbl SET objectName=@ObjectName,X1=@X1,X2=@X2,X3=@X3,X4=@X4,Y1=@Y1,Y2=@Y2,Y3=@Y3,Y4=@Y4,voiceURL=@VoiceURL,objectId=@ObjectId,imageId=(SELECT imageId FROM Images_tbl WHERE imageId=@imgId);", connection);
         insert_table.Parameters.AddWithValue("ObjectName", obj.Name);
         insert_table.Parameters.AddWithValue("X1", obj.X1);
         insert_table.Parameters.AddWithValue("X2", obj.X2);
         insert_table.Parameters.AddWithValue("X3", obj.X3);
         insert_table.Parameters.AddWithValue("X4", obj.X4);
         insert_table.Parameters.AddWithValue("Y1", obj.Y1);
         insert_table.Parameters.AddWithValue("Y2", obj.Y2);
         insert_table.Parameters.AddWithValue("Y3", obj.Y3);
         insert_table.Parameters.AddWithValue("Y4", obj.Y4);
         insert_table.Parameters.AddWithValue("VoiceURL", obj.VoiceURL);
         insert_table.Parameters.AddWithValue("ObjectId", objectId);
         insert_table.Parameters.AddWithValue("imgId", obj.ImageID);
         insert_table.ExecuteNonQuery();
         connection.Close();
     }
 }
        public IHttpActionResult GetObjectById(int id)
        {
            COMimageObject obj = BLLobject.GetObjectById(id);

            if (obj == null)
            {
                return(BadRequest("object does not exist"));
            }
            return(Ok(obj));
        }
        public IHttpActionResult PostObject([FromBody] COMimageObject obj)
        {
            COMimageObject o = BLLobject.GetObjectById(obj.ObjectId);

            if (o != null)
            {
                return(BadRequest("object already exist"));
            }
            BLLobject.AddObject(obj);
            return(Ok());
        }
        //send image to vision api and return all objects detected
        //wirhout insert them into database
        public static List <COMimageObject> CustomVisionApi(COMimage img, string base64)
        {
            //Directory.GetCurrentDirectory() +
            Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"C:\keys\wordproject-29b2e0d3e0d5.json");
            // Instantiates a client
            var client = ImageAnnotatorClient.Create();

            // Load the image file into memory
            byte[] byteBuffer             = Convert.FromBase64String(base64);
            System.Drawing.Bitmap bitmap1 = null;
            //convert byte[] to bitmap
            MemoryStream memoryStream = new MemoryStream(byteBuffer);

            memoryStream.Position = 0;
            bitmap1 = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(memoryStream);
            memoryStream.Close();
            memoryStream = null;
            // byteBuffer = null;
            //rotate the image-bitmap , return byte[]
            //  var res = RotateImage(bitmap1);
            var image = Image.FromBytes(byteBuffer);//convert to image
            // Performs label detection on the image file
            var response = client.DetectLocalizedObjects(image);
            List <COMimageObject> objects = new List <COMimageObject>();

            foreach (var annotation in response)
            {
                COMimageObject obj = new COMimageObject();
                obj.ImageID  = img.ImageID;
                obj.Name     = annotation.Name;
                obj.VoiceURL = "";
                obj.X1       = annotation.BoundingPoly.NormalizedVertices[0].X;
                obj.X2       = annotation.BoundingPoly.NormalizedVertices[1].X;
                obj.X3       = annotation.BoundingPoly.NormalizedVertices[2].X;
                obj.X4       = annotation.BoundingPoly.NormalizedVertices[3].X;
                obj.Y1       = annotation.BoundingPoly.NormalizedVertices[0].Y;
                obj.Y2       = annotation.BoundingPoly.NormalizedVertices[1].Y;
                obj.Y3       = annotation.BoundingPoly.NormalizedVertices[2].Y;
                obj.Y4       = annotation.BoundingPoly.NormalizedVertices[3].Y;
                objects.Add(obj);
            }
            return(objects);
        }
 public static void AddObject(COMimageObject obj)
 {
     DALimageObject.AddObject(obj);
 }
        public static List <string> VisionApi(int categoryId, int UserId, string URL, Dictionary <string, int> categoriesCounter, Dictionary <string, int> voicesCounter)
        {
            Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"C:\keys\wordproject-29b2e0d3e0d5.json");
            // Instantiates a client
            var client = ImageAnnotatorClient.Create();
            // Load the image file into memory
            var image = Image.FromFile(URL);
            // Performs label detection on the image file
            var response = client.DetectLocalizedObjects(image);//
            //found most common word in objects list
            Dictionary <string, int> countingDic = new Dictionary <string, int>();
            string common = string.Empty;

            foreach (var annotation in response)
            {
                if (countingDic.ContainsKey(annotation.Name))
                {
                    int max = 1;
                    countingDic[annotation.Name]++;
                    if (max < countingDic[annotation.Name])
                    {
                        max    = countingDic[annotation.Name];
                        common = annotation.Name;
                    }
                }
                else
                {
                    countingDic.Add(annotation.Name, 1);
                }
            }
            COMimage      img = new COMimage();
            string        imgUrl;
            List <string> ans   = new List <string>();
            int           c     = 0;
            int           imgId = -1;

            try
            {
                imgUrl = Storage(categoryId, URL, categoriesCounter);
                // if image in storage
                //insert image info db
                img.CategoryID = categoryId;
                img.URL        = imgUrl;
                img.UserId     = UserId;
                //DALimageObject.Refresh();
                img.BeginIndex = BLLobject.GetObjects().Count;
                DALimage.Addimage(img);
                imgId = DALimage.GetImageIdByURL(img.URL);
                //insert objects into db
                Dictionary <Bounding, string> finalList = new Dictionary <Bounding, string>();
                foreach (var annotation in response)
                {
                    List <double> x = new List <double>();
                    List <double> y = new List <double>();
                    foreach (var item in annotation.BoundingPoly.NormalizedVertices)
                    {
                        x.Add(item.X);
                        y.Add(item.Y);
                    }
                    bool     help     = false;
                    Bounding bounding = new Bounding(x, y);
                    //if there is item with same bounding box and the existing is common name and the new not- swap them.
                    foreach (var item in finalList)
                    {
                        if (item.Key.IsEqual(bounding))
                        {
                            help = true;
                            if (item.Value == common && annotation.Name != common)
                            {
                                finalList.Remove(item.Key);
                                finalList.Add(bounding, annotation.Name);
                            }
                        }
                    }
                    if (help == false)
                    {
                        finalList.Add(bounding, annotation.Name);
                    }
                    else
                    {
                        help = true;
                    }
                }
                foreach (var item in finalList)
                {
                    COMimageObject obj = new COMimageObject();
                    obj.ImageID = imgId;
                    obj.Name    = item.Value;
                    obj.X1      = item.Key.X1;
                    obj.X2      = item.Key.X2;
                    obj.X3      = item.Key.X3;
                    obj.X4      = item.Key.X4;
                    obj.Y1      = item.Key.Y1;
                    obj.Y2      = item.Key.Y2;
                    obj.Y3      = item.Key.Y3;
                    obj.Y4      = item.Key.Y4;
                    try
                    {
                        obj.VoiceURL = BLLtextToSpeach.VoiceStorage(UserId, BLLimage.GetImageById(obj.ImageID).CategoryID, BLLtextToSpeach.TextToSpeach(obj.Name), voicesCounter);
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                    DALimageObject.AddObject(obj);
                    c++;
                    ans.Add(obj.Name);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return(ans);
        }