Reputation: 368
Thanks for reading already, I've been trying to get this to work for a day and I didn't get closer to the solution.
I'm trying to get this object tracker to work. When I use a video from my webcam (with: python object-tracker-single.py -d 0
, everything works as expected.
But as soon as I'm trying to use a video file (i've tried different formats: .mp4
, .mkv
& .avi
. I also tried to use the file given in the repository, that didn't work as well. To see if there is a File not found
-Error, I passed an invalid path to the function and an error got printed, that has not been printed when I used the other videos. So the file(s) i'm using is(/are) valid and not corrupted.
I installed dlib
through homebrew
following this article and compiled OpenCV
from the official source. This is the CMakeCache.txt that CMake
spit out. After the first time I compiled it, I added opencv_contrib to the mix, thinking that it could help (I also think that I actually needed it), but that didn't fix the problem.
This is the code I'm having problems with:
# Create the VideoCapture object
cam = cv2.VideoCapture(source)
# If Camera Device is not opened, exit the program
if not cam.isOpened():
print "Video device or file couldn't be opened"
exit()
print "Press key `p` to pause the video to start tracking"
while True:
# Retrieve an image and Display it.
retval, img = cam.read() #<-- this returns false when trying to read a video
if not retval:
print "Cannot capture frame device"
exit()
At the marked line, retval
equals False
and image
equals None
.
It would already help me if I could somehow debug this behavior, but I didn't find any way of doing so.
I found that many Windows Users had problems with missing ffmpeg
support, but that is not the case for me, since I used ffmpeg
in previous (not OpenCV-related) projects and CMakeCache.txt
reports that ffmpeg
has been found and the compilation succeeded.
I also tried using a fully qualified file-name for the video file, which either resulted in Video device or file couldn't be opened
or the given problem.
If you have any idea how this problem can be completely resolved, have an Idea on how to solve it or can provide me with a way of properly debugging this behavior, I'd be super glad to here it!
Thanks already!
-
System: MacBook Pro (macOS Sierra 10.12.6
)
OpenCV Version: 3.3.0
Dlib Version (not necessary imo, but hey): 19.4.0
Edit 2
Output of cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-master/modules ../ >> result.txt
: pastebin
Upvotes: 0
Views: 4441
Reputation: 1843
I had the same issue with OpenCV 3.3.0. on Ubuntu 14.04.
The reason cv2.VideoCapture failed, seems to be because of a wrong installation of OpenCV.
The official way of installation should be followed: http://docs.opencv.org/trunk/d7/d9f/tutorial_linux_install.html
I had installed using pip, clearly not working.
I ended up just pulling a docker image: https://hub.docker.com/r/victorhcm/opencv/
That worked perfectly.
Upvotes: 1
Reputation: 368
I've found a workaround by now using the scikit-video and the linked skvideo.io.FFmpegReader()
function. Through that I was able to read the all the videos I tested inbefore frame-by-frame.
This is the code that replaces the code in my original question:
reader = skvideo.io.FFmpegReader("/Users/timomueller/Desktop/test_video_carcounting.mp4", inputdict=inputparameters, outputdict=outputparameters);
print "Press key `p` to pause the video to start tracking"
for img in reader.nextFrame():
#img contains the frame - the for loop runs until there is
#no frame available anymore
If you're facing the same problem as I do, I think that scikit-video
is a good workaround and I'll focus on working on my actual problem instead of fixing OpenCV
from now on.
Installation through pip
is quiet easy. Simply sudo pip install sk-video
and wait until the command finishes. Then you can import skvideo.io
and everything should work fine!
The only problem I was having with this workaround is that img
seems tinted as if it lost it's colors or as if there were incorrect settings regarding color space, pixel format, etc. For me this is not crucial, so I will simply ignore that and probably work on it later on. (If I find a fix, I'll update this answer). For everyone that needs correct colors, take a look at the documentation regarding inputdict
and outputdict
(skvideo.io.FFmpegReader(file, inputdict, outputdict)
). As well as FFmpeg
's video filters. It should be possible to fix the tinted colors with that or with proper pixel format settings in the outputdict
.
But maybe the tinting problem doesn't affect you, in that case: I hope this helped.
Be sure to add your answer if you find an actual solution to the problem though!
Upvotes: 0