def generate_and_save_images(model, epoch, test_input): predictions = model(test_input, training=False) fig = plt.figure(figsize=(4, 4)) for i in range(predictions.shape[0]): plt.subplot(4, 4, i + 1) plt.imshow(normalize(predictions[i, :, :, 0], input_range=(-1, 1), output_range=(0, 255)), cmap='gray') plt.axis('off') plt.savefig('./images/epoch_{:04d}.png'.format(epoch)) plt.close()
def preprocess(raw, frames): global noise global count global approach img = frames[0] gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces, _, confidence = classifier.detectMultiScale3(gray, outputRejectLevels=True, minSize=(25, 25), maxSize=(80, 80)) max_ind = -1 max_metric = 0 max_ind_2 = -1 for i in range(len(faces)): metric = confidence[i] if metric > max_metric: max_metric = metric max_ind = i max_metric = 0 for i in range(len(faces)): if i == max_ind: continue metric = confidence[i] if metric > max_metric: max_metric = metric max_ind_2 = i for i in [max_ind, max_ind_2]: if i < 0: continue x, y, w, h = faces[i] noise = (1 - r) * noise + r * approach count += 1 if count >= stability: count = 0 approach = tf.random.normal([1, 100]) gen_img = colorifier(generator(noise, training=False), training=False) gen_img = normalize(gen_img, input_range=(-1, 1), output_range=(0, 255)) gen_img = np.reshape(gen_img, (28,28,3)) gen_img = cv2.resize(gen_img, (w, h)) weights = cv2.resize(gaussian_tensor, (w, h)) img[y: y + h, x: x + w] = gen_img * weights + (1 - weights) * img[y: y + h, x: x + w] return img
capture = cv2.VideoCapture(0) classifier = cv2.CascadeClassifier('../dataset/haarcascade_frontalface_default.xml') colorifier = tf.keras.models.load_model(path) while True: cur_time = time.time() _, img = capture.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = classifier.detectMultiScale(gray) if len(faces) > 0: faces = list(faces) faces.sort(key=lambda rect: -rect[2] * rect[3]) x, y, w, h = faces[0] inp_img = normalize(cv2.resize(gray[y: y + h, x: x + h], (28, 28))) inp_img = np.reshape(inp_img, (1, 28, 28, 1)) gen_img = colorifier(inp_img, training=False) gen_img = normalize(gen_img, input_range=(-1, 1), output_range=(0, 255)) gen_img = np.reshape(gen_img, (28,28,3)) gen_img = cv2.resize(gen_img, (w, h)) img[y: y + h, x: x + w] = gen_img cv2.imshow('img', img) k = cv2.waitKey(30) & 0xff if k==27: break capture.release()
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt import PIL from gan.config import * from gan.generative_network import make_generator_model from gan.discriminative_network import make_discriminator_model from dataset.preproccesing import load_data, normalize if os.path.exists(data_file) and not update_data_file: gray = np.load(data_file) else: gray = load_data('../dataset/images/', should_log=True, load_color=False) gray = normalize(gray) np.save(data_file, gray) dataset = tf.data.Dataset.from_tensor_slices(gray).shuffle(BUFFER_SIZE).batch( BATCH_SIZE) generator = make_generator_model() discriminator = make_discriminator_model() cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True) def discriminator_loss(real_output, fake_output): real_loss = cross_entropy(tf.ones_like(real_output), real_output) fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output) total_loss = real_loss + fake_loss
import numpy as np import tensorflow as tf import cv2 from dataset.preproccesing import load_data, normalize from colorify.make_model import make_model from colorify.config import * if os.path.exists(gray_data_file) and not update_data_file: color = np.load(color_data_file) gray = np.load(gray_data_file) else: color, gray = load_data('../dataset/images/', should_log=True) color, gray = normalize(color), normalize(gray) np.save(color_data_file, color) np.save(gray_data_file, gray) gray = tf.data.Dataset.from_tensor_slices(gray) color = tf.data.Dataset.from_tensor_slices(color) dataset = tf.data.Dataset.zip( (gray, color)).shuffle(BUFFER_SIZE).batch(BATCH_SIZE) log_dir = ".\\logs\\fit\\" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir) model = make_model() model.compile(optimizer='adam', loss='mse') model.fit(dataset, epochs=10, callbacks=[tensorboard_callback])
generator = tf.keras.models.load_model(gen_path) noise = tf.random.normal( [1, 100]) if rand_start else np.load('./inputs/best_noise_input.npy') while True: cur_time = time.time() _, img = capture.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = classifier.detectMultiScale(gray) if len(faces) > 0: faces = list(faces) faces.sort(key=lambda rect: -rect[2] * rect[3]) x, y, w, h = faces[0] noise = r * noise + (1 - r) * tf.random.normal([1, 100]) gen_img = generator(noise, training=False) gen_img = normalize(gen_img, input_range=(-1, 1), output_range=(0, 255)) gen_img = np.reshape(gen_img, (28, 28)) gen_img = cv2.resize(cv2.cvtColor(gen_img, cv2.COLOR_GRAY2BGR), (w, h)) img[y:y + h, x:x + w] = gen_img cv2.imshow('img', img) k = cv2.waitKey(30) & 0xff if k == 27: break capture.release()
matcher = tf.keras.models.load_model(path) while True: cur_time = time.time() _, img = capture.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = classifier.detectMultiScale(gray) if len(faces) > 0: faces = list(faces) faces.sort(key=lambda rect: -rect[2] * rect[3]) x, y, w, h = faces[0] inp_img = img[y:y + h, x:x + w] inp_img = cv2.resize(inp_img, (28, 28)) inp_img = cv2.cvtColor(inp_img, cv2.COLOR_BGR2GRAY) inp_img = normalize(inp_img) inp_img = np.reshape(inp_img, (1, 28, 28, 1)) out_img = generator(matcher(inp_img)) out_img = np.reshape(out_img, (28, 28)) out_img = normalize(out_img, input_range=(-1, 1), output_range=(0, 255)) out_img = cv2.resize(out_img, (w, h)) out_img = cv2.cvtColor(out_img, cv2.COLOR_GRAY2BGR) img[y:y + h, x:x + w] = out_img cv2.imshow('img', img) k = cv2.waitKey(30) & 0xff if k == 27: break