Beispiel #1
0
 def __init__(self,
              root,
              width=0,
              height=0,
              color=vx.DF_IMAGE_VIRT,
              value=None,
              vx_img=None):
     if vx_img is not None:
         self.setup_vx_img(vx_img)
     else:
         self.width = width
         self.height = height
         self.color = color
         try:
             if isinstance(root, Context):  # For real image
                 if value is None:
                     self.image = vx.CreateImage(root.vx_context, width,
                                                 height, color)
                 else:
                     self.image = vx.CreateUniformImage(
                         root.vx_context, width, height, color, value,
                         "int")
                 self.virtual_image = False
             elif isinstance(root, Graph):  # For virtual image
                 self.image = vx.CreateVirtualImage(root.graph, width,
                                                    height, color)
                 self.virtual_image = True
         except Exception as exception:
             raise exception
Beispiel #2
0
    def test_graph(self):
        c = vx.CreateContext()
        g = vx.CreateGraph(c)
        assert vx.GetStatus(vx.reference(g)) == vx.SUCCESS
        assert vx.QueryReference(vx.reference(g), vx.REF_ATTRIBUTE_TYPE, 'vx_enum') == (vx.SUCCESS, vx.TYPE_GRAPH)
        assert vx.IsGraphVerified(g) == vx.false_e
        assert vx.QueryGraph(g, vx.GRAPH_ATTRIBUTE_NUMNODES, 'vx_uint32') == (vx.SUCCESS, 0)

        img = vx.CreateImage(c, 640, 480, vx.DF_IMAGE_U8)
        dx = vx.CreateImage(c, 640, 480, vx.DF_IMAGE_S16)
        dy = vx.CreateImage(c, 640, 480, vx.DF_IMAGE_S16)
        node = vx.Sobel3x3Node(g, img, dx, dy)
        assert vx.VerifyGraph(g) == vx.SUCCESS
        assert vx.ProcessGraph(g) == vx.SUCCESS
        assert vx.ScheduleGraph(g) == vx.SUCCESS
        assert vx.WaitGraph(g) == vx.SUCCESS

        p = vx.GetParameterByIndex(node, 0)
        assert vx.AddParameterToGraph(g, p) == vx.SUCCESS
        p2 = vx.GetGraphParameterByIndex(g, 0)
        assert vx.SetGraphParameterByIndex(g, 0, vx.reference(dx)) == vx.SUCCESS
        assert vx.VerifyGraph(g) != vx.SUCCESS
        assert vx.SetGraphParameterByIndex(g, 0, vx.reference(img)) == vx.SUCCESS
        assert vx.VerifyGraph(g) == vx.SUCCESS
        assert vx.IsGraphVerified(g) == vx.true_e

        def callback(node):
            callback.called = True
            return vx.SUCCESS

        assert vx.AssignNodeCallback(node, callback) == vx.SUCCESS
        assert vx.ProcessGraph(g) == vx.SUCCESS
        assert callback.called

        def callback(node):
            raise TypeError('Escaping from callback')
            return vx.SUCCESS

        assert vx.AssignNodeCallback(node, callback) != vx.SUCCESS
        assert vx.AssignNodeCallback(node, None) == vx.SUCCESS
        assert vx.AssignNodeCallback(node, callback) == vx.SUCCESS
        assert vx.VerifyGraph(g) == vx.SUCCESS
        assert vx.ProcessGraph(g) != vx.SUCCESS

        img = vx.CreateVirtualImage(g, 640, 480, vx.DF_IMAGE_RGB)
        assert vx.GetStatus(vx.reference(img)) == vx.SUCCESS

        assert vx.ReleaseGraph(g) == vx.SUCCESS
        assert vx.ReleaseContext(c) == vx.SUCCESS
