def post(self, picture): # load image from request imgdata = base64.b64decode(picture.replace('&', '/')) with open('drawings/drawing.jpg', 'wb') as f: f.write(imgdata) X = load_test_image() # load optimized theta arrays theta1_opt = np.load('theta1_opt_76-95.npy') theta2_opt = np.load('theta2_opt_76-95.npy') # make prediction and get percentages pred, percentages = analize_single(theta1_opt, theta2_opt, X) class_ids, ids_to_class = get_class_ids() prediction = ids_to_class[str(pred[0])] slice = dict() slice.update({'prediction': prediction}) i = 0 for percentage in percentages: slice.update({ids_to_class[str(i)]: percentage}) i += 1 return slice, 201
def test_histogram_add_fixed_color(): """ Test Histogram.add_fixed_color """ width_A, height_A, input_pixels_A = utils.load_test_image('flower') width_B, height_B, input_pixels_B = utils.load_test_image('flower-huechange-1') assert width_A == width_B width, height = width_A, height_A attr = liq.Attr() hist = liq.Histogram(attr) image_A = attr.create_rgba(input_pixels_A, width, height, 0) hist.add_image(attr, image_A) image_B = attr.create_rgba(input_pixels_B, width, height, 0) hist.add_image(attr, image_B) FIXED_COLORS = [ liq.Color(255, 0, 0, 255), # red liq.Color(0, 0, 255, 255), # blue liq.Color(128, 0, 128, 255), # purple liq.Color(255, 255, 0, 255), # yellow ] for fixedColor in FIXED_COLORS: hist.add_fixed_color(fixedColor, 0) result = hist.quantize(attr) result_palette = result.get_palette() # Check that we have a decently-sized palette assert len(result_palette) > 128 # Try remapping both images -- make sure we don't get an exception # or something result.remap_image(image_A) result.remap_image(image_B) # Check that the fixed colors are present in the output palette for fixedColor in FIXED_COLORS: assert fixedColor in result_palette
def image_callback(value, image): # Load the map as a bytearray width, height, input_pixels = utils.load_test_image(value) buffer = bytearray(width * height) for i in range(0, len(input_pixels), 4): buffer[i // 4] = input_pixels[i] # Test both the getter and setter methods image.importance_map = buffer with pytest.raises(AttributeError): image.importance_map
def image_callback(value, image): # Load the background as an Image width, height, input_pixels = utils.load_test_image(value) attr = liq.Attr() background = attr.create_rgba(input_pixels, width, height, 0) backgrounds.append(background) # Test both the getter and setter methods image.background = background with pytest.raises(AttributeError): image.background
def test_histogram_basic(): """ Basic test of Histogram: - __init__() - add_image() - quantize() """ # Testing with three input images width_A, height_A, input_pixels_A = utils.load_test_image('flower') width_B, height_B, input_pixels_B = utils.load_test_image('flower-huechange-1') width_C, height_C, input_pixels_C = utils.load_test_image('flower-huechange-2') assert width_A == width_B == width_C assert height_A == height_B == height_C width, height = width_A, height_A attr = liq.Attr() hist = liq.Histogram(attr) image_A = attr.create_rgba(input_pixels_A, width, height, 0) hist.add_image(attr, image_A) image_B = attr.create_rgba(input_pixels_B, width, height, 0) hist.add_image(attr, image_B) image_C = attr.create_rgba(input_pixels_C, width, height, 0) hist.add_image(attr, image_C) result = hist.quantize(attr) # Check that we have a decently-sized palette assert len(result.get_palette()) > 128 # Try remapping all three images -- make sure we don't get an # exception or something result.remap_image(image_A) result.remap_image(image_B) result.remap_image(image_C)
def test_attr_copy(): """ Test Attr.copy() """ width, height, input_pixels = utils.load_test_image('flower') attr = liq.Attr() attr.max_colors = 88 attr.min_posterization = 3 attr.min_quality = 55 attr2 = attr.copy() assert attr2.max_colors == 88 assert attr2.min_posterization == 3 assert attr2.min_quality == 55
def test_buffer_too_small_error(): """ Trigger LIQ_BUFFER_TOO_SMALL and ensure that liq.BufferTooSmallError is raised """ # Load the background as an Image width, height, input_pixels = utils.load_test_image('test-card') attr = liq.Attr() background = attr.create_rgba(input_pixels, width, height, 0) def image_callback(value, image): # The image is too large, so using it as a background should fail with pytest.raises(liq.BufferTooSmallError): image.background = background utils.try_multiple_values('alpha-gradient', [None], image_callback=image_callback)
parser.add_argument("target_ds") args = parser.parse_args() print("model loaded.") d_img = path.join(args.target_ds, "images") + "/*" d_mask = path.join(args.target_ds, "masks") + "/*" img_paths = sorted(glob.glob(d_img)) mask_paths = sorted(glob.glob(d_mask)) j, wrong_count = 0, 0 y_pred, y_true = [], [] print(img_paths) for img_path, mask_path in zip(img_paths, mask_paths): img = load_test_image(img_path, dsize=(IMG_HEIGHT, IMG_WIDTH)) img_hsv = rgb2hsv(img, normalization=True) mask = pb_predict_mask(sess, img) > CLOUD_THRES #mask = pb_predict_window_mask(sess, img_hsv, window_size = SLIDING_WINDOW_SIZE) > CLOUD_THRES #mask = np.zeros_like(img_hsv[...,0]) mask_pct = np.sum(mask) / (mask.shape[0] * mask.shape[1]) * 100 gt_mask = load_mask(mask_path, dsize=(IMG_HEIGHT, IMG_WIDTH)) / 255.0 > CLOUD_THRES gt_mask_pct = np.sum(gt_mask) / (gt_mask.shape[0] * gt_mask.shape[1]) * 100 y_pred.append(mask_pct) y_true.append(gt_mask_pct) if pct_to_label(mask_pct) != pct_to_label(gt_mask_pct): wrong_count += 1 print("Wrong Prediction. {} has around {}% of sky area. Predicted:{}%". format(img_path, PCT_LVL[pct_to_label(gt_mask_pct)],
def test_histogram_add_colors(): """ Test Histogram.add_colors(), as well as the HistogramEntry class """ # First, quantize flower-huechange-1.jpg on its own width, height, input_pixels = utils.load_test_image('flower-huechange-1') attr = liq.Attr() other_image = attr.create_rgba(input_pixels, width, height, 0) result = other_image.quantize(attr) result_pixels = result.remap_image(other_image) result_palette = result.get_palette() # ~ # Create a list of HistogramEntrys for it entries = [] for i, color in enumerate(result_palette): count = result_pixels.count(i) entry = liq.HistogramEntry(color, count) entries.append(entry) # Test HistogramEntry getters assert entry.color == color assert entry.count == count # Test setters entry.color = result_palette[0] entry.count = 50 assert entry.color == result_palette[0] assert entry.count == 50 # (Set properties back to what they should be) entry.color = color entry.count = count assert entries # ~ # Set up a Histogram attr = liq.Attr() hist = liq.Histogram(attr) # Add flower.jpg as an image width, height, input_pixels = utils.load_test_image('flower') image = attr.create_rgba(input_pixels, width, height, 0) hist.add_image(attr, image) # Add the HistogramEntrys for flower-huechange-1.jpg hist.add_colors(attr, entries, 0) # And quantize result = hist.quantize(attr) # ~ # Check that we have a decently-sized palette assert len(result.get_palette()) > 128 # Try remapping both images -- make sure we don't get an exception # or something result.remap_image(image) result.remap_image(other_image)
# for i in range(len(x_train)): # pre = np.squeeze(model.predict(x_train[i:i+1]), axis=(0,3)) # train_patch_pre_label = np.array(list(get_patch_label_from_array(pre,th=0.21))) # ground_truth_label = np.array(list(get_patch_label_from_array(y_train[i,:,:,0],th=0.21))) # train_acc.append(np.mean(np.equal(train_patch_pre_label[:,2],ground_truth_label[:,2]))) # # # valid acc # for i in range(len(x_valid)): # pre = np.squeeze(model.predict(x_valid[i:i+1]), axis=(0,3)) # train_patch_pre_label = np.array(list(get_patch_label_from_array(pre,th=0.21))) # ground_truth_label = np.array(list(get_patch_label_from_array(y_valid[i,:,:,0],th=0.21))) # train_acc.append(np.mean(np.equal(train_patch_pre_label[:,2],ground_truth_label[:,2]))) # make submission from utils import load_test_image import cv2 as cv test_mat,test_img_id = load_test_image() th = 0.21 f = open('submission.csv','w') f.write('id,prediction\n') for i in range(len(test_img_id)): print(i) img = np.expand_dims(cv.resize(test_mat[i], dsize=(400, 400), interpolation=cv.INTER_CUBIC),axis=0) pre = np.squeeze(model.predict(img),axis=(0,3)) pre_resize = cv.resize(pre,dsize=(test_mat[0].shape[0],test_mat[0].shape[1]),interpolation=cv.INTER_CUBIC) train_patch_pre_label = list(get_patch_label_from_array(pre_resize,th=th)) for line in train_patch_pre_label: final_string = "{:03d}_{}_{},{}".format(test_img_id[i], line[0], line[1], line[2]) f.write(final_string+'\n')