def main():
    out_dir = 'images'
    try:
        os.makedirs(out_dir)
    except OSError:
        pass

    def callback(ws, **kw):
        def image_handler(*data):
            print(data[0])
            height, width = image_size(data[2])
            print((height, width))
            fn = '%s/%.5d_%.5d.jpg' % (out_dir, height, width)
            open(fn, 'w').write(data[2])

        def imageb64_handler(*data):
            data = list(data)
            data[2] = base64.b64decode(data[2])
            image_handler(*data)

        ws.subscribe('image', image_handler)
        ws.subscribe('imageb64', imageb64_handler)
        ws.handler_loop()

    wearscript.parse(callback, argparse.ArgumentParser())
def main():
    out_dir = 'big2glass_test'
    try:
        os.makedirs(out_dir)
    except OSError:
        pass
    doit(out_dir)
    return
    images = []  # (time, x, y, data)
    def callback(ws, **kw):
        def handler(*data):
            image_data = base64.b64decode(data[1])
            open(out_dir + '/%f-%d-%d.jpg' % (data[3], data[2][0], data[2][1]), 'w').write(image_data)
            images.append([data[3], data[2][0],
                           data[2][1], image_data])
            if len(images) == 6:
                ws.publish('annotationimagepoints', 'calibpt:%f' % data[3], data[1], 1, 'Click the point you looked at')

        def calib_pt_handler(channel, pts):
            print(pts)
            open(out_dir + '/%s-pt.js' % channel.split(':')[-1], 'w').write(json.dumps(pts[0]))
        def test():
            time.sleep(.1)
            image_data = base64.b64encode(open(glob.glob(out_dir + '/*.jpg')[0]).read())
            ws.publish('annotationimagepoints', 'calibpt', image_data, 1, 'Click the point you looked at')
        #gevent.spawn(test)
            
        ws.subscribe('calibimageb64pt', handler)
        ws.subscribe('calibpt', calib_pt_handler)
        ws.handler_loop()
    wearscript.parse(callback, argparse.ArgumentParser())
Example #3
0
def main():
    def callback(ws, **kw):
        print('Got args[%r]' % (kw,))
        print('Demo callback, prints all inputs and sends nothing')
        run = [None]
        kw.update(PARAMS)
        while run[0] != 'QUIT':
            for box, frame, hull, timestamp in pupil_iter(**kw):
                if kw.get('debug'):
                    run[0] = debug_iter(box, frame, hull, timestamp)
                if run[0]:
                    break
                if box is None:
                    continue
                ws.send('sensors', 'Pupil Eyetracker', {'Pupil Eyetracker': -2}, {'Pupil Eyetracker': [[[box[0][1], box[0][0], max(box[1][0], box[1][1])], time.time(), int(time.time() * 1000000000)]]})
                gevent.sleep(0)

            print(ws.receive())
    parser = argparse.ArgumentParser()
    parser.add_argument('--dump')
    parser.add_argument('--load')
    parser.add_argument('--command_thresh', type=int, default=6)
    parser.add_argument('--calib')
    parser.add_argument('--plot', action='store_true')
    parser.add_argument('--debug', action='store_true')
    wearscript.parse(callback, parser)
Example #4
0
def main():
    image_points = ImagePoints()
    image_match = ImageMatch()
    match_points = {}

    ar_model = picarus_takeout.ModelChain(msgpack.dumps([{'kw':{}, 'name': 'picarus.ARMarkerDetector'}]))
    print()
    
    def callback(ws, **kw):

        def image_handler(*data):
            groupDevice = '' # data[0].split(':', 1)[1]
            print('Image[%s]' % data[0])
            if groupDevice in match_points:
                st = time.time()
                pts = image_points(data[2])
                print('Points[%f]' % (time.time() - st))
                st = time.time()
                h = image_match(match_points[groupDevice], pts)
                print('Match[%f]' % (time.time() - st))
                print('Sending')
                ws.publish('warph:' + groupDevice, h.ravel().tolist())
            tags, tag_size = msgpack.loads(ar_model.process_binary(data[2]))
            ws.publish('warptags:image', np.array(tags).reshape(tag_size).tolist())

        def warp_sample_handler(*data):
            print('Warp Sample')
            match_points[data[1]] = image_points(data[2])
            tags, tag_size = msgpack.loads(ar_model.process_binary(data[2]))
            ws.publish('warptags:sample', np.array(tags).reshape(tag_size).tolist())

        ws.subscribe('image', image_handler)
        ws.subscribe('warpsample', warp_sample_handler)
        ws.handler_loop()
    wearscript.parse(callback, argparse.ArgumentParser())
