I was playing around with the OpenCV library, and tried to open a small image, with the following code:
import cv2
img = cv2.imread(r'penguin.jpeg')
print(img)
To basically take a look at the array of pixels in the image, however the print simply returns None.
Both my .py file and the image are in my desktop, so I believe the problem is not the path.
I am also aware of some issues with imread() and JPEG images, however I get the same result with the PNG version of this image.
This had been working fine up until today, so I am kinda clueless.
Can anyone tell me what might be happening or what I might be doing less correctly?
Thank you so much in advance!
Consulting the OpenCV-Python Tutorials we are warned that
Even if the image path is wrong, it won’t throw any error, but print img will give you None
where print img is the Python 2 analog of your print(img).
The code you have written is correct and having replicated your problem, I print an array representation of a test penguin.jpeg image locally.
As commented by Rashid Ladjouzi, the path is probably incorrect especially given that you mention the script worked previously. I would test this with the following code which should return True:
import os
print(r'penguin.jpeg' in os.listdir("."))
I have followed the advanced ski-image panoramic image tutorial (1) and gotten it to work properly. However, trying to use UAV images (Adobe Buttes Flight 1 Raw) downloaded from (2), results in an interesting problem when using the skimage ImageCollection. Printing the problem images gives me some sort of wrapper:
"PIL.MpoImagePlugin.MpoImageFile image mode=RGB size=4000x3000 at 0x1EB09B86D30"
If I use io.imread to read a problem image it appears to work, printing a shape of "(2,)". However, attempting to print the individually read image gives a type error:
"unorderable types: int() >= MpoImageFile()"
followed by a system error:
"method-wrapper 'le' of MpoImageFile object at 0x000001EB09F20CC0 returned a result with an error set"
I'm really at a loss here. I'm relatively new to python and don't understand why the programs not working. The images are a bit large (5.65 mb) but my main program handles (slowly) the 'good' images.
I've tried the following solutions to no avail:
1) uninstalling pillow, installing both libjpeg & libz (the names seem to have changed) then reinstalling pillow github.com/scikit-image/scikit-image/issues/2000.
2) I'm not using a GPU, parallel processing, or tensorflow github.com/scikit-image/scikit-image/issues/2000,
3) I've also made sure my Anaconda version is fully updated
This is the minimalist example I made to demonstrate the issue. It should run in any jupyter notebook.
import numpy as np
from skimage.color import rgb2gray
from skimage import io
imgs = io.ImageCollection('test\*')
"""
print(imgs[0]) # Looks okay, numpy array
print(imgs[1]) # wrapper
print(imgs[2]) # Looks okay, numpy array
"""
for i in range(6):
print(np.shape(imgs[i]))
individual = io.imread('test\DJI_0002.jpg')
print(np.shape(individual))
#print(individual)
After some more testing this issue goes away when the images are resized at 50%. Is there a limit to the image size scimage can read? This is still not an acceptable solution, I would greatly prefer not having to resize all images that get pieced together.
I recently setup opencv 3.0 for python on my ubuntu pc using the following tutorial http://www.pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/
So I ran the following code to get the number of frames of an mp4 video
import cv2
cap = cv2.VideoCapture('vid.mp4')
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print length
Strangely I get "-1" as output for "length". The video does infact load and I get accurate values for
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) #480
and
int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) #640
so its a wonder why frame count doesn't work. Please Help.
The problem was with the opencv build (as it also occured in c++). I ended up re-building my opencv and everything worked fine after
I want grab each video frames from libvlc in a (2D) numpy array using python.
I expect to use vlc.MediaPlayer.video_set_callbacks(...)
with [callbacks][1] managing an numpy array but there is no documentation about using callback with python.
Any Idea or alternative ways ?
[1]: decorated with vlc.cb.VideoLockCb and its friends
Maybe have a look at PyFFmpeg ? I do not use this python module, thus I don't know how well it works but it does adevertize the feature you require.
Edit
OpenCV is also worth a try. It is probably more stable.
OpenCV offers no less than two python bindings. The later, better one being cv2 is integrated with numpy. It is much easier to use than the older cv module. As an example, you can read video frames with the following code:
import cv2
filename = 'myfile.mpg'
capture = cv2.VideoCapture(filename)
retval, im = capture.read()
Each call to .read will output a new frame of the video.
I'm using openCV via python on linux (ubuntu 12.04), and I have a logitech c920 from which I'd like to grab images. Cheese is able to grab frames up to really high resolutions, but whenever I try to use openCV, I only get 640x480 images. I have tried:
import cv
cam = cv.CaptureFromCAM(-1)
cv.SetCaptureProperty(cam,cv.CV_CAP_PROP_FRAME_WIDTH,1920)
cv.SetCaptureProperty(cam,cv.CV_CAP_PROP_FRAME_WIDTH,1080)
but this yields output of "0" after each of the last two lines, and when I subsequently grab a frame via:
image = cv.QueryFrame(cam)
The resulting image is still 640x480.
I've tried installing what seemed to be related tools via (outside of python):
sudo apt-get install libv4l-dev v4l-utils qv4l2 v4l2ucp
and I can indeed apparently manipulate the camera's settings (again, outside of python) via:
v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=1
v4l2-ctl --set-parm=30
and observe that:
v4l2-ctl -V
indeed suggests that something has been changed:
Format Video Capture:
Width/Height : 1920/1080
Pixel Format : 'H264'
Field : None
Bytes per Line : 3840
Size Image : 4147200
Colorspace : sRGB
But when I pop into the python shell, the above code behaves exactly the same as before (printing zeros when trying to set the properties and obtaining an image that is 640x480).
Being able to bump up the resolution of the capture is pretty mission critical for me, so I'd greatly appreciate any pointers anyone can provide.
From the docs,
The function cvSetCaptureProperty sets the specified property of video capturing. Currently the function supports only video files: CV_CAP_PROP_POS_MSEC, CV_CAP_PROP_POS_FRAMES, CV_CAP_PROP_POS_AVI_RATIO .
NB This function currently does nothing when using the latest CVS download on linux with FFMPEG (the function contents are hidden if 0 is used and returned).
I had the same problem as you. Ended up going into the OpenCV source and changing the default parameters in modules/highgui/src/cap_v4l.cpp, lines 245-246 and rebuilding the project.
#define DEFAULT_V4L_WIDTH 1920
#define DEFAULT_V4L_HEIGHT 1080
This is for OpenCV 2.4.8
It seems to be variable by cammera.
AFIK, Logitech cameras have particularly bad linux support (though It;s gotten better) Most of their issues are with advanced features like focus control. i would advise sticking with basic cameras (IE manual focus Logitech cameras) just to play it safe.
My built in laptop camera has no issue and displays at normal resolution.
My external logitech pro has issues initalizing.
However, I can overcome the resolution issue with these two lines.
Yes, they are the same as you used.
cv.SetCaptureProperty(self.capture,cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
cv.SetCaptureProperty(self.capture,cv.CV_CAP_PROP_FRAME_HEIGHT, 720)
My Logitech still throws errors but the resolution is fine.
Please make sure the resolution you set is a supported by your camera or v4l will yell at you. If I set an unsupported native resolution, I have zero success.
Not sure if it works, but you can try to force the parameters to your values after you instantiate camera object:
import cv
cam = cv.CaptureFromCAM(-1)
os.system("v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=1")
os.system("v4l2-ctl --set-parm=30")
image = cv.QueryFrame(cam)
That's a bit hacky, so expect a crash.
## Sets up the camera to capture video
cap = cv2.VideoCapture(device)
width = 1280
height = 720
#set the width and height
cap.set(3,width)
cap.set(4,height)