Search code examples
opencvvideoencryptionvideo-streamingwebcam

It s there any way to acces the frames taken with opencv for encription?


I try to make an encrypted video conference, so the data will be sent UDP mode, I accessed the webcam with openCV in python, it is there any whay to acces the data from webcam as bits so that I can encript-transfer-decript and show the immage on the desired station? Or what other library(python/java/c/c++) can help me wth taking data from my webcam?


Solution

  • I'm using OpenCV 3.3 for Python(3.5) on Ubuntu 16.04.

    For you question, to encrypt the frame taken from video and transform by TCP/UDP:

    On Side A:

    (1) use `cap.read` to take frame form `videoCapture` as `np.array`
    (2) use `np.tobytes` to convert from `np.array` to `bytes`
    (3) do encryption on bytes(can be switched with step 2).
    (4) transfer `bytes` using TCP/UDP 
    

    On Side B:

    (1) receive from A
    (2) do decryption on the received bytes
    (3) use `np.frombuffer` to convert from `bytes` to `np.array`, then `reshape`. you get the data.
    

    A sample code snippet:

    1. On Side A
    cap = cv2.VideoCapture(0)
    
    assert cap.isOpened()
    
    ## (1) read
    ret, frame = cap.read()
    sz = frame.shape
    
    ## (2) numpy to bytes 
    frame_bytes = frame.tobytes()
    print(type(frame_bytes))
    # <class 'bytes'>
    
    ## (3) encode 
    # ...
    
    ## (4) transfer
    # ...
    
    cap.release()
    
    1. On Side B
    ## (1) receive
    ## (2) decode 
    
    ## (3) frombuffer and reshape to the same size on Side A
    frame_frombytes = np.frombuffer(frame_bytes, dtype=np.uint8).reshape(sz)
    print(type(frame_frombytes))
    ## <class 'numpy.ndarray'>
    

    Related answers:

    (1) Convert str to numpy.ndarray

    (2) Can't write video by opencv in Python

    (3) How to transfer cv::VideoCapture frames through socket in client-server model (OpenCV C++)?