Example #5
0
def main():
    parser = argparse.ArgumentParser(description='WearScript IRC Bot')
    parser.add_argument('server')
    parser.add_argument('channel')
    parser.add_argument('nickname')
    parser.add_argument('--port', default=6667, type=int)
    wearscript.parse(callback, parser)
Example #6
0
def main():
    def callback(ws, **kw):
        loop = gevent.spawn(ws.handler_loop)
        print('Got args[%r]' % (kw, ))
        print('Demo callback, prints all inputs and sends nothing')
        run = [None]
        calibdump = kw.get('calibdump')
        print('calibdump: %s' % calibdump)
        if calibdump:
            try:
                os.makedirs(calibdump)
            except OSError:
                pass

            def image_callback(*data):
                print(data[0])
                open(os.path.join(calibdump, '%f.jpg' % data[1]),
                     'w').write(data[2])

            ws.subscribe('image', image_callback)
        kw.update(PARAMS)
        while run[0] != 'QUIT':
            for box, frame, hull, timestamp, extra in pupil_iter(**kw):
                if kw.get('debug'):
                    run[0] = debug_iter(
                        box, frame, hull, timestamp,
                        extra)  # added extra as a parameter  5/25

                if ((run[0] != 'RELOAD') and
                    (run[0] != None)):  #changed this to not to break on reload
                    break
                if run[0] == 'RELOAD':  # added this to see if we can change parameters
                    kw.update(PARAMS)
                    print('updated parameters %s' % PARAMS)
                if box is None:
                    continue
                ws.publish('sensors:eyetracker', {'Pupil Eyetracker': -2}, {
                    'Pupil Eyetracker': [[[
                        box[0][1], box[0][0], extra['ratio'], extra['area'],
                        extra['radius'], PARAMS['save_data']
                    ],
                                          time.time(),
                                          int(time.time() * 1000000000)]]
                })
                time.sleep(1)

            print(ws.receive())
        loop.kill()

    parser = argparse.ArgumentParser()
    parser.add_argument('--dump')
    parser.add_argument('--load')
    parser.add_argument('--command_thresh', type=int, default=6)
    parser.add_argument('--calib')
    parser.add_argument('--calibdump')
    parser.add_argument('--camera_id', type=int, default=0)
    parser.add_argument('--plot', action='store_true')
    parser.add_argument('--debug', action='store_true')
    wearscript.parse(callback, parser)
Example #7
0
def main():
    print >> sys.stderr, "Generating export from frontmost keynote slideshow..."
    set_show()
    print >> sys.stderr, "Generating build previews..."
    prepare_show()
    generate_key()
    print >> sys.stderr, "Starting server..."
    address = start_serving()
    print >> sys.stderr, "Now serving on: http://%s:%d" % (address)
    print >> sys.stderr, "The PIN number is: %s" % (KEY)

    wearscript.parse(callback, argparse.ArgumentParser())

    try:
        while True:
            raw_input()
    finally:
        stop_serving()
