Beispiel #1
0

def become_dark(path):
    to_black_img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    for x in range(to_black_img.shape[0]):
        for y in range(to_black_img.shape[1]):
            to_black_img[x, y] = 0
    cv2.imwrite(path, to_black_img)


print("Loading trained model...")
model = keras.models.load_model("/home/eyue/graduation_desigh_QinJiang/my_model/"
                                "Concrete_Crack_Classification_model.model")
print("Trained model loaded!")

preprocessd_save_path = img_preprocess.preprocess(preprocess_path, IMAGE_RESIZE)

image_names = [name for name in os.listdir(preprocessd_save_path) for item in IMAGES_FORMAT if
                   os.path.splitext(name)[1] == item]
# print(os.path.splitext(image_names[0])[0])

for m in range(0, len(image_names)):
    every_save_path = os.path.join(preprocessd_save_path, "%s" % os.path.splitext(image_names[m])[0])  # 存储每张图片分割后、重组后、以及评价的目录
    os.mkdir(every_save_path)
    preprocessd_img = os.path.join(preprocessd_save_path, image_names[m])
    print(preprocessd_img)
    save_to_every = cv2.imread(preprocessd_img, cv2.IMREAD_GRAYSCALE)
    cv2.imwrite(os.path.join(every_save_path, image_names[m]), save_to_every)


    cropped_path = crop_and_compose.image_crop(preprocessd_img, every_save_path)
    # Init config
    if args.model_dir == "":
        config = Config(args.model_file, args.params_file)
    else:
        config = Config(args.model_dir)
    config.enable_use_gpu(500, 0)
    config.switch_ir_optim()
    config.enable_memory_optim()
    config.enable_tensorrt_engine(workspace_size=1 << 30, precision_mode=PrecisionType.Float32,max_batch_size=1, min_subgraph_size=5, use_static=False, use_calib_mode=False)
        
    # Create predictor
    predictor = create_predictor(config)

    # Set input
    img = cv2.imread(args.img_path)
    img = preprocess(img)
    input_names = predictor.get_input_names()
    input_tensor = predictor.get_input_handle(input_names[0])
    input_tensor.reshape(img.shape)
    input_tensor.copy_from_cpu(img.copy())

    # Run
    predictor.run()

    # Set output
    output_names = predictor.get_output_names()
    output_tensor = predictor.get_output_handle(output_names[0])
    output_data = output_tensor.copy_to_cpu()
    
    print("Predict class index: ", np.argmax(output_data))
Beispiel #3
0
#!/usr/bin/python3
import os
import argparse
import json
import cv2
from img_preprocess import preprocess

parser = argparse.ArgumentParser()
parser.add_argument("filename",
                    help="converts image to json request",
                    type=str)
args = parser.parse_args()

input_file = args.filename

img = preprocess(cv2.imread(input_file))

request = {"instances": img.tolist()}

output_file = os.path.splitext(input_file)[0] + '.json'
with open(output_file, 'w') as out:
    json.dump(request, out)