question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to make Vertical Text Dataset?

See original GitHub issue

Hello, I’m used your code for generating OCR dataset in many case. Thanks for your efforts really 😄

But, I want to make vertical text image…

https://github.com/ankush-me/SynthText/blob/master/text_utils.py#L165 this section would decide “text mask” rendering… but It’s hard to make vertical text data. At present, only horizontal characters are made.

on this line : https://github.com/ankush-me/SynthText/blob/master/text_utils.py#L219

original code :

newrect = font.get_rect(ch)
newrect.y = last_rect.y
if i > mid_idx:
newrect.topleft = (last_rect.topright[0]+2, newrect.topleft[1])
            else:
                newrect.topright = (last_rect.topleft[0]-2, newrect.topleft[1])
            newrect.centery = max(newrect.height, min(fsize[1] - newrect.height, newrect.centery + curve[i]))
            try:
                bbrect = font.render_to(surf, newrect, ch, rotation=rots[i])
            except ValueError:
                bbrect = font.render_to(surf, newrect, ch)
            bbrect.x = newrect.x + bbrect.x
            bbrect.y = newrect.y - bbrect.y
            bbs.append(np.array(bbrect))
            last_rect = newrect

my code :

newrect = font.get_rect(ch)
            # newrect.y = last_rect.y
            newrect.x = last_rect.x
            if i > mid_idx:
                # print(ch, " <- right")
                # newrect.topleft = (last_rect.topright[0] + 2, newrect.topleft[1])
                # newrect.topleft = (last_rect.topright[0], newrect.topleft[1] + 2)
                # newrect.topleft = (newrect.topright[0], last_rect.topleft[1] + 2)
                # newrect.topright = (newrect.bottomright[0], last_rect.bottomright[1] + 3)
                newrect.topleft = (newrect.bottomleft[0], last_rect.bottomleft[1] + 2)
            else:
                # print(ch, " <- left")
                # newrect.topright = (last_rect.topleft[0] - 2, newrect.topleft[1])
                # newrect.topright = (last_rect.topleft[0], newrect.topleft[1] - 2)
                # newrect.topright = (newrect.topright[0], last_rect.topleft[1] - 2)
                newrect.bottomright = (newrect.topright[0], last_rect.topright[1] - 2)
            newrect.centery = max(newrect.height, min(fsize[1] - newrect.height, newrect.centery + curve[i]))
            # newrect.centerx = max(newrect.width, min(fsize[0] - newrect.width, newrect.centerx + curve[i]))
            try:
                bbrect = font.render_to(surf, newrect, ch, rotation=rots[i])
            except ValueError:
                print("render error, without rotation..")
                bbrect = font.render_to(surf, newrect, ch)
            bbrect.x = newrect.x + bbrect.x
            bbrect.y = newrect.y - bbrect.y
            bbs.append(np.array(bbrect))
            last_rect = newrect

sample result : 2018-05-15 10 44 29 2018-05-15 10 44 36 2018-05-15 10 44 43

sample result with annotation : 2018-05-15 10 45 05 2018-05-15 10 45 12 2018-05-15 10 45 19

The possibilities are there, but they are not perfect. I really need your help…

Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
Once88commented, Aug 22, 2019

Final Result : c1

I just changed render_multiline and render_sample in text_utils.py

