File size: 5,251 Bytes
d5465e6 17d778a d5465e6 96a96d9 d5465e6 96a96d9 e2d5557 317bdbc 96a96d9 5d4bd93 e2d5557 96a96d9 e2d5557 96a96d9 dac9a79 6684d9c dac9a79 d5465e6 96a96d9 6684d9c d5465e6 13270de b30ce1c d5465e6 17d778a 98dff60 17d778a 98dff60 d5465e6 dac9a79 6684d9c dac9a79 d5465e6 6684d9c 52a0611 b30ce1c 6684d9c d5465e6 cc797bf d5465e6 b30ce1c 597cce4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import cv2
import os
import numpy as np
import json
import argparse
import streamlit as st
@st.cache
def get_arguments():
"""Return the values of CLI params"""
parser = argparse.ArgumentParser()
parser.add_argument("--image_folder", default="images")
parser.add_argument("--image_width", default=400, type=int)
args = parser.parse_args()
return getattr(args, "image_folder"), getattr(args, "image_width")
@st.cache
def get_images_list(path_to_folder: str) -> list:
"""Return the list of images from folder
Args:
path_to_folder (str): absolute or relative path to the folder with images
"""
image_names_list = [
x for x in os.listdir(path_to_folder) if x[-3:] in ["jpg", "peg", "png"]
]
return image_names_list
@st.cache
def load_image(image_name: str, path_to_folder: str, bgr2rgb: bool = True):
"""Load the image
Args:
image_name (str): name of the image
path_to_folder (str): path to the folder with image
bgr2rgb (bool): converts BGR image to RGB if True
"""
path_to_image = os.path.join(path_to_folder, image_name)
image = cv2.imread(path_to_image)
if bgr2rgb:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image
def upload_image(bgr2rgb: bool = True):
"""Uoload the image
Args:
bgr2rgb (bool): converts BGR image to RGB if True
"""
file = st.sidebar.file_uploader(
"Upload your image (jpg, jpeg, or png)", ["jpg", "jpeg", "png"]
)
image = cv2.imdecode(np.fromstring(file.read(), np.uint8), 1)
if bgr2rgb:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image
@st.cache
def load_augmentations_config(
placeholder_params: dict, path_to_config: str = "configs/augmentations.json"
) -> dict:
"""Load the json config with params of all transforms
Args:
placeholder_params (dict): dict with values of placeholders
path_to_config (str): path to the json config file
"""
with open(path_to_config, "r") as config_file:
augmentations = json.load(config_file)
for name, params in augmentations.items():
params = [fill_placeholders(param, placeholder_params) for param in params]
return augmentations
def fill_placeholders(params: dict, placeholder_params: dict) -> dict:
"""Fill the placeholder values in the config file
Args:
params (dict): original params dict with placeholders
placeholder_params (dict): dict with values of placeholders
"""
# TODO: refactor
if "placeholder" in params:
placeholder_dict = params["placeholder"]
for k, v in placeholder_dict.items():
if isinstance(v, list):
params[k] = []
for element in v:
if element in placeholder_params:
params[k].append(placeholder_params[element])
else:
params[k].append(element)
else:
if v in placeholder_params:
params[k] = placeholder_params[v]
else:
params[k] = v
params.pop("placeholder")
return params
def get_params_string(param_values: dict) -> str:
"""Generate the string from the dict with parameters
Args:
param_values (dict): dict of "param_name" -> "param_value"
"""
params_string = ", ".join(
[k + "=" + str(param_values[k]) for k in param_values.keys()]
)
return params_string
def get_placeholder_params(image):
return {
"image_width": image.shape[1],
"image_height": image.shape[0],
"image_half_width": int(image.shape[1] / 2),
"image_half_height": int(image.shape[0] / 2),
}
def select_transformations(augmentations: dict, interface_type: str) -> list:
# in the Simple mode you can choose only one transform
if interface_type == "Simple":
transform_names = [
st.sidebar.selectbox(
"Select a transformation:", sorted(list(augmentations.keys()))
)
]
# in the professional mode you can choose several transforms
elif interface_type == "Professional":
transform_names = [
st.sidebar.selectbox(
"Select transformation №1:", sorted(list(augmentations.keys()))
)
]
while transform_names[-1] != "None":
transform_names.append(
st.sidebar.selectbox(
f"Select transformation №{len(transform_names) + 1}:",
["None"] + sorted(list(augmentations.keys())),
)
)
transform_names = transform_names[:-1]
return transform_names
def show_random_params(data: dict, interface_type: str = "Professional"):
"""Shows random params used for transformation (from A.ReplayCompose)"""
if interface_type == "Professional":
st.subheader("Random params used")
random_values = {}
for applied_params in data["replay"]["transforms"]:
random_values[
applied_params["__class_fullname__"].split(".")[-1]
] = applied_params["params"]
st.write(random_values)
|