Example #8
0
def main():
    image_points = ImagePoints()
    image_match = ImageMatch()
    match_points = {}

    ar_model = picarus_takeout.ModelChain(
        msgpack.dumps([{
            'kw': {},
            'name': 'picarus.ARMarkerDetector'
        }]))
    print()

    def callback(ws, **kw):
        def image_handler(*data):
            groupDevice = ''  # data[0].split(':', 1)[1]
            print('Image[%s]' % data[0])
            if groupDevice in match_points:
                st = time.time()
                pts = image_points(data[2])
                print('Points[%f]' % (time.time() - st))
                st = time.time()
                h = image_match(match_points[groupDevice], pts)
                print('Match[%f]' % (time.time() - st))
                print('Sending')
                ws.publish('warph:' + groupDevice, h.ravel().tolist())
            tags, tag_size = msgpack.loads(ar_model.process_binary(data[2]))
            ws.publish('warptags:image',
                       np.array(tags).reshape(tag_size).tolist())

        def warp_sample_handler(*data):
            print('Warp Sample')
            match_points[data[1]] = image_points(data[2])
            tags, tag_size = msgpack.loads(ar_model.process_binary(data[2]))
            ws.publish('warptags:sample',
                       np.array(tags).reshape(tag_size).tolist())

        ws.subscribe('image', image_handler)
        ws.subscribe('warpsample', warp_sample_handler)
        ws.handler_loop()

    wearscript.parse(callback, argparse.ArgumentParser())
def main():
    out_dir = 'images'
    try:
        os.makedirs(out_dir)
    except OSError:
        pass
    def callback(ws, **kw):
        def image_handler(*data):
            print(data[0])
            height, width = image_size(data[2])
            print((height, width))
            fn = '%s/%.5d_%.5d.jpg' % (out_dir, height, width)
            open(fn, 'w').write(data[2])
        def imageb64_handler(*data):
            data = list(data)
            data[2] = base64.b64decode(data[2])
            image_handler(*data)
        ws.subscribe('image', image_handler)
        ws.subscribe('imageb64', imageb64_handler)
        ws.handler_loop()
    wearscript.parse(callback, argparse.ArgumentParser())
Example #10
0
def main():
    def callback(ws, **kw):
        loop = gevent.spawn(ws.handler_loop)
        print('Got args[%r]' % (kw,))
        print('Demo callback, prints all inputs and sends nothing')
        run = [None]
        calibdump = kw.get('calibdump')
        if calibdump:
            try:
                os.makedirs(calibdump)
            except OSError:
                pass
            def image_callback(*data):
                print(data[0])
                open(os.path.join(calibdump, '%f.jpg' % data[1]), 'w').write(data[2])
            ws.subscribe('image:glass', image_callback)
        kw.update(PARAMS)
        while run[0] != 'QUIT':
            for box, frame, hull, timestamp, extra in pupil_iter(**kw):
                if kw.get('debug'):
                    run[0] = debug_iter(box, frame, hull, timestamp)
                if run[0]:
                    break
                if box is None:
                    continue
                ws.publish('sensors:eyetracker', {'Pupil Eyetracker': -2}, {'Pupil Eyetracker': [[[box[0][1], box[0][0], extra['ratio'], extra['area'], extra['radius']], time.time(), int(time.time() * 1000000000)]]})
                time.sleep(0)

            print(ws.receive())
        loop.kill()
    parser = argparse.ArgumentParser()
    parser.add_argument('--dump')
    parser.add_argument('--load')
    parser.add_argument('--command_thresh', type=int, default=6)
    parser.add_argument('--calib')
    parser.add_argument('--calibdump')
    parser.add_argument('--camera_id', type=int, default=0)
    parser.add_argument('--plot', action='store_true')
    parser.add_argument('--debug', action='store_true')
    wearscript.parse(callback, parser)
