Advertisement
smathot

Video playback with OpenCV / PyGame

Oct 19th, 2012
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. import cv
  2. import pygame
  3.  
  4. # Full path to the video file
  5. path = '/path/to/my/video.avi'
  6.  
  7. # Open the video and determine the video dimensions
  8. video = cv.CreateFileCapture(path)
  9. width = cv.GetCaptureProperty(video, cv.CV_CAP_PROP_FRAME_WIDTH)
  10. height = cv.GetCaptureProperty(video, cv.CV_CAP_PROP_FRAME_HEIGHT)     
  11.  
  12. # The video needs to be converted twice, so we need to intermediate OpenC
  13. # matrices
  14. src_tmp = cv.CreateMat(exp.height, exp.width, cv.CV_8UC3)
  15. src_rgb = cv.CreateMat(exp.height, exp.width, cv.CV_8UC3)          
  16.  
  17. # A loop to play the video file. This can also be a while loop until a key
  18. # is pressed. etc.
  19. for i in range(100):
  20.  
  21.     # Get a frame, resize it to the full screen, convert it to the proper
  22.     # color format, and finally convert it to a PyGamesurface
  23.     src = cv.QueryFrame(video)
  24.     cv.Resize(src, src_tmp)
  25.     cv.CvtColor(src_tmp, src_rgb, cv.CV_BGR2RGB)
  26.     surf = pygame.image.frombuffer(src_rgb.tostring(), cv.GetSize(src_rgb), 'RGB')                                     
  27.    
  28.     # Now you can draw whatever you want onto the PyGame surface!
  29.     pygame.draw.rect(surf, (255,0,0), (100, 100, 200, 200))
  30.    
  31.     # Show the PyGame surface!
  32.     exp.surface.blit(surf, (0, 0))         
  33.     pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement