public static List<COMimage> Getimages() { using (var connection = new MySqlConnection(csb.ConnectionString)) { List<COMimage> list = new List<COMimage>(); try { connection.Open(); } catch (Exception) { throw; } MySqlCommand select_all_images = new MySqlCommand("SELECT * FROM Images_tbl", connection); using (var reader = select_all_images.ExecuteReader()) { while (reader.Read()) { COMimage image = new COMimage() { ImageID = Convert.ToInt32(reader["imageId"]), URL = Convert.ToString(reader["imageURL"]), UserId = Convert.ToInt32(reader["userId"]), CategoryID = Convert.ToInt32(reader["categoryId"]), BeginIndex = Convert.ToInt32(reader["beginIndex"]), EndIndex = Convert.ToInt32(reader["endIndex"]), }; list.Add(image); } } return list; } }
public static COMimage GetImageById(int id) { using (var connection = new MySqlConnection(csb.ConnectionString)) { COMimage image = new COMimage(); try { connection.Open(); } catch (Exception) { throw; } MySqlCommand get_image_by_id = new MySqlCommand("SELECT * FROM Images_tbl WHERE imageId=@imgId;", connection); get_image_by_id.Parameters.AddWithValue("imgId", id); using (var reader = get_image_by_id.ExecuteReader()) { if (reader.Read()) { image.ImageID = Convert.ToInt32(reader["imageId"]); image.URL = Convert.ToString(reader["imageURL"]); image.UserId = Convert.ToInt32(reader["userId"]); image.CategoryID = Convert.ToInt32(reader["categoryId"]); image.BeginIndex = Convert.ToInt32(reader["beginIndex"]); image.EndIndex = Convert.ToInt32(reader["endIndex"]); } } return image; } }
public IHttpActionResult GetImageById(int id) { COMimage img = BLLimage.GetImageById(id); if (img == null) { return(BadRequest("image does not exist")); } return(Ok(img)); }
public IHttpActionResult PostImage([FromBody] COMimage img) { COMimage im = BLLimage.GetImageById(img.ImageID); if (im != null) { return(BadRequest("image already exist")); } BLLimage.AddImage(img, Form1.categoriesCounter, Form1.voicesCounter); return(Ok()); }
private void button7_Click(object sender, EventArgs e) { COMimage img = new COMimage(); img.UserId = Convert.ToInt32(comboBox3.SelectedValue); img.CategoryID = Convert.ToInt32(comboBox1.SelectedValue); img.URL = path; label10.Text = ""; foreach (string objName in BLLimage.AddImage(img, categoriesCounter, voicesCounter)) { label10.Text += objName + "\n"; } groupBox3.Visible = false; }
//insert image to db and storage without inserting its objects. //return objects list //[HttpPost] // //public static WebResult<int> InsertImages() //{ // HttpResponseMessage response = new HttpResponseMessage(); // var httpRequest = HttpContext.Current.Request; // string temp = "~/UploadFile/"; // string uploaded_image; // if (httpRequest.Files.Count > 0) // { // for (var i = 0; i < httpRequest.Files.Count; i++) // { // var postedFile = httpRequest.Files[i]; // var filePath = HttpContext.Current.Server.MapPath(temp + postedFile.FileName); // postedFile.SaveAs(filePath); // uploaded_image = SendToStorage(filePath); // InitImageDetailsAsync(filePath); // InitImageDetailsAsync(filePath); // } // } // return new WebResult<int>() // { // Status = true, // Message = "Success", // Value = 1 // }; // //return response; //} //[HttpPost] //public void PostInsertImageReturnObjects(int id, int catId) //{ // var httpRequest = HttpContext.Current.Request; // var re= httpRequest.GetBufferlessInputStream(); // ////=httpRequest.Params["image_data"]; // // re = httpRequest.Form["image_data"]; // string temp = "~/UploadFile/"; // string uploaded_image; // if (httpRequest.Files.Count > 0) // { // for (var i = 0; i < httpRequest.Files.Count; i++) // { // var postedFile = httpRequest.Files[i]; // var filePath = HttpContext.Current.Server.MapPath(temp + postedFile.FileName); // postedFile.SaveAs(filePath); // //List < COMimageObject > // } // } // ///// // string fileName = string.Empty; // try // { // if (HttpContext.Current.Request.Files.AllKeys.Any()) // { // // Get the uploaded image from the Files collection // var httpPostedFile = HttpContext.Current.Request.Files["image_data"]; // fileName = httpPostedFile.FileName; // } // //if (httpPostedFile != null) // //{ // // // OBtient le path du fichier // // fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"), httpPostedFile.FileName); // // // Sauvegarde du fichier dans UploadedFiles sur le serveur // // httpPostedFile.SaveAs(fileSavePath); // //} // //string base64 = MyBase64.URL.Substring(23); // COMimage img = new COMimage(); // img.UserId = id; // img.CategoryID = catId; // img.URL = ""; // //List<COMimageObject> objs = BLLimage.GetImageFromUserReturnObjectsList(img, MyBase64.URL); // // return objs; // } // catch (Exception) // { // throw; // } //} public List <COMimageObject> PostInsertImageReturnObjects([FromBody] string MyBase64, int id, int catId) { try { //string base64 = MyBase64.URL.Substring(23); COMimage img = new COMimage(); img.UserId = id; img.CategoryID = catId; img.URL = ""; List <COMimageObject> objs = BLLimage.GetImageFromUserReturnObjectsList(img, MyBase64); return(objs); } catch (Exception) { throw; } }
//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); }
//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); }
// [HttpPost] public void PostObjects([FromBody] List <COMimageObject> objs, int id, int catid) { try { COMimage img = new COMimage(); img.UserId = id; img.CategoryID = catid; img.URL = ""; int imageid = BLLgoogleVision.UserImageStorageAndDB(img, objs[0].VoiceURL); Dictionary <string, int> dic = new Dictionary <string, int>(); foreach (COMimageObject item in objs) { item.ImageID = imageid; item.VoiceURL = BLLtextToSpeach.VoiceStorage(id, catid, BLLtextToSpeach.TextToSpeach(item.Name), dic); BLLobject.AddObject(item); } } catch (Exception) { //return BadRequest(); throw; } }
private void button15_Click(object sender, EventArgs e) { label3.Text = ""; label3.Name = ""; COMimage img = new COMimage(); img.UserId = Convert.ToInt32(comboBox3.SelectedValue); img.CategoryID = Convert.ToInt32(comboBox1.SelectedValue); img.URL = path; try { //objs = BLLimage.GetImageFromUserReturnObjectsList(img); // BLLgoogleVision.UserImageStorageAndDB(img); foreach (var item in objs) { label3.Text += item.Name + "\n"; } } catch (Exception) { throw; } }
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); }
//insert image into db public static void Addimage(COMimage img) { using (var connection = new MySqlConnection(csb.ConnectionString)) { Int64 imageId = nextImageId(); try { connection.Open(); } catch (Exception) { throw; } MySqlCommand insert_table = new MySqlCommand("INSERT INTO Images_tbl SET imageURL=@ImageURL,beginIndex=@BeginIndex,endIndex=@EndIndex,imageId=@ImageId,categoryId=(SELECT categoryId FROM Categories_tbl WHERE categoryId=@catId),userId=(SELECT userId FROM users_tbl WHERE userId=@uId);", connection); insert_table.Parameters.AddWithValue("ImageURL", img.URL); insert_table.Parameters.AddWithValue("BeginIndex", img.BeginIndex); insert_table.Parameters.AddWithValue("EndIndex", img.EndIndex); insert_table.Parameters.AddWithValue("ImageId", imageId); insert_table.Parameters.AddWithValue("catId", img.CategoryID); insert_table.Parameters.AddWithValue("uId", img.UserId); insert_table.ExecuteNonQuery(); connection.Close(); } }
public static List <COMimageObject> GetImageFromUserReturnObjectsList(COMimage img, string base64) { return(BLLgoogleVision.CustomVisionApi(img, base64)); }
public static List <string> AddImage(COMimage img, Dictionary <string, int> categoriesCounter, Dictionary <string, int> voicesCounter) { return(BLLgoogleVision.VisionApi(img.CategoryID, img.UserId, img.URL, categoriesCounter, voicesCounter)); }
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); }