def main():
    out_dir = 'big2glass_test'
    try:
        os.makedirs(out_dir)
    except OSError:
        pass
    doit(out_dir)
    return
    images = []  # (time, x, y, data)

    def callback(ws, **kw):
        def handler(*data):
            image_data = base64.b64decode(data[1])
            open(out_dir + '/%f-%d-%d.jpg' % (data[3], data[2][0], data[2][1]),
                 'w').write(image_data)
            images.append([data[3], data[2][0], data[2][1], image_data])
            if len(images) == 6:
                ws.publish('annotationimagepoints', 'calibpt:%f' % data[3],
                           data[1], 1, 'Click the point you looked at')

        def calib_pt_handler(channel, pts):
            print(pts)
            open(out_dir + '/%s-pt.js' % channel.split(':')[-1],
                 'w').write(json.dumps(pts[0]))

        def test():
            time.sleep(.1)
            image_data = base64.b64encode(
                open(glob.glob(out_dir + '/*.jpg')[0]).read())
            ws.publish('annotationimagepoints', 'calibpt', image_data, 1,
                       'Click the point you looked at')

        #gevent.spawn(test)

        ws.subscribe('calibimageb64pt', handler)
        ws.subscribe('calibpt', calib_pt_handler)
        ws.handler_loop()

    wearscript.parse(callback, argparse.ArgumentParser())
Example #12
0
def main():
    def callback(ws, **kw):

        global save_data
        save_data = False
        '''
        This method gets the mouse's click in order to save the data or not.
        '''
        def my_mouse_callback(event, x, y, flags, param):
            global save_data
            print "Estoy dentro de la funcion del click"
            if event == cv.CV_EVENT_LBUTTONDBLCLK:  # here event is left mouse button double-clicked
                save_data = not save_data

        '''
        This method gets the mouse's click in order to save the data or not.
        '''

        def get_image(chan, timestamp, image):
            print('Image[%s] Time[%f] Bytes[%d]' %
                  (chan, timestamp, len(image))
                  )  #gets the image parameters from subscribe's method
            global refresh
            global image_dec

            if refresh:
                img_stream_str = cStringIO.StringIO(
                    base64.b64decode(image)
                )  #Decode de image codified in base64 and store it on img_stream_str
                img_array = np.asarray(
                    bytearray(img_stream_str.read()), dtype=np.uint8
                )  #obtain the bytes from the image decodified
                image_dec = cv2.imdecode(
                    img_array, cv2.CV_LOAD_IMAGE_COLOR)  #Load the image
                image_dec = cv2.resize(
                    image_dec, (640, 360))  #resize to the window's size

        '''
        This method includes the position's logic  and the data obtained through the subscribe method at the bottom of this file.
        '''

        def get_sensors(chan, names, result):
            print('Sensors[%s] Names[%r] Samples[%r]' % (chan, names, result))
            x = xreal = y = yreal = yref = ratio = area = radius = time1 = time2 = 0.0  #Variable declaration
            xtemp = 0
            ytemp = 0
            global image_dec, db, path, key_session
            eyeTrackerValues = result['Pupil Eyetracker'][0][
                0]  #Positions X, Y
            x = eyeTrackerValues[0]  # X
            y = eyeTrackerValues[1]  # Y
            ratio = eyeTrackerValues[2]  # ratio
            area = eyeTrackerValues[3]  # area
            radius = eyeTrackerValues[4]  # radius
            time1 = result['Pupil Eyetracker'][0][1]  #Time 1
            time2 = result['Pupil Eyetracker'][0][2]  #Time 2
            xint = int(round(x))
            yint = int(round(y))
            '''### X Position Logic ###'''
            xmax = kw.get('xmax')  # gets the xmax parameter
            xmin = kw.get('xmin')  # gets the xmin parameter
            ymax = kw.get('ymax')  # gets the ymax parameter
            ymin = kw.get('ymin')  # gets the ymax parameter

            #Calculate the position on the window
            xdiff = xmax - xmin
            ydiff = ymax - ymin
            xtemp = xmax - xint
            ytemp = yint - ymin

            # X limitations
            if xtemp <= 0:
                xtemp = 1
            elif xtemp >= xdiff:
                xtemp = xdiff
            xreal = (xtemp * 640) / xdiff
            '''### Y Position Logic ###'''
            # Y limitations
            if ytemp <= 0:
                ytemp = 1
            elif ytemp >= ydiff:
                ytemp = ydiff
            yreal = (ytemp * 360) / ydiff  # Position Calculated
            print("x: [%d]" % (x))
            print("y: [%d]" % (y))
            print("xreal: [%d]" % (xreal))
            print("yreal: [%d]" % (yreal))

            myclone = cv2.copyMakeBorder(
                image_dec, 0, 0, 0, 0, cv2.BORDER_DEFAULT
            )  #Clone the image to replace the old one each time a position is gotten
            image_view = myclone
            xresult = int(round(xreal))
            yresult = int(round(yreal))
            cv2.ellipse(image_view, (xresult, yresult), (50, 20), 0, 0, 360,
                        (0, 255, 0), -1)  #It draws the ellipse

            #The following shows the strings with the eye positions
            font = cv2.FONT_ITALIC
            texrx = str(int(x))
            texry = str(int(y))
            textxf = str(xresult)
            textyf = str(yresult)
            cv2.putText(image_view, 'Final X: ' + textxf, (10, 320), font, 0.6,
                        (255, 255, 255), 2)
            cv2.putText(image_view, 'Final Y: ' + textyf, (10, 350), font, 0.6,
                        (255, 255, 255), 2)
            cv2.putText(image_view, 'Pos X: ' + texrx, (150, 320), font, 0.6,
                        (255, 255, 255), 2)
            cv2.putText(image_view, 'Pos Y: ' + texry, (150, 350), font, 0.6,
                        (255, 255, 255), 2)

            #If save_data = True insert into the database the eye parameters as well as the image on the filesystem.
            #Besides, draw the REC figure on the window
            if (save_data):
                imagepath = datetime.datetime.now().strftime(
                    "%Y%m%d%H%M%S.jpg")
                cv2.imwrite(path + '/%s' % (imagepath, ), image_view)
                inserted_data = dbEye.insert_eye_data(db, xresult, yresult,
                                                      radius, ratio,
                                                      key_session, path,
                                                      imagepath, 640, 360, 0)
                cv2.circle(image_view, (565, 15), 10, (0, 0, 255), -1)
                cv2.putText(image_view, 'REC', (580, 20), font, 0.6,
                            (255, 255, 255), 2)

            cv2.namedWindow("Descodificada")
            cv2.setMouseCallback("Descodificada", my_mouse_callback)
            print "Double click selected"

            #SHOW THE MAIN WINDOW
            cv2.imshow("Descodificada", image_view)
            time.sleep(0.5)
            #Wait enought till getting the next position
            key = cv2.waitKey(400)
            if key == 27:
                refresh = False
            elif key == 13:
                refresh = True

        #The subscribe methods that gets the image and position from publish ones.
        ws.subscribe('image', get_image)
        ws.subscribe('sensors', get_sensors)
        print "INIT"
        ws.handler_loop()
        print "END"

    parser = argparse.ArgumentParser()
    parser.add_argument('--xmax', type=int, default=500)
    parser.add_argument('--xmin', type=int, default=200)
    parser.add_argument('--ymax', type=int, default=330)
    parser.add_argument('--ymin', type=int, default=160)
    wearscript.parse(callback, parser)
