Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 2bd0c09
Author: Rodan <rodan@rodan-dev.simssa.ca>
Date:   Wed Jun 11 12:15:19 2014 -0400

    Fix: memory issue for lyric extraction correction post-process

    Had to process the final page in chunks, not all at once.

commit 9786156
Author: Ryan Bannon <ryan.bannon@gmail.com>
Date:   Wed Jun 11 11:25:04 2014 -0400

    Fix: forgot "self." because I'm stupid

commit 59ecb0b
Author: Ryan Bannon <ryan.bannon@gmail.com>
Date:   Wed Jun 11 11:22:44 2014 -0400

    Fix: function sig wrong

commit 5980b29
Author: Ryan Bannon <ryan.bannon@gmail.com>
Date:   Wed Jun 11 11:19:27 2014 -0400

    Fix: syntax (I'm an idiot)

commit 4384eb7
Author: Ryan Bannon <ryan.bannon@gmail.com>
Date:   Wed Jun 11 11:16:57 2014 -0400

    Fix: syntax (I hate Python)

commit 56b477d
Author: Ryan Bannon <ryan.bannon@gmail.com>
Date:   Wed Jun 11 11:15:35 2014 -0400

    Fix: syntax error

commit 83b597c
Author: Ryan Bannon <ryan.bannon@gmail.com>
Date:   Wed Jun 11 11:12:28 2014 -0400

    Fix attempt: memory error

    Fixing Memory Error when processing lyric extraction correction post-processing.
  • Loading branch information
mrbannon committed Jun 11, 2014
1 parent 1a6fa82 commit 662c5cb
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions rodan/jobs/gamera/custom/pixel_segment/celery_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from rodan.jobs.gamera.custom.gamera_custom_base import GameraCustomTask

class PixelSegmentTask(GameraCustomTask):

COLOUR_SWAP_PIXELS_BOX_HEIGHT = 100

max_retries = None
name = 'gamera.custom.lyric_extraction.pixel_segment'

Expand Down Expand Up @@ -41,8 +44,7 @@ def process_image(self, task_image, settings):

# Convert red to white and black to green.
colour_swap = {(255, 0, 0): (255, 255, 255), (0, 255, 0): (0, 0, 0)}
box = (0, 0, imageOriginal.size[0], imageOriginal.size[1])
imageOriginal = self._colour_swap_pixels(imageOriginal, box, colour_swap)
imageOriginal = self._colour_swap_pixels(imageOriginal, colour_swap)

# Convert back to gamera image and return.
finalImage = from_pil(imageOriginal)
Expand Down Expand Up @@ -136,15 +138,31 @@ def _colour_pixels(self, image, box, colour, collisionGeometry):

# Colours all pixels defined in "swap" keys with their "swap" values for the box dimensions.
# See other '_colour_pixels_' methods...very similar.
def _colour_swap_pixels(self, image, box, colour_swap):
imageCrop = image.crop(box)
imageCrop.load()
imagePixelData = list(imageCrop.getdata())
for i in range(len(imagePixelData)):
if (imagePixelData[i] in colour_swap):
imagePixelData[i] = colour_swap[imagePixelData[i]]
imageCrop.putdata(imagePixelData)
image.paste(imageCrop, box)
#
# NOTE: we're not doing the swap on the entire image at once as this can cause memory proglems
# on large images. Rather, we do vertical sections of the image one at a time.
def _colour_swap_pixels(self, image, colour_swap):
imageHeight = image.size[1]
count = 0
done = False
while not done:
upperLeftY = count * self.COLOUR_SWAP_PIXELS_BOX_HEIGHT
lowerRightY = upperLeftY + self.COLOUR_SWAP_PIXELS_BOX_HEIGHT
if lowerRightY >= imageHeight:
lowerRightY = imageHeight
done = True
box = (0, upperLeftY, image.size[0], lowerRightY)
imageCrop = image.crop(box)
imageCrop.load()
imagePixelData = list(imageCrop.getdata())
for i in range(len(imagePixelData)):
if (imagePixelData[i] in colour_swap):
imagePixelData[i] = colour_swap[imagePixelData[i]]
imageCrop.putdata(imagePixelData)
image.paste(imageCrop, box)
del imagePixelData
del imageCrop
count += 1
return image


0 comments on commit 662c5cb

Please sign in to comment.