def randomcrop(img, side): # crop sidexside randomly from given image. h, w = img.shape[0:2] minside = min(h, w) scale = minside / side # assert scale > 1 cs = chosen_scale = np.random.uniform(scale * .45, scale * .95) img = vis.resize_perfect(img, h / cs, w / cs, cubic=True) h, w = img.shape[0:2] sh, sw = np.random.choice(h - side), np.random.choice(w - side) cropped = img[sh:sh + side, sw:sw + side, :] if np.random.uniform() > 0.5: cropped = np.flip(cropped, axis=1) # random brightness contrast cropped = cropped.astype(np.float32) cropped = cropped * (np.random.uniform(0.6, 1.7, size=(1, )) + np.random.uniform(-.4, .4, size=(3, ))) cropped = cropped + np.random.uniform(-0.4, 0.4, size=(3, )) * 255 cropped = np.random.normal(loc=cropped, scale=np.random.uniform(3, 25)) cropped = np.clip(cropped, 0, 255) cropped = cropped.astype(np.uint8) return cropped
def load_image(self, path, is_filename=True, scale=1.0, target_width=None): orig = load_image(path, is_filename) # crop to face region # from facial import facecrop # orig = facecrop(orig) if target_width is not None: scale = target_width / orig.shape[1] self.orig = orig self.scale = scale # set expected output h and w self.orig_h, self.orig_w = orig.shape[0:2] self.target_h, self.target_w = int(self.orig_h * scale), int( self.orig_w * scale) self.orig_hw = [self.orig_h, self.orig_w] self.target_hw = [self.target_h, self.target_w] # resize target = vis.resize_perfect(self.orig, self.target_h, self.target_w, cubic=True, a=3) # log print( 'loading image {}, scale:{:2.2f}, orig[{}x{}], now[{}x{}]'.format( path, scale, self.orig_w, self.orig_h, self.target_w, self.target_h)) # floatify target = (target / 255.).astype('float32') target = self.preprocess(target) if self.grayscale: # b/w target = np.einsum('ijk,kt->ijt', target, np.array([[.3], [.6], [.1]])) # clip to normal range target = np.clip(target, 0, 1) self.target = target self.set_metric()
def load_image(self, path, is_filename=True, scale=1.0, target_width=None): if is_filename: # read image from file orig = cv2.imread(path) assert orig is not None else: # use in memory image orig = path if target_width is not None: scale = target_width / orig.shape[1] self.orig = orig self.scale = scale # set expected output h and w self.orig_h, self.orig_w = orig.shape[0:2] self.target_h, self.target_w = int(self.orig_h * scale), int( self.orig_w * scale) self.orig_hw = [self.orig_h, self.orig_w] self.target_hw = [self.target_h, self.target_w] # resize target = vis.resize_perfect(self.orig, self.target_h, self.target_w, cubic=True, a=3) # log print( 'loading image {}, scale:{:2.2f}, orig[{}x{}], now[{}x{}]'.format( path, scale, self.orig_w, self.orig_h, self.target_w, self.target_h)) # floatify target = (target / 255.).astype('float32') # b/w target = (target[:, :, 1:2] + target[:, :, 2:3]) * .5 # clip to normal range target = np.clip(target * 1 + 0.1, 0, 1) self.target = target # loss metric precomputation self.precomputated = self.loss_metric.prepare(self.target)
def jeffdemo(): jeff = load_image('../jeff.jpg') h, w = jeff.shape[0:2] jeff = vis.resize_perfect(jeff, 192, 192) print(jeff.shape) vis.show_autoscaled(jeff) jefff = np.divide(jeff, 255., dtype=np.float32) jeffff = forenet_infer(jefff) print(jeffff.shape) jeffff = np.transpose(jeffff, [2, 0, 1]) jeffff.shape += (1, ) vis.show_batch_autoscaled(jeffff * 0.5 + 0.5) cv2.waitKey(0)
def resize(image, maxwidth): cmw = max(image.shape[0:2]) return vis.resize_perfect(image, maxwidth, maxwidth, cubic=True, a=3)
# canvas width w = 256 def newcanvas(): return np.ones((w, w, 1), dtype='uint8') * 255 mc = ManyConnected(w=w) v = mc.to_vec() print(type(v), v.shape) mc.from_vec(v) target = cv2.imread('hjt.jpg') target = vis.resize_perfect(target, w, w, cubic=True, a=3) target = (target / 255.).astype('float32') target = (target[:, :, 1:2] + target[:, :, 2:3]) * .5 target = np.clip(target * 1 + 0.1, 0, 1) def pyramid(img): a = [img] for i in range(5): a.append(cv2.pyrDown(a[-1])) return a target_pyr = pyramid(target)
def update(): sess = get_session() _, l = sess.run([updateop, loss]) return l def getcanvas(): sess = get_session() c = sess.run([canvas])[0] return c[0] return settarg, update, getcanvas jeff = load_image('../jeff.jpg') h,w = jeff.shape[0:2] jeff = vis.resize_perfect(jeff, 192, 192) print(jeff.shape) settarg, update, getcanvas = feed_gen(jeff) get_session().run(gvi()) settarg(jeff) def show(): vis.show_autoscaled(jeff,name='jeff') canvas = getcanvas() vis.show_autoscaled(canvas) cv2.waitKey(1) show()