def main(): """ acl resource initialization """ if not os.path.exists(OUTPUT_DIR): os.mkdir(OUTPUT_DIR) #ACL resource initialization acl_resource = AclResource() acl_resource.init() model = Model(model_path) images_list = [ os.path.join(INPUT_DIR, img) for img in os.listdir(INPUT_DIR) if os.path.splitext(img)[1] in IMG_EXT ] for pic in images_list: orig_shape, orig_l, l_data = preprocess(pic) result_list = model.execute([ l_data, ]) postprocess(result_list, pic, orig_shape, orig_l) break print("Execute end")
def main(): """ main """ #create output directory if not os.path.exists(OUTPUT_DIR): os.mkdir(OUTPUT_DIR) #acl init acl_resource = AclResource() acl_resource.init() #load model model = Model(MODEL_PATH) src_dir = os.listdir(INPUT_DIR) #infer picture for pic in src_dir: if not pic.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')): print('it is not a picture, %s, ignore this file and continue,' % pic) continue #read picture pic_path = os.path.join(INPUT_DIR, pic) bgr_img = cv2.imread(pic_path).astype(np.float32) #get pic data orig_shape, rgb_img = preprocess(bgr_img) #inference result_list = model.execute([rgb_img, ]) #postprocess postprocess(result_list, orig_shape, bgr_img, pic) print("Execute end")
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 = Dvpp(acl_resource) self._model = Model(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, "../outputs"), os.path.basename(image_file)) origin_img = Image.open(image_file) draw = ImageDraw.Draw(origin_img) font = ImageFont.load_default() draw.text((10, 50), object_class, font=font, fill=255) origin_img.save(output_path)
class Classify(object): """ Classify """ 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 = Model(model_path) @display_time def pre_process(self, image): """ pre_process """ bgr_img = cv2.imread(image).astype(np.float32) bgr_img = bgr_img / 255.0 resized_image = cv2.resize(bgr_img, (299, 299)) return resized_image @display_time def inference(self, resized_image): """ inference """ return self._model.execute([ resized_image, ]) @display_time 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:-7:-1] print("images:{}".format(image_file)) print("======== top5 inference results: =============") for n in top_k: object_class = 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 = get_image_net_class(top_k[0]) object_value = vals[top_k[0]] output_path = os.path.join(os.path.join(SRC_PATH, "../outputs"), os.path.basename(image_file)) origin_img = cv2.imread(image_file) font = cv2.FONT_HERSHEY_SIMPLEX origin_img = cv2.putText(origin_img, object_class, (10, 100), font, 3, (255, 255, 255), 3) origin_img = cv2.putText(origin_img, str(object_value), (10, 200), font, 2, (255, 255, 255), 3) cv2.imwrite(output_path, origin_img)
def main(): acl_resource = AclResource() acl_resource.init() detect = VggSsd(acl_resource, MODEL_WIDTH, MODEL_HEIGHT) model = Model(MODEL_PATH) chan = presenteragent.presenter_channel.open_channel(MASK_DETEC_CONF) if chan is None: print("Open presenter channel failed") return lenofUrl = len(sys.argv) if lenofUrl <= 1: print("[ERROR] Please input mp4/Rtsp URL") exit() elif lenofUrl >= 3: print("[ERROR] param input Error") exit() URL = sys.argv[1] URL1 = re.match('rtsp://', URL) URL2 = re.search('.mp4', URL) if URL1 is None and URL2 is None: print("[ERROR] should input correct URL") exit() cap = video.AclVideo(URL) while True: # Read a frame ret, image = cap.read() if ret != 0: print("read None image, break") break #pre process model_input = detect.pre_process(image) if model_input is None: print("Pre process image failed") break # inference result = model.execute(model_input) if result is None: print("execute mode failed") break # post process jpeg_image, detection_list = detect.post_process(result, image) if jpeg_image is None: print("The jpeg image for present is None") break chan.send_detection_data(CAMERA_FRAME_WIDTH, CAMERA_FRAME_HEIGHT, jpeg_image, detection_list)
class ModelProcessor: def __init__(self, acl_resource, params): self._acl_resource = acl_resource self.params = params self._model_width = params['width'] self._model_height = params['height'] assert 'model_dir' in params and params[ 'model_dir'] is not None, 'Review your param: model_dir' assert os.path.exists( params['model_dir']), "Model directory doesn't exist {}".format( params['model_dir']) # load model from path, and get model ready for inference self.model = Model(acl_resource, params['model_dir']) def predict(self, img_original): #preprocess image to get 'model_input' model_input = self.preprocess(img_original) # execute model inference infer_output = self.model.execute([model_input]) # postprocessing: category = self.post_process(infer_output) return category def preprocess(self, img_original): ''' preprocessing: resize image to model required size, and normalize value ''' scaled_img_data = cv2.resize(img_original, (self._model_width, self._model_height)) normalized_img = scaled_img_data - np.array([123, 117, 104]) # Caffe model after conversion, need input to be NCHW; the orignal image is NHWC, need to be transposed (use .copy() to change memory format) preprocessed_img = np.asarray(normalized_img, dtype=np.float16).transpose([2, 0, 1]).copy() return preprocessed_img def post_process(self, infer_output): print("post process") data = infer_output[0] vals = data.flatten() top_k = vals.argsort()[-1:-6:-1] print("======== top5 inference results: =============") for n in top_k: object_class = get_image_net_class(n) print("label:%d confidence: %f, class: %s" % (n, vals[n], object_class)) object_class = get_image_net_class(top_k[0]) return object_class
def main(): """ acl resource initialization """ acl_resource = AclResource() acl_resource.init() #load model model = Model(model_path) chan = presenteragent.presenter_channel.open_channel(COLORIZATION_CONF) if chan is None: print("Open presenter channel failed") return lenofUrl = len(sys.argv) if lenofUrl <= 1: print("[ERROR] Please input mp4/Rtsp URL") exit() elif lenofUrl >= 3: print("[ERROR] param input Error") exit() URL = sys.argv[1] URL1 = re.match('rtsp://', URL) URL2 = re.search('.mp4', URL) if URL1 is None and URL2 is None: print("[ERROR] should input correct URL") exit() cap = cv.VideoCapture(URL) #Gets the total frames frames_num = cap.get(7) currentFrames = 0 while True: #read image ret, frame = cap.read() if ret is not True: print("read None image, break") break if currentFrames == frames_num - 1: currentFrames = 0 cap.set(1, 0) currentFrames += 1 #Gets the L channel value orig_shape, orig_l, l_data = preprocess(frame) result_list = model.execute([l_data,]) result_jpeg = postprocess(result_list, orig_shape, orig_l) chan.send_image(orig_shape[0], orig_shape[1], result_jpeg)
def main(): """ main """ if (len(sys.argv) != 2): print("Please input video path") exit(1) #acl init acl_resource = AclResource() acl_resource.init() #load model model = Model(MODEL_PATH) #open video video_path = sys.argv[1] print("open video ", video_path) cap = cv2.VideoCapture(video_path) fps = cap.get(cv2.CAP_PROP_FPS) Width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) Height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) lf.set_img_size((Width, Height)) #create output directory if not os.path.exists(OUTPUT_DIR): os.mkdir(OUTPUT_DIR) output_Video = os.path.basename(video_path) output_Video = os.path.join(OUTPUT_DIR, output_Video) fourcc = cv2.VideoWriter_fourcc( *'mp4v') # DIVX, XVID, MJPG, X264, WMV1, WMV2 outVideo = cv2.VideoWriter(output_Video, fourcc, fps, (Width, Height)) # Read until video is completed while (cap.isOpened()): ret, frame = cap.read() if ret == True: #preprocess orig_shape, rgb_img, framecv = preprocess(frame) #inference result_list = model.execute([ rgb_img, ]) #postprocess frame = postprocess(result_list, framecv, orig_shape) outVideo.write(frame) # Break the loop else: break cap.release() outVideo.release() print("Execute end")
class ModelProcessor: def __init__(self, acl_resource, params): self._acl_resource = acl_resource self.params = params self._model_width = params['width'] self._model_height = params['height'] assert 'model_dir' in params and params[ 'model_dir'] is not None, 'Review your param: model_dir' assert os.path.exists( params['model_dir']), "Model directory doesn't exist {}".format( params['model_dir']) # load model from path, and get model ready for inference self.model = Model(acl_resource, params['model_dir']) def predict(self, img_original): #preprocess image to get 'model_input' model_input = self.preprocess(img_original) # execute model inference result = self.model.execute([model_input]) # postprocessing: use the heatmaps (the second output of model) to get the joins and limbs for human body # Note: the model has multiple outputs, here we used a simplified method, which only uses heatmap for body joints # and the heatmap has shape of [1,14], each value correspond to the position of one of the 14 joints. # The value is the index in the 92*92 heatmap (flatten to one dimension) heatmaps = result[1] # calculate the scale of original image over heatmap, Note: image_original.shape[0] is height scale = np.array([ img_original.shape[1] / heatmap_width, img_original.shape[0] / heatmap_height ]) canvas, joint_list = decode_pose(heatmaps[0], scale, img_original) return canvas, joint_list def preprocess(self, img_original): ''' preprocessing: resize image to model required size, and normalize value between [0,1] ''' scaled_img_data = cv2.resize(img_original, (self._model_width, self._model_height)) preprocessed_img = np.asarray(scaled_img_data, dtype=np.float32) / 255. return preprocessed_img
def main(): """ acl resource initialization """ acl_resource = AclResource() acl_resource.init() model = Model(model_path) with codecs.open(dict_path, 'r', 'utf-8') as reader: for line in reader: token = line.strip() token_dict[token] = len(token_dict) with open(sample_path, "r") as f: text = f.read() with open(label_path, "r", encoding="utf-8") as f: label_dict = json.loads(f.read()) X1, X2 = preprocess(text) X1 = np.ascontiguousarray(X1, dtype='float32') X2 = np.ascontiguousarray(X2, dtype='float32') X1 = np.expand_dims(X1, 0) X2 = np.expand_dims(X2, 0) s_time = time.time() result_list = model.execute([X1, X2]) e_time = time.time() print(result_list) y = postprocess(result_list) if not os.path.exists(output_dir): os.mkdir(output_dir) save_to_file(output_dir + 'prediction_label.txt', label_dict[str(y)]) print("Original text: %s" % text) print("Prediction label: %s" % label_dict[str(y)]) print("Cost time:", e_time - s_time) print("Execute end")
class Cartoonization(object): def __init__(self, model_path, model_width, model_height): self.device_id = 0 self.context = None self.stream = None self._model_path = model_path self._model_width = model_width self._model_height = model_height self._dvpp = None def __del__(self): if self._model: del self._model if self._dvpp: del self._dvpp if self.stream: acl.rt.destroy_stream(self.stream) if self.context: acl.rt.destroy_context(self.context) acl.rt.reset_device(self.device_id) acl.finalize() print("[Sample] class Samle release source success") def _init_resource(self): print("[Sample] init resource stage:") ret = acl.init() check_ret("acl.rt.set_device", ret) ret = acl.rt.set_device(self.device_id) check_ret("acl.rt.set_device", ret) self.context, ret = acl.rt.create_context(self.device_id) check_ret("acl.rt.create_context", ret) self.stream, ret = acl.rt.create_stream() check_ret("acl.rt.create_stream", ret) self.run_mode, ret = acl.rt.get_run_mode() check_ret("acl.rt.get_run_mode", ret) print("[Sample] Init resource stage success") def init(self): # init acl resource self._init_resource() self._dvpp = Dvpp(self.stream, self.run_mode) # init dvpp ret = self._dvpp.init_resource() if ret != SUCCESS: print("Init dvpp failed") return FAILED # load model self._model = Model(self.run_mode, self._model_path) ret = self._model.init_resource() if ret != SUCCESS: print("Init model failed") return FAILED return SUCCESS def pre_process(self, image): yuv_image = self._dvpp.jpegd(image) crop_and_paste_image = \ self._dvpp.crop_and_paste(yuv_image, image.width, image.height, self._model_width, self._model_height) print("[Sample] crop_and_paste yuv end") return crop_and_paste_image def inference(self, resized_image): return self._model.execute(resized_image.data(), resized_image.size) def post_process(self, infer_output, image_file, origin_image): print("[Sample] post process") data = ((np.squeeze(infer_output[0]) + 1) * 127.5) img = cv2.cvtColor(data, cv2.COLOR_RGB2BGR) img = cv2.resize(img, (origin_image.width, origin_image.height)) output_path = os.path.join("../outputs", os.path.basename(image_file)) cv2.imwrite(output_path, img)
return image_new if __name__ == '__main__': # model name, input image file, and confidence score for bboxes model_name = 'vgg_ssd' img_file = 'test_img/ship.jpg' conf_thres = 0.85 # initialize graph and resource acl_resource = AclResource() acl_resource.init() # create model MODEL_PATH = model_name + ".om" print("MODEL_PATH:", MODEL_PATH) model = Model(acl_resource, MODEL_PATH) # load image file image = cv2.imread(img_file) #image preprocessing input_image = PreProcessing(image) # model inference resultList = model.execute([input_image]) # postprocessing: select bboxes with scores higher than confidence score(adjustable), plot bboxes/labels on the image and save to output dir PostProcessing(resultList, conf_thres)
def main(): acl_resource = AclResource() acl_resource.init() #Create an object to perform detection preprocessing, postprocessing and so on detect = VggSsd(acl_resource, MODEL_WIDTH, MODEL_HEIGHT) #Load the pre-trained model from path model = Model(MODEL_PATH) #Connect to the Presenter Server according to the config file chan = presenteragent.presenter_channel.open_channel(MASK_DETEC_CONF) if chan == None: print("Open presenter channel failed") return #Start the Pi camera on the Atlas 200DK, input argument for the camera port(0 or 1) cap = Camera(0) while True: #Read an image frame from the camera image = cap.read() if image is None: print("read None image, break") break #Image preprocessing model_input = detect.pre_process(image) if model_input == None: print("Pre process image failed") break #Send the data to the model for detection result = model.execute(model_input) if result is None: print("execute mode failed") break #Post-process the detection result jpeg_image, detection_list = detect.post_process(result, image) for i in range(len(detection_list)): #Print the detection result, the detection box coordinates, #detected label, confidence print(detection_list[i].box.lt.x, detection_list[i].box.lt.y, detection_list[i].box.rb.x, detection_list[i].box.rb.y, detection_list[i].result_text, detection_list[i].confidence) ##################################################################################################################################################################### d = math.sqrt( pow(detection_list[i].box.lt.x - detection_list[i].box.rb.x, 2) + pow(detection_list[i].box.lt.y - detection_list[i].box.rb.y, 2)) if (d > 80): msg = detection_list[i].result_text bytesToSend = str.encode(msg) serverAddressPort = ("192.168.1.22", 20001) bufferSize = 1024 UDPClientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) UDPClientSocket.sendto(bytesToSend, serverAddressPort) for j in range(i + 1, len(detection_list)): x_cor_1 = detection_list[i].box.lt.x + abs( (detection_list[i].box.lt.x - detection_list[i].box.rb.x)) / 2 y_cor_1 = detection_list[i].box.lt.y + abs( (detection_list[i].box.lt.y - detection_list[i].box.rb.y)) / 2 x_cor_2 = detection_list[j].box.lt.x + abs( (detection_list[j].box.lt.x - detection_list[j].box.rb.x)) / 2 y_cor_2 = detection_list[j].box.lt.y + abs( (detection_list[j].box.lt.y - detection_list[j].box.rb.y)) / 2 width_1 = abs(detection_list[i].box.lt.x - detection_list[i].box.rb.x) width_2 = abs(detection_list[j].box.lt.x - detection_list[j].box.rb.x) width_thrs = abs(min(width_1, width_2) * 3) diss = math.sqrt( pow(x_cor_1 - x_cor_2, 2) + pow(y_cor_1 - y_cor_2, 2)) #print("Too close!!!!!!", j , "distance:",diss,"threshhold:",width_thrs) if width_1 > 50 and width_2 > 50: if diss < width_thrs: msg = "too_close" bytesToSend = str.encode(msg) serverAddressPort = ("192.168.1.22", 20001) bufferSize = 1024 UDPClientSocket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM) UDPClientSocket.sendto(bytesToSend, serverAddressPort) ######################################################################################################################################################## if jpeg_image == None: print("The jpeg image for present is None") break #Send the result to the Present Server to display onto the browser chan.send_detection_data(CAMERA_FRAME_WIDTH, CAMERA_FRAME_HEIGHT, jpeg_image, detection_list)
def execute(model_path): ## Initialization ## #initialize acl runtime acl_resource = AclResource() acl_resource.init() # load offline model for face detection model_face = Model(acl_resource, MODEL_PATH_FACE) model_head_pose = Model(acl_resource, MODEL_PATH_HEAD_POSE) ## Prepare Model ## # parameters for model path and model inputs model_parameters = { 'model_dir': model_path, 'width': 368, # model input width 'height': 368, # model input height } # perpare model instance: init (loading model from file to memory) # model_processor: preprocessing + model inference + postprocessing model_processor = ModelProcessor(acl_resource, model_parameters) last_five_frame_result = [] # Initialize Camera cap = Camera(id = 0, fps = 10) # Read reference images img_left1 = cv2.imread(LEFT1_PATH) img_left2 = cv2.imread(LEFT2_PATH) img_right1 = cv2.imread(RIGHT1_PATH) img_right2 = cv2.imread(RIGHT2_PATH) img_stop = cv2.imread(STOP_PATH) # Get reference output canvas_left1,joint_list_left1 = model_processor.predict(img_left1) canvas_left2,joint_list_left2 = model_processor.predict(img_left2) canvas_right1,joint_list_right1 = model_processor.predict(img_right1) canvas_right2,joint_list_right2 = model_processor.predict(img_right2) canvas_stop,joint_list_stop = model_processor.predict(img_stop) # Get angles from reference images angle_left1=getangle(joint_list_left1) angle_left2=getangle(joint_list_left2) angle_right1=getangle(joint_list_right1) angle_right2=getangle(joint_list_right2) angle_stop=getangle(joint_list_stop) # Initialize count countleft=0 countright=0 countstop=0 ## Presenter Server Output ## chan = presenteragent.presenter_channel.open_channel(BODYPOSE_CONF) if chan == None: print("Open presenter channel failed") return predict = StateMachine() while True: ## Read one frame of the input video ## img_original = cap.read() if not img_original: print('Error: Camera read failed') break ## HEAD POSE BEGIN ## # Camera Input (YUV) to RGB Image image_byte = img_original.tobytes() image_array = np.frombuffer(image_byte, dtype=np.uint8) img_original = YUVtoRGB(image_array) img_original = cv2.flip(img_original, -1) # Make copy of image for head model processing and body model processing img_bodypose = copy.deepcopy(img_original) img_headpose = copy.deepcopy(img_original) ## Model Prediction ## # model_processor.predict: processing + model inference + postprocessing # canvas: the picture overlayed with human body joints and limbs # img_bodypose is modified with skeleton canvas, joint_list_input = model_processor.predict(img_bodypose) angle_input=getangle(joint_list_input) dif5=abs(angle_input-angle_left1) dif6=abs(angle_input-angle_left2) dif7=abs(angle_input-angle_right1) dif8=abs(angle_input-angle_right2) dif9=abs(angle_input-angle_stop) result = "invalid" # last_five_result = "invalid" if all( i < 25 for i in dif5): result = "left1" elif all( i < 25 for i in dif6): result = "left2" elif all( i < 25 for i in dif7): result = "right1" elif all( i < 25 for i in dif8): result = "right2" elif all( i < 25 for i in dif9): result = "stop" font = cv2.FONT_HERSHEY_SIMPLEX bottomLeftCornerOfText = (10, 100) fontScale = 1 fontColor = (255,255,255) lineType = 2 cv2.putText(img_bodypose, result, bottomLeftCornerOfText, font, fontScale, fontColor, lineType) ## FACE DETECTION MODEL BEGIN ## input_image = PreProcessing_face(img_headpose) face_flag = False try: resultList_face = model_face.execute([input_image]).copy() # draw bounding box on img_bodypose xmin, ymin, xmax, ymax = PostProcessing_face(img_bodypose, resultList_face) bbox_list = [xmin, ymin, xmax, ymax] face_flag = True except: print('No face detected') # FACE DETECTION MODEL END ## ## HEADPOSE BEGIN ## head_status_string = "No output" if face_flag is True: input_image = PreProcessing_head(img_headpose, bbox_list) try: resultList_head = model_head_pose.execute([input_image]).copy() except Exception as e: print('No head pose estimation output') # draw headpose points on image facepointList, head_status_string, canvas = PostProcessing_head(resultList_head, bbox_list, img_bodypose) print('Headpose:', head_status_string) headpose_result = head_status_string ## HEADPOSE END ## predict.staterunner(result,headpose_result) ## Present Result ## # convert to jpeg image for presenter server display _,jpeg_image = cv2.imencode('.jpg', img_bodypose) # construct AclImage object for presenter server jpeg_image = AclImage(jpeg_image, img_original.shape[0], img_original.shape[1], jpeg_image.size) # send to presenter server chan.send_detection_data(img_original.shape[0], img_original.shape[1], jpeg_image, []) cap.release()
def main(): if not os.path.exists(OUTPUT_DIR): os.mkdir(OUTPUT_DIR) if not os.path.exists(OUTPUT_TXT_DIR): os.mkdir(OUTPUT_TXT_DIR) #acl资源初始化 acl_resource = AclResource() acl_resource.init() #加载模型 model = Model(acl_resource, MODEL_PATH) src_dir = os.listdir(INPUT_DIR) print("src_dir = ", src_dir) #从data目录逐张读取图片进行推理 for pic in src_dir: #读取图片 pic_path = os.path.join(INPUT_DIR, pic) pic_name = pic.split('.')[0] print(pic_name) bgr_img = cv2.imread(pic_path) t1 = time.time() img, ratio = letterbox(bgr_img, new_shape=(320, 640)) # resize to (320,640,3) img = img[:, :, ::-1] #bgr to rgb img = img.transpose(2, 0, 1) #(3,320,640) img = np.ascontiguousarray(img) img = img.astype(np.float32) img = img / 255.0 data = np.concatenate((img[:, ::2, ::2], img[:, 1::2, ::2], img[:, ::2, 1::2], img[:, 1::2, 1::2]), axis=0) #[12,160,320] t2 = time.time() result_list = model.execute([ data, ]) t3 = time.time() post = yolov5_post(result_list) #[1,25200,12] result_return = non_max_suppression(post, conf_thres=conf_threshold, iou_thres=iou_threshold) if len(result_return['detection_classes']): det = np.array(result_return['detection_boxes'])[:, :4] bbox = scale_coords((320, 640), det, bgr_img.shape, ratio) t4 = time.time() print("result = ", result_return) print("preprocess cost:", t2 - t1) print("forward cost:", t3 - t2) print("postprocess cost:", t4 - t3) print("total cost:", t4 - t1) print("FPS:", 1 / (t4 - t1)) for i in range(len(result_return['detection_classes'])): box = bbox[i] class_name = result_return['detection_classes'][i] confidence = result_return['detection_scores'][i] cv2.rectangle(bgr_img, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), colors[i % 6]) p3 = (max(int(box[0]), 15), max(int(box[1]), 15)) out_label = class_name cv2.putText(bgr_img, out_label, p3, cv2.FONT_ITALIC, 0.6, colors[i % 6], 1) output_file = os.path.join(OUTPUT_DIR, "out_" + pic) print("output:%s" % output_file) cv2.imwrite(output_file, bgr_img) pic_name = pic.split('.')[0] predict_result_path = os.path.join(OUTPUT_TXT_DIR, str(pic_name) + '.txt') with open(predict_result_path, 'w') as f: for i in range(len(result_return['detection_classes'])): box = bbox[i] class_name = result_return['detection_classes'][i] confidence = result_return['detection_scores'][i] box = list(map(int, box)) box = list(map(str, box)) confidence = '%.4f' % confidence bbox_mess = ' '.join([ class_name, confidence, box[0], box[1], box[2], box[3] ]) + '\n' f.write(bbox_mess) print("Execute end")
class Classify(object): def __init__(self, acl_resource, model_path, model_width, model_height): self.total_buffer = None self._model_path = model_path self._model_width = model_width self._model_height = model_height self._model = Model(model_path) self._dvpp = Dvpp(acl_resource) print("The App arg is __init__") def __del__(self): if self.total_buffer: acl.rt.free(self.total_buffer) if self._dvpp: del self._dvpp print("[Sample] class Samle release source success") def pre_process(self, image): yuv_image = self._dvpp.jpegd(image) print("decode jpeg end") resized_image = self._dvpp.resize(yuv_image, self._model_width, self._model_height) print("resize yuv end") return resized_image def batch_process(self, resized_image_list, batch): resized_img_data_list = [] resized_img_size = resized_image_list[0].size total_size = batch * resized_img_size stride = 0 for resized_image in resized_image_list: resized_img_data_list.append(resized_image.data()) self.total_buffer, ret = acl.rt.malloc(total_size, ACL_MEM_MALLOC_HUGE_FIRST) check_ret("acl.rt.malloc", ret) for i in range(len(resized_image_list)): ret = acl.rt.memcpy(self.total_buffer + stride, resized_img_size,\ resized_img_data_list[i], resized_img_size,\ ACL_MEMCPY_DEVICE_TO_DEVICE) check_ret("acl.rt.memcpy", ret) stride += resized_img_size return total_size def inference(self, resized_image_list, batch): total_size = self.batch_process(resized_image_list, batch) batch_buffer = {'data': self.total_buffer, 'size':total_size} return self._model.execute([batch_buffer, ]) def post_process(self, infer_output, batch_image_files, number_of_images): print("post process") datas = infer_output[0] for number in range(number_of_images): data = datas[number] vals = data.flatten() top_k = vals.argsort()[-1:-6:-1] print("images:{}".format(batch_image_files[number])) print("======== top5 inference results: =============") for n in top_k: object_class = get_image_net_class(n) print("label:%d confidence: %f, class: %s" % (n, vals[n], object_class)) #Use Pillow to write the categories with the highest confidence on the image and save them locally if len(top_k): object_class = get_image_net_class(top_k[0]) output_path = os.path.join("../outputs", os.path.basename(batch_image_files[number])) origin_img = Image.open(batch_image_files[number]) draw = ImageDraw.Draw(origin_img) font = ImageFont.truetype("SourceHanSansCN-Normal.ttf", size=30) draw.text((10, 50), object_class, font=font, fill=255) origin_img.save(output_path)
print('Cosine similarity between faces: ', cos_sim) if __name__ == '__main__': model_name = 'sphereface' img_file1 = cv2.imread('face1.jpeg') img_file2 = cv2.imread('test_human.jpg') # initialize acl resource acl_resource = AclResource() acl_resource.init() # load offline model MODEL_PATH = model_name + ".om" print("MODEL_PATH:", MODEL_PATH) model = Model(acl_resource, MODEL_PATH) # load image file input_image1 = PreProcessing(img_file1) print(input_image1.shape) input_image2 = PreProcessing(img_file2) print(input_image2.shape) # om model inference resultList1 = model.execute([input_image1])[0].copy() resultList2 = model.execute([input_image2])[0].copy() #postprocessing to compare results PostProcessing(resultList1, resultList2)
def main(): if not os.path.exists(OUTPUT_DIR): os.mkdir(OUTPUT_DIR) if not os.path.exists(OUTPUT_TXT_DIR): os.mkdir(OUTPUT_TXT_DIR) #acl资源初始化 acl_resource = AclResource() acl_resource.init() #加载模型 model = Model(acl_resource, MODEL_PATH) src_dir = os.listdir(INPUT_DIR) print("src_dir = ", src_dir) #从data目录逐张读取图片进行推理 t_pre = 0 t_for = 0 t_post = 0 for pic in src_dir: #读取图片 pic_path = os.path.join(INPUT_DIR, pic) bgr_img = cv.imread(pic_path) #预处理 t1 = time.time() #data, w, h= preprocess(pic_path) data, w, h = preprocess_cv2(bgr_img) t2 = time.time() t_pre += (t2-t1) #送进模型推理 result_list = model.execute([data,]) t3 = time.time() t_for += (t3-t2) #处理推理结果 result_return = post_process(result_list, w, h) t4 = time.time() t_post += (t4-t3) print("result = ", result_return) print("preprocess cost:", t2-t1) print("forward cost:", t3-t2) print("proprocess cost:", t4-t3) for i in range(len(result_return['detection_classes'])): box = result_return['detection_boxes'][i] class_name = result_return['detection_classes'][i] confidence = result_return['detection_scores'][i] cv.rectangle(bgr_img, (int(box[1]), int(box[0])), (int(box[3]), int(box[2])), colors[i%6], 2) p3 = (max(int(box[1]), 15), max(int(box[0]), 15)) out_label = class_name cv.putText(bgr_img, out_label, p3, cv.FONT_ITALIC, 0.6, colors[i%6], 1) output_file = os.path.join(OUTPUT_DIR, "out_" + pic) print("output:%s" % output_file) cv.imwrite(output_file, bgr_img) pic_name = pic.split('.')[0] predict_result_path = os.path.join(OUTPUT_TXT_DIR, str(pic_name)+'.txt') with open(predict_result_path, 'w') as f: for i in range(len(result_return['detection_classes'])): box = result_return['detection_boxes'][i] class_name = result_return['detection_classes'][i] confidence = result_return['detection_scores'][i] box = list(map(int, box)) box = list(map(str, box)) confidence = '%.4f' % confidence bbox_mess = ' '.join([class_name, confidence, box[1], box[0], box[3], box[2]]) + '\n' f.write(bbox_mess) num = len(src_dir) print("avg preprocess cost:", t_pre/num) print("avg forward cost:", t_for/num) print("avg proprocess cost:", t_post/num) total = t_pre/num + t_for/num + t_post/num print("avg total cost:", total) print("avg FPS:", 1/(total)) print("Execute end")
acl_resource = AclResource() acl_resource.init() # model path MODEL_PATH = model_name + ".om" print("MODEL_PATH:", MODEL_PATH) # create mask detection model model = Model(acl_resource, MODEL_PATH) # original bgr image for plotting ori_img = cv2.imread(img_file) # image preprocessing: "data" is the preprocesses image for inference, "orig" is the original image data, orig = PreProcessing(img_file) # om model inference resultList = model.execute([data]) # image postprocessing: decoding model output to bboxes information, using nms to select bboxes. return detected bboxes info: axis, confidence score and category result_return = post_process(resultList, orig) print("result = ", result_return) # plot bbox/label on the image and save for i in range(len(result_return['detection_classes'])): box = result_return['detection_boxes'][i] class_name = result_return['detection_classes'][i] confidence = result_return['detection_scores'][i] cv2.rectangle(ori_img, (int(box[1]), int(box[0])), (int(box[3]), int(box[2])), colors[i % 6]) p3 = (max(int(box[1]), 15), max(int(box[0]), 15)) out_label = class_name
MODEL_PATH = model_name_face_det + ".om" model_face = Model(acl_resource, MODEL_PATH) #load offline model for head pose estimation MODEL_PATH = model_name_head_pose + ".om" model_head_pose = Model(acl_resource, MODEL_PATH) # load image file image = cv2.imread(img_file) # preprocessing face detection input_image = PreProcessing_face(image) #face model infer and post processing try: resultList_face = model_face.execute([input_image]).copy() xmin, ymin, xmax, ymax = PostProcessing_face(image, resultList_face) bbox_list = [xmin, ymin, xmax, ymax] except: print('Error in face detection output') # # Preprocessing head pose estimation input_image = PreProcessing_head(image, bbox_list) # head pose estimation model inference try: resultList_head = model_head_pose.execute([input_image]).copy() except Exception as e: print('Error in output') #post processing to obtain coordinates for lines drawing
class Classify(object): 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 def __del__(self): if self._model: del self._model if self._dvpp: del self._dvpp if self.stream: acl.rt.destroy_stream(self.stream) if self.context: acl.rt.destroy_context(self.context) acl.rt.reset_device(self.device_id) acl.finalize() print("[Sample] Sample release source success") def _init_resource(self): print("[Sample] init resource stage:") ret = acl.init() check_ret("acl.rt.set_device", ret) ret = acl.rt.set_device(self.device_id) check_ret("acl.rt.set_device", ret) self.context, ret = acl.rt.create_context(self.device_id) check_ret("acl.rt.create_context", ret) self.stream, ret = acl.rt.create_stream() check_ret("acl.rt.create_stream", ret) self.run_mode, ret = acl.rt.get_run_mode() check_ret("acl.rt.get_run_mode", ret) print("Init resource stage success") def init(self): self._init_resource() self._dvpp = Dvpp(self.stream, self.run_mode) ret = self._dvpp.init_resource() if ret != SUCCESS: print("Init dvpp failed") return FAILED self._model = Model(self.run_mode, self._model_path) ret = self._model.init_resource() if ret != SUCCESS: print("Init model failed") return FAILED return SUCCESS def pre_process(self, image): yuv_image = self._dvpp.jpegd(image) print("decode jpeg end") 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): return self._model.execute(resized_image.data(), resized_image.size) def post_process(self, infer_output, image_file): print("post process") data = infer_output[0] vals = data.flatten() top_k = vals.argsort()[-1:-6:-1] object_class = get_image_net_class(top_k[0]) output_path = os.path.join(os.path.join(SRC_PATH, "../outputs/"), 'out_' + os.path.basename(image_file)) origin_img = Image.open(image_file) draw = ImageDraw.Draw(origin_img) font = ImageFont.load_default() font.size = 50 draw.text((10, 50), object_class, font=font, fill=255) origin_img.save(output_path) object_class = get_image_net_class(top_k[0]) return object_class
class Classify(object): def __init__(self, model_path, model_width, model_height): self.device_id = 0 self.context = None self.stream = None self._model_path = model_path self._model_width = model_width self._model_height = model_height self._dvpp = None def __del__(self): if self._model: del self._model if self._dvpp: del self._dvpp if self.stream: acl.rt.destroy_stream(self.stream) if self.context: acl.rt.destroy_context(self.context) acl.rt.reset_device(self.device_id) acl.finalize() print("[Sample] class Samle release source success") def destroy(self): self.__del__ def _init_resource(self): print("[Sample] init resource stage:") #ret = acl.init() #check_ret("acl.rt.set_device", ret) ret = acl.rt.set_device(self.device_id) check_ret("acl.rt.set_device", ret) self.context, ret = acl.rt.create_context(self.device_id) check_ret("acl.rt.create_context", ret) self.stream, ret = acl.rt.create_stream() check_ret("acl.rt.create_stream", ret) self.run_mode, ret = acl.rt.get_run_mode() check_ret("acl.rt.get_run_mode", ret) print("Init resource stage success") def init(self): self._init_resource() self._dvpp = Dvpp(self.stream, self.run_mode) ret = self._dvpp.init_resource() if ret != SUCCESS: print("Init dvpp failed") return FAILED self._model = Model(self.run_mode, self._model_path) ret = self._model.init_resource() if ret != SUCCESS: print("Init model failed") return FAILED return SUCCESS def pre_process(self, image): yuv_image = self._dvpp.jpegd(image) print("decode jpeg end") 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): return self._model.execute(resized_image.data(), resized_image.size) def post_process(self, infer_output, image_file): 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 = get_image_net_class(n) print("label:%d confidence: %f, class: %s" % (n, vals[n], object_class)) object_class = get_image_net_class(top_k[0]) return object_class