<s> from __future__ import division <EOL> import numpy as np <EOL> import scipy . ndimage . measurements as nd <EOL> filmTypes = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class telecinePerforation ( ) : <EOL> """<STR_LIT>""" <EOL> filmType = '<STR_LIT>' <EOL> sizeMargin = <NUM_LIT> <EOL> windowWidth = <NUM_LIT:0> <EOL> isInitialised = False <EOL> imageSize = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> ROIslice = None <EOL> ROIxy = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> ROIwh = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> ROIcentrexy = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> ROIthreshold = <NUM_LIT:0> <EOL> ROIimg = None <EOL> ROIuseGreenChannel = True <EOL> found = False <EOL> thresholdVal = <NUM_LIT> <EOL> expectedSize = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> position = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> centre = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> yDiff = <NUM_LIT:0> <EOL> aspectRange = ( <NUM_LIT:0.0> , <NUM_LIT:0.0> ) <EOL> widthRange = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> heightRange = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> checkEdges = <NUM_LIT:0> <EOL> checkLeftEdge = True <EOL> perforationAspectRatio = { '<STR_LIT>' : ( <NUM_LIT> / <NUM_LIT> ) , '<STR_LIT>' : ( <NUM_LIT> / <NUM_LIT> ) } <EOL> frameHeightMultiplier = { '<STR_LIT>' : <NUM_LIT> / <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> / <NUM_LIT> } <EOL> frameWidthMultiplier = { '<STR_LIT>' : <NUM_LIT> / <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> / <NUM_LIT> } <EOL> useBGR = True <EOL> def convert2grey ( img ) : <EOL> if self . useBGR : <EOL> return np . dot ( img [ ... , : <NUM_LIT:3> ] , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) . astype ( np . uint8 ) <EOL> else : <EOL> return np . dot ( img [ ... , : <NUM_LIT:3> ] , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) . astype ( np . uint8 ) <EOL> def init ( self , filmType , imageSize , expectedSize , cx ) : <EOL> if imageSize [ <NUM_LIT:0> ] > imageSize [ <NUM_LIT:1> ] : <EOL> self . imageSize = ( imageSize [ <NUM_LIT:1> ] , imageSize [ <NUM_LIT:0> ] ) <EOL> self . setFilmType ( filmType ) <EOL> self . ROIcentrexy [ <NUM_LIT:0> ] = int ( cx ) <EOL> self . setPerforationSize ( expectedSize ) <EOL> def setFilmType ( self , filmType ) : <EOL> if filmType in filmTypes : <EOL> self . isInitialised = False <EOL> self . filmType = filmType <EOL> aspectRatio = self . perforationAspectRatio [ filmType ] <EOL> aspectMargin = aspectRatio * ( self . sizeMargin / <NUM_LIT:2> ) <EOL> self . aspectRange = ( aspectRatio - aspectMargin , aspectRatio + aspectMargin ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" . format ( filmType ) ) <EOL> def setPerforationSize ( self , size ) : <EOL> w , h = size <EOL> if w > <NUM_LIT:0> and h > <NUM_LIT:0> : <EOL> w_margin = int ( w * self . sizeMargin ) <EOL> h_margin = int ( h * self . sizeMargin ) <EOL> self . widthRange = ( w - w_margin , w + w_margin ) <EOL> self . heightRange = ( h - h_margin , h + h_margin ) <EOL> self . expectedSize = size <EOL> self . isInitialised = True <EOL> else : <EOL> self . expectedSize = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . ROIimg = None <EOL> self . isInitialised = False <EOL> self . setROI ( ) <EOL> def setROI ( self ) : <EOL> img_h , img_w = self . imageSize <EOL> if self . isInitialised : <EOL> h = int ( img_h / <NUM_LIT:2> ) <EOL> if self . filmType == '<STR_LIT>' : <EOL> y = int ( img_h / <NUM_LIT:4> ) <EOL> else : <EOL> y = int ( img_h / <NUM_LIT:50> ) <EOL> w = int ( ( self . expectedSize [ <NUM_LIT:0> ] + ( self . expectedSize [ <NUM_LIT:0> ] * self . sizeMargin ) ) / <NUM_LIT:2> ) <EOL> roiL = max ( <NUM_LIT:0> , self . ROIcentrexy [ <NUM_LIT:0> ] - w ) <EOL> roiR = min ( img_w , self . ROIcentrexy [ <NUM_LIT:0> ] + w ) <EOL> self . ROIcentrexy = [ int ( roiL + ( roiR - roiL ) / <NUM_LIT:2> ) , int ( y + ( h / <NUM_LIT:2> ) ) ] <EOL> else : <EOL> y = <NUM_LIT:0> <EOL> h = img_h <EOL> roiL = <NUM_LIT:0> <EOL> roiR = int ( img_w / <NUM_LIT:2> ) <EOL> self . ROIcentrexy = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> self . ROIxy = ( roiL , y ) <EOL> self . ROIwh = ( roiR - roiL , h ) <EOL> self . ROIslice = np . index_exp [ y : y + h , roiL : roiR ] <EOL> self . ROIimg = np . zeros ( ( roiR - roiL , h ) , dtype = np . uint8 ) <EOL> def setROIimg ( self , img ) : <EOL> if img . shape [ : <NUM_LIT:2> ] == self . imageSize : <EOL> if len ( img . shape ) > <NUM_LIT:2> : <EOL> if self . ROIuseGreenChannel : <EOL> i = img [ self . ROIslice ] <EOL> self . ROIimg = i [ : , : , <NUM_LIT:1> ] <EOL> else : <EOL> self . ROIimg = self . convert2grey ( img [ self . ROIslice ] ) <EOL> else : <EOL> self . ROIimg = img [ self . ROIslice ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' . format ( self . imageSize , img . shape [ : <NUM_LIT:2> ] ) ) <EOL> def cropToSlice ( self , ( x , y , w , h ) ) : <EOL> x = max ( x , <NUM_LIT:0> ) <EOL> y = max ( y , <NUM_LIT:0> ) <EOL> w = max ( w , <NUM_LIT:1> ) <EOL> h = max ( h , <NUM_LIT:1> ) <EOL> return np . index_exp [ y : y + h , x : x + w ] <EOL> def findFirstFromCoords ( self , img , startPosition , windowWidth ) : <EOL> self . isInitialised = False <EOL> self . found = False <EOL> self . imageSize = img . shape [ : <NUM_LIT:2> ] <EOL> self . setROI ( ) <EOL> self . setROIimg ( img ) <EOL> xStart = startPosition [ <NUM_LIT:0> ] <EOL> yStart = startPosition [ <NUM_LIT:1> ] <EOL> win = windowWidth // <NUM_LIT:2> <EOL> vROI = self . ROIimg [ : , xStart - win : xStart + win ] <EOL> threshVal = int ( vROI . max ( ) * self . thresholdVal ) <EOL> vROI = np . median ( vROI , axis = <NUM_LIT:1> ) < threshVal <EOL> hROI = self . ROIimg [ yStart - win : yStart + win , : ] <EOL> hROI = np . median ( hROI , axis = <NUM_LIT:0> ) < threshVal <EOL> if hROI [ xStart - win : xStart + win ] . any ( ) or vROI [ yStart - win : yStart + win ] . any ( ) : <EOL> print ( "<STR_LIT>" . format ( startPosition ) ) <EOL> else : <EOL> x , y = self . ROIxy <EOL> w , h = self . ROIwh <EOL> bot = vROI [ yStart : ] . argmax ( ) <EOL> bot = yStart + bot if bot > <NUM_LIT:0> else h <EOL> vROI = vROI [ : yStart ] <EOL> top = vROI [ : : - <NUM_LIT:1> ] . argmax ( ) <EOL> top = yStart - top if top > <NUM_LIT:0> else <NUM_LIT:0> <EOL> right = hROI [ xStart : ] . argmax ( ) <EOL> right = xStart + right if right > <NUM_LIT:0> else w <EOL> hROI = hROI [ : xStart ] <EOL> left = hROI [ : : - <NUM_LIT:1> ] . argmax ( ) <EOL> left = xStart - left if left > <NUM_LIT:0> else <NUM_LIT:0> <EOL> w = right - left <EOL> h = bot - top <EOL> aspect = float ( w ) / float ( h ) <EOL> if self . aspectRange [ <NUM_LIT:0> ] <= aspect <= self . aspectRange [ <NUM_LIT:1> ] : <EOL> self . setPerforationSize ( ( w , h ) ) <EOL> self . setPerfPosition ( x + left + ( ( right - left ) / <NUM_LIT:2> ) , y + top + ( h / <NUM_LIT:2> ) ) <EOL> self . windowWidth = w - ( w * self . sizeMargin * <NUM_LIT:2> ) <EOL> self . isInitialised = True <EOL> self . ROIcentrexy [ <NUM_LIT:0> ] = self . centre [ <NUM_LIT:0> ] <EOL> self . setROI ( ) <EOL> self . found = True <EOL> else : <EOL> print ( "<STR_LIT>" . format ( aspect , self . aspectRange ) ) <EOL> def setPerfPosition ( self , cx , cy ) : <EOL> self . centre = ( int ( cx ) , int ( cy ) ) <EOL> self . position = ( int ( cx - self . expectedSize [ <NUM_LIT:0> ] / <NUM_LIT:2> ) , int ( cy - self . expectedSize [ <NUM_LIT:1> ] / <NUM_LIT:2> ) ) <EOL> self . yDiff = int ( self . centre [ <NUM_LIT:1> ] - self . ROIcentrexy [ <NUM_LIT:1> ] ) <EOL> def findVertical ( self , img ) : <EOL> self . found = False <EOL> self . setROIimg ( img ) <EOL> expectedW , expectedH = self . expectedSize <EOL> xStart = self . ROIwh [ <NUM_LIT:0> ] // <NUM_LIT:2> <EOL> yStart = self . ROIcentrexy [ <NUM_LIT:1> ] - self . ROIxy [ <NUM_LIT:1> ] <EOL> win = ( expectedW - ( expectedW * self . sizeMargin ) ) // <NUM_LIT:2> <EOL> vROI = self . ROIimg [ : , xStart - win : xStart + win ] <EOL> threshVal = int ( vROI . max ( ) * self . thresholdVal ) <EOL> vROI = np . median ( vROI , axis = <NUM_LIT:1> ) < threshVal <EOL> x , y = self . ROIxy <EOL> w , h = self . ROIwh <EOL> bot = vROI [ yStart : ] . argmax ( ) <EOL> bot = yStart + bot if bot > <NUM_LIT:0> else h <EOL> vROI = vROI [ : yStart ] <EOL> top = vROI [ : : - <NUM_LIT:1> ] . argmax ( ) <EOL> top = yStart - top if top > <NUM_LIT:0> else <NUM_LIT:0> <EOL> if self . checkEdges == <NUM_LIT:1> : <EOL> bot = top + expectedH <EOL> elif self . checkEdges == <NUM_LIT:2> : <EOL> top = bot - expectedH <EOL> aspect = float ( expectedW ) / float ( bot - top ) <EOL> if self . aspectRange [ <NUM_LIT:0> ] <= aspect <= self . aspectRange [ <NUM_LIT:1> ] : <EOL> x , y = self . ROIxy <EOL> self . setPerfPosition ( x + xStart , y + top + ( ( bot - top ) / <NUM_LIT:2> ) ) <EOL> self . found = True <EOL> else : <EOL> print ( "<STR_LIT>" . format ( aspect , self . aspectRange ) ) <EOL> if not ( self . found ) : <EOL> self . findVerticalAlternative ( ) <EOL> def findVerticalAlternative ( self ) : <EOL> self . found = False <EOL> cx = self . ROIwh [ <NUM_LIT:0> ] // <NUM_LIT:2> <EOL> expectedW , expectedH = self . expectedSize <EOL> win = ( expectedW - ( expectedW * self . sizeMargin ) ) // <NUM_LIT:2> <EOL> vROI = self . ROIimg [ : , cx - win : cx + win ] <EOL> vROI = np . median ( vROI , axis = <NUM_LIT:1> ) <EOL> threshVal = int ( vROI . max ( ) * self . thresholdVal ) <EOL> vROIthres = vROI >= threshVal <EOL> candidate = None <EOL> if vROIthres . min ( ) != vROIthres . max ( ) : <EOL> lbl , numLbl = nd . label ( vROIthres ) <EOL> obj = nd . find_objects ( lbl ) <EOL> brightest = <NUM_LIT:0> <EOL> for s in obj : <EOL> print s <EOL> sBright = np . mean ( vROI [ s ] ) <EOL> sHeight = s [ <NUM_LIT:0> ] . stop - s [ <NUM_LIT:0> ] . start <EOL> if ( self . heightRange [ <NUM_LIT:0> ] <= sHeight <= self . heightRange [ <NUM_LIT:1> ] ) and sBright > brightest : <EOL> candidate = s [ <NUM_LIT:0> ] <EOL> brightest = sBright <EOL> if candidate : <EOL> self . setPerfPosition ( self . ROIcentrexy [ <NUM_LIT:0> ] , self . ROIxy [ <NUM_LIT:1> ] + candidate . start + ( ( candidate . stop - candidate . start ) / <NUM_LIT:2> ) ) <EOL> self . found = True <EOL> def findLeftEdge ( self ) : <EOL> if self . found : <EOL> expectedW , expectedH = self . expectedSize <EOL> win = ( expectedH - ( expectedH * self . sizeMargin ) ) // <NUM_LIT:2> <EOL> centre = ( self . centre [ <NUM_LIT:0> ] - self . ROIxy [ <NUM_LIT:0> ] , self . centre [ <NUM_LIT:1> ] - self . ROIxy [ <NUM_LIT:1> ] ) <EOL> hROI = self . ROIimg [ centre [ <NUM_LIT:1> ] - win : centre [ <NUM_LIT:1> ] + win , : centre [ <NUM_LIT:0> ] ] <EOL> threshVal = int ( hROI . max ( ) * self . thresholdVal ) <EOL> hROI = np . median ( hROI , axis = <NUM_LIT:0> ) < threshVal <EOL> left = hROI [ : : - <NUM_LIT:1> ] . argmax ( ) <EOL> left = centre [ <NUM_LIT:0> ] - left if left > <NUM_LIT:0> else <NUM_LIT:0> <EOL> self . position = ( left + self . ROIxy [ <NUM_LIT:0> ] , self . position [ <NUM_LIT:1> ] ) <EOL> self . centre = ( left + ( self . expectedSize [ <NUM_LIT:0> ] // <NUM_LIT:2> ) + self . ROIxy [ <NUM_LIT:0> ] , self . centre [ <NUM_LIT:1> ] ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def find ( self , img ) : <EOL> if self . isInitialised : <EOL> self . findVertical ( img ) <EOL> if self . found and self . checkLeftEdge : <EOL> self . findLeftEdge ( ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) </s>
