2f261ca5ec091973ffb2c5bd4c4ed7873eb03855
[opencv] / samples / python / capture-cam.py
1 #! /usr/bin/env python
2
3 import sys
4
5 # import the necessary things for OpenCV
6 from opencv import cv
7 from opencv import highgui
8
9 # the codec existing in cvcapp.cpp,
10 # need to have a better way to specify them in the future
11 # WARNING: I have see only MPEG1VIDEO working on my computer
12 H263 = 0x33363255
13 H263I = 0x33363249
14 MSMPEG4V3 = 0x33564944
15 MPEG4 = 0x58564944
16 MSMPEG4V2 = 0x3234504D
17 MJPEG = 0x47504A4D
18 MPEG1VIDEO = 0x314D4950
19 AC3 = 0x2000
20 MP2 = 0x50
21 FLV1 = 0x31564C46
22
23 #############################################################################
24 # so, here is the main part of the program
25
26 if __name__ == '__main__':
27
28     # a small welcome
29     print "OpenCV Python capture video"
30
31     # first, create the necessary window
32     highgui.cvNamedWindow ('Camera', highgui.CV_WINDOW_AUTOSIZE)
33
34     # move the new window to a better place
35     highgui.cvMoveWindow ('Camera', 10, 10)
36
37     try:
38         # try to get the device number from the command line
39         device = int (sys.argv [1])
40
41         # got it ! so remove it from the arguments
42         del sys.argv [1]
43     except (IndexError, ValueError):
44         # no device number on the command line, assume we want the 1st device
45         device = 0
46
47     if len (sys.argv) == 1:
48         # no argument on the command line, try to use the camera
49         capture = highgui.cvCreateCameraCapture (device)
50     else:
51         # we have an argument on the command line,
52         # we can assume this is a file name, so open it
53         capture = highgui.cvCreateFileCapture (sys.argv [1])            
54
55     # check that capture device is OK
56     if not capture:
57         print "Error opening capture device"
58         sys.exit (1)
59
60     # capture the 1st frame to get some propertie on it
61     frame = highgui.cvQueryFrame (capture)
62
63     # get size of the frame
64     frame_size = cv.cvGetSize (frame)
65
66     # get the frame rate of the capture device
67     fps = highgui.cvGetCaptureProperty (capture, highgui.CV_CAP_PROP_FPS)
68     if fps == 0:
69         # no fps getted, so set it to 30 by default
70         fps = 30
71
72     # create the writer
73     writer = highgui.cvCreateVideoWriter ("captured.mpg", MPEG1VIDEO,
74                                           fps, frame_size, True)
75
76     # check the writer is OK
77     if not writer:
78         print "Error opening writer"
79         sys.exit (1)
80         
81     while 1:
82         # do forever
83
84         # 1. capture the current image
85         frame = highgui.cvQueryFrame (capture)
86         if frame is None:
87             # no image captured... end the processing
88             break
89
90         # write the frame to the output file
91         highgui.cvWriteFrame (writer, frame)
92
93         # display the frames to have a visual output
94         highgui.cvShowImage ('Camera', frame)
95
96         # handle events
97         k = highgui.cvWaitKey (5)
98
99         if k % 0x100 == 27:
100             # user has press the ESC key, so exit
101             break
102
103     # end working with the writer
104     # not working at this time... Need to implement some typemaps...
105     # but exiting without calling it is OK in this simple application
106     #highgui.cvReleaseVideoWriter (writer)