` def render_multiline(self,font,text): “”" renders multiline TEXT on the pygame surface SURF with the font style FONT. A new line in text is denoted by \n, no other characters are escaped. Other forms of white-spaces should be converted to space.

    returns the updated surface, words and the character bounding boxes.
    """
    # get the number of lines
    lines = [c for c in text]
    lengths = [len(l) for l in lines]

    # font parameters:
    print("get_sized_height: ", font.get_sized_height())
    line_spacing = font.get_sized_height() + 1
    # line_spacing = font.get_sized_height() + 1

    # initialize the surface to proper size:
    line_bounds = font.get_rect(lines[np.argmax(lengths)])
    fsize = (round(2.0*line_bounds.width), round(line_spacing*len(lines)))
    # fsize = (round(2.0*line_bounds.width), round(1.25*line_spacing*len(lines)))
    surf = pygame.Surface(fsize, pygame.locals.SRCALPHA, 32)

    bbs = []
    space = font.get_rect('O')
    x, y = 0, 0
    idx = 0
    for l in lines:
        idx += 1
        x = 0 # carriage-return
        # y += line_spacing # line-feed
        y += round(line_spacing * 0.8) # line-feed
        if idx == 5 or idx == 11:
            y += round(line_spacing * 0.2)

        for ch in l: # render each character
            if ch.isspace(): # just shift
                x += space.width
            else:
                # render the character
                ch_bounds = font.render_to(surf, (x,y), ch)
                ch_bounds.x = x + ch_bounds.x
                ch_bounds.y = y - ch_bounds.y
                # todo 修改字符间距
                # x += ch_bounds.width
                x += ch_bounds.width + 5
                bbs.append(np.array(ch_bounds))

    # get the union of characters for cropping:
    r0 = pygame.Rect(bbs[0])
    rect_union = r0.unionall(bbs)

    # get the words:
    words = ' '.join(text.split())

    # crop the surface to fit the text:
    bbs = np.array(bbs)
    surf_arr, bbs = crop_safe(pygame.surfarray.pixels_alpha(surf), rect_union, bbs, pad=5)
    surf_arr = surf_arr.swapaxes(0,1)
    #self.visualize_bb(surf_arr,bbs)
    return surf_arr, words, bbs`

in render_sample function, I just change text = self.text_source.sample(nline,nchar,text_type) to # text = self.text_source.sample(nline,nchar,text_type) text_arr = random.sample('ABCDEFGHIJKLMNOPQRSTUVWXYZ',3) text = ''.join(text_arr) + 'U' text2 = random.sample('0123456789',7) text2 = ''.join(text2) text = text + text2

1reaction
SSUHancommented, May 16, 2018

It’s hard to explain but

ch_bounds = font.render_to(surf, (x, y), ch) 

ch_bounds.y didn’t act like horizontal case. So, one character of upside was always missing. Sample Result :

“surf_arr_pixel_alpha.png” from pygame.surfarray.pixels_alpha(surf) : surf_pixels_alpha

“surf_arr.png” from self.crop_safe(surf_pixels_alpha, rect_union, bbs, pad=5) : surf_arr

“surf_arr_swapaxes.png” from surf_arr.swapaxes(0, 1) : surf_arr_swapaxes

