<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> import cv2 <EOL> from common import Sketcher <EOL> class App : <EOL> def __init__ ( self , fn ) : <EOL> self . img = cv2 . imread ( fn ) <EOL> if self . img is None : <EOL> raise Exception ( '<STR_LIT>' % fn ) <EOL> h , w = self . img . shape [ : <NUM_LIT:2> ] <EOL> self . markers = np . zeros ( ( h , w ) , np . int32 ) <EOL> self . markers_vis = self . img . copy ( ) <EOL> self . cur_marker = <NUM_LIT:1> <EOL> self . colors = np . int32 ( list ( np . ndindex ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ) ) ) * <NUM_LIT:255> <EOL> self . auto_update = True <EOL> self . sketch = Sketcher ( '<STR_LIT>' , [ self . markers_vis , self . markers ] , self . get_colors ) <EOL> def get_colors ( self ) : <EOL> return map ( int , self . colors [ self . cur_marker ] ) , self . cur_marker <EOL> def watershed ( self ) : <EOL> m = self . markers . copy ( ) <EOL> cv2 . watershed ( self . img , m ) <EOL> overlay = self . colors [ np . maximum ( m , <NUM_LIT:0> ) ] <EOL> vis = cv2 . addWeighted ( self . img , <NUM_LIT:0.5> , overlay , <NUM_LIT:0.5> , <NUM_LIT:0.0> , dtype = cv2 . CV_8UC3 ) <EOL> cv2 . imshow ( '<STR_LIT>' , vis ) <EOL> def run ( self ) : <EOL> while True : <EOL> ch = <NUM_LIT> & cv2 . waitKey ( <NUM_LIT:50> ) <EOL> if ch == <NUM_LIT> : <EOL> break <EOL> if ch >= ord ( '<STR_LIT:1>' ) and ch <= ord ( '<STR_LIT>' ) : <EOL> self . cur_marker = ch - ord ( '<STR_LIT:0>' ) <EOL> print '<STR_LIT>' , self . cur_marker <EOL> if ch == ord ( '<STR_LIT:U+0020>' ) or ( self . sketch . dirty and self . auto_update ) : <EOL> self . watershed ( ) <EOL> self . sketch . dirty = False <EOL> if ch in [ ord ( '<STR_LIT:a>' ) , ord ( '<STR_LIT:A>' ) ] : <EOL> self . auto_update = not self . auto_update <EOL> print '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] [ self . auto_update ] <EOL> if ch in [ ord ( '<STR_LIT:r>' ) , ord ( '<STR_LIT:R>' ) ] : <EOL> self . markers [ : ] = <NUM_LIT:0> <EOL> self . markers_vis [ : ] = self . img <EOL> self . sketch . show ( ) <EOL> cv2 . destroyAllWindows ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> try : <EOL> fn = sys . argv [ <NUM_LIT:1> ] <EOL> except : <EOL> fn = '<STR_LIT>' <EOL> print __doc__ <EOL> App ( fn ) . run ( ) </s>
