//get list of images return for each image list of objects
        public static List <ImageWithObject> CreateMat(List <COMimage> imageList, bool IsAllImages = false)
        {
            if (imageList.Count() <= 0)
            {
                return(null);
            }
            //answer list
            List <ImageWithObject> ImagesAndObjectsMat = new List <ImageWithObject>();
            //list with all objects of all images from all categories
            List <COMimageObject> ObjList = DALimageObject.Getobjects();

            foreach (COMimage img in imageList)
            {
                ImageWithObject imageAndObject = new ImageWithObject();
                imageAndObject.image            = new COMimage();
                imageAndObject.imageObjects     = new List <COMimageObject>();
                imageAndObject.image.ImageID    = img.ImageID;
                imageAndObject.image.URL        = img.URL;
                imageAndObject.image.BeginIndex = img.BeginIndex;
                imageAndObject.image.EndIndex   = img.EndIndex;
                imageAndObject.image.CategoryID = img.CategoryID;
                List <COMimageObject> objs = BLLobject.GetObjects().FindAll(obj => obj.ImageID == img.ImageID);
                objs.ForEach(obj => imageAndObject.imageObjects.Add(obj));
                ImagesAndObjectsMat.Add(imageAndObject);
            }
            return(ImagesAndObjectsMat);
        }
        //storage image and insert it into atabase without
        //insert its objects into db yet.
        public static int UserImageStorageAndDB(COMimage img, string base64)
        {
            int imgId;

            try
            {
                //image storage
                img.URL = UserImageStorage(img, base64);
                //insert image into db
                //DALimageObject.Refresh();
                img.BeginIndex = BLLobject.GetObjects().Count;
                DALimage.Addimage(img);//insert image into db
                imgId = DALimage.GetImageIdByURL(img.URL);
            }
            catch (Exception)
            {
                throw;
            }
            return(imgId);
        }
        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);
        }