Anyway, the function: render_multiline(self, font, text) for vertical text would be below

    def render_multiline(self, font, text):
        """
        renders multiline TEXT on the pygame surface SURF with the
        font style FONT.
        A new line in text is denoted by \n, no other characters are
        escaped. Other forms of white-spaces should be converted to space.

        returns the updated surface, words and the character bounding boxes.
        """
        # get the number of lines
        lines = text.split('\n')
        lengths = [len(l) for l in lines]

        # font parameters:
        line_spacing = font.get_sized_height() + 1

        # initialize the surface to proper size:
        line_bounds = font.get_rect(lines[np.argmax(lengths)])
        print("line bounds : ", line_bounds, type(line_bounds))
        # horizontal
        # fsize = (round(2.0 * line_bounds.width), round(1.25 * line_spacing * len(lines)))
        # fsize = (round(2.0 * line_bounds.height), round(1.25 * line_spacing * len(lines)))

        # vertical
        fsize = (round(1.5 * line_spacing * len(lines)), round(2.0 * line_bounds.width))
        # fsize = (round(1.25 * line_spacing * len(lines)), round(2.0 * line_bounds.height))
        surf = pygame.Surface(fsize, pygame.locals.SRCALPHA, 32)
        print("surf.get_size() : ", surf.get_size())

        """
        https://www.pygame.org/docs/ref/freetype.html#pygame.freetype.Font.render_to
        """
        bbs = []
        space = font.get_rect('O')
        print("space height : ", space.height)
        # print("space height : ", space.height)
        x, y = 0, 0
        # horizontal
        # for l in lines:
        #     x = 20  # carriage-return
        #     y += line_spacing  # line-feed
        #     print(l)
        #
        #     for ch in l:  # render each character
        #         if ch.isspace():  # just shift
        #             x += space.width
        #         else:
        #             # render the character
        #             print("{} : x, y : ".format(ch), x, y)
        #             ch_bounds = font.render_to(surf, (x, y), ch)
        #             print("{} : before ch_bounds : ".format(ch), ch_bounds, end='\t')
        #             ch_bounds.x = x + ch_bounds.x
        #             ch_bounds.y = y - ch_bounds.y
        #             print("after ch_bounds : ", ch_bounds)
        #             x += ch_bounds.width
        #             bbs.append(np.array(ch_bounds))

        # vertical
        for l in lines:
            # x = 0  # carriage-return
            # y += line_spacing  # line-feed
            print(l)
            y = 1.5 * space.height
            x += line_spacing

            for ch in l:  # render each character
                if ch.isspace():  # just shift
                    # x += space.width
                    print("is space : ", space.height)
                    y += space.height
                else:
                    # render the character
                    print("{} : x, y : ".format(ch), x, y)
                    ch_bounds = font.render_to(surf, (x, y), ch)
                    # ch_bounds.x = x + ch_bounds.x
                    print("{} : before ch_bounds : ".format(ch), ch_bounds, end='\t')
                    ch_bounds.y = y + ch_bounds.y - 1.5*space.height
                    ch_bounds.x = x - ch_bounds.x
                    # ch_bounds.y = y - ch_bounds.y
                    print("after ch_bounds : ", ch_bounds)
                    # x += ch_bounds.width
                    # y += ch_bounds.height
                    y += 1.25 * ch_bounds.height
                    bbs.append(np.array(ch_bounds))

        # get the union of characters for cropping:
        r0 = pygame.Rect(bbs[0])
        print("r0 : ", r0)
        rect_union = r0.unionall(bbs)
        print("rect_union : ", rect_union, type(rect_union))

        # get the words:
        words = ' '.join(text.split())

        # crop the surface to fit the text:
        bbs = np.array(bbs)
        surf_pixels_alpha = pygame.surfarray.pixels_alpha(surf)
        cv2.imwrite('surf_pixels_alpha.png', surf_pixels_alpha)
        surf_arr, bbs = self.crop_safe(surf_pixels_alpha, rect_union, bbs, pad=5)
        # print("surf arr : ", surf_arr, type(surf_arr), surf_arr.shape)
        cv2.imwrite('surf_arr.png', surf_arr)
        surf_arr = surf_arr.swapaxes(0, 1)
        cv2.imwrite('surf_arr_swapaxes.png', surf_arr)
        # self.visualize_bb(surf_arr,bbs)
        return surf_arr, words, bbs

Final Result : 2018-05-16 11 00 29

Read more comments on GitHub >

github_iconTop Results From Across the Web

Simultaneous Recognition of Horizontal and Vertical Text in ...
Using these datasets, we proved that our proposed model can accurately recognize both vertical and horizontal text and can achieve ...
Read more >
How to Make Text Vertical in Excel (2 Easy Ways) - ExcelDemy
Then, go to the Home tab> Orientation icon> click Vertical Text. Making Text Vertical in Excel. And now, your text will be vertically...
Read more >
Development of Vertical Text Interpreter for Natural Scene ...
Vertical Text Interpreter detects and recognizes vertically oriented scene texts in natural scenes, including vertically-stacked texts, bottom- ...
Read more >
How To Make Text Vertical In Google Sheets (Rotate Text)
Select the cells where you want to make the vertical · In the toolbar, click on the text rotation icon · Click on...
Read more >
ABBYY FlexiCapture: Extracting Vertical Text - YouTube
Learn how to extract vertical text in ABBYY FlexiCapture.Thank you for watching our video! To find out more about UFC, Inc and the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found