Beispiel #3
0
    def test_user_kernel(self):
        c = vx.CreateContext()

        def func(node, parameters, num):
            func.called = True
            assert num == 2
            input, output = vx.from_reference(parameters[0]), vx.from_reference(parameters[0])
            _, r = vx.GetValidRegionImage(input)
            _, addr, indata = vx.AccessImagePatch(input, r, 0, None, None, vx.READ_ONLY)
            _, addr, outdata = vx.AccessImagePatch(output, r, 0, None, None, vx.WRITE_ONLY)
            outdata[0], outdata[100] = indata[100], indata[0]
            vx.CommitImagePatch(output, r, 0, addr, outdata)
            vx.CommitImagePatch(input, r, 0, addr, indata)
            return vx.SUCCESS

        def validate_input(node, index):
            validate_input.called = True
            assert index == 0
            param = vx.GetParameterByIndex(node, index)
            image = vx.QueryParameter(param, vx.PARAMETER_ATTRIBUTE_REF, 'vx_image')[1]
            if vx.QueryImage(image, vx.IMAGE_ATTRIBUTE_FORMAT, 'vx_df_image') == (vx.SUCCESS, vx.DF_IMAGE_U8):
                s = vx.SUCCESS
            else:
                s = vx.ERROR_INVALID_VALUE
            vx.ReleaseImage(image)
            vx.ReleaseParameter(param)
            return s

        def validate_output(node, index, meta):
            validate_output.called = True
            assert index == 1
            param0 = vx.GetParameterByIndex(node, 0)
            input = vx.QueryParameter(param0, vx.PARAMETER_ATTRIBUTE_REF, 'vx_image')[1]
            param1 = vx.GetParameterByIndex(node, index)
            output = vx.QueryParameter(param1, vx.PARAMETER_ATTRIBUTE_REF, 'vx_image')[1]
            _, width = vx.QueryImage(input, vx.IMAGE_ATTRIBUTE_WIDTH, 'vx_uint32')
            _, height = vx.QueryImage(input, vx.IMAGE_ATTRIBUTE_HEIGHT, 'vx_uint32')
            vx.SetMetaFormatAttribute(meta, vx.IMAGE_ATTRIBUTE_WIDTH, width, 'vx_uint32')
            vx.SetMetaFormatAttribute(meta, vx.IMAGE_ATTRIBUTE_HEIGHT, height, 'vx_uint32')
            vx.SetMetaFormatAttribute(meta, vx.IMAGE_ATTRIBUTE_FORMAT, vx.DF_IMAGE_U8, 'vx_df_image')
            vx.ReleaseImage(input)
            vx.ReleaseImage(output)
            vx.ReleaseParameter(param0)
            vx.ReleaseParameter(param1)
            return vx.SUCCESS

        enum = vx.KERNEL_BASE(vx.ID_DEFAULT, 7) + 1
        kernel = vx.AddKernel(c, b"org.test.hello", enum, func, 2, validate_input, validate_output, None, None)
        assert vx.GetStatus(vx.reference(kernel)) == vx.SUCCESS
        assert vx.AddParameterToKernel(kernel, 0, vx.INPUT, vx.TYPE_IMAGE, vx.PARAMETER_STATE_REQUIRED) == vx.SUCCESS
        assert vx.AddParameterToKernel(kernel, 1, vx.OUTPUT, vx.TYPE_IMAGE, vx.PARAMETER_STATE_REQUIRED) == vx.SUCCESS
        assert vx.SetKernelAttribute(kernel, vx.KERNEL_ATTRIBUTE_LOCAL_DATA_SIZE, 42, 'vx_size') == vx.SUCCESS
        assert vx.FinalizeKernel(kernel) == vx.SUCCESS

        g = vx.CreateGraph(c)
        img = vx.CreateImage(c, 640, 480, vx.DF_IMAGE_U8)
        _, r = vx.GetValidRegionImage(img)
        assert r.start_x == 0
        assert r.end_x == 640
        _, addr, data = vx.AccessImagePatch(img, r, 0, None, None, vx.WRITE_ONLY)
        data[0], data[100] = b'H', b'I'
        assert vx.CommitImagePatch(img, r, 0, addr, data) == vx.SUCCESS

        virt = vx.CreateVirtualImage(g, 0, 0, vx.DF_IMAGE_VIRT)
        # virt = vx.CreateImage(c, 640, 480, vx.DF_IMAGE_U8)
        node = vx.CreateGenericNode(g, kernel)
        vx.SetParameterByIndex(node, 0, vx.reference(img))
        vx.SetParameterByIndex(node, 1, vx.reference(virt))

        assert vx.VerifyGraph(g) == vx.SUCCESS
        assert validate_input.called
        assert validate_output.called
        assert vx.QueryImage(virt, vx.IMAGE_ATTRIBUTE_WIDTH, 'vx_uint32') == (vx.SUCCESS, 640)
        assert vx.QueryImage(virt, vx.IMAGE_ATTRIBUTE_HEIGHT, 'vx_uint32') == (vx.SUCCESS, 480)
        assert vx.ProcessGraph(g) == vx.SUCCESS
        assert func.called == True

        _, addr, data = vx.AccessImagePatch(virt, r, 0, None, None, vx.READ_ONLY)
        assert data[0], data[100] == 'IH'
        vx.CommitImagePatch(virt, r, 0, addr, data)

        img2 = vx.CreateImage(c, 640, 480, vx.DF_IMAGE_U16)
        vx.SetParameterByIndex(node, 0, vx.reference(img2))
        assert vx.VerifyGraph(g) == vx.ERROR_INVALID_VALUE


        enum = vx.KERNEL_BASE(vx.ID_DEFAULT, 7) + 2
        kernel = vx.AddKernel(c, b"org.test.hello2", enum, func, 2, validate_input, validate_output, None, None)
        assert vx.RemoveKernel(kernel) == vx.SUCCESS

        assert vx.ReleaseGraph(g) == vx.SUCCESS
        assert vx.ReleaseContext(c) == vx.SUCCESS
Beispiel #4
0
from pyvx import vx

context = vx.CreateContext()
images = [
    vx.CreateImage(context, 640, 480, vx.DF_IMAGE_UYVY),
    vx.CreateImage(context, 640, 480, vx.DF_IMAGE_S16),
    vx.CreateImage(context, 640, 480, vx.DF_IMAGE_U8),
]
graph = vx.CreateGraph(context)
virts = [
    vx.CreateVirtualImage(graph, 0, 0, vx.DF_IMAGE_VIRT),
    vx.CreateVirtualImage(graph, 0, 0, vx.DF_IMAGE_VIRT),
    vx.CreateVirtualImage(graph, 0, 0, vx.DF_IMAGE_VIRT),
    vx.CreateVirtualImage(graph, 0, 0, vx.DF_IMAGE_VIRT),
]
vx.ChannelExtractNode(graph, images[0], vx.CHANNEL_Y, virts[0])
vx.Gaussian3x3Node(graph, virts[0], virts[1])
vx.Sobel3x3Node(graph, virts[1], virts[2], virts[3])
vx.MagnitudeNode(graph, virts[2], virts[3], images[1])
vx.PhaseNode(graph, virts[2], virts[3], images[2])
status = vx.VerifyGraph(graph)

if status == vx.SUCCESS:
    status = vx.ProcessGraph(graph)
else:
    print("Verification failed.")
vx.ReleaseContext(context)