Advertisement
Guest User

Multiprocessing with OpenCV and PyGTK

a guest
Sep 16th, 2010
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.90 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Multiprocess Transparent Webcam Demo | sept 17, 2010
  3. # by HartsAntler - bhartsho@yahoo.com
  4. # License: GNU GPL3
  5. # Tested with Ubuntu Lucid, all you should need is: apt-get install python-opencv python-pygame
  6.  
  7. import multiprocessing, ctypes
  8. from multiprocessing import sharedctypes
  9.  
  10. import os, sys, time
  11. import pygame
  12. #import Image, ImageDraw, ImageChops
  13.  
  14. import gtk, glib, cairo, pango, gobject
  15.  
  16. BG_RED = 0.98
  17. BG_GREEN = 0.98
  18. BG_BLUE = 0.99
  19. BG_ALPHA = 0.0
  20. BG_COLOR = (BG_RED,BG_GREEN,BG_BLUE,BG_ALPHA)
  21.  
  22. def transparent_window( win, color=BG_COLOR, decorate=False ):
  23.     win.set_decorated( decorate )
  24.     # Tell GTK+ that we want to draw the windows background ourself.
  25.     # If we don't do this then GTK+ will clear the window to the
  26.     # opaque theme default color, which isn't what we want.
  27.     win.set_app_paintable(True)
  28.     make_transparent( win, color )
  29.  
  30.  
  31. def expose_transparent(widget, event):
  32.     cr = widget.window.cairo_create()
  33.     r,g,b,a = widget._trans_color_hack
  34.     cr.set_source_rgba(r, g, b, a) # Transparent
  35.     # Draw the background
  36.     cr.set_operator(cairo.OPERATOR_SOURCE)
  37.     cr.paint()
  38.     return False
  39.  
  40. def make_transparent(widget, color=BG_COLOR ):
  41.     widget._trans_color_hack = color
  42.     # The X server sends us an expose event when the window becomes
  43.     # visible on screen. It means we need to draw the contents.  On a
  44.     # composited desktop expose is normally only sent when the window
  45.     # is put on the screen. On a non-composited desktop it can be
  46.     # sent whenever the window is uncovered by another.
  47.     #
  48.     # The screen-changed event means the display to which we are
  49.     # drawing changed. GTK+ supports migration of running
  50.     # applications between X servers, which might not support the
  51.     # same features, so we need to check each time.
  52.     widget.connect('expose-event', expose_transparent)
  53.     #win.connect('screen-changed', self.screen_changed)
  54.     # To check if the display supports alpha channels, get the colormap
  55.     screen = widget.get_screen()
  56.     colormap = screen.get_rgba_colormap()
  57.     # Now we have a colormap appropriate for the screen, use it
  58.     widget.set_colormap(colormap)
  59.     return False
  60.  
  61.  
  62.  
  63. import opencv as cv
  64. from opencv import highgui
  65. cv.cvSetNumThreads(3)       # 4 threads might be wasteful
  66. print 'opencv threads', cv.cvGetNumThreads()
  67.  
  68. '''
  69. useful utils:
  70. cv.Ipl2NumPy
  71. cv.Ipl2PIL
  72. cv.NumPy2Ipl
  73. cv.NumPy2PIL
  74. cv.PIL2Ipl
  75. cv.PIL2NumPy
  76.  
  77.  
  78. notes:
  79. CV_ADAPTIVE_THRESH_GAUSSIAN_C
  80. CV_ADAPTIVE_THRESH_MEAN_C
  81. CV_CALIB_CB_ADAPTIVE_THRESH
  82. CV_THRESH_BINARY
  83. CV_THRESH_BINARY_INV
  84. #something else? CV_THRESH_MASK     # adaptive only?
  85. #CV_THRESH_OTSU
  86. CV_THRESH_TOZERO
  87. CV_THRESH_TOZERO_INV
  88. CV_THRESH_TRUNC
  89. cvAdaptiveThreshold
  90. cvThreshHist
  91. cvThreshold
  92.  
  93. cvAdaptiveThreshold(*args)
  94.    cvAdaptiveThreshold(CvArr src, CvArr dst, double max_value, int adaptive_method = 0,
  95.        int threshold_type = 0, int block_size = 3,
  96.        double param1 = 5)
  97.  
  98. cvThreshold(*args)
  99.    cvThreshold(CvArr src, CvArr dst, double threshold, double max_value,
  100.        int threshold_type) -> double
  101.  
  102.  
  103. '''
  104.  
  105. _colorspaces = '''
  106. CV_BGR2BGR555
  107. CV_BGR2BGR565
  108. CV_BGR2BGRA
  109. CV_BGR2GRAY
  110. CV_BGR2HLS
  111. CV_BGR2HSV
  112. CV_BGR2Lab
  113. CV_BGR2Luv
  114. CV_BGR2RGB
  115. CV_BGR2RGBA
  116. CV_BGR2XYZ
  117. CV_BGR2YCrCb
  118. CV_BGR5552BGR
  119. CV_BGR5552BGRA
  120. CV_BGR5552GRAY
  121. CV_BGR5552RGB
  122. CV_BGR5552RGBA
  123. CV_BGR5652BGR
  124. CV_BGR5652BGRA
  125. CV_BGR5652GRAY
  126. CV_BGR5652RGB
  127. CV_BGR5652RGBA
  128. CV_BGRA2BGR
  129. CV_BGRA2BGR555
  130. CV_BGRA2BGR565
  131. CV_BGRA2GRAY
  132. CV_BGRA2RGB
  133. CV_BGRA2RGBA
  134. CV_BayerBG2BGR
  135. CV_BayerBG2RGB
  136. CV_BayerGB2BGR
  137. CV_BayerGB2RGB
  138. CV_BayerGR2BGR
  139. CV_BayerGR2RGB
  140. CV_BayerRG2BGR
  141. CV_BayerRG2RGB
  142. CV_GRAY2BGR
  143. CV_GRAY2BGR555
  144. CV_GRAY2BGR565
  145. CV_GRAY2BGRA
  146. CV_GRAY2RGB
  147. CV_GRAY2RGBA
  148. CV_HLS2BGR
  149. CV_HLS2RGB
  150. CV_HSV2BGR
  151. CV_HSV2RGB
  152. CV_Lab2BGR
  153. CV_Lab2RGB
  154. CV_Luv2BGR
  155. CV_Luv2RGB
  156. CV_RGB2BGR
  157. CV_RGB2BGR555
  158. CV_RGB2BGR565
  159. CV_RGB2BGRA
  160. CV_RGB2GRAY
  161. CV_RGB2HLS
  162. CV_RGB2HSV
  163. CV_RGB2Lab
  164. CV_RGB2Luv
  165. CV_RGB2RGBA
  166. CV_RGB2XYZ
  167. CV_RGB2YCrCb
  168. CV_RGBA2BGR
  169. CV_RGBA2BGR555
  170. CV_RGBA2BGR565
  171. CV_RGBA2BGRA
  172. CV_RGBA2GRAY
  173. CV_RGBA2RGB
  174. CV_XYZ2BGR
  175. CV_XYZ2RGB
  176. CV_YCrCb2BGR
  177. CV_YCrCb2RGB
  178. '''
  179. ColorSpaces = {}
  180. ColorSpacesByValue = {}
  181. for name in _colorspaces.splitlines():
  182.     name = name.strip()
  183.     if name:
  184.         value = getattr(cv,name)
  185.         ColorSpacesByValue[ value ] = name
  186.         ColorSpaces[ name ] = value
  187.  
  188. def pygame_to_pil_img(pg_img):
  189.     imgstr = pygame.image.tostring(pg_img, 'RGB')
  190.     return Image.fromstring('RGB', pg_img.get_size(), imgstr)
  191.  
  192. def pil_to_pygame_img(pil_img):
  193.     imgstr = pil_img.tostring()
  194.     return pygame.image.fromstring(imgstr, pil_img.size, 'RGB')
  195.  
  196. class Trackable(object):
  197.     def __init__(self, haar):
  198.         self.haar = haar
  199.         self._cv_storage = cv.cvCreateMemStorage(0)
  200.         self.score = .0
  201.         self.rects = []
  202.         self.color = None
  203.         self.grayscale = None
  204.  
  205.     #       cv.cvSetImageROI( grayscale, cv.cvRect(fx, fy, fw, fh) )
  206.     #       cv.cvClearMemStorage(storage)   # this invalidates the f.x, f.* attributes
  207.     def detector( self, grayscale, scale=1 ):
  208.         self.grayscale = grayscale
  209.         storage = self._cv_storage
  210.         cv.cvClearMemStorage(storage)
  211.         # equalize histogram
  212.         cv.cvEqualizeHist(grayscale, grayscale)
  213.  
  214.         _rects = cv.cvHaarDetectObjects(grayscale, self.haar, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(25, 25))
  215.         rects = []
  216.         if _rects:
  217.             for r in _rects:
  218.                 rects.append( pygame.rect.Rect(r.x*scale, r.y*scale, r.width*scale, r.height*scale) )
  219.         return rects
  220.  
  221. _cfg_ubytes =  'active alpha blur athresh_block_size thresh_min thresh_max'.split()
  222. _cfg_ubytes += 'FXstencil FXblur FXsobel FXathresh FXthresh FXdetect'.split()
  223. class LayerConfig( ctypes.Structure ):
  224.     _fields_ = [ ('colorspace',ctypes.c_int) ]
  225.     for tag in _cfg_ubytes: _fields_.append( (tag, ctypes.c_ubyte) )
  226.     #for tag in _cfg_ints: _fields_.append( (tag, ctypes.c_int) )
  227.     del tag
  228.  
  229. class UI(object):
  230.     def __init__(self, active, layers):
  231.         self.active = active        # shared
  232.         self.layers = layers        # shared
  233.  
  234.         ## gtk ##
  235.         self.window = win = gtk.Window()
  236.         win.set_title( 'Harts MultiProcess Transparent WebCam' )
  237.         win.connect('destroy', lambda w: gtk.main_quit() )
  238.         transparent_window( win, decorate=True )
  239.         root = gtk.HBox(); root.set_border_width( 3 )
  240.         win.add( root )
  241.  
  242.         #####################################
  243.         eb = gtk.EventBox()
  244.         root.pack_start( eb )
  245.         self._drawing_area = da = gtk.DrawingArea()
  246.         da.set_size_request( 640,480 )
  247.         da.connect('realize', self.realize)
  248.         eb.add( da )
  249.         make_transparent(da)
  250.  
  251.         ##################
  252.         eb = gtk.EventBox()
  253.         root.pack_start( eb, expand=False )
  254.         split = gtk.VBox(); eb.add( split )
  255.  
  256.         ex = gtk.Expander( 'settings' ); ex.set_expanded(False)
  257.         split.pack_start( ex, expand=False )
  258.  
  259.  
  260.         ex = gtk.Expander( 'adjust layers' ); ex.set_expanded(True)
  261.         split.pack_start( ex, expand=True)
  262.  
  263.         note = gtk.Notebook()
  264.         note.set_tab_pos( gtk.POS_RIGHT )
  265.         ex.add( note )
  266.         for layer in layers:
  267.                 cspace = ColorSpacesByValue[ layer.colorspace ]
  268.                 tag = cspace.split('2')[-1]
  269.                 page = gtk.HBox()
  270.                 lab = gtk.Label(tag)
  271.                 note.append_page( page, lab )
  272.                 col1, col2 = gtk.VBox(), gtk.VBox()
  273.                 page.pack_start( col1, expand=False )
  274.                 page.pack_start( col2, expand=True )
  275.  
  276.                 for name in dir(layer):
  277.                     if not name.startswith('_') and name != 'colorspace':
  278.                         val = getattr( layer, name )
  279.                         if name.startswith('FX') or name == 'active':
  280.                             b = gtk.CheckButton( name )
  281.                             b.set_active( bool(val) )
  282.                             b.connect('toggled', lambda b,lay,nam: setattr(lay,nam,bool(b.get_active())), layer, name)
  283.                             #frame = gtk.Frame(name)
  284.                             #frame.add( b )
  285.                             col1.pack_start( b )
  286.                         else:
  287.                             adjust = gtk.Adjustment(
  288.                                 value=val,
  289.                                 lower=0, upper=255,
  290.                                 step_incr=1 )
  291.                             adjust.connect("value_changed", lambda a,lay,nam: setattr(lay,nam,int(a.value)), layer,name)
  292.                             scale = gtk.HScale( adjust )
  293.                             scale.set_digits(0)
  294.                             frame = gtk.Frame(name)
  295.                             frame.add( scale )
  296.                             col2.pack_start( frame )
  297.  
  298.         win.show_all()
  299.  
  300.     def realize( self, da ):
  301.         wid = da.window.xid
  302.         os.environ['SDL_WINDOWID'] = str(wid)       # child fork respects environ
  303.         self.subprocess = p = multiprocessing.Process(target=subprocess, args=(active,layers))
  304.         p.start()
  305.         #p.join()
  306.  
  307. class Camera(object):
  308.     try:    ## opencv haar detect is multithreaded! ##
  309.         DO_HAAR = True
  310.         FaceCascade = cv.cvLoadHaarClassifierCascade('haarcascades/haarcascade_frontalface_alt.xml', cv.cvSize(1,1))
  311.         EyesCascade = cv.cvLoadHaarClassifierCascade('haarcascades/haarcascade_eye.xml', cv.cvSize(1,1))
  312.     except:
  313.         print 'download opencv latest source code, and copy the haarcascades folder to here'
  314.         DO_HAAR = False
  315.  
  316.     def __init__(self, layers):
  317.         self.layers = layers
  318.  
  319.         pygame.display.init()
  320.         SSBACKEND = pygame.transform.get_smoothscale_backend()
  321.         if SSBACKEND == 'GENERIC':
  322.             try: pygame.transform.set_smoothscale_backend( 'SSE' ); SSBACKEND = 'SSE'
  323.             except: print 'SSE backend not available'
  324.         print 'smooth scale backend', SSBACKEND
  325.  
  326.  
  327.         self.prevfaces = None
  328.         self.active = True
  329.         self.index = 0
  330.         for arg in sys.argv:
  331.             if arg.startswith('camera='): self.index = int(arg.split('=')[-1]); break
  332.         self.camera_pointer = highgui.cvCreateCameraCapture(self.index)     # this is the old swig bindings ubuntu lucid works fine
  333.  
  334.         # HIGHGUI ERROR: V4L: setting property #16 is not supported
  335.         #highgui.cvSetCaptureProperty( self.camera_pointer, highgui.CV_CAP_PROP_CONVERT_RGB, True )
  336.         ## default color space is BGR - linux ##
  337.         self.resize_capture( 640, 480 )
  338.         self.resize_output( 640, 480 )
  339.  
  340.         if self.DO_HAAR:
  341.             self.track_face = Trackable( self.FaceCascade )
  342.  
  343.     def resize_capture( self, x,y ):
  344.         self.cwidth = x
  345.         self.cheight = y
  346.         highgui.cvSetCaptureProperty( self.camera_pointer, highgui.CV_CAP_PROP_FRAME_WIDTH, self.cwidth )
  347.         highgui.cvSetCaptureProperty( self.camera_pointer, highgui.CV_CAP_PROP_FRAME_HEIGHT, self.cheight )
  348.  
  349.     def resize_output( self, x, y ):
  350.         self.owidth = x
  351.         self.oheight = y
  352.         pygame.display.set_mode((x,y), 0, 32 )      # size, flags, depth
  353.         self.screen = pygame.display.get_surface()
  354.  
  355.     def loop(self):
  356.         ## BGR - linux
  357.         _frame = highgui.cvQueryFrame(self.camera_pointer)      # grabFrame returns 1?
  358.         if not _frame: print 'lost connection to webcam?'
  359.  
  360.         self.screen.fill( (0,0,0,0) )
  361.         surf = self.screen.copy()
  362.         surf.fill( (0,0,255) )
  363.         surf.set_alpha(255)
  364.         stencil = None
  365.         for layer in self.layers:
  366.             if layer.active:
  367.                 #print layer
  368.                 _gray8 = cv.cvCreateImage((self.cwidth,self.cheight), cv.IPL_DEPTH_8U, 1)
  369.                 _gray32 = cv.cvCreateImage((self.cwidth,self.cheight), cv.IPL_DEPTH_32F, 1)
  370.  
  371.                 a = cv.cvCreateImage((self.cwidth,self.cheight), cv.IPL_DEPTH_8U, 3)
  372.                 cv.cvCvtColor(_frame, a, layer.colorspace)
  373.                 #print 'colorspaced converted'
  374.                 ## FX
  375.                 if layer.FXblur:            # blur before threshing
  376.                     blur = layer.blur
  377.                     if blur < 1: blur = 1
  378.                     cv.cvSmooth( a, a, cv.CV_BLUR, blur )
  379.                 #if layer.FXsobel:
  380.                 if layer.FXthresh:
  381.                     cv.cvThreshold( a, a, layer.thresh_min, layer.thresh_max, cv.CV_THRESH_BINARY )
  382.                 if layer.FXathresh:
  383.                     cv.cvCvtColor(a, _gray8, cv.CV_RGB2GRAY)
  384.                     blocksize = layer.athresh_block_size
  385.                     if blocksize <= 2: blocksize = 3
  386.                     if blocksize % 2 != 1: blocksize += 1
  387.                     cv.cvAdaptiveThreshold(_gray8, _gray8, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY, blocksize )
  388.                     cv.cvCvtColor(_gray8, a, cv.CV_GRAY2RGB)
  389.  
  390.                 ## pygame
  391.                 b = pygame.image.frombuffer(a.imageData, (self.cwidth,self.cheight), 'RGB')
  392.                 b.set_alpha( layer.alpha )
  393.                 surf.blit(b, (0,0))
  394.  
  395.         self.screen.lock()
  396.         array = pygame.surfarray.pixels_alpha(self.screen)
  397.         if stencil:
  398.             alpha = pygame.surfarray.pixels3d( stencil )
  399.             array[:] = alpha[:,:,0]
  400.             del alpha
  401.         else: array[:] = 255            # values higher than 256 are additive!
  402.         del array
  403.         self.screen.unlock()
  404.         self.screen.blit( surf, (0,0) )
  405.         pygame.display.flip()
  406.         return self.active
  407.  
  408.         _rgb = cv.cvCreateImage((self.cwidth,self.cheight), cv.IPL_DEPTH_8U, 3)
  409.         cv.cvCvtColor(_frame, _rgb, cv.CV_BGR2RGB)          # interesting chaos if _frame,_rgb is flipped
  410.         rgb = pygame.image.frombuffer(_rgb.imageData, (self.cwidth,self.cheight), 'RGB')
  411.         cfg = self.config['rgb']
  412.         rgb.set_alpha( cfg['alpha'] )
  413.  
  414.         cfg = self.config['hsv']
  415.         _hsv = cv.cvCreateImage((self.cwidth,self.cheight), cv.IPL_DEPTH_8U, 3)
  416.         cv.cvCvtColor(_rgb, _hsv, cv.CV_RGB2HSV)
  417.         blur = cfg['blur']
  418.         if blur < 1: blur = 1
  419.         cv.cvSmooth( _hsv, _hsv, cv.CV_BLUR, blur )     # big help for removing noise
  420.         hsv = pygame.image.frombuffer(_hsv.imageData, (self.cwidth,self.cheight), 'RGB')
  421.         hsv.set_alpha( cfg['alpha'] )
  422.  
  423.  
  424.         _chrome = cv.cvCreateImage((self.cwidth,self.cheight), cv.IPL_DEPTH_8U, 3)
  425.         cv.cvCvtColor(_rgb, _chrome, cv.CV_RGB2YCrCb) #cv.cvCvtColor(_rgb, _luv, cv.CV_RGB2HLS)     # blocky reds
  426.         chrome = pygame.image.frombuffer(_chrome.imageData, (self.cwidth,self.cheight), 'RGB')
  427.         cfg = self.config['chrome']
  428.         chrome.set_alpha( cfg['alpha'] )
  429.  
  430.  
  431.         # create grayscale version first (faster resize)
  432.         grayscale = cv.cvCreateImage((self.cwidth,self.cheight), cv.IPL_DEPTH_8U, 1)
  433.         grayscale2 = cv.cvCreateImage((self.cwidth/2,self.cheight/2), cv.IPL_DEPTH_8U, 1)
  434.         cv.cvCvtColor(_rgb, grayscale, cv.CV_RGB2GRAY)
  435.  
  436.         interpolation = 0       # detector much better without interp
  437.         cv.cvResize(grayscale, grayscale2, interpolation)
  438.         if self.DO_HAAR: faces = self.track_face.detector( grayscale2, scale=2 )        # operates in grayscale
  439.         else: faces = []
  440.  
  441.         cfg = self.config['simple-thresh']
  442.         blur = cfg['blur']
  443.         if blur < 1: blur = 1
  444.         cv.cvSmooth( grayscale, grayscale, cv.CV_BLUR, blur )       # big help for removing noise
  445.         _thresh = cv.cvCreateImage((self.cwidth,self.cheight), cv.IPL_DEPTH_8U, 3)
  446.         _thresh8 = cv.cvCreateImage((self.cwidth,self.cheight), cv.IPL_DEPTH_8U, 1)
  447.         cv.cvThreshold( grayscale, _thresh8, cfg['min'], cfg['max'], cv.CV_THRESH_BINARY )
  448.         cv.cvCvtColor(_thresh8, _thresh, cv.CV_GRAY2RGB)
  449.         threshed = pygame.image.frombuffer(_thresh.imageData, (self.cwidth,self.cheight), 'RGB')
  450.         threshed.set_alpha( cfg['alpha'] )
  451.  
  452.         cfg = self.config['opencv-adaptive-thresh']
  453.         ## Gaussian gives more detail than Mean - AdaptiveThreshold
  454.         blocksize = cfg['block-size']
  455.         if blocksize <= 2: blocksize = 3
  456.         if blocksize % 2 != 1: blocksize += 1
  457.         cv.cvAdaptiveThreshold(grayscale, _thresh8, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY, blocksize )
  458.         cv.cvCvtColor(_thresh8, _thresh, cv.CV_GRAY2RGB)
  459.         athreshed = pygame.image.frombuffer(_thresh.imageData, (self.cwidth,self.cheight), 'RGB')
  460.         athreshed.set_alpha( cfg['alpha'] )
  461.  
  462.         _sobel = cv.cvCreateImage((self.cwidth,self.cheight), cv.IPL_DEPTH_8U, 3)
  463.         _sobel32 = cv.cvCreateImage((self.cwidth,self.cheight), cv.IPL_DEPTH_32F, 1)
  464.         _sobel8 = cv.cvCreateImage((self.cwidth,self.cheight), cv.IPL_DEPTH_8U, 1)
  465.         cv.cvSobel( grayscale, _sobel32, 1, 1, 5 )  #xorder, yorder, aperture
  466.         #cv.cvSobel( _thresh8, _sobel32, 3, 3, 19 ) #xorder, yorder, aperture
  467.         cv.cvConvert( _sobel32, _sobel8 ); cv.cvCvtColor(_sobel8, _sobel, cv.CV_GRAY2RGB)
  468.         sobel = pygame.image.frombuffer(_sobel.imageData, (self.cwidth,self.cheight), 'RGB')
  469.         cfg = self.config['sobel']
  470.         sobel.set_alpha( cfg['alpha'] )
  471.  
  472.         if not faces and self.prevfaces: faces = self.prevfaces
  473.         else: self.prevfaces = faces
  474.         pgthreshed = hsv.copy()
  475.         for rect in faces:
  476.             acolor = pygame.transform.average_color(rgb, rect)
  477.             pygame.transform.threshold(pgthreshed,rgb,acolor,(30,100,30),(0,0,0),1)     #  arg1=dest, arg2=source (must be different)
  478.             break
  479.         cfg = self.config['pygame-adaptive-thresh']
  480.         pgthreshed.set_alpha( cfg['alpha'] )
  481.  
  482.         #if self.cwidth*2 == self.owidth and self.cheight*2 == self.oheight:
  483.         #   surf = pygame.transform.scale2x( surf )
  484.         #elif self.cwidth != self.owidth or self.cheight != self.oheight:
  485.         #   if SSBACKEND != 'GENERIC':
  486.         #       surf = pygame.transform.smoothscale( surf, (self.owidth, self.oheight) )
  487.         #   else:
  488.         #       surf = pygame.transform.scale( surf, (self.owidth, self.oheight) )
  489.  
  490.         self.screen.fill( (0,0,0,0) )       # my trick
  491.  
  492.         s = self.screen.copy()
  493.         s.fill( (0,0,255) )
  494.         for surf in (threshed, athreshed, pgthreshed, chrome, sobel, hsv, rgb):
  495.             if surf.get_alpha():
  496.                 s.blit(surf, (0,0))
  497.  
  498.         #s=s.convert_alpha()
  499.         self.screen.lock()
  500.         alpha = pygame.surfarray.pixels3d( threshed )
  501.         array = pygame.surfarray.pixels_alpha(self.screen)
  502.         array[:] = alpha[:,:,0]
  503.         del array
  504.         del alpha
  505.         self.screen.unlock()
  506.  
  507.         self.screen.blit( s, (0,0) )
  508.  
  509.  
  510.         for rect in faces:
  511.             pygame.draw.rect( self.screen, (0,255,0, 128), rect, 1 )
  512.         #self.screen.set_alpha(0)
  513.  
  514.         pygame.display.flip()
  515.         return self.active
  516.  
  517.  
  518. #CV_BGR2HLS
  519. #CV_BGR2Luv
  520. #CV_BGR2XYZ
  521. _default_spaces = [
  522.     (cv.CV_BGR2RGB,),
  523.     (cv.CV_BGR2HSV,),
  524.     (cv.CV_BGR2Lab,),
  525.     (cv.CV_BGR2YCrCb,),
  526. ]
  527.  
  528. def subprocess( active, layers ):
  529.     cam = Camera( layers )
  530.     while active.value:
  531.         cam.loop()
  532.  
  533.  
  534. if __name__ == '__main__':
  535.     active = sharedctypes.Value('i', 1, lock=False)
  536.     layers = sharedctypes.Array( LayerConfig, _default_spaces, lock=False )
  537.     layers[0].active = 1
  538.     layers[0].alpha = 255
  539.  
  540.     gui = UI( active, layers )
  541.     gtk.main()
  542.     gui.active.value = 0
  543.     gui.subprocess.join()
  544.     print 'main process exit'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement