2023-08-25 08:00:26 +08:00
|
|
|
# distutils: language = c++
|
2024-07-24 00:27:10 +08:00
|
|
|
# cython: c_string_encoding=ascii, language_level=3
|
2023-08-25 08:00:26 +08:00
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
cimport numpy as cnp
|
|
|
|
from libc.string cimport memcpy
|
|
|
|
|
2024-06-07 05:31:56 +08:00
|
|
|
from msgq.visionipc.visionipc cimport cl_mem
|
|
|
|
from msgq.visionipc.visionipc_pyx cimport VisionBuf, CLContext as BaseCLContext
|
2023-08-25 08:00:26 +08:00
|
|
|
from .commonmodel cimport CL_DEVICE_TYPE_DEFAULT, cl_get_device_id, cl_create_context
|
2024-09-24 10:06:52 +08:00
|
|
|
from .commonmodel cimport mat3, ModelFrame as cppModelFrame
|
2023-09-01 10:40:53 +08:00
|
|
|
|
2023-08-25 08:00:26 +08:00
|
|
|
|
|
|
|
cdef class CLContext(BaseCLContext):
|
|
|
|
def __cinit__(self):
|
|
|
|
self.device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT)
|
|
|
|
self.context = cl_create_context(self.device_id)
|
|
|
|
|
|
|
|
cdef class CLMem:
|
|
|
|
@staticmethod
|
|
|
|
cdef create(void * cmem):
|
|
|
|
mem = CLMem()
|
|
|
|
mem.mem = <cl_mem*> cmem
|
|
|
|
return mem
|
|
|
|
|
|
|
|
cdef class ModelFrame:
|
|
|
|
cdef cppModelFrame * frame
|
|
|
|
|
|
|
|
def __cinit__(self, CLContext context):
|
|
|
|
self.frame = new cppModelFrame(context.device_id, context.context)
|
|
|
|
|
|
|
|
def __dealloc__(self):
|
|
|
|
del self.frame
|
|
|
|
|
2024-11-14 11:27:11 +08:00
|
|
|
def prepare(self, VisionBuf buf, float[:] projection, CLMem output):
|
2023-08-25 08:00:26 +08:00
|
|
|
cdef mat3 cprojection
|
|
|
|
memcpy(cprojection.v, &projection[0], 9*sizeof(float))
|
2024-11-14 11:27:11 +08:00
|
|
|
cdef unsigned char * data
|
|
|
|
if output is None:
|
|
|
|
data = self.frame.prepare(buf.buf.buf_cl, buf.width, buf.height, buf.stride, buf.uv_offset, cprojection, NULL)
|
|
|
|
else:
|
|
|
|
data = self.frame.prepare(buf.buf.buf_cl, buf.width, buf.height, buf.stride, buf.uv_offset, cprojection, output.mem)
|
|
|
|
if not data:
|
|
|
|
return None
|
|
|
|
return np.asarray(<cnp.uint8_t[:self.frame.buf_size]> data)
|