def __init__(self, acl_resource, model_path, model_width, model_height): self._acl_resource = acl_resource self._model_width = model_width self._model_height = model_height self._model = AclLiteModel(model_path)
class Classify(object): """ define gesture class """ def __init__(self, acl_resource, model_path, model_width, model_height): self._model_path = model_path self._model_width = model_width self._model_height = model_height self._dvpp = AclLiteImageProc(acl_resource) self._model = AclLiteModel(model_path) def __del__(self): if self._dvpp: del self._dvpp print("[Sample] class Samle release source success") def pre_process(self, image): """ pre_precess """ yuv_image = self._dvpp.jpegd(image) resized_image = self._dvpp.resize(yuv_image, self._model_width, self._model_height) print("resize yuv end") return resized_image def inference(self, resized_image): """ inference """ return self._model.execute([ resized_image, ]) def post_process(self, infer_output, image_file): """ post_process """ print("post process") data = infer_output[0] vals = data.flatten() top_k = vals.argsort()[-1:-6:-1] print("images:{}".format(image_file)) print("======== top5 inference results: =============") for n in top_k: object_class = image_net_classes.get_image_net_class(n) print("label:%d confidence: %f, class: %s" % (n, vals[n], object_class)) #using pillow, the category with the highest confidence is written on the image and saved locally if len(top_k): object_class = image_net_classes.get_image_net_class(top_k[0]) output_path = os.path.join(os.path.join(SRC_PATH, "../out"), os.path.basename(image_file)) origin_img = Image.open(image_file) draw = ImageDraw.Draw(origin_img) font = ImageFont.truetype( "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", size=20) draw.text((10, 50), object_class, font=font, fill=255) origin_img.save(output_path)
class CrowdCount(object): """ crowdCount """ def __init__(self, model_path, model_width, model_height): self.device_id = 0 self.context = None self.stream = None self._model = None self.run_mode = None self._model_path = model_path self._model_width = model_width self._model_height = model_height self._dvpp = None self._model = None def init(self): """ init """ self._dvpp = AclLiteImageProc() self._model = AclLiteModel(self._model_path) return constants.SUCCESS def pre_process(self, image): """ image preprocess """ image_dvpp = image.copy_to_dvpp() yuv_image = self._dvpp.jpegd(image_dvpp) crop_and_paste_image = \ self._dvpp.crop_and_paste(yuv_image, image.width, image.height, self._model_width, self._model_height) return crop_and_paste_image def inference(self, input_data): """ model inference """ return self._model.execute(input_data) def post_process(self, image, infer_output, image_file): """ Post-processing, analysis of inference results """ orig = cv2.imread(image_file, 1) orig = cv2.resize(orig, (image.width, image.height)) data = infer_output[0] vals = data.flatten() res = np.sum(vals, axis=0) result = round(res / 1000.0) data_2 = data.reshape(800, 1408) heatMap = data_2[:image.height, :image.width] heatMap = heatMap.astype(np.uint8) heatMap = cv2.GaussianBlur(heatMap, (0, 0), 5, 5, cv2.BORDER_DEFAULT) cv2.normalize(heatMap, heatMap, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC1) heatMap = cv2.applyColorMap(heatMap, cv2.COLORMAP_JET) add_img = cv2.addWeighted(orig, 1, heatMap, 0.5, 0.0) cv2.putText(add_img, str(result), (30, 60), cv2.FONT_HERSHEY_PLAIN, 5, (0, 0, 255), 8) output_path = os.path.join("../out", os.path.basename(image_file)) cv2.imwrite(output_path, add_img)
class Hpa(object): """ Class for portrait segmentation """ def __init__(self, model_path, model_width, model_height): self._model_path = model_path self._model_width = model_width self._model_height = model_height self._img_width = 0 self._img_height = 0 self._model = None def init(self): """ Initialize """ # Load model self._model = AclLiteModel(self._model_path) return constants.SUCCESS def pre_process(self, im): """ image preprocess """ self._img_width = im.size[0] self._img_height = im.size[1] im = im.resize((224, 224)) # hwc img = np.array(im) # rgb to bgr img = img[:, :, ::-1] img = img.astype("float16") result = img.transpose([2, 0, 1]).copy() return result def inference(self, input_data): """ model inference """ return self._model.execute(input_data) def sigmoid(self, x): """ sigmod function """ return 1. / (1 + np.exp(-x)) def visualize(self, file_name, pred): """ visualize """ # 1, ID and name corresponding id_2_label = [ "Mitochondria", "Nucleus", "Endoplasmic reticulum", "Nuclear speckles", "Plasma membrane", "Nucleoplasm", "Cytosol", "Nucleoli", "Vesicles", "Golgi apparatus" ] # 2. Read pictures setFont = ImageFont.truetype('./font.ttf', 20) fillColor = "#fff" im = Image.open(file_name) im = im.resize((512, 512)) draw = ImageDraw.Draw(im) pred = pred.flatten() top1 = np.argmax(pred) print(id_2_label[top1], pred[top1]) label = "%s : %.2f%%" % (id_2_label[top1], float(pred[top1]) * 100) pred[top1] = 0 draw.text(xy=(20, 20), text=label, font=setFont, fill=fillColor) top2 = np.argmax(pred) print(top2, pred.shape) label = "%s : %.2f%%" % (id_2_label[top2], float(pred[top2]) * 100) pred[top2] = 0 draw.text(xy=(20, 50), text=label, font=setFont, fill=fillColor) top3 = np.argmax(pred) label = "%s : %.2f%%" % (id_2_label[top3], float(pred[top3]) * 100) pred[top3] = 0 draw.text(xy=(20, 80), text=label, font=setFont, fill=fillColor) # save photo im.save("../out/out_" + os.path.basename(file_name)) def post_process(self, result, image_name): """ post_process """ score = np.array(result[0]) pred = self.sigmoid(score) # visualize self.visualize(image_name, pred)
args = parser.parse_args() model_name = 'Hand_detection' img_file = args.input_image # initialize acl resource acl_resource = AclLiteResource() acl_resource.init() #load model PROJECT_SRC_PATH = os.path.realpath(__file__).rsplit("/", 1)[0] MODEL_PATH = os.path.join(PROJECT_SRC_PATH, "../model/" + model_name + ".om") print("MODEL_PATH:", MODEL_PATH) try: model = AclLiteModel(MODEL_PATH) # model = AclLiteModel(acl_resource, MODEL_PATH) except Exception as e: print("AclLiteModel loads error from", MODEL_PATH) # load image file img_path = os.path.join(path, args.input_image) test_image = cv2.imread(img_path) input_image = PreProcessing(test_image) # om model inference inferenceList = model.execute([input_image]) # postprocessing and save inference results PostProcessing(test_image, inferenceList)
def __init__(self, acl_resource, model_path, model_width, model_height): self._model_path = model_path self._model_width = model_width self._model_height = model_height self._dvpp = AclLiteImageProc(acl_resource) self._model = AclLiteModel(model_path)
def __init__(self, model_path, model_width, model_height): self._model_path = model_path self._model_width = model_width self._model_height = model_height self._model = AclLiteModel(model_path)