def test_missing_model(): """ Try to load a filter that doesn't exists. Raise KeyError """ with pytest.raises(KeyError): Instafilter("xxx")
def test_get_model_names(): """ Check that the module loads the list of names and is non-empty. """ names = Instafilter.get_models() assert isinstance(names, list) assert len(names) > 1
def test_missing_image(): """ Try to filter an image that doesn't exist. Raise FileNotFoundError """ model = Instafilter("Lo-Fi") with pytest.raises(OSError): model("xxx")
def test_filter_image(): """ Filter an image. Check that it changed. """ model = Instafilter("Lo-Fi") f_image = __local__ / "Normal.jpg" img0 = cv2.imread(str(f_image)) img1 = model(f_image) diff = (img0 - img1).sum() assert abs(diff) > 0
def test_RGB_mode(): """ Filter an image with RGB mode on and off. Check that we get different results. """ model = Instafilter("Lo-Fi") f_image = __local__ / "Normal.jpg" img1 = model(f_image) img2 = model(f_image, is_RGB=True) diff = (img1 - img2).sum() assert abs(diff) > 0
def run(mode, image_file): try: # read image_file f_id = str(uuid.uuid4()) f_name = secure_filename(image_file.filename) image_byte = image_file.read() image = byte_to_image(image_byte) # make input&result file folder paths = [os.path.join(INPUT_DIR, f_id)] os.makedirs(paths[0], exist_ok=True) # save image to input folder in_filename = os.path.join(paths[0], f_name) image.save(in_filename, quality=100, subsampling=0) # run model model = Instafilter(mode) new_image = model(in_filename) # convert nparray to image new_image = cv2.cvtColor(new_image, cv2.COLOR_BGR2RGB) ouput_image = Image.fromarray(new_image) # convert result image to byte format byte_image = image_to_byte(ouput_image) return byte_image except Exception as e: print(e) return 500 finally: # remove input folder if paths: remove_file(paths) paths.clear()
from werkzeug.utils import secure_filename # import for server from flask import Flask, render_template, request, Response, send_file, jsonify from queue import Queue, Empty import threading import time # flask server app = Flask(__name__) # limit input file size under 2MB app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024 # model loading model = Instafilter('Crema') # request queue setting requests_queue = Queue() BATCH_SIZE = 1 CHECK_INTERVAL = 0.1 # static variable models = [ '1977', 'Aden', 'Amaro', 'Ashby', 'Brannan', 'Brooklyn', 'Charmes', 'Clarendon', 'Crema', 'Dogpatch', 'Earlybird', 'Gingham', 'Ginza', 'Hefe', 'Helena', 'Hudson', 'Inkwell', 'Juno', 'Kelvin', 'Lark', 'Lo-Fi', 'Ludwig', 'Mayfair', 'Melvin', 'Moon', 'Nashville', 'Perpetua', 'Reyes', 'Rise', 'Sierra', 'Skyline', 'Slumber', 'Stinson', 'Sutro', 'Toaster', 'Valencia', 'Vesper', 'Walden', 'Willow', 'X-ProII' ]
from instafilter import Instafilter st.set_option("deprecation.showfileUploaderEncoding", False) st.beta_set_page_config( layout="wide", initial_sidebar_state="expanded", ) url = "https://github.com/thoppe/instafilter" st.markdown("# [Instafilter]({url}) demo") model_name = st.sidebar.selectbox( "Choose a filter", sorted(Instafilter.get_models()), index=20, ) model = Instafilter(model_name) raw_image_bytes = st.file_uploader("Choose an image...") if raw_image_bytes is not None: img0 = np.array(Image.open(raw_image_bytes)) with st.spinner(text="Applying filter..."): # Apply the model, convert to BGR first and after img1 = model(img0[:, :, ::-1], is_RGB=False)[:, :, ::-1] st.image([img1, img0],
import cv2 from pathlib import Path from instafilter import Instafilter scale_size = 0.741 f_source = "train_new_model/input/Normal.jpg" save_dest = Path("examples") save_dest.mkdir(exist_ok=True) img0 = cv2.imread(f_source) for name in Instafilter.get_models(): f_save = save_dest / (name + ".jpg") model = Instafilter(name) img1 = model(f_source) img2 = cv2.resize(img1, None, fx=scale_size, fy=scale_size) print(f"Saving {name}, {img2.shape}") cv2.imwrite(str(f_save), img2)