import sys, time, math, array from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * import videoInput videoWidth, videoHeight = 320,240 videoScale = 640/videoWidth videoInput.openVideo("/dev/video", videoWidth, videoHeight) prevFrame = videoInput.grabVideo() imageArray = array.array('B') showVideo = True orthoCam = OrthoCamera(-0.5, videoWidth-0.5, -0.5, videoHeight-0.5, -1000, 1000) #orthoCam = OrthoCamera(0, videoWidth, 0, videoHeight, -1000, 1000) lastFrame = 90 lastFrameTime = time.time() def draw(): orthoCam.apply() glRasterPos2f(0, videoHeight-1) glPixelZoom(videoScale, -videoScale) if showVideo: glDrawPixels(videoWidth, videoHeight, GL_RGB, GL_UNSIGNED_BYTE, prevFrame) else: glDrawPixels(videoWidth, videoHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, imageArray.tostring()) glutSwapBuffers() t = time.time() global lastFrame, lastFrameTime if lastFrame % 100 == 0: print t-lastFrameTime, ' -> ', 1.0/(t-lastFrameTime), 'fps' lastFrame = (lastFrame+1)%100 lastFrameTime = t def checkVideo(): global imageArray, prevFrame frame = videoInput.grabVideo() if len(imageArray)>0: del imageArray[0:len(imageArray)] for i in range(0,len(frame),3): if abs(ord(frame[i]) - ord(prevFrame[i])) < 64: imageArray.append(0) else: imageArray.append(255) prevFrame = frame def update(): checkVideo() glutPostRedisplay() def keyboard(key, x, y): global showVideo if key == chr(27): sys.exit(0) elif key == ' ': showVideo = not showVideo glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE) glutInitWindowSize(videoWidth*videoScale, videoHeight*videoScale) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutIdleFunc(update) glutMainLoop()