def random_prediction(): r = random.randint(0, TRAIN_PATH.shape[0] - 1) img = np.asarray( Image.open( LIP_DATASET_SRC / TRAIN_PATH[r, 0] ) ) print("Original:") show_img(img) rescaled_img = preprocess_image(img) test_predict = model.predict(tf.expand_dims(rescaled_img, axis=0)) plot_parsing_map(test_predict, label2int=ATRDataset.LABEL2INT) pred_mask = create_mask(test_predict)[0] show_img(pred_mask)
import numpy as np import matplotlib.pyplot as plt import os os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" # 在推断模式中,将使用running_mean和running_variance作为bn层的mean和variance tf.keras.backend.set_learning_phase(False) # 加载模型 model = tf.saved_model.load('saved_model/overfit_model-1000e') # 80个类别的名称 class_name = read_class_name('./model_data/coco_classes_chinese.txt') # 图片名称 file = 'car2.png' # 图片放缩成608,608,像素值归一化 image, image_data, image_shape = preprocess_image(img_path='train-set/origin/' + file) # 进行目标检测算法 res_class, res_score, res_boxes = detect(image_data, model) if len(res_boxes) > 0: # image_shape 为原图大小高宽格式, # 还原成相对于原图的位置 res_boxes = res_boxes * np.tile(list(image_shape), 2) # 把框画在图片上 draw_boxes(image, res_score, res_boxes, res_class, class_name, generate_colors(class_name)) plt.imshow(image) plt.show() image.save('train-set/detected/'+file, quality=100) else: print('未检测到任何目标')
import numpy as np import os os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" model = DarkNet() # 编译网络,生成weights参数结构 model(tf.keras.layers.Input(shape=(608, 608, 3))) # 解析权重参数 yolo_weights = parse_yolo_v2_model_weights(model.weight_shapes, 'yolov2_weights/yolov2.weights') # 将权重参数导入模型 model.set_weights(yolo_weights) # 80个类别的名称 class_name = read_class_name('./model_data/coco_classes_chinese.txt') # 图片名称 f = 'car1.png' # 图片放缩成608,608,像素值归一化 image, image_data, image_shape = preprocess_image(img_path='images/'+f) # 进行目标检测算法 res_class, res_score, res_boxes = detect(image_data, model) # image_shape 为原图大小高宽格式, # 还原成相对于原图的位置 res_boxes = res_boxes * np.tile(list(image_shape), 2) # 把框画在图片上 draw_boxes(image, res_score, res_boxes, res_class, class_name, generate_colors(class_name)) # plt.imshow(image) # plt.show() # 保存图片 image.save('images/out/' + f, quality=100)
model(tf.keras.layers.Input(shape=(608, 608, 3))) print(model.summary()) # 解析权重参数 yolo_weights = parse_yolo_v2_model_weights(model.weight_shapes, 'yolov2_weights/yolov2.weights') # 将权重参数导入模型 model.set_weights(yolo_weights) # 80个类别的名称 class_name = read_class_name('./model_data/coco_classes_chinese.txt') root_path = 'images' # 检测文件夹下的所有图片,深度为1 for f in os.listdir(root_path): nf = os.path.join(root_path, f) if os.path.isfile(nf): # 图片放缩成608,608,像素值归一化 image, image_data, image_shape = preprocess_image(img_path=nf) # 进行目标检测算法 res_class, res_score, res_boxes = detect(image_data, model) # image_shape 为原图大小高宽格式, # 还原成相对于原图的位置 res_boxes = res_boxes * np.tile(list(image_shape), 2) # 把框画在图片上 draw_boxes(image, res_score, res_boxes, res_class, class_name, generate_colors(class_name)) # plt.imshow(image) # plt.show() # 保存图片 image.save('images/out/' + f, quality=100)
def predict(filename): channel = grpc.insecure_channel("localhost:8500") stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) request = predict_pb2.PredictRequest() # model_name request.model_spec.name = "cropper_model" # signature name, default is 'serving_default' request.model_spec.signature_name = "serving_default" start = time.time() """ ========================================= ===== Crop and align id card image ========================================= """ filepath = app.config["IMAGE_UPLOADS"] + "/" + filename # preprocess image img, original_image, original_width, original_height = preprocess_image( filepath, Cropper.TARGET_SIZE) if img.ndim == 3: img = np.expand_dims(img, axis=0) # request to cropper model request.inputs["input_1"].CopyFrom( tf.make_tensor_proto(img, dtype=np.float32, shape=img.shape)) try: result = stub.Predict(request, 10.0) result = result.outputs["tf_op_layer_concat_14"].float_val result = np.array(result).reshape((-1, 9)) except Exception as e: print(e) cropper = Cropper() cropper.set_best_bboxes(result, original_width=original_width, original_height=original_height, iou_threshold=0.5) # respone to client if image is invalid if not cropper.respone_client(threshold_idcard=0.8): return render_template('upload_image_again.html') cropper.set_image(original_image=original_image) # output of cropper part aligned_image = getattr(cropper, "image_output") cv2.imwrite('app/static/aligned_images/' + filename, aligned_image) aligned_image = cv2.cvtColor(aligned_image, cv2.COLOR_BGR2RGB) """ =========================================== ==== Detect informations in aligned image =========================================== """ # preprocess aligned image original_height, original_width, _ = aligned_image.shape img = cv2.resize(aligned_image, Detector.TARGET_SIZE) img = np.float32(img / 255.) # model_name request.model_spec.name = "detector_model" # signature name, default is 'serving_default' request.model_spec.signature_name = "serving_default" if img.ndim == 3: img = np.expand_dims(img, axis=0) # new request to detector model request.inputs["input_1"].CopyFrom( tf.make_tensor_proto(img, dtype=np.float32, shape=img.shape)) try: result = stub.Predict(request, 10.0) result = result.outputs["tf_op_layer_concat_14"].float_val result = np.array(result).reshape((-1, 13)) except Exception as e: print(e) detector = Detector() detector.set_best_bboxes(result, original_width=original_width, original_height=original_height, iou_threshold=0.5) detector.set_info_images(original_image=aligned_image) # output of detector part info_images = getattr(detector, "info_images") """ ===================================== ==== Reader infors from infors image ===================================== """ keys = list(info_images.keys()) keys.remove("thoi_han") keys.remove("chan_dung") infors = dict() # init default value of quoc_tich, dan_toc infors['quoc_tich'] = "" infors['dan_toc'] = "" if "quoc_tich" in keys: infors['quoc_tich'] = ["Việt Nam"] keys.remove("quoc_tich") if "sex" in keys: info_image = info_images["sex"] infors["sex"] = list() for i in range(len(info_image)): img = info_image[i]['image'] s = reader.predict(img) if "Na" in s: infors["sex"].append("Nam") else: infors["sex"].append("Nữ") keys.remove("sex") if "dan_toc" in keys: info_image = info_images["dan_toc"] infors["dan_toc"] = list() for i in range(len(info_image)): img = info_image[i]['image'] s = reader.predict(img) s = s.split(" ")[-1] infors["dan_toc"].append(s) keys.remove("dan_toc") for key in keys: infors[key] = list() info_image = info_images[key] for i in range(len(info_image)): img = info_image[i]['image'] s = reader.predict(img) infors[key].append(s) que_quan_0 = infors['que_quan'][0] que_quan_1 = '' noi_thuong_tru_0 = infors['noi_thuong_tru'][0] noi_thuong_tru_1 = '' if len(infors['que_quan']) == 2: que_quan_1 = infors['que_quan'][1] if len(infors['noi_thuong_tru']) == 2: noi_thuong_tru_1 = infors['noi_thuong_tru'][1] print("total_time:{}".format(time.time() - start)) return render_template('predict.html', id=infors['id'][0].replace(" ", ""), full_name=infors['full_name'][0], date_of_birth=infors['date_of_birth'][0], sex=infors['sex'][0], quoc_tich=infors['quoc_tich'], dan_toc=infors['dan_toc'], que_quan_0=que_quan_0, que_quan_1=que_quan_1, noi_thuong_tru_0=noi_thuong_tru_0, noi_thuong_tru_1=noi_thuong_tru_1, filename=str(filename))