Attachment 'SimpleReceptiveField.py'

Download

   1 #!/usr/bin/env python
   2 
   3 """
   4 
   5 SimpleReceptiveField.py : linking the webcam to a crude-but-adaptive neuron and
   6 let it spike in the loudspeaker.
   7 
   8 Laurent Perrinet, 2010. Credits: see http://www.incm.cnrs-mrs.fr/LaurentPerrinet/SimpleCellDemo
   9 
  10 this depends on OpenCV + numpy + pil, which intalls fine with MacPorts: {{{
  11 sudo port install opencv +python26 +tbb
  12 sudo port install py26-numpy py26-scipy py26-pil py26-distribute py26-pip python_select
  13 sudo python_select python26
  14 sudo port install py26-ipython py26-matplotlib py26-mayavi py26-pyobjc2-cocoa  # optionnally
  15 }}}
  16 
  17 Credits: see pysight.py and EgoViewer.py
  18 
  19 $Id: SimpleReceptiveField.py 4251 2010-10-10 10:47:29Z perrinet $
  20 
  21 """
  22 try:
  23     import pyaudio
  24     AUDIO = True
  25 except:
  26     print('Could not import pyaudio, disabling sound')
  27     AUDIO = False
  28 
  29 from numpy import zeros, linspace, hstack, transpose, pi
  30 
  31 import cv
  32 
  33 import numpy as np
  34 import time
  35 
  36 # TODO : plot histogram and membrane potential
  37 # TODO : remove background of RF
  38 
  39 #########################################################
  40 #########################################################
  41 NUM_SAMPLES = 1024
  42 # neural parameters
  43 spike = 255*np.ones(45) # that's a crude spike!
  44 quant = 512
  45 rate = 0.01
  46 adaptive = True
  47 sample = 5
  48 sigma = 8.
  49 
  50 import numpy as np
  51 
  52 #============================================================================
  53 # Set up input
  54 #============================================================================
  55 def cv2array(im):
  56     depth2dtype = {
  57         cv.IPL_DEPTH_8U: 'uint8',
  58         cv.IPL_DEPTH_8S: 'int8',
  59         cv.IPL_DEPTH_16U: 'uint16',
  60         cv.IPL_DEPTH_16S: 'int16',
  61         cv.IPL_DEPTH_32S: 'int32',
  62         cv.IPL_DEPTH_32F: 'float32',
  63         cv.IPL_DEPTH_64F: 'float64',
  64     }
  65 
  66     arrdtype = im.depth
  67     a = np.fromstring(
  68          im.tostring(),
  69          dtype=depth2dtype[im.depth],
  70          count=im.width*im.height*im.nChannels)
  71     a.shape = (im.height, im.width, im.nChannels)
  72 
  73     return a
  74 
  75 def retina(img, ret=None):
  76     """
  77     dummy retina:
  78      - smooth
  79      - derive
  80 
  81     """
  82     dst = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_32F, 3)
  83 #    ret_ = cv.CloneImage(ret)
  84 # format = (frame.width,frame.height)
  85 # frameHSV = cv.CreateImage(format, cv.IPL_DEPTH_8U, 3)
  86 # 
  87 # # calculate HSV and split
  88 # cv.CvtColor(frame, frameHSV, cv.CV_BGR2HSV)
  89 # cv.Split(frameHSV, frameH, frameS, frameBW, None)
  90 # cv.Copy(frame, frameShow)
  91 # 
  92 #     cv.CvtColor(im, im, cv.CV_RGB2GRAY)
  93     cv.Laplace(img, dst)
  94     cv.Smooth(dst, dst, smoothtype=cv.CV_GAUSSIAN, param1=7, param2=0, param3=0, param4=0)
  95 
  96 #     image = np.flipud(np.fliplr(cv2array(dst)))
  97 #     image /= np.max(np.abs(image))
  98 #     if not(ret == None): 
  99 # #         print cv.GetSize(img), cv.GetSize(ret)
 100 #         cv.AddWeighted(dst, 1, ret, -1., 0., dst)
 101 
 102     return dst # cv.GetMat(dst)
 103 
 104 
 105 def do_RF(init=False):
 106 #         if not(os.path.isfile('RF.png'))
 107 # 
 108 #         x, y = np.mgrid[-1:1:1j*img.height , -1:1:1j*img.width]
 109 #         mask = np.exp(-.5*(x**2/(.15*4/3)**2 +y**2/.15**2))
 110 #         mat = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_32F, 3)
 111 # # cv.CreateMat(img.height, img.width, cv.CV_32FC1)
 112 #         cv.SetData(mat, cv.fromarray(mask).tostring(), 12*img.width)
 113          #cv.GetImage()
 114 #         dst = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_32F, 3)
 115 
 116 #         cv.Mul(RF, mat, RF, 1.)
 117 # 
 118     img = cv.QueryFrame(capture)
 119 # TODO:        img = cv.GetMat(cv.QueryFrame(capture))
 120     if init:
 121         RF = retina(img, ret)
 122     else:
 123         RF = retina(img)
 124 #       mat = cv.fromarray(RF)
 125 #         cv.Resize(RF, img_, interpolation=cv.CV_INTER_LINEAR)
 126     cv.ShowImage("Receptive Field", RF) # cv.fromarray(RF, allowND=True))
 127 
 128     return RF
 129 
 130 #============================================================================
 131 # Create the model.
 132 #============================================================================
 133 corr = 0
 134 voltage = 0.5
 135 hist = np.ones(quant) / quant
 136 result = cv.CreateMat(1, 1, cv.CV_32FC1)
 137 
 138 def neuron(im, voltage, hist):
 139     if voltage > 1.: voltage = 0.
 140     cv.MatchTemplate(im, RF, result, cv.CV_TM_CCORR_NORMED)
 141     corr = result[0, 0]
 142     quantile = int(((corr+1)/2) * quant)-1
 143     if adaptive:
 144         cumhist = np.cumsum(hist)
 145         voltage = cumhist[quantile]
 146         if voltage > .9:
 147             if AUDIO: stream.write(spike)
 148             voltage = 2.
 149 
 150         hist[quantile] += rate
 151         hist /= np.sum(hist)
 152 
 153     else:
 154         if corr > .15:
 155             if AUDIO: stream.write(spike)
 156 
 157     return corr, voltage
 158 #============================================================================
 159 if __name__ == "__main__":
 160 #     print "OpenCV version: %s (%d, %d, %d)" % (cv.VERSION,
 161 #                                                cv.MAJOR_VERSION,
 162 #                                                cv.MINOR_VERSION,
 163 #                                                cv.SUBMINOR_VERSION)
 164 #  
 165 #     print "Press ESC to exit ..."
 166 #  
 167 # 
 168 
 169     if AUDIO:
 170         # open audio stream to the speakers
 171         p = pyaudio.PyAudio()
 172         # initialize loudspeaker
 173         stream = p.open(format=pyaudio.paInt16,
 174                     channels=1,
 175                     rate=44100,
 176                     output=True)
 177 
 178     snapshotTime = time.time()
 179     capture = cv.CaptureFromCAM(1)
 180     # check if capture device is OK
 181     if not capture:
 182         print "Error opening capture device"
 183         sys.exit(1)
 184 
 185     downsize = 4
 186 
 187     img_ = cv.QueryFrame(capture)
 188 
 189     cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, img_.height / downsize)
 190     cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, img_.width / downsize)
 191     cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FORMAT, cv.IPL_DEPTH_32F)
 192 
 193     font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, thickness=2, lineType=cv.CV_AA)
 194     font_ = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, thickness=3, lineType=cv.CV_AA)
 195 
 196 
 197     print ' Startup time ', (time.time() - snapshotTime)*1000, ' ms'
 198     snapshotTime = time.time()
 199 
 200 
 201 #     print img.height / 8, img.width / 8 #img.rows / 8, img.cols / 8
 202 #     small = cv.CreateImage((img.height / 8, img.width / 8), cv.IPL_DEPTH_16S, 3)
 203 #     cv.Resize(img, small)
 204 #     dst = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_32F, 3)
 205 
 206     try:
 207 #         ret = cv.CreateImage(cv.GetSize(img_), cv.IPL_DEPTH_32F, 3)
 208 #         cv.Set(ret, 0.)
 209 # 
 210         cv.NamedWindow("Receptive Field", 0)
 211         RF = do_RF()
 212         cv.NamedWindow("Retina", 0)
 213 #         cv.NamedWindow("Info", 1)
 214 #         cv.MoveWindow("Info", 3*RF.width, 0)
 215         cv.MoveWindow("Receptive Field", 0*RF.width , 0)
 216         cv.ResizeWindow("Receptive Field", 2*RF.width, 2*RF.height)
 217 #         ret = cv.CreateImage(cv.GetSize(RF), cv.IPL_DEPTH_32F, 3)
 218         cv.Set(RF, 0.)
 219         cv.PutText(RF, 'SimpleCellDemo', (12, 48), font, cv.RGB(0, 255, 0))
 220         cv.PutText(RF, 'Press Esc to exit', (12, 96), font, cv.RGB(255, 0, 0))
 221         cv.PutText(RF, 'Press r to (re)draw', (12, 144), font, cv.RGB(0, 0, 255))
 222         cv.ShowImage("Receptive Field", RF)
 223         img = cv.QueryFrame(capture)
 224         ret = retina(img)
 225         cv.ShowImage("Retina", ret)
 226         cv.ResizeWindow("Retina", 2*RF.width, 2*RF.height)
 227         cv.MoveWindow("Retina", 2*RF.width, 0)
 228 
 229         while True:
 230             snapshotTime = time.time()
 231             img = cv.QueryFrame(capture)
 232             ret = retina(img, ret)
 233 #             cv.Resize(img, small)
 234             corr, Vm = neuron(ret, voltage, hist)
 235             backshotTime = time.time()
 236             fps = 1. / (backshotTime - snapshotTime)
 237 #             cv.Resize(ret, img_, interpolation=cv.CV_INTER_LINEAR)
 238             cv.PutText(ret, str('%d'  %fps) + ' fps', (12, 24), font_, cv.RGB(255, 255, 255))
 239             cv.PutText(ret, str('%d'  %fps) + ' fps', (12, 24), font, cv.RGB(0, 0, 0))
 240 
 241             cv.ShowImage("Retina", ret)
 242             key = cv.WaitKey(1)
 243 #             if not(key == -1): print key
 244             if key == 114: do_RF()
 245             if key == 27: break
 246 
 247     finally:
 248         # Always close the camera stream
 249         if AUDIO: stream.close()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2014-10-25 14:23:08, 0.6 KB) [[attachment:Makefile]]
  • [get | view] (2012-03-17 18:47:06, 28134.7 KB) [[attachment:SimpleReceptiveField.app.zip]]
  • [get | view] (2014-10-25 14:27:28, 7.8 KB) [[attachment:SimpleReceptiveField.py]]
  • [get | view] (2014-10-25 14:27:28, 4.6 KB) [[attachment:SimpleReceptiveField.zip]]
  • [get | view] (2014-10-25 14:27:29, 0.5 KB) [[attachment:pysight.py]]
  • [get | view] (2010-04-19 08:21:19, 321.4 KB) [[attachment:screenshot.png]]
  • [get | view] (2014-10-25 14:27:36, 1.7 KB) [[attachment:setup.py]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.

welcome: please sign in