test_video_resource.py
Go to the documentation of this file.
1 #! /usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 """
4 
5 Copyright (c) 2015 PAL Robotics SL.
6 Released under the BSD License.
7 
8 Created on 7/14/15
9 
10 @author: Sammy Pfeiffer
11 
12 test_video_resource.py contains
13 a testing code to see if opencv can open a video stream
14 useful to debug if video_stream does not work
15 """
16 
17 import cv2
18 import sys
19 
20 if __name__ == '__main__':
21  if len(sys.argv) < 2:
22  print "You must give an argument to open a video stream."
23  print " It can be a number as video device, e.g.: 0 would be /dev/video0"
24  print " It can be a url of a stream, e.g.: rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov"
25  print " It can be a video file, e.g.: myvideo.mkv"
26  exit(0)
27 
28  resource = sys.argv[1]
29  # If we are given just a number, interpret it as a video device
30  if len(resource) < 3:
31  resource_name = "/dev/video" + resource
32  resource = int(resource)
33  else:
34  resource_name = resource
35  print "Trying to open resource: " + resource_name
36  cap = cv2.VideoCapture(resource)
37  if not cap.isOpened():
38  print "Error opening resource: " + str(resource)
39  print "Maybe opencv VideoCapture can't open it"
40  exit(0)
41 
42  print "Correctly opened resource, starting to show feed."
43  rval, frame = cap.read()
44  while rval:
45  cv2.imshow("Stream: " + resource_name, frame)
46  rval, frame = cap.read()
47  key = cv2.waitKey(20)
48  # print "key pressed: " + str(key)
49  # exit on ESC, you may want to uncomment the print to know which key is ESC for you
50  if key == 27 or key == 1048603:
51  break
52  cv2.destroyWindow("preview")
53 


video_stream_opencv
Author(s): Sammy Pfeiffer
autogenerated on Wed Jun 19 2019 19:58:35