def main(): """ Program execution with picture directory parameters """ if (len(sys.argv) != 2): print("The App arg is invalid") exit(1) acl_resource = AclResource() acl_resource.init() #Instantiation classification detection, incoming om model path, model input width and height parameters classify = Classify(acl_resource, MODEL_PATH, MODEL_WIDTH, MODEL_HEIGHT) #Get the picture storage directory from the parameters, and infer picture by picture image_dir = sys.argv[1] images_list = [ os.path.join(image_dir, img) for img in os.listdir(image_dir) if os.path.splitext(img)[1] in IMG_EXT ] #Create a directory and save the infer results if not os.path.isdir(os.path.join(SRC_PATH, "../outputs")): os.mkdir(os.path.join(SRC_PATH, "../outputs")) for image_file in images_list: #read the picture image = AclImage(image_file) image_dvpp = image.copy_to_dvpp() #preprocess image resized_image = classify.pre_process(image_dvpp) print("pre process end") #inference result = classify.inference(resized_image) #post process classify.post_process(result, image_file)
def main(): """ Program execution with picture directory parameters """ if (len(sys.argv) != 2): print("The App arg is invalid") exit(1) acl_resource = AclResource() acl_resource.init() #Instance classification detection, pass into the OM model storage path, model input width and height parameters classify = Classify(acl_resource, MODEL_PATH, MODEL_WIDTH, MODEL_HEIGHT) #From the parameters of the picture storage directory, reasoning by a picture image_dir = sys.argv[1] images_list = [ os.path.join(image_dir, img) for img in os.listdir(image_dir) if os.path.splitext(img)[1] in IMG_EXT ] #Create a directory to store the inference results if not os.path.isdir('../outputs'): os.mkdir('../outputs') resized_image_list = [] batch_image_files = [] num = 0 batch_amount = len(images_list) // BATCH left = len(images_list) % BATCH for image_file in images_list: num += 1 #Read the pictures image = AclImage(image_file) image_dvpp = image.copy_to_dvpp() #preprocess image resized_image = classify.pre_process(image_dvpp) print("pre process end") batch_image_files.append(image_file) resized_image_list.append(resized_image) if batch_amount > 0: #Each set of BATCH pictures, reasoning and post-processing if num == BATCH: #Reasoning pictures result = classify.inference(resized_image_list, BATCH) #process inference results classify.post_process(result, batch_image_files, BATCH) batch_amount -= 1 num = 0 batch_image_files = [] resized_image_list = [] else: #remaining images are inferred and post-processed if num == left: #Reasoning pictures result = classify.inference(resized_image_list, BATCH) #The inference results are processed classify.post_process(result, batch_image_files, left)
def main(): if (len(sys.argv) != 2): print("The App arg is invalid") exit(1) classify = Classify(MODEL_PATH, MODEL_WIDTH, MODEL_HEIGHT) ret = classify.init() if not os.path.isdir(os.path.join(SRC_PATH, "../outputs")): os.mkdir(os.path.join(SRC_PATH, "../outputs")) image_dir = sys.argv[1] images_list = [ os.path.join(image_dir, img) for img in os.listdir(image_dir) if os.path.splitext(img)[1] in IMG_EXT ] for image_file in images_list: image = AclImage(image_file) resized_image = classify.pre_process(image) print("pre process end") result = classify.inference(resized_image) result_img_encode = classify.post_process(result, image_file) return result_img_encode
def main_process(): classify = Classify(MODEL_PATH, MODEL_WIDTH, MODEL_HEIGHT) ret = classify.init() if not os.path.isdir('./outputs'): os.mkdir('./outputs') image_dir = "./origin" images_list = [ os.path.join(image_dir, img) for img in os.listdir(image_dir) if os.path.splitext(img)[1] in IMG_EXT ] for image_file in images_list: image = AclImage(image_file) resized_image = classify.pre_process(image) print("pre process end") result = classify.inference(resized_image) result_img_encode = classify.post_process(result, image_file) #acl_resource.destroy() #classify.destroy() return result_img_encode
def main(): # check param if (len(sys.argv) != 2): print("The App arg is invalid") exit(1) # instantiation Cartoonization object cartoonization = Cartoonization(MODEL_PATH, MODEL_WIDTH, MODEL_HEIGHT) # init ret = cartoonization.init() check_ret("Cartoonization.init ", ret) # get all pictures image_dir = sys.argv[1] images_list = [os.path.join(image_dir, img) for img in os.listdir(image_dir) if os.path.splitext(img)[1] in IMG_EXT] # create dir to save result if not os.path.isdir('../outputs'): os.mkdir('../outputs') for image_file in images_list: # read image image = AclImage(image_file) # preprocess crop_and_paste_image = cartoonization.pre_process(image) print("[Sample] pre process end") # inference result = cartoonization.inference(crop_and_paste_image) # postprocess cartoonization.post_process(result, image_file, image)
def jpegd(self, image): ''' :param image: input image :return: AclImage ''' print('[Dvpp] jpeg decode stage:') print("[Dvpp] jpeg width %d, height %d, size %d" % (image.width, image.height, image.size), \ " data ", image.data()) output_desc, out_buffer = self._gen_jpegd_out_pic_desc(image) image_dvpp = image.copy_to_dvpp(self._run_mode) ret = acl.media.dvpp_jpeg_decode_async(self._dvpp_channel_desc, image_dvpp.data(), image_dvpp.size, output_desc, self._stream) if ret != ACL_ERROR_NONE: print("dvpp_jpeg_decode_async failed ret={}".format(ret)) return None ret = acl.rt.synchronize_stream(self._stream) if ret != ACL_ERROR_NONE: print("dvpp_jpeg_decode_async failed ret={}".format(ret)) return None width = align_up128(image.width) height = align_up16(image.height) return AclImage(out_buffer, width, height, yuv420sp_size(width, height), MEMORY_DVPP)
def crop_and_paste(self, image, width, height, crop_and_paste_width, crop_and_paste_height): ''' :image: input image :width: input image width :height: input image height :crop_and_paste_width: crop_and_paste_width :crop_and_paste_height: crop_and_paste_height :return: return AclImage ''' print('[Dvpp] vpc crop and paste stage:') input_desc = self._gen_input_pic_desc(image) output_desc, out_buffer, out_buffer_size = \ self._gen_resize_out_pic_desc(crop_and_paste_width, crop_and_paste_height) self._crop_config = acl.media.dvpp_create_roi_config( 0, (width >> 1 << 1) - 1, 0, (height >> 1 << 1) - 1) self._paste_config = acl.media.dvpp_create_roi_config( 0, crop_and_paste_width - 1, 0, crop_and_paste_height - 1) ret = acl.media.dvpp_vpc_crop_and_paste_async(self._dvpp_channel_desc, input_desc, output_desc, self._crop_config, self._paste_config, self._stream) check_ret("acl.media.dvpp_vpc_crop_and_paste_async", ret) ret = acl.rt.synchronize_stream(self._stream) check_ret("acl.rt.synchronize_stream", ret) print('[Dvpp] vpc crop and paste stage success') stride_width = align_up16(crop_and_paste_width) stride_height = align_up2(crop_and_paste_height) return AclImage(out_buffer, stride_width, stride_height, out_buffer_size, MEMORY_DVPP)
def read(self): frame_data = CameraOutputC() ret = libatlas.ReadCameraFrame(self._id, byref(frame_data)) if (ret != CAMERA_OK): print("ERROR:Read camera %d failed" % (self._id)) return None return AclImage(addressof(frame_data.data.contents), self._width, self._height, self._size, MEMORY_DVPP)
def read(self): frame = ImageDataC() image = None ret = libatlas.ReadDecodedFrame(self._channel_id, byref(frame)) if ret == READ_VIDEO_OK: image = AclImage(addressof(frame.data.contents), frame.width, frame.height, frame.size, MEMORY_DVPP) elif ret == VIDEO_DECODE_FINISH: log_info("Video has been read finished") else: log_error("Read frame error: ", ret) return ret, image
def resize(self, image, resize_width, resize_height): print('[Dvpp] vpc resize stage:') input_desc = self._gen_input_pic_desc(image) output_desc, out_buffer, out_buffer_size = \ self._gen_resize_out_pic_desc(resize_width, resize_height) ret = acl.media.dvpp_vpc_resize_async(self._dvpp_channel_desc, input_desc, output_desc, self._resize_config, self._stream) check_ret("acl.media.dvpp_vpc_resize_async", ret) ret = acl.rt.synchronize_stream(self._stream) check_ret("acl.rt.synchronize_stream", ret) print('[Dvpp] vpc resize stage success') stride_width = align_up16(image.width) stride_height = align_up2(image.height) return AclImage(out_buffer, stride_width, stride_height, out_buffer_size, MEMORY_DVPP)
def jpege(self, image, width_align=128, height_align=16): ''' yuv420sp to jpeg :param image: input image :param width_align: width align :param height_align: height align :return: AclImage ''' # create input image desc input_desc = self._gen_input_pic_desc(image, width_align, height_align) output_size, ret = acl.media.dvpp_jpeg_predict_enc_size( input_desc, self._jpege_config) if (ret != ACL_ERROR_NONE): print("Predict jpege output size failed") return None output_buffer, ret = acl.media.dvpp_malloc(output_size) if (ret != ACL_ERROR_NONE): print("Malloc jpege output memory failed") return None output_size_array = np.array([output_size], dtype=np.int32) output_size_ptr = acl.util.numpy_to_ptr(output_size_array) ret = acl.media.dvpp_jpeg_encode_async(self._dvpp_channel_desc, input_desc, output_buffer, output_size_ptr, self._jpege_config, self._stream) if (ret != ACL_ERROR_NONE): print("Jpege failed, ret ", ret) return None ret = acl.rt.synchronize_stream(self._stream) if (ret != ACL_ERROR_NONE): print("Jpege synchronize stream failed, ret ", ret) return None # release resources acl.media.dvpp_destroy_pic_desc(input_desc) return AclImage(output_buffer, image.width, image.height, int(output_size_array[0]), MEMORY_DVPP)
def jpegd(self, image): print('[Dvpp] vpc decode stage:') device_image = image.copy_to_device(self._run_mode) output_desc, out_buffer = self._gen_jpegd_out_pic_desc(image) ret = acl.media.dvpp_jpeg_decode_async(self._dvpp_channel_desc, device_image.data(), image.size, output_desc, self._stream) if ret != ACL_ERROR_NONE: print("dvpp_jpeg_decode_async failed ret={}".format(ret)) return None ret = acl.rt.synchronize_stream(self._stream) if ret != ACL_ERROR_NONE: print("dvpp_jpeg_decode_async failed ret={}".format(ret)) return None width = align_up128(image.width) height = align_up16(image.height) return AclImage(out_buffer, width, height, yuv420sp_size(width, height), MEMORY_DVPP)