On Board Camera with Parallel Processing using `posix_ipc` Tool

I try to use parallel processing for (large) video/image from (default) on board camera. I couldn’t
figure out memory location for semaphore and shared memory.

HW: (default) on board camera in TX2
SW: Jetpack 3.3, Python 3.6, posix_ipc tool

My code is like this

from PIL import Image
import sys
import os
import pdb
import urllib
from posix_ipc import *
from mmap import *
import subprocess

# Python modules
import mmap
import os
import sys
import hashlib

# 3rd party modules
import posix_ipc


# globals
ipc = {}
ipc['app2py'] = None
ipc['py2app'] = None
ipc['shm_app2py'] = None
ipc['shm_py2app'] = None

# ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
def ipc_init():
    global ipc
    # Semaphore(name, [flags = 0, [mode = 0600, [initial_value = 0]]])
    # Creates a new semaphore or opens an existing one.
    ipc['app2py'] = Semaphore("/ipc_app2py", flags=O_CREAT)
    ipc['py2app'] = Semaphore("/ipc_py2app", flags=O_CREAT)
    # SharedMemory(name, [flags = 0, [mode = 0600, ]]])
    # Creates a new shared memory segment or opens an existing one.
    ipc['shm_app2py'] = SharedMemory("/ipc_shm_app2py", flags=O_CREAT,
        size=SHMEM_BUF_SIZE_BYTES)
    ipc['shm_py2app'] = SharedMemory("/ipc_shm_py2app", flags=O_CREAT,
        size=SHMEM_BUF_SIZE_BYTES)

    # -----  -----  -----
    # spawn the camera server + rendering process

    # 1st trial
    # subprocess.Popen(['../tegra_mm_jetpack33/samples/12_camera_v4l2_cuda/camera_v4l2_cuda',
    #     '-d', '/dev/video4'])

    # 2nd trial
    subprocess.Popen(['v4l2-ctl', '--set-fmt-video',
        'width=1280,height=720,pixelformat=BG10',
        '-d', '/dev/video0',
        '--set-ctrl', 'bypass_mode=0'])


def ipc_pend():
    global ipc

    print("ipc: waiting...")
    # ex) acquire([timeout = None])
    # Waits (conditionally) until the semaphore's value is > 0
    # and then returns, decrementing the semaphore.
    ipc['app2py'].acquire()
    print("ipc: got event!")
    # Read at most n bytes from file descriptor fd.
    # Return a string containing the bytes read.
    data = os.read(ipc['shm_app2py'].fd, 300 * 300 * 5 * 3)
    # Set the current position of file descriptor fd to position pos,
    # modified by how: SEEK_SET or 0 to set the position relative
    os.lseek(ipc['shm_app2py'].fd, 0, os.SEEK_SET)
    img = Image.frombytes('RGB', (1500,300), data)

    return img


def ipc_post(buf):
    global ipc

    print("ipc: posting...")
    os.lseek(ipc['shm_py2app'].fd, 0, os.SEEK_SET)
    os.write(ipc['shm_py2app'].fd, buf)
    os.fsync(ipc['shm_py2app'].fd)
    ipc['py2app'].release()


# ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
def main():
    ipc_init()

    imgs = [None] * 5

    while True:
        image = ipc_pend();

        # Loading and Preprocessing Image
        py2app_response_arr = []
        py2app_response_arr.append(float('nan'))
        ipc_post(bytearray(np.array(py2app_response_arr, dtype=np.float32)))


if __name__ == "__main__":
    main()

As you see I try to use memory location app2py, py2app, shm_app2py, shm_py2app (code line 22). When I use camera_v4l2_cuda sample code (1st trial code line 44, USB input not on board camera), the code works.

When I use v4l2-ctl method (2nd trial code line 48, on board camera), the code not work (semaphore doesn’t increase). It means code execution stuck at ipc['app2py'].acquire() line…

Which memory location should I use?

Do I miss any other things?