-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_pywrapper.py
75 lines (62 loc) · 2.79 KB
/
test_pywrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import ctypes
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
if __name__ == '__main__':
# load input data
input_image = Image.open('demo.png').convert('RGBA')
input_data = np.asarray(input_image)
height, width, channels = input_data.shape
# load libgpupixel_pywrapper.dll
current_dir = os.path.dirname(os.path.abspath(__file__))
os.environ['PATH'] += os.pathsep + current_dir
dll_file = 'libgpupixel_pywrapper.dll'
dll_path = os.path.abspath(dll_file)
lib = ctypes.CDLL(dll_path, winmode=0)
# define types of arguments and returns
lib.GPUPixelWrapper_create.restype = ctypes.c_void_p
lib.GPUPixelWrapper_initialize.argtypes = [ctypes.c_void_p]
lib.GPUPixelWrapper_setCallbacks.argtypes = [ctypes.c_void_p]
lib.GPUPixelWrapper_setParameters.argtypes = [ctypes.c_void_p, ctypes.c_float, ctypes.c_float, ctypes.c_float, ctypes.c_float, ctypes.c_float, ctypes.c_float]
lib.GPUPixelWrapper_run.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_uint8), ctypes.c_int, ctypes.c_int, ctypes.c_int]
lib.GPUPixelWrapper_run.restype = ctypes.POINTER(ctypes.c_uint8)
lib.GPUPixelWrapper_destroy.argtypes = [ctypes.POINTER(ctypes.c_uint8)]
lib.GPUPixelWrapper_release.argtypes = [ctypes.c_void_p]
# create GPUPixelWrapper instance
gpu_pixel = lib.GPUPixelWrapper_create()
# intialization
if lib.GPUPixelWrapper_initialize(gpu_pixel):
# set callbacks for input and output
lib.GPUPixelWrapper_setCallbacks(gpu_pixel)
# set parameters for filters
beautyValue = 10.0
whithValue = 10.0
thinFaceValue = 10.0
bigeyeValue = 10.0
lipstickValue = 10.0
blusherValue = 10.0
lib.GPUPixelWrapper_setParameters(gpu_pixel, beautyValue, whithValue, thinFaceValue, bigeyeValue, lipstickValue, blusherValue)
# run image processing pipeline
output_data_ptr = lib.GPUPixelWrapper_run(gpu_pixel, input_data.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)), width, height, channels)
# convert output pointer to numpy array
output_data = np.ctypeslib.as_array(output_data_ptr, shape=(height, width, channels))
output_result = output_data.copy()
plt.figure(figsize=(9, 7))
plt.subplot(121)
plt.title('Input')
plt.imshow(input_data[:, :, :3])
plt.axis('off')
plt.subplot(122)
plt.title('Output')
plt.imshow(output_result[:, :, :3])
plt.axis('off')
plt.tight_layout()
plt.show()
# destroy output data
lib.GPUPixelWrapper_destroy(output_data_ptr)
try:
# try to realse output buffer in gpu_pixel
lib.GPUPixelWrapper_release(gpu_pixel)
except Exception as e:
pass