コード例 #1
0
        public static string UserImageStorage(COMimage image, string base64)
        {
            int counter = BLLimage.Getimages().FindAll(img => img.UserId == image.UserId).Count;

            Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"C:\keys\wordproject-29b2e0d3e0d5.json");


            string imageName = BLLuser.GetUserById(image.UserId).CategoryName + counter + ".jpg";
            string path      = System.IO.Path.GetTempFileName();

            byte[] byte1 = Convert.FromBase64String(base64);
            try
            {
                File.WriteAllBytes(path, byte1);
            }
            catch (Exception e)
            {
                throw (e);
            }
            //string imageName = "bla2";
            string bucketName = "usersimages";
            var    storage    = StorageClient.Create();

            using (var f = File.OpenRead(path))
                try
                {
                    var res = storage.UploadObject(bucketName, imageName, null, f);
                    //(bucketName +"/"+ folderName, imageName, null, f);
                    image.URL = "https://storage.googleapis.com/" + bucketName + "/" + imageName;
                }
                catch (Exception)
                {
                    throw;
                }
            return(image.URL);
        }
コード例 #2
0
        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);
        }