def pil2vx(context, pil_image): height = pil_image.height width = pil_image.width addr = vx.imagepatch_addressing_t(width, height, 1, width, vx.SCALE_UNITY, vx.SCALE_UNITY, 1, 3) arr = list(pil_image.getdata()) npArr = np.reshape(arr, (-1, width)) npArr = np.uint8(npArr) vx_image = vx.CreateImageFromHandle(context, vx.DF_IMAGE_U8, addr, npArr[0], vx.IMPORT_TYPE_HOST) print vx.QueryImage(vx_image, vx.IMAGE_ATTRIBUTE_WIDTH, 'vx_uint32') == (vx.SUCCESS, width) print vx.QueryImage(vx_image, vx.IMAGE_ATTRIBUTE_FORMAT, 'vx_enum') == (vx.SUCCESS, vx.DF_IMAGE_U8) return vx_image
def vx2np(vx_image): status, width = vx.QueryImage(vx_image, vx.IMAGE_ATTRIBUTE_WIDTH, 'vx_uint32') status, height = vx.QueryImage(vx_image, vx.IMAGE_ATTRIBUTE_HEIGHT, 'vx_uint32') rect = vx.rectangle_t(0, 0, width - 1, height - 1) addr = vx.imagepatch_addressing_t(width, height, 1, width, vx.SCALE_UNITY, vx.SCALE_UNITY, 1, 1) rdata = array('B', [0]) * (width * height) status, addr, ptr = vx.AccessImagePatch(vx_image, rect, 0, addr, rdata, vx.READ_ONLY) vx.CommitImagePatch(vx_image, rect, 0, addr, rdata) npArray = np.reshape(rdata, (-1, width)) npArray2 = np.uint8(npArray) return npArray2
def get_width(self): s = vx.GetStatus(vx.reference(self.image)) status, width = vx.QueryImage(self.image, vx.IMAGE_ATTRIBUTE_WIDTH, 'vx_uint32') if status != 0: raise NameError('Failed to query VX_IMAGE_ATTRIBUTE_WIDTH') return width
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
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 test_pyramid(self): c = vx.CreateContext() pyramid = vx.CreatePyramid(c, 4, vx.SCALE_PYRAMID_HALF, 640, 480, vx.DF_IMAGE_U8) assert vx.GetStatus(vx.reference(pyramid)) == vx.SUCCESS assert vx.QueryReference(vx.reference(pyramid), vx.REF_ATTRIBUTE_TYPE, 'vx_enum') == (vx.SUCCESS, vx.TYPE_PYRAMID) assert vx.QueryPyramid(pyramid, vx.PYRAMID_ATTRIBUTE_WIDTH, 'vx_uint32') == (vx.SUCCESS, 640) img = vx.GetPyramidLevel(pyramid, 1) assert img assert vx.QueryImage(img, vx.IMAGE_ATTRIBUTE_WIDTH, 'vx_uint32') == (vx.SUCCESS, 320) g = vx.CreateGraph(c) p = vx.CreateVirtualPyramid(g, 3, vx.SCALE_PYRAMID_HALF, 0, 0, vx.DF_IMAGE_VIRT) assert vx.ReleasePyramid(p) == vx.SUCCESS assert vx.ReleasePyramid(pyramid) == vx.SUCCESS assert vx.ReleaseContext(c) == vx.SUCCESS
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
def test_image(self): c = vx.CreateContext() img = vx.CreateImage(c, 640, 480, vx.DF_IMAGE_RGB) assert vx.GetStatus(vx.reference(img)) == vx.SUCCESS assert vx.QueryReference(vx.reference(img), vx.REF_ATTRIBUTE_TYPE, 'vx_enum') == (vx.SUCCESS, vx.TYPE_IMAGE) roi = vx.CreateImageFromROI(img, vx.rectangle_t(10, 10, 100, 100)) assert vx.GetStatus(vx.reference(roi)) == vx.SUCCESS assert vx.ReleaseImage(roi) == vx.SUCCESS roi = None const = vx.CreateUniformImage(c, 640, 480, vx.DF_IMAGE_S16, 7, 'vx_int16') assert vx.GetStatus(vx.reference(const)) == vx.SUCCESS const = vx.CreateUniformImage(c, 640, 480, vx.DF_IMAGE_RGB, (7, 8, 9), 'vx_uint8[]') assert vx.GetStatus(vx.reference(const)) == vx.SUCCESS addr = vx.imagepatch_addressing_t(640, 480, 1, 640, vx.SCALE_UNITY, vx.SCALE_UNITY, 1, 1) data = array('B', [0x42]) * (640 * 480) hand = vx.CreateImageFromHandle(c, vx.DF_IMAGE_U8, (addr,), (data,), vx.IMPORT_TYPE_HOST) assert vx.GetStatus(vx.reference(hand)) == vx.SUCCESS hand = vx.CreateImageFromHandle(c, vx.DF_IMAGE_U8, addr, data, vx.IMPORT_TYPE_HOST) assert vx.GetStatus(vx.reference(hand)) == vx.SUCCESS hand = vx.CreateImageFromHandle(c, vx.DF_IMAGE_RGB, (addr, addr, addr), (data, data, data), vx.IMPORT_TYPE_HOST) assert vx.GetStatus(vx.reference(hand)) == vx.SUCCESS assert vx.QueryImage(img, vx.IMAGE_ATTRIBUTE_WIDTH, 'vx_uint32') == (vx.SUCCESS, 640) assert vx.SetImageAttribute(img, vx.IMAGE_ATTRIBUTE_SPACE, vx.COLOR_SPACE_BT601_525, 'vx_enum') == vx.SUCCESS assert vx.QueryImage(img, vx.IMAGE_ATTRIBUTE_SPACE, 'vx_enum') == (vx.SUCCESS, vx.COLOR_SPACE_BT601_525) assert vx.GetContext(vx.reference(img)) == c r = vx.rectangle_t(10, 20, 30, 40) s = vx.ComputeImagePatchSize(img, r, 0) status, addr, ptr = vx.AccessImagePatch(img, r, 0, None, None, vx.READ_AND_WRITE) assert status == vx.SUCCESS assert addr.dim_x == addr.dim_y == 20 ptr[0] = b'H' assert vx.CommitImagePatch(img, r, 0, addr, ptr) == vx.SUCCESS status, addr, ptr = vx.AccessImagePatch(img, r, 0, None, None, vx.READ_AND_WRITE) assert status == vx.SUCCESS assert ptr[0] == b'H' pixel = vx.FormatImagePatchAddress1d(ptr, 0, addr) assert pixel[0] == b'H' assert vx.CommitImagePatch(img, r, 0, addr, ptr) == vx.SUCCESS assert 7 not in data addr = vx.imagepatch_addressing_t(20, 20, 1, 20, vx.SCALE_UNITY, vx.SCALE_UNITY, 1, 1) rdata = array('B', [0]) * (20 * 20) status, addr, ptr = vx.AccessImagePatch(hand, r, 0, addr, rdata, vx.READ_AND_WRITE) assert rdata[1] == 0x42 rdata[1] = 7 pixel = vx.FormatImagePatchAddress1d(ptr, 1, addr) assert pixel[0] == b'\x07' pixel = vx.FormatImagePatchAddress2d(ptr, 0, 0, addr) assert pixel[0] == b'\x42' assert vx.CommitImagePatch(hand, r, 0, addr, ptr) == vx.SUCCESS assert data[11 + 20*640] == 7 status, r = vx.GetValidRegionImage(const) assert status == vx.SUCCESS assert r.end_x == 640 assert r.end_y == 480 assert vx.ReleaseContext(c) == vx.SUCCESS
def get_height(self): status, height = vx.QueryImage(self.image, vx.IMAGE_ATTRIBUTE_HEIGHT, 'vx_uint32') if status != 0: raise NameError('Failed to query VX_IMAGE_ATTRIBUTE_HEIGHT') return height
def get_color(self): status, color = vx.QueryImage(self.image, vx.IMAGE_ATTRIBUTE_FORMAT, 'vx_df_image') if status != 0: raise NameError('Failed to query VX_IMAGE_ATTRIBUTE_FORMAT') return COLOR[color]