{"action":"created","issue":{"url":"https://api.github.com/repos/ankush-me/SynthText/issues/114","repository_url":"https://api.github.com/repos/ankush-me/SynthText","labels_url":"https://api.github.com/repos/ankush-me/SynthText/issues/114/labels{/name}","comments_url":"https://api.github.com/repos/ankush-me/SynthText/issues/114/comments","events_url":"https://api.github.com/repos/ankush-me/SynthText/issues/114/events","html_url":"https://github.com/ankush-me/SynthText/issues/114","id":323039301,"number":114,"title":"How to make Vertical Text Dataset?","user":{"login":"SSUHan","id":8327479,"avatar_url":"https://avatars0.githubusercontent.com/u/8327479?v=4","gravatar_id":"","url":"https://api.github.com/users/SSUHan","html_url":"https://github.com/SSUHan","followers_url":"https://api.github.com/users/SSUHan/followers","following_url":"https://api.github.com/users/SSUHan/following{/other_user}","gists_url":"https://api.github.com/users/SSUHan/gists{/gist_id}","starred_url":"https://api.github.com/users/SSUHan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SSUHan/subscriptions","organizations_url":"https://api.github.com/users/SSUHan/orgs","repos_url":"https://api.github.com/users/SSUHan/repos","events_url":"https://api.github.com/users/SSUHan/events{/privacy}","received_events_url":"https://api.github.com/users/SSUHan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2018-05-15T01:54:35Z","updated_at":"2018-05-16T02:19:32Z","closed_at":null,"author_association":"NONE","body":"Hello, I'm used your code for generating OCR dataset in many case. Thanks for your efforts really :D\r\n\r\nBut, I want to make vertical text image..\r\n\r\nhttps://github.com/ankush-me/SynthText/blob/master/text_utils.py#L165\r\nthis section would decide \"text mask\" rendering.. but It's hard to make vertical text data. At present, only horizontal characters are made.\r\n\r\non this line : https://github.com/ankush-me/SynthText/blob/master/text_utils.py#L219\r\n\r\noriginal code : \r\n```python\r\nnewrect = font.get_rect(ch)\r\nnewrect.y = last_rect.y\r\nif i > mid_idx:\r\nnewrect.topleft = (last_rect.topright[0]+2, newrect.topleft[1])\r\n            else:\r\n                newrect.topright = (last_rect.topleft[0]-2, newrect.topleft[1])\r\n            newrect.centery = max(newrect.height, min(fsize[1] - newrect.height, newrect.centery + curve[i]))\r\n            try:\r\n                bbrect = font.render_to(surf, newrect, ch, rotation=rots[i])\r\n            except ValueError:\r\n                bbrect = font.render_to(surf, newrect, ch)\r\n            bbrect.x = newrect.x + bbrect.x\r\n            bbrect.y = newrect.y - bbrect.y\r\n            bbs.append(np.array(bbrect))\r\n            last_rect = newrect\r\n```\r\n\r\nmy code : \r\n```python\r\nnewrect = font.get_rect(ch)\r\n            # newrect.y = last_rect.y\r\n            newrect.x = last_rect.x\r\n            if i > mid_idx:\r\n                # print(ch, \" <- right\")\r\n                # newrect.topleft = (last_rect.topright[0] + 2, newrect.topleft[1])\r\n                # newrect.topleft = (last_rect.topright[0], newrect.topleft[1] + 2)\r\n                # newrect.topleft = (newrect.topright[0], last_rect.topleft[1] + 2)\r\n                # newrect.topright = (newrect.bottomright[0], last_rect.bottomright[1] + 3)\r\n                newrect.topleft = (newrect.bottomleft[0], last_rect.bottomleft[1] + 2)\r\n            else:\r\n                # print(ch, \" <- left\")\r\n                # newrect.topright = (last_rect.topleft[0] - 2, newrect.topleft[1])\r\n                # newrect.topright = (last_rect.topleft[0], newrect.topleft[1] - 2)\r\n                # newrect.topright = (newrect.topright[0], last_rect.topleft[1] - 2)\r\n                newrect.bottomright = (newrect.topright[0], last_rect.topright[1] - 2)\r\n            newrect.centery = max(newrect.height, min(fsize[1] - newrect.height, newrect.centery + curve[i]))\r\n            # newrect.centerx = max(newrect.width, min(fsize[0] - newrect.width, newrect.centerx + curve[i]))\r\n            try:\r\n                bbrect = font.render_to(surf, newrect, ch, rotation=rots[i])\r\n            except ValueError:\r\n                print(\"render error, without rotation..\")\r\n                bbrect = font.render_to(surf, newrect, ch)\r\n            bbrect.x = newrect.x + bbrect.x\r\n            bbrect.y = newrect.y - bbrect.y\r\n            bbs.append(np.array(bbrect))\r\n            last_rect = newrect\r\n```\r\n\r\nsample result : \r\n<img width=\"530\" alt=\"2018-05-15 10 44 29\" src=\"https://user-images.githubusercontent.com/8327479/40032153-d737bc24-582d-11e8-9b34-e62445e1d433.png\">\r\n<img width=\"347\" alt=\"2018-05-15 10 44 36\" src=\"https://user-images.githubusercontent.com/8327479/40032169-ecb17ec8-582d-11e8-8bde-6682786e9170.png\">\r\n<img width=\"556\" alt=\"2018-05-15 10 44 43\" src=\"https://user-images.githubusercontent.com/8327479/40032186-fc9766fe-582d-11e8-9029-21377c44e5cc.png\">\r\n\r\nsample result with annotation : \r\n<img width=\"512\" alt=\"2018-05-15 10 45 05\" src=\"https://user-images.githubusercontent.com/8327479/40032196-07b65464-582e-11e8-9ac1-ba3e55398e8b.png\">\r\n<img width=\"332\" alt=\"2018-05-15 10 45 12\" src=\"https://user-images.githubusercontent.com/8327479/40032202-13cefc74-582e-11e8-963d-b9993fb669e8.png\">\r\n<img width=\"552\" alt=\"2018-05-15 10 45 19\" src=\"https://user-images.githubusercontent.com/8327479/40032207-1b406240-582e-11e8-9af3-1819ac1fa18e.png\">\r\n\r\nThe possibilities are there, but they are not perfect. \r\nI really need your help..\r\n\r\nThanks.\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/ankush-me/SynthText/issues/comments/389373589","html_url":"https://github.com/ankush-me/SynthText/issues/114#issuecomment-389373589","issue_url":"https://api.github.com/repos/ankush-me/SynthText/issues/114","id":389373589,"user":{"login":"SSUHan","id":8327479,"avatar_url":"https://avatars0.githubusercontent.com/u/8327479?v=4","gravatar_id":"","url":"https://api.github.com/users/SSUHan","html_url":"https://github.com/SSUHan","followers_url":"https://api.github.com/users/SSUHan/followers","following_url":"https://api.github.com/users/SSUHan/following{/other_user}","gists_url":"https://api.github.com/users/SSUHan/gists{/gist_id}","starred_url":"https://api.github.com/users/SSUHan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SSUHan/subscriptions","organizations_url":"https://api.github.com/users/SSUHan/orgs","repos_url":"https://api.github.com/users/SSUHan/repos","events_url":"https://api.github.com/users/SSUHan/events{/privacy}","received_events_url":"https://api.github.com/users/SSUHan/received_events","type":"User","site_admin":false},"created_at":"2018-05-16T02:19:32Z","updated_at":"2018-05-16T02:19:32Z","author_association":"NONE","body":"It's hard to explain but \r\n```python\r\nch_bounds = font.render_to(surf, (x, y), ch) \r\n```\r\nch_bounds.y didn't act like horizontal case.  So, one character of upside was always missing. \r\nSample Result : \r\n\r\n\"surf_arr_pixel_alpha.png\" from pygame.surfarray.pixels_alpha(surf) :\r\n![surf_pixels_alpha](https://user-images.githubusercontent.com/8327479/40092878-9aa104c8-58fa-11e8-8ded-6933fb953326.png)\r\n\r\n\"surf_arr.png\" from self.crop_safe(surf_pixels_alpha, rect_union, bbs, pad=5) :\r\n![surf_arr](https://user-images.githubusercontent.com/8327479/40092895-b23acb46-58fa-11e8-9c7b-ee35e5cb60b7.png)\r\n\r\n\"surf_arr_swapaxes.png\" from surf_arr.swapaxes(0, 1) :\r\n![surf_arr_swapaxes](https://user-images.githubusercontent.com/8327479/40092900-baa4cfac-58fa-11e8-86b1-80644e200de8.png)\r\n\r\n\r\nAnyway, the function: render_multiline(self, font, text) for vertical text would be below\r\n```python\r\n    def render_multiline(self, font, text):\r\n        \"\"\"\r\n        renders multiline TEXT on the pygame surface SURF with the\r\n        font style FONT.\r\n        A new line in text is denoted by \\n, no other characters are\r\n        escaped. Other forms of white-spaces should be converted to space.\r\n\r\n        returns the updated surface, words and the character bounding boxes.\r\n        \"\"\"\r\n        # get the number of lines\r\n        lines = text.split('\\n')\r\n        lengths = [len(l) for l in lines]\r\n\r\n        # font parameters:\r\n        line_spacing = font.get_sized_height() + 1\r\n\r\n        # initialize the surface to proper size:\r\n        line_bounds = font.get_rect(lines[np.argmax(lengths)])\r\n        print(\"line bounds : \", line_bounds, type(line_bounds))\r\n        # horizontal\r\n        # fsize = (round(2.0 * line_bounds.width), round(1.25 * line_spacing * len(lines)))\r\n        # fsize = (round(2.0 * line_bounds.height), round(1.25 * line_spacing * len(lines)))\r\n\r\n        # vertical\r\n        fsize = (round(1.5 * line_spacing * len(lines)), round(2.0 * line_bounds.width))\r\n        # fsize = (round(1.25 * line_spacing * len(lines)), round(2.0 * line_bounds.height))\r\n        surf = pygame.Surface(fsize, pygame.locals.SRCALPHA, 32)\r\n        print(\"surf.get_size() : \", surf.get_size())\r\n\r\n        \"\"\"\r\n        https://www.pygame.org/docs/ref/freetype.html#pygame.freetype.Font.render_to\r\n        \"\"\"\r\n        bbs = []\r\n        space = font.get_rect('O')\r\n        print(\"space height : \", space.height)\r\n        # print(\"space height : \", space.height)\r\n        x, y = 0, 0\r\n        # horizontal\r\n        # for l in lines:\r\n        #     x = 20  # carriage-return\r\n        #     y += line_spacing  # line-feed\r\n        #     print(l)\r\n        #\r\n        #     for ch in l:  # render each character\r\n        #         if ch.isspace():  # just shift\r\n        #             x += space.width\r\n        #         else:\r\n        #             # render the character\r\n        #             print(\"{} : x, y : \".format(ch), x, y)\r\n        #             ch_bounds = font.render_to(surf, (x, y), ch)\r\n        #             print(\"{} : before ch_bounds : \".format(ch), ch_bounds, end='\\t')\r\n        #             ch_bounds.x = x + ch_bounds.x\r\n        #             ch_bounds.y = y - ch_bounds.y\r\n        #             print(\"after ch_bounds : \", ch_bounds)\r\n        #             x += ch_bounds.width\r\n        #             bbs.append(np.array(ch_bounds))\r\n\r\n        # vertical\r\n        for l in lines:\r\n            # x = 0  # carriage-return\r\n            # y += line_spacing  # line-feed\r\n            print(l)\r\n            y = 1.5 * space.height\r\n            x += line_spacing\r\n\r\n            for ch in l:  # render each character\r\n                if ch.isspace():  # just shift\r\n                    # x += space.width\r\n                    print(\"is space : \", space.height)\r\n                    y += space.height\r\n                else:\r\n                    # render the character\r\n                    print(\"{} : x, y : \".format(ch), x, y)\r\n                    ch_bounds = font.render_to(surf, (x, y), ch)\r\n                    # ch_bounds.x = x + ch_bounds.x\r\n                    print(\"{} : before ch_bounds : \".format(ch), ch_bounds, end='\\t')\r\n                    ch_bounds.y = y + ch_bounds.y - 1.5*space.height\r\n                    ch_bounds.x = x - ch_bounds.x\r\n                    # ch_bounds.y = y - ch_bounds.y\r\n                    print(\"after ch_bounds : \", ch_bounds)\r\n                    # x += ch_bounds.width\r\n                    # y += ch_bounds.height\r\n                    y += 1.25 * ch_bounds.height\r\n                    bbs.append(np.array(ch_bounds))\r\n\r\n        # get the union of characters for cropping:\r\n        r0 = pygame.Rect(bbs[0])\r\n        print(\"r0 : \", r0)\r\n        rect_union = r0.unionall(bbs)\r\n        print(\"rect_union : \", rect_union, type(rect_union))\r\n\r\n        # get the words:\r\n        words = ' '.join(text.split())\r\n\r\n        # crop the surface to fit the text:\r\n        bbs = np.array(bbs)\r\n        surf_pixels_alpha = pygame.surfarray.pixels_alpha(surf)\r\n        cv2.imwrite('surf_pixels_alpha.png', surf_pixels_alpha)\r\n        surf_arr, bbs = self.crop_safe(surf_pixels_alpha, rect_union, bbs, pad=5)\r\n        # print(\"surf arr : \", surf_arr, type(surf_arr), surf_arr.shape)\r\n        cv2.imwrite('surf_arr.png', surf_arr)\r\n        surf_arr = surf_arr.swapaxes(0, 1)\r\n        cv2.imwrite('surf_arr_swapaxes.png', surf_arr)\r\n        # self.visualize_bb(surf_arr,bbs)\r\n        return surf_arr, words, bbs\r\n```\r\n\r\nFinal Result : \r\n<img width=\"612\" alt=\"2018-05-16 11 00 29\" src=\"https://user-images.githubusercontent.com/8327479/40092944-facfc91a-58fa-11e8-8f68-993bd9b6d2bc.png\">\r\n\r\n"}}
{"actor":{"display_login":"SSUHan"}}