annyoung

python get image from video using cv2 (VIDEOIO ERROR: V4L: can't find camera device) 본문

프로그래밍

python get image from video using cv2 (VIDEOIO ERROR: V4L: can't find camera device)

nopsled 2019. 1. 30. 10:59

* MENTION : Ubuntu 16.05 LTS with python 2.7 installed on Azure.


This example works fine on my local. but doesn't work on my VM. so i'm keep messing up.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
import cv2
 
def video2frame(invideofilename, save_path):
    vidcap = cv2.VideoCapture(invideofilename)
    count = 0
    while True:
        success,image = vidcap.read()
        if not success:
            break
        print ('Read a new frame: ', success)
        fname = "{}.jpg".format("{0:05d}".format(count))
        cv2.imwrite(save_path + fname, image) # save frame as JPEG file
        count += 1
    print("{} images are extracted in {}."format(count, save_path))
cs


but, finally i realized that my virtual machine doesn't have any carmera device.


1
2
3
4
5
6
7
8
9
10
11
Python 2.7.12 (default, Nov 12 201814:36:49)
[GCC 5.4.0 20160609] on linux2
Type "help""copyright""credits" or "license" for more information.
>>> import cv2
>>> cv2.VideoCapture(0)
VIDEOIO ERROR: V4L: can't open camera by index 0
<VideoCapture 0x7f44197b6810>
>>> cv2.VideoCapture(-1)
VIDEOIO ERROR: V4L: can't find camera device
<VideoCapture 0x7f44197b6770>
>>>
cs


i'm try to install many things.


1
2
3
4
5
6
7
8
9
git clone https://github.com/opencv/opencv.git
 
mkdir build
 
cd build
 
cmake ../opencv
 
make
cs



1
sudo apt-get install cheese libav-tools libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libx264-dev
cs


but finally resolve this problem when i install this opencv and many libs.






* additional : still doesn't work if i use cv2.VideoCapture from video url. so i have to change logic. just use local video path if you use get video meta from video url.


1
2
3
4
5
6
7
# don't use like this if you doing on VM with cv2 module.
import cv2
vid = cv2.VideoCapture("https://example.com/test.mp4")
success, img = vid.read()
if success:
    print len(img)
    print type(img)
cs


1
2
3
4
5
6
7
# change from video url to local path.
import cv2
vid = cv2.VideoCapture("./video.mp4")
success, img = vid.read()
if success:
    print len(img)
    print type(img)
cs


Don't use video url. if you doing on VM with cv2 module.




Comments