Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- from OpenGL.GL import *
- from OpenGL.GLU import *
- from OpenGL.GLUT import *
- import numpy as np
- import sys
- # ============================================================================
- VIDEO_SOURCE = "d:/jenkins/plants/output_640-400_1.mp4"
- # ============================================================================
- class OGLVideoView(object):
- def __init__(self, capture):
- self.capture = capture
- self.width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
- self.height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
- glutInitWindowSize(self.width, self.height)
- glutInitWindowPosition(100, 100)
- glutCreateWindow("OpenGL + OpenCV")
- self.init()
- def init(self):
- glClearColor(0.0, 0.0, 0.0, 1.0)
- glutDisplayFunc(self.display)
- glutReshapeFunc(self.reshape)
- glutKeyboardFunc(self.keyboard)
- glutIdleFunc(self.idle)
- def display(self):
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- glEnable(GL_TEXTURE_2D)
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
- # Set Projection Matrix
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- gluOrtho2D(0, self.width, 0, self.height)
- # Switch to Model View Matrix
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- # Draw textured Quads
- glBegin(GL_QUADS)
- glTexCoord2f(0.0, 0.0)
- glVertex2f(0.0, 0.0)
- glTexCoord2f(1.0, 0.0)
- glVertex2f(self.width, 0.0)
- glTexCoord2f(1.0, 1.0)
- glVertex2f(self.width, self.height)
- glTexCoord2f(0.0, 1.0)
- glVertex2f(0.0, self.height)
- glEnd()
- glFlush()
- glutSwapBuffers()
- def reshape(self, w, h):
- if h == 0:
- h = 1
- glViewport(0, 0, w, h)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- # allows for reshaping the window without distoring shape
- nRange = 1.0
- if w <= h:
- glOrtho(-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange)
- else:
- glOrtho(-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange)
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- def keyboard(self, key, x, y):
- if key == chr(27):
- sys.exit()
- def idle(self):
- ret, image = self.capture.read()
- if not ret:
- print "Failed to read image"
- sys.exit(-1)
- # Create Texture
- glTexImage2D(GL_TEXTURE_2D ,0, GL_RGB
- , self.width, self.height
- , 0, GL_BGR, GL_UNSIGNED_BYTE
- , np.flip(image, 0))
- glutPostRedisplay()
- def main(self):
- glutMainLoop()
- # ============================================================================
- if __name__ == "__main__":
- capture = cv2.VideoCapture(VIDEO_SOURCE)
- if not capture.isOpened():
- sys.exit(-1)
- glutInit(sys.argv)
- view = OGLVideoView(capture)
- view.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement