コード例 #1
0
        private List <TextLine> MakeLinesFromWord(AnnotateImageResponse response)
        {
            List <TextLine> lines = new List <TextLine>();

            for (int i = 1; i < response.TextAnnotations.Count; i++)
            {
                if (lines.Count == 0)
                {
                    TextLine l = new TextLine();
                    l.Words = new List <Classes.Word>();
                    GetDataFromResponse(response, lines, i, l);
                }
                else
                {
                    int bottom = response.TextAnnotations[i].BoundingPoly.Vertices[2].Y.Value;
                    var line   = lines.Where(c => Math.Abs(c.Bounds.Bottom - bottom) <= 10).FirstOrDefault();
                    if (line == null)
                    {
                        TextLine l = new TextLine();
                        l.Words = new List <Classes.Word>();
                        GetDataFromResponse(response, lines, i, l);
                    }
                    else
                    {
                        Classes.Word w = new Classes.Word();

                        w.Text       = response.TextAnnotations[i].Description;
                        w.Confidence = 100;
                        int x      = response.TextAnnotations[i].BoundingPoly.Vertices[0].X.Value;
                        int y      = response.TextAnnotations[i].BoundingPoly.Vertices[0].Y.Value;
                        int width  = response.TextAnnotations[i].BoundingPoly.Vertices[1].X.Value - response.TextAnnotations[i].BoundingPoly.Vertices[0].X.Value;
                        int height = response.TextAnnotations[i].BoundingPoly.Vertices[2].Y.Value - response.TextAnnotations[i].BoundingPoly.Vertices[0].Y.Value;
                        w.Bounds = new System.Drawing.Rectangle(x, y, width, height);

                        line.Words.Add(w);
                    }
                }
            }

            lines.All(l =>
            {
                l.Words.Sort(delegate(Classes.Word w1, Classes.Word w2)
                {
                    return(w1.Bounds.X.CompareTo(w2.Bounds.X));
                });
                int x    = l.Words.FirstOrDefault().Bounds.X;
                int y    = l.Words.FirstOrDefault().Bounds.Y;
                int x2   = l.Words.LastOrDefault().Bounds.Right;
                l.Text   = GetTextForLine(l);
                l.Bounds = new System.Drawing.Rectangle(x, y, x2 - x, l.Bounds.Bottom - y);
                return(true);
            });

            lines.Sort(delegate(TextLine l1, TextLine l2)
            {
                return(l1.Bounds.Y.CompareTo(l2.Bounds.Y));
            });

            return(lines);
        }
コード例 #2
0
        private static void GetDataFromResponse(AnnotateImageResponse response, List <TextLine> lines, int i, TextLine l)
        {
            Classes.Word w = new Classes.Word();

            w.Text       = response.TextAnnotations[i].Description;
            w.Confidence = 100;
            int x      = response.TextAnnotations[i].BoundingPoly.Vertices[0].X.Value;
            int y      = response.TextAnnotations[i].BoundingPoly.Vertices[0].Y.Value;
            int width  = response.TextAnnotations[i].BoundingPoly.Vertices[1].X.Value - response.TextAnnotations[i].BoundingPoly.Vertices[0].X.Value;
            int height = response.TextAnnotations[i].BoundingPoly.Vertices[2].Y.Value - response.TextAnnotations[i].BoundingPoly.Vertices[0].Y.Value;

            w.Bounds = new System.Drawing.Rectangle(x, y, width, height);

            l.Words.Add(w);
            l.Bounds = w.Bounds;
            lines.Add(l);
        }