Example #13
0
import wearscript
import argparse

if __name__ == '__main__':

    def callback(ws, **kw):
        print('Got args[%r]' % (kw, ))
        print('Demo callback, prints all inputs and sends nothing')
        while 1:
            print(ws.receive())

    wearscript.parse(callback, argparse.ArgumentParser())
Example #14
0
def ws_parse(parser):
    print "running ws_parse"
    wearscript.parse(callback, parser)
Example #15
0
def main():
    def callback(ws, **kw):
        print('Got args[%r]' % (kw,))
        print('Demo callback, prints all inputs and sends nothing')
        run = [None]
        kw.update(PARAMS)
        import servo
        def background():
            alpha = 1
            v = None
            scrollMin = 0
            scrollMax = 1200
            while True:
                data = ws.receive()
                if data[0] == 'blob' and data[1] == 'audio':
                    continue
                    m = .4
                    M = .75
                    d = float(data[2])
                    if not np.isfinite(d):
                        continue
                    d = scale(d, m, M)
                    if v is None:
                        v = d
                    v = (1 - alpha) * v + alpha  * d
                    print((d, v))
                    #servo.mouth(v)
                    #gevent.sleep(.05)
                elif data[0] == 'blob' and data[1] == 'scroll':
                    print(data)
                    #servo.mouth(scale(float(data[2]), scrollMin, scrollMax))
                elif data[0] == 'blob' and data[1] == 'finger' and data[2] == '+':
                    servo.mouth(1)
                elif data[0] == 'blob' and data[1] == 'finger' and data[2] == '-':
                    servo.mouth(0)
                elif data[0] == 'sensors':
                    continue
                    #for sample in data[3].get('MPL Orientation', []):
                    #    servo.mouth(sample[0][0] / 360)
                    print(data)
                    for sample in data[3].get('LTR-506ALS Light sensor', []):
                        d = sample[0][0]
                        d = scale(d, 5, 175)
                        print('Light: ' + str(d))
                        #servo.mouth(d)
                    #for sample in data[3].get('MPL Orientation', []):
                    #    servo.mouth(sample[0][0] / 360)
                else:
                    pass
                    #print(data)
                #gevent.sleep(.01)
        gevent.spawn(background)
        while run[0] != 'QUIT':
            for box, frame, hull, timestamp in pupil_iter(**kw):
                if kw.get('debug'):
                    run[0] = debug_iter(box, frame, hull, timestamp)
                if run[0]:
                    break
                if box is None:
                    continue
                eyex = scale(box[0][0], 170, 440)
                eyey = scale(box[0][1], 95, 278)
                servo.eye_x(eyex)
                servo.eye_y(1 - eyey)
                #ws.send('sensors', 'Pupil Eyetracker', {'Pupil Eyetracker': -2}, {'Pupil Eyetracker': [[[box[0][1], box[0][0], max(box[1][0], box[1][1])], time.time(), int(time.time() * 1000000000)]]})
    parser = argparse.ArgumentParser()
    parser.add_argument('--dump')
    parser.add_argument('--load')
    parser.add_argument('--command_thresh', type=int, default=6)
    parser.add_argument('--calib')
    parser.add_argument('--plot', action='store_true')
    parser.add_argument('--debug', action='store_true')
    wearscript.parse(callback, parser)
