Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection.Emit;
  8. using System.Threading.Tasks;
  9. using Newtonsoft.Json;
  10. using Recognize_TextSdk;
  11.  
  12. namespace ImageCut
  13. {
  14. public static class JsonParser
  15. {
  16. private static int count = 1;
  17.  
  18. public static List<SubPicture> GetData(string jsonPath, string imagePath)
  19. {
  20. Image img = Image.FromFile(imagePath);
  21. string json = File.ReadAllText(jsonPath);
  22. // In case of a Json that was tranfered from the XML file
  23. Model model = JsonConvert.DeserializeObject<Model>(json);
  24. List<SubPicture> subPictures = model.Annotation.Objects;
  25.  
  26. //In case of a Json file that was returned from the ML
  27. // List<SubPicture> subPictures = JsonConvert.DeserializeObject<List<SubPicture>>(json);
  28.  
  29. foreach (SubPicture subPicture in subPictures)
  30. {
  31. if (subPicture.Label == "label")
  32. {
  33. Bitmap newImg = CropImage(img, subPicture.TopLeft.X, subPicture.TopLeft.Y, subPicture.BottomRight.X, subPicture.BottomRight.Y);
  34.  
  35. newImg.Save($@".\{count}.jpg", ImageFormat.Jpeg);
  36.  
  37. Task<string> task = TextExtractor.ExtractTextAsync($@".\{count++}.jpg");
  38.  
  39. var label = task.GetAwaiter().GetResult();
  40.  
  41. subPicture.Label = $"label_{label}";
  42. }
  43.  
  44. }
  45.  
  46. return subPictures;
  47. }
  48.  
  49. private static Bitmap CropImage(Image source, int x1, int y1, int x2, int y2)
  50. {
  51. Rectangle crop = new Rectangle(x1, y1, x2 - x1, y2 - y1);
  52.  
  53. var bmp = new Bitmap(crop.Width, crop.Height);
  54. using (var gr = Graphics.FromImage(bmp))
  55. {
  56. gr.DrawImage(source, new Rectangle(0, 0, bmp.Width, bmp.Height), crop, GraphicsUnit.Pixel);
  57. }
  58.  
  59. return bmp;
  60. }
  61. }
  62.  
  63. public class Model
  64. {
  65. public Annotation Annotation { get; set; }
  66. }
  67.  
  68. public class Annotation
  69. {
  70.  
  71. public List<SubPicture> Objects { get; set; }
  72.  
  73. public Annotation()
  74. {
  75. this.Objects = new List<SubPicture>();
  76. }
  77. }
  78.  
  79. public class SubPicture
  80. {
  81. public Point TopLeft { get; set; }
  82. public Point BottomRight { get; set; }
  83. public string Label { get; set; }
  84.  
  85. public SubPicture()
  86. {
  87. this.TopLeft = new Point();
  88. this.BottomRight = new Point();
  89. }
  90.  
  91. public override string ToString()
  92. {
  93. return $"{this.Label} ({this.TopLeft.X}, {this.TopLeft.Y})";
  94. }
  95. }
  96.  
  97. public class Point
  98. {
  99. public int X { get; set; }
  100. public int Y { get; set; }
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement