commonmodel_pyx.pyx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # distutils: language = c++
  2. # cython: c_string_encoding=ascii
  3. import numpy as np
  4. cimport numpy as cnp
  5. from libc.string cimport memcpy
  6. from cereal.visionipc.visionipc cimport cl_mem
  7. from cereal.visionipc.visionipc_pyx cimport VisionBuf, CLContext as BaseCLContext
  8. from .commonmodel cimport CL_DEVICE_TYPE_DEFAULT, cl_get_device_id, cl_create_context
  9. from .commonmodel cimport mat3, sigmoid as cppSigmoid, ModelFrame as cppModelFrame
  10. def sigmoid(x):
  11. return cppSigmoid(x)
  12. cdef class CLContext(BaseCLContext):
  13. def __cinit__(self):
  14. self.device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT)
  15. self.context = cl_create_context(self.device_id)
  16. cdef class CLMem:
  17. @staticmethod
  18. cdef create(void * cmem):
  19. mem = CLMem()
  20. mem.mem = <cl_mem*> cmem
  21. return mem
  22. cdef class ModelFrame:
  23. cdef cppModelFrame * frame
  24. def __cinit__(self, CLContext context):
  25. self.frame = new cppModelFrame(context.device_id, context.context)
  26. def __dealloc__(self):
  27. del self.frame
  28. def prepare(self, VisionBuf buf, float[:] projection, CLMem output):
  29. cdef mat3 cprojection
  30. memcpy(cprojection.v, &projection[0], 9*sizeof(float))
  31. cdef float * data = self.frame.prepare(buf.buf.buf_cl, buf.width, buf.height, buf.stride, buf.uv_offset, cprojection, output.mem)
  32. if not data:
  33. return None
  34. return np.asarray(<cnp.float32_t[:self.frame.buf_size]> data)