Access webcam with opencv

NOTE: It is possible to install opend cv using

pip install opencv-python==3.3.0.9

But the camera will not be detected using this installation. opencv should be installed from source (which will take a long time). Therefore, it is not recommended to use opencv if you just want to access the camera

In [82]:
import cv2
In [83]:
cap = cv2.VideoCapture(0)
In [84]:
ret, frame = cap.read() #Captures frames in BGR format (Depends on webcam)
In [85]:
cap.release()
In [86]:
ret
Out[86]:
True
In [87]:
frame
Out[87]:
array([[[178, 183, 161],
        [181, 186, 165],
        [184, 186, 161],
        ...,
        [ 21,  10,  16],
        [ 18,  10,  12],
        [ 19,  12,  13]],

       [[173, 182, 157],
        [178, 186, 162],
        [181, 183, 163],
        ...,
        [ 23,  10,  14],
        [ 18,  10,  12],
        [ 19,  12,  13]],

       [[181, 188, 162],
        [181, 188, 162],
        [180, 183, 167],
        ...,
        [ 19,  11,  14],
        [ 17,  12,  13],
        [ 17,  12,  13]],

       ...,

       [[ 55,  60,  59],
        [ 54,  59,  58],
        [ 56,  59,  57],
        ...,
        [ 93,  83,  85],
        [101,  81,  87],
        [101,  81,  87]],

       [[ 53,  59,  63],
        [ 52,  58,  61],
        [ 54,  59,  58],
        ...,
        [100,  84,  85],
        [105,  83,  89],
        [105,  83,  89]],

       [[ 51,  58,  64],
        [ 50,  57,  63],
        [ 52,  59,  58],
        ...,
        [114,  90,  87],
        [113,  87,  89],
        [113,  87,  89]]], dtype=uint8)
In [88]:
import matplotlib.pyplot as plt
%matplotlib inline
In [89]:
frame.shape
Out[89]:
(480, 640, 3)
In [90]:
plt.imshow(frame) #BGR format
Out[90]:
<matplotlib.image.AxesImage at 0x7f0149e93630>
In [91]:
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
In [92]:
plt.imshow(rgb_frame)
Out[92]:
<matplotlib.image.AxesImage at 0x7f014a259a20>