import wearscript
import argparse


def callback(ws, **kw):

    def get_image(chan, timestamp, image):
        print('Image[%s] Time[%f] Bytes[%d]' % (chan, timestamp, len(image)))

    def get_sensors(chan, names, samples):
        print('Sensors[%s] Names[%r] Samples[%r]' % (chan, names, samples))

    ws.subscribe('image', get_image)
    ws.subscribe('sensors', get_sensors)
    ws.handler_loop()

wearscript.parse(callback, argparse.ArgumentParser())
Example #17
0
 def ws_parse(self, parser):
     print "running ws_parse"
     wearscript.parse(self.callback, parser)
Example #18
0
    for a in args:
        assert 0 <= a < 256
        SERIAL.write(chr(a))

if __name__ == '__main__':
    import argparse
    import msgpack
    import base64
    import wearscript
    if 0:
        #payload = [255, 0, 0, 0, 0, 0,  255, 0, 0, 255, 255, 0, 255, 2, 0, 255, 255, 0];
        kw = {'serialport': '/dev/ttyACM0'}
        for i in range(len(payload) / 6):
            command(kw['serialport'], *payload[i * 6: (i + 1) * 6])
            time.sleep(.5)
        time.sleep(10)
        quit()
    def callback(ws, **kw):
        print('Listening...')
        while 1:
            data = ws.receive()
            if data[0] == 'blob' and data[1] == 'arduino':
                print(data)
                payload = msgpack.loads(base64.b64decode(data[2]))
                print(payload)
                for i in range(len(payload) / 6):
                    command(kw['serialport'], *payload[i * 6: (i + 1) * 6])
    parser = argparse.ArgumentParser()
    parser.add_argument('--serialport', default='/dev/ttyACM0')
    wearscript.parse(callback, parser)