def __init__(self): self.webcam = Webcam() self.webcam.start() self.glyphs = Glyphs() self.cone = None self.sphere = None self.texture_background = None
def __init__(self): # initialise webcam and start thread self.webcam = Webcam() self.webcam.start() # initialise glyphs self.glyphs = Glyphs() # initialise shapes self.cone = None self.sphere = None # self.hp = HP() # initialise texture self.texture_background = None
def __init__(self): # initialise webcam and start thread self.webcam = Webcam() self.webcam.start() # initialise glyphs self.glyphs = Glyphs() # initialise shapes self.cone = None self.sphere = None # initialise texture self.texture_background = None
def __init__(self): # initialise config self.config_provider = ConfigProvider() # initialise robots self.rocky_robot = RockyRobot() self.sporty_robot = SportyRobot() # initialise webcam self.webcam = Webcam() # initialise glyphs self.glyphs = Glyphs() self.glyphs_cache = None # initialise browser self.browser = None if self.config_provider.browser: self.browser = Browser() # initialise texture self.texture_background = None
class OpenGLGlyphs: INVERSE_MATRIX = np.array([[1.0, 1.0, 1.0, 1.0], [-1.0, -1.0, -1.0, -1.0], [-1.0, -1.0, -1.0, -1.0], [1.0, 1.0, 1.0, 1.0]]) def __init__(self): self.webcam = Webcam() self.webcam.start() self.glyphs = Glyphs() self.cone = None self.sphere = None self.texture_background = None def _init_gl(self, Width, Height): glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(1.0) glDepthFunc(GL_LESS) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(33.7, 1.3, 0.1, 100.0) glMatrixMode(GL_MODELVIEW) self.cone = OBJ('cone.obj') self.sphere = OBJ('sphere.obj') glEnable(GL_TEXTURE_2D) self.texture_background = glGenTextures(1) def _draw_scene(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() image = self.webcam.get_current_frame() bg_image = cv2.flip(image, 0) bg_image = Image.fromarray(bg_image) ix = bg_image.size[0] iy = bg_image.size[1] bg_image = bg_image.tostring("raw", "BGRX", 0, -1) glBindTexture(GL_TEXTURE_2D, self.texture_background) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, bg_image) glBindTexture(GL_TEXTURE_2D, self.texture_background) glPushMatrix() glTranslatef(0.0, 0.0, -10.0) self._draw_background() glPopMatrix() image = self._handle_glyphs(image) glutSwapBuffers() def _handle_glyphs(self, image): glyphs = [] try: glyphs = self.glyphs.detect(image) except Exception as ex: print(ex) if not glyphs: return for glyph in glyphs: rvecs, tvecs, glyph_name = glyph rmtx = cv2.Rodrigues(rvecs)[0] view_matrix = np.array( [[rmtx[0][0], rmtx[0][1], rmtx[0][2], tvecs[0]], [rmtx[1][0], rmtx[1][1], rmtx[1][2], tvecs[1]], [rmtx[2][0], rmtx[2][1], rmtx[2][2], tvecs[2]], [0.0, 0.0, 0.0, 1.0]]) view_matrix = view_matrix * self.INVERSE_MATRIX view_matrix = np.transpose(view_matrix) glPushMatrix() glLoadMatrixd(view_matrix) print(view_matrix) if glyph_name == SHAPE_CONE: glCallList(self.cone.gl_list) elif glyph_name == SHAPE_SPHERE: glCallList(self.sphere.gl_list) glPopMatrix() def _draw_background(self): glBegin(GL_QUADS) glTexCoord2f(0.0, 1.0) glVertex3f(-4.0, -3.0, 0.0) glTexCoord2f(1.0, 1.0) glVertex3f(4.0, -3.0, 0.0) glTexCoord2f(1.0, 0.0) glVertex3f(4.0, 3.0, 0.0) glTexCoord2f(0.0, 0.0) glVertex3f(-4.0, 3.0, 0.0) glEnd() def main(self): glutInit() glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(640, 480) glutInitWindowPosition(800, 400) self.window_id = glutCreateWindow("nichi Technologies") glutDisplayFunc(self._draw_scene) glutIdleFunc(self._draw_scene) self._init_gl(640, 480) glutMainLoop()
class SaltwashAR: # constants INVERSE_MATRIX = np.array([[ 1.0, 1.0, 1.0, 1.0], [-1.0,-1.0,-1.0,-1.0], [-1.0,-1.0,-1.0,-1.0], [ 1.0, 1.0, 1.0, 1.0]]) def __init__(self): # initialise config self.config_provider = ConfigProvider() # initialise robots self.rocky_robot = RockyRobot() self.sporty_robot = SportyRobot() # initialise webcam self.webcam = Webcam() # initialise glyphs self.glyphs = Glyphs() self.glyphs_cache = None # initialise browser self.browser = None if self.config_provider.browser: self.browser = Browser() # initialise texture self.texture_background = None def _init_gl(self): glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(1.0) glDepthFunc(GL_LESS) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(33.7, 1.3, 0.1, 100.0) glMatrixMode(GL_MODELVIEW) # load robots frames self.rocky_robot.load_frames(self.config_provider.animation) self.sporty_robot.load_frames(self.config_provider.animation) # start threads self.webcam.start() if self.browser: self.browser.start() # assign texture glEnable(GL_TEXTURE_2D) self.texture_background = glGenTextures(1) def _draw_scene(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() # reset robots self.rocky_robot.is_detected = False self.sporty_robot.is_detected = False # get image from webcam image = self.webcam.get_current_frame() # handle background self._handle_background(image) # handle glyphs self._handle_glyphs(image) # handle browser self._handle_browser() glutSwapBuffers() def _handle_background(self, image): # convert image to OpenGL texture format bg_image = cv2.flip(image, 0) bg_image = Image.fromarray(bg_image) ix = bg_image.size[0] iy = bg_image.size[1] bg_image = bg_image.tostring("raw", "BGRX", 0, -1) # create background texture glBindTexture(GL_TEXTURE_2D, self.texture_background) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, bg_image) # draw background glBindTexture(GL_TEXTURE_2D, self.texture_background) glPushMatrix() glTranslatef(0.0,0.0,-10.0) glBegin(GL_QUADS) glTexCoord2f(0.0, 1.0); glVertex3f(-4.0, -3.0, 0.0) glTexCoord2f(1.0, 1.0); glVertex3f( 4.0, -3.0, 0.0) glTexCoord2f(1.0, 0.0); glVertex3f( 4.0, 3.0, 0.0) glTexCoord2f(0.0, 0.0); glVertex3f(-4.0, 3.0, 0.0) glEnd( ) glPopMatrix() def _handle_glyphs(self, image): # attempt to detect glyphs glyphs = [] try: glyphs = self.glyphs.detect(image) except Exception as ex: print(ex) # manage glyphs cache if glyphs: self.glyphs_cache = glyphs elif self.glyphs_cache: glyphs = self.glyphs_cache self.glyphs_cache = None else: return for glyph in glyphs: rvecs, tvecs, _, glyph_name = glyph # build view matrix rmtx = cv2.Rodrigues(rvecs)[0] view_matrix = np.array([[rmtx[0][0],rmtx[0][1],rmtx[0][2],tvecs[0]], [rmtx[1][0],rmtx[1][1],rmtx[1][2],tvecs[1]], [rmtx[2][0],rmtx[2][1],rmtx[2][2],tvecs[2]], [0.0 ,0.0 ,0.0 ,1.0 ]]) view_matrix = view_matrix * self.INVERSE_MATRIX view_matrix = np.transpose(view_matrix) # load view matrix and draw cube glPushMatrix() glLoadMatrixd(view_matrix) if glyph_name == ROCKY_ROBOT: self.rocky_robot.is_detected = True if self.browser and self.browser.is_speaking: self.rocky_robot.next_frame(True) else: self.rocky_robot.next_frame(False) elif glyph_name == SPORTY_ROBOT: self.sporty_robot.is_detected = True if self.browser and self.browser.is_speaking: self.sporty_robot.next_frame(True) else: self.sporty_robot.next_frame(False) glColor3f(1.0, 1.0, 1.0) glPopMatrix() def _handle_browser(self): # check browser instantiated if not self.browser: return # handle browser if self.rocky_robot.is_detected: self.browser.load(ROCK) elif self.sporty_robot.is_detected: self.browser.load(SPORT) else: self.browser.halt() def main(self): # setup and run OpenGL glutInit() glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(640, 480) glutInitWindowPosition(800, 400) self.window_id = glutCreateWindow("SaltwashAR") glutDisplayFunc(self._draw_scene) glutIdleFunc(self._draw_scene) self._init_gl() glutMainLoop()
class OpenGLGlyphs: # constants INVERSE_MATRIX = np.array([[ 1.0, 1.0, 1.0, 1.0], [-1.0,-1.0,-1.0,-1.0], [-1.0,-1.0,-1.0,-1.0], [ 1.0, 1.0, 1.0, 1.0]]) def __init__(self): # initialise webcam and start thread self.webcam = Webcam() self.webcam.start() # initialise glyphs self.glyphs = Glyphs() # initialise shapes self.cone = None self.sphere = None # initialise texture self.texture_background = None def _init_gl(self, Width, Height): glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(1.0) glDepthFunc(GL_LESS) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(33.7, 1.3, 0.1, 100.0) glMatrixMode(GL_MODELVIEW) # assign shapes self.cone = OBJ('cone.obj') self.sphere = OBJ('sphere.obj') # assign texture glEnable(GL_TEXTURE_2D) self.texture_background = glGenTextures(1) def _draw_scene(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() # get image from webcam image = self.webcam.get_current_frame() # convert image to OpenGL texture format bg_image = cv2.flip(image, 0) bg_image = Image.fromarray(bg_image) ix = bg_image.size[0] iy = bg_image.size[1] bg_image = bg_image.tobytes("raw", "BGRX", 0, -1) # create background texture glBindTexture(GL_TEXTURE_2D, self.texture_background) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, bg_image) # draw background glBindTexture(GL_TEXTURE_2D, self.texture_background) glPushMatrix() glTranslatef(0.0,0.0,-10.0) self._draw_background() glPopMatrix() # handle glyphs image = self._handle_glyphs(image) glutSwapBuffers() def _handle_glyphs(self, image): # attempt to detect glyphs glyphs = [] try: glyphs = self.glyphs.detect(image) except Exception as ex: print(ex) if not glyphs: return for glyph in glyphs: rvecs, tvecs, glyph_name = glyph # build view matrix rmtx = cv2.Rodrigues(rvecs)[0] view_matrix = np.array([[rmtx[0][0],rmtx[0][1],rmtx[0][2],tvecs[0]], [rmtx[1][0],rmtx[1][1],rmtx[1][2],tvecs[1]], [rmtx[2][0],rmtx[2][1],rmtx[2][2],tvecs[2]], [0.0 ,0.0 ,0.0 ,1.0 ]]) view_matrix = view_matrix * self.INVERSE_MATRIX view_matrix = np.transpose(view_matrix) # load view matrix and draw shape glPushMatrix() glLoadMatrixd(view_matrix) if glyph_name == SHAPE_CONE: glCallList(self.cone.gl_list) elif glyph_name == SHAPE_SPHERE: glCallList(self.sphere.gl_list) glPopMatrix() def _draw_background(self): # draw background glBegin(GL_QUADS) glTexCoord2f(0.0, 1.0); glVertex3f(-4.0, -3.0, 0.0) glTexCoord2f(1.0, 1.0); glVertex3f( 4.0, -3.0, 0.0) glTexCoord2f(1.0, 0.0); glVertex3f( 4.0, 3.0, 0.0) glTexCoord2f(0.0, 0.0); glVertex3f(-4.0, 3.0, 0.0) glEnd( ) def main(self): # setup and run OpenGL glutInit() glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(640, 480) glutInitWindowPosition(800, 400) self.window_id = glutCreateWindow("OpenGL Glyphs") glutDisplayFunc(self._draw_scene) glutIdleFunc(self._draw_scene) self._init_gl(640, 480) glutMainLoop()
class OpenGLGlyphs: # constants INVERSE_MATRIX = np.array([[ 1.0, 1.0, 1.0, 1.0], [-1.0,-1.0,-1.0,-1.0], [-1.0,-1.0,-1.0,-1.0], [ 1.0, 1.0, 1.0, 1.0]]) def __init__(self): # initialise webcam and start thread self.webcam = Webcam() self.webcam.start() # initialise glyphs self.glyphs = Glyphs() # initialise shapes self.cone = None self.sphere = None # self.hp = HP() # initialise texture self.texture_background = None def _init_gl(self, Width, Height): glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(1.0) glDepthFunc(GL_LESS) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(33.7, 1.3, 0.1, 100.0) glMatrixMode(GL_MODELVIEW) # assign shapes self.cone = OBJ('texturedCube.obj') self.sphere = OBJ('./sortedOut/hair.obj') # assign texture glEnable(GL_TEXTURE_2D) self.texture_background = glGenTextures(1) def _draw_scene(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() # get image from webcam image = self.webcam.get_current_frame() # convert image to OpenGL texture formatz bg_image = cv2.flip(image, 0) bg_image = np.asarray(bg_image) bg_image = Image.fromarray(bg_image) ix = bg_image.size[0] iy = bg_image.size[1] bg_image = bg_image.tobytes("raw", "BGRX", 0, -1) # create background texture glBindTexture(GL_TEXTURE_2D, self.texture_background) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, bg_image) # draw background glBindTexture(GL_TEXTURE_2D, self.texture_background) glPushMatrix() glTranslatef(0.0,0.0,-10.0) self._draw_background() glPopMatrix() # handle glyphs image = self._handle_headpose(image) glutSwapBuffers() def _handle_headpose(self, image): # attempt to detect glyphs glyphs = [] # glyphs = self.hp.main(image) try: glyphs = self.glyphs.detect(image) except Exception as ex: print(ex) if not glyphs: return for glyph in glyphs: rvecs, tvecs, glyph_name = glyph # build view matrix rmtx = cv2.Rodrigues(rvecs)[0] view_matrix = np.array([[rmtx[0][0],rmtx[0][1],rmtx[0][2],tvecs[0]], [rmtx[1][0],rmtx[1][1],rmtx[1][2],tvecs[1]], [rmtx[2][0],rmtx[2][1],rmtx[2][2],tvecs[2]], [0.0 ,0.0 ,0.0 ,1.0 ]]) view_matrix = view_matrix * self.INVERSE_MATRIX view_matrix = np.transpose(view_matrix) # load view matrix and draw shape glPushMatrix() glLoadMatrixd(view_matrix) if glyph_name == SHAPE_CONE: glCallList(self.cone.gl_list) elif glyph_name == SHAPE_SPHERE: glCallList(self.sphere.gl_list) glPopMatrix() def _draw_background(self): # draw background glBegin(GL_QUADS) glTexCoord2f(0.0, 1.0); glVertex3f(-4.0, -3.0, 0.0) glTexCoord2f(1.0, 1.0); glVertex3f( 4.0, -3.0, 0.0) glTexCoord2f(1.0, 0.0); glVertex3f( 4.0, 3.0, 0.0) glTexCoord2f(0.0, 0.0); glVertex3f(-4.0, 3.0, 0.0) glEnd( ) def main(self): # setup and run OpenGL glutInit() glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(640, 480) glutInitWindowPosition(800, 400) self.window_id = glutCreateWindow(b"OpenGL Glyphs") glutDisplayFunc(self._draw_scene) glutIdleFunc(self._draw_scene) self._init_gl(640, 480) glutMainLoop()
SCALE = 10 BORDER = 300 X_SPACE = 40 Y_SPACE = 60 surface = cairo.PDFSurface( "map.pdf", COLS * X_SIZE * SCALE + (COLS - 1) * X_SPACE + 2 * BORDER, ROWS * Y_SIZE * SCALE + (ROWS - 1) * Y_SPACE + 2 * BORDER) context = cairo.Context(surface) context.set_line_cap(cairo.LineCap.ROUND) context.set_font_size(15) context.select_font_face('Arial', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) glyphs = Glyphs(context, X_SIZE, Y_SIZE, SCALE) for i in range(ROWS * COLS): y = i // COLS x = i % COLS glyphs.drawGlyph(BORDER + X_SIZE // 2 * SCALE + x * (X_SIZE * SCALE + X_SPACE), BORDER + y * (Y_SIZE * SCALE + Y_SPACE), glyph=i) text = '0x{:03X}'.format(i) _, _, textwidth, _, _, _ = context.text_extents(text) context.move_to(
Y_SIZE = 5 ROWS = 1 COLS = 500 SCALE = 10 BORDER = 300 X_SPACE = 40 Y_SPACE = 60 surface = cairo.PDFSurface( "evolution.pdf", COLS * X_SIZE * SCALE + (COLS - 1) * X_SPACE + 2 * BORDER, ROWS * Y_SIZE * SCALE + (ROWS - 1) * Y_SPACE + 2 * BORDER) context = cairo.Context(surface) context.set_line_cap(cairo.LineCap.ROUND) glyphs = Glyphs(context, X_SIZE, Y_SIZE, SCALE) glyph = glyphs.genGlyph() for y in range(ROWS): for x in range(COLS): glyphs.drawGlyph(BORDER + X_SIZE // 2 * SCALE + x * (X_SIZE * SCALE + X_SPACE), BORDER + y * (Y_SIZE * SCALE + Y_SPACE), glyph=glyph) # xx = randint(0, X_SIZE // 2 - 1) # yy = randint(0, Y_SIZE - 1) # length = glyphs.getBitWidth(xx, yy) # startBit = glyphs.getStartBit(xx, yy)