I am using Python 3.4 and opencv(64 bits). My question is about property identifier parameters such as CV_CAP_PROP_FRAME_WIDTH, or CV_CAP_PROP_FRAME_HEIGHT for video capture. The full documentation for this is here: http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=get#cv2.VideoCapture.get. My program works fine when I hard code these numbers in but it does not recognize any of the terms given in the documentation. I read some earlier posts that these were available in cv but are not available in cv2 library. Has there been any updates? I can not find anything else here… I don't like using hard-coded numbers. Any advice? Thanks in advance.
Opencv – Video capture PROPID parameters in openCV
opencvpython-3.xvideo-capture
Related Solutions
Your task should be relatively easy to accomplish using OpenCV and Python. It seems that you are new to OpenCV, so I will try and keep my explanation thorough, but feel free to ask if you need any clarity.
I am not sure if you are getting your data from a live camera video feed, or are post-processing recorded footage. Either way...
Get data. If using a live feed:
capture = cv2.VideoCapture(0)
If using recorded footage:
capture = cv2.VideoCapture("your_reading_file.avi")
Initialise video writer. Look at this question for help with codecs- finding a working codec is not trivial. I am also using Windows 7 x64, and the below-mentioned codec was the only one that worked for me. Also, set the variable fps
as close to the actual incoming video framerate as possible- you can not change it once you have started writing frames.
flag, frame = capture.read() # **EDIT:** to get frame size
width = np.size(frame, 1) #here is why you need numpy! (remember to "import numpy as np")
height = np.size(frame, 0)
writer = cv2.VideoWriter(filename="your_writing_file.avi",
fourcc=cv2.cv.CV_FOURCC('I', 'Y', 'U', 'V'), #this is the codec that works for me
fps=15, #frames per second, I suggest 15 as a rough initial estimate
frameSize=(width, height))
Process this data and add your text. Lastly, write the edited frame to a video file.
while True:
flag, frame = capture.read() #Flag returns 1 for success, 0 for failure. Frame is the currently processed frame
if flag == 0: #Something is wrong with your data, or the end of the video file was reached
break
x = width/2
y = height/2 #change to the desired coordinates
text_color = (255,0,0) #color as (B,G,R)
cv2.putText(frame, "your_string", (x,y), cv2.FONT_HERSHEY_PLAIN, 1.0, text_color, thickness=1, lineType=cv2.CV_AA)
writer.write(frame) #write to the video file
As simple as that! I use the above code to write text to video files almost daily, so it definitely works. The only potential problems I can foresee are with the codecs, which I unfortunately do not know a lot about. I hope that this may solve your problem, feel free to ask more questions.
EDIT: Answers to your comment's questions.
1.) As far as I know, you can only use .avi because you have to use an uncompressed format with OpenCV. I am afraid I have no knowledge of using other (compressed) formats. Maybe you could use a third-party program to do pre/post-conversion? The reason for the frame
exception was my mistake, I have edited the answer to include the missing line.
2.) I'm afraid I have no idea how to read metadata. If I find out I will let you know. My own hackish solution for finding video framerate is to let OpenCV run through the video once, using the Time
module to calculate the average framerate. This estimate can then be used when writing the video file.
3.) I have found that the size of the resulting video may differ significantly from the original depending on several factors, the most important being how close the chosen fps
was to the actual original framerate.
4.) As for other fonts, there are several available. I can refer you to this question for a quick overview. Here is the relevant documentation:
fontFace – Font type. One of FONT_HERSHEY_SIMPLEX,
FONT_HERSHEY_PLAIN,
FONT_HERSHEY_DUPLEX,
FONT_HERSHEY_COMPLEX,
FONT_HERSHEY_TRIPLEX,
FONT_HERSHEY_COMPLEX_SMALL,
FONT_HERSHEY_SCRIPT_SIMPLEX, or
FONT_HERSHEY_SCRIPT_COMPLEX,
where each of the font ID’s can be combined with FONT_HERSHEY_ITALIC to get the slanted letters.
using
resize(image, image, Size(640, 360), 0, 0, INTER_CUBIC);
after read(image) seems to be the best solution to solve this problem. So the total (test) code becomes:
VideoCapture cap("path/to/file");
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 360);
Mat image;
cap.read(image);
resize(image, image, Size(640, 360), 0, 0, INTER_CUBIC);
namedWindow("firstframe", 1);
imshow("firstframe", image);
waitKey(0);
If anyone knows of a better way, please let me know.
Best Answer
I stumbled upon the properties while looking through related posts on the internet.
For Python OpenCV 3, they are directly in
cv2
module but have to be accessed without the leadingCV_
. For example the two properties you mentioned can be accessed as:A working example would be:
Note that this only applies to OpenCV 3. For older versions of OpenCV (i.e. 2.X.X) the
cv2.cv.CV_CAP_PROP_FRAME_HEIGHT
syntax should work.