同步操作将从 yxgy/samples 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import osimport numpy as npfrom PIL import Imageimport aclimport acllite_utils as utilsimport acllite_logger as acl_logimport constants as constclass AclLiteImage(object):"""Image data and operation classWrap image data and operation method, support jpeg, png, yuv file andmemory dataAttributes:_run_mode: device run mode_data: image binary data or numpy array_memory_type: the data in which memory, include dvpp,device and np arraywidth: image widthheight: image heightalign_width: align image widthalign_height: align image height_encode_format: image format_load_ok: load image success or not"""_run_mode, _ = acl.rt.get_run_mode()def __init__(self, image, width=0, height=0, align_width=0, align_height=0,size=0, memory_type=const.MEMORY_NORMAL):"""Create AclLiteImage instanceArgs:image: image data, binary, numpy array or file pathwidth: image width. if image is jpeg or png file,this arg is not nesscessaryheight: image height. if image is jpeg or png file, this arg isnot nesscessarysize: image data size. if image is file path, this arg is notnesscessarymemory_type: memory type of image data. if image is file path, thisarg is not nesscessary"""self._data = Noneself._bytes_data = Noneself._memory_type = memory_typeself.width = 0self.height = 0self.align_width = 0self.align_height = 0self.size = 0self._encode_format = const.ENCODE_FORMAT_UNKNOWself._load_ok = Trueif isinstance(image, str):self._instance_by_image_file(image, width, height)elif isinstance(image, int):self._instance_by_buffer(image, width, height, align_width, align_height, size)elif isinstance(image, np.ndarray):self._instance_by_nparray(image, width, height, align_width, align_height)else:acl_log.log_error("Create instance failed for ""unknow image data type")def __del__(self):self.destroy()def is_loaded(self):"""Image file load resultWhen create image instance by file, call this method to checkfile load success or notReturns:True: load successFalse: load failed"""return self._load_okdef byte_data_to_np_array(self):"""Trans image data to np array"""if self._type == const.IMAGE_DATA_NUMPY:return self._data.copy()return utils.copy_data_as_numpy(self._data, self.size,self._memory_type, AclLiteImage._run_mode)def data(self):"""Get image binary data"""if self._type == const.IMAGE_DATA_NUMPY:if "bytes_to_ptr" in dir(acl.util):self._bytes_data = self._data.tobytes()factor_ptr = acl.util.bytes_to_ptr(self._bytes_data)else:factor_ptr = acl.util.numpy_to_ptr(self._data)return factor_ptrelse:return self._datadef copy_to_dvpp(self):"""Copy image data to dvpp"""device_ptr = utils.copy_data_to_dvpp(self.data(), self.size,self._run_mode)if device_ptr is None:acl_log.log_error("Copy image to dvpp failed")return Nonereturn AclLiteImage(device_ptr, self.width, self.height, 0, 0,self.size, const.MEMORY_DVPP)def copy_to_host(self):""""Copy data to host"""if self._type == const.IMAGE_DATA_NUMPY:data_np = self._data.copy()return AclLiteImage(data_np, self.width, self.height, 0, 0)data = Nonemem_type = const.MEMORY_HOSTif AclLiteImage._run_mode == const.ACL_HOST:if self.is_local():data = utils.copy_data_host_to_host(self._data, self.size)else:data = utils.copy_data_device_to_host(self._data, self.size)else:data = utils.copy_data_device_to_device(self._data, self.size)mem_type = const.MEMORY_DEVICEif data is None:acl_log.log_error("Copy image to host failed")return Nonereturn AclLiteImage(data, self.width, self.height, 0, 0, self.size, mem_type)def is_local(self):"""Image data is in host server memory and access directly or not"""# in atlas200dk, all kind memory can access directlyif AclLiteImage._run_mode == const.ACL_DEVICE:return True# in atlas300, only acl host memory or numpy array can access directlyelif ((AclLiteImage._run_mode == const.ACL_HOST) and((self._memory_type == const.MEMORY_HOST) or(self._memory_type == const.MEMORY_NORMAL))):return Trueelse:return Falsedef save(self, filename):"""Save image as file"""image_np = self.byte_data_to_np_array()image_np.tofile(filename)def destroy(self):"""Release image memory"""if (self._data is None) or (self.size == 0):acl_log.log_error("Release image abnormaly, data is None")returnif self._memory_type == const.MEMORY_DEVICE:acl.rt.free(self._data)elif self._memory_type == const.MEMORY_HOST:acl.rt.free_host(self._data)elif self._memory_type == const.MEMORY_DVPP:acl.media.dvpp_free(self._data)# numpy no need releaseself._data = Noneself.size = 0def _instance_by_image_file(self, image_path, width, height):# Get image format by filename suffixself._encode_format = self._get_image_format_by_suffix(image_path)if self._encode_format == const.ENCODE_FORMAT_UNKNOW:acl_log.log_error("Load image %s failed" % (image_path))self._load_ok = Falsereturn# Read image data from file to memoryself._data = np.fromfile(image_path, dtype=np.byte)self._type = const.IMAGE_DATA_NUMPYself.size = self._data.itemsize * self._data.sizeself._memory_type = const.MEMORY_NORMAL# Get image parameters of jpeg or png file by pillowif ((self._encode_format == const.ENCODE_FORMAT_JPEG) or(self._encode_format == const.ENCODE_FORMAT_PNG)):image = Image.open(image_path)self.width, self.height = image.sizeelse:# pillow can not decode yuv, so need input widht and height argsself.width = widthself.height = heightdef _get_image_format_by_suffix(self, filename):suffix = os.path.splitext(filename)[-1].strip().lower()if (suffix == ".jpg") or (suffix == ".jpeg"):image_format = const.ENCODE_FORMAT_JPEGelif suffix == ".png":image_format = const.ENCODE_FORMAT_PNGelif suffix == ".yuv":image_format = const.ENCODE_FORMAT_YUV420_SPelse:acl_log.log_error("Unsupport image format: ", suffix)image_format = const.ENCODE_FORMAT_UNKNOWreturn image_formatdef _instance_by_buffer(self, image_buffer, width, height, align_width, align_height, size):self.width = widthself.height = heightself.align_height = align_heightself.align_width = align_widthself.size = sizeself._data = image_bufferself._type = const.IMAGE_DATA_BUFFERdef _instance_by_nparray(self, data, width, height, align_width, align_height):self.width = widthself.height = heightself.align_height = align_heightself.align_width = align_widthself.size = data.itemsize * data.sizeself._data = dataself._type = const.IMAGE_DATA_NUMPYself._memory_type = const.MEMORY_NORMAL
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。