def view_image_regions(region_generator, dimensions, display_scale): for reg in region_generator: print "view_image_regions:" print reg.as_dict original = reg.load_cropped_resized_sample(dimensions, utils.RegionModifiers()) sample = reg.load_cropped_resized_sample(dimensions) display_shape = (dimensions[1] * display_scale, dimensions[0] * display_scale) resized_sample = utils.resize_sample(sample, display_shape, use_interp=False) resized_original = utils.resize_sample(original, display_shape, use_interp=False) combined_shape = (display_shape[0], display_shape[1] * 2) combined = np.zeros((combined_shape[0], combined_shape[1], 3), np.uint8) cw = display_shape[1] combined[:, 0:cw] = resized_original combined[:, cw : 2 * cw] = resized_sample cv2.imshow("view_image_regions", combined) # cv2.imshow('view_image_regions', resized) while True: key = cv2.waitKey(1) & 0xFF if key == 27: # ESC key cv2.destroyAllWindows() return elif key != 255: break
def view_image_regions(region_generator, dimensions, display_scale): for reg in region_generator: print 'view_image_regions:' print reg.as_dict original = reg.load_cropped_resized_sample(dimensions, utils.RegionModifiers()) sample = reg.load_cropped_resized_sample(dimensions) display_shape = (dimensions[1] * display_scale, dimensions[0] * display_scale) resized_sample = utils.resize_sample(sample, display_shape, use_interp=False) resized_original = utils.resize_sample(original, display_shape, use_interp=False) combined_shape = (display_shape[0], display_shape[1] * 2) combined = np.zeros((combined_shape[0], combined_shape[1], 3), np.uint8) cw = display_shape[1] combined[:, 0:cw] = resized_original combined[:, cw:2 * cw] = resized_sample cv2.imshow('view_image_regions', combined) # cv2.imshow('view_image_regions', resized) while True: key = cv2.waitKey(1) & 0xFF if key == 27: # ESC key cv2.destroyAllWindows() return elif key != 255: break
def classify(self, img, pixel_rect): img_h, img_w = img.shape[:2] img_dims = (img_w, img_h) w, h = self.window_dims window_shape = (h, w) window_aspect = w / float(h) # print window_aspect # Enlarge to window_dims: enlarged_rect = pixel_rect.enlarge_to_aspect(window_aspect) # Ensure rectangle is located within its image: object_rect = enlarged_rect.translated([0,0], img_dims) # if not pixel_rect.lies_within_frame(img_dims): sample = utils.crop_rectangle(img, object_rect) sample = utils.resize_sample(sample, shape=window_shape) sample = self.prepare_sample(sample) flat_sample = sample.reshape(sample.shape[0] * sample.shape[1] * sample.shape[2]) feed = {self.x: [flat_sample], self.keep_prob: 1.0} label_probs = self.sess.run(tf.nn.softmax(self.logits), feed_dict=feed) pos_prob, neg_prob = label_probs[0] is_object = pos_prob > neg_prob return is_object, object_rect, pos_prob
def classify(self, img, pixel_rect): img_h, img_w = img.shape[:2] img_dims = (img_w, img_h) w, h = self.window_dims window_shape = (h, w) window_aspect = w / float(h) # print window_aspect # Enlarge to window_dims: enlarged_rect = pixel_rect.enlarge_to_aspect(window_aspect) # Ensure rectangle is located within its image: object_rect = enlarged_rect.translated([0, 0], img_dims) # if not pixel_rect.lies_within_frame(img_dims): sample = utils.crop_rectangle(img, object_rect) sample = utils.resize_sample(sample, shape=window_shape) sample = self.prepare_sample(sample) flat_sample = sample.reshape(sample.shape[0] * sample.shape[1] * sample.shape[2]) feed = {self.x: [flat_sample], self.keep_prob: 1.0} label_probs = self.sess.run(tf.nn.softmax(self.logits), feed_dict=feed) pos_prob, neg_prob = label_probs[0] is_object = pos_prob > neg_prob return is_object, object_rect, pos_prob
def image_pyramid(img, scale_factor=1.1, min_dims=(32, 32)): def shape_is_valid(img): h, w = img.shape[:2] mw, mh = min_dims return h >= mh and w >= mw while shape_is_valid(img): yield img img = utils.resize_sample(img, scale=1.0 / scale_factor)
def image_pyramid(img, scale_factor=1.1, min_dims=(32, 32)): def shape_is_valid(img): h, w = img.shape[:2] mw, mh = min_dims return h >= mh and w >= mw while shape_is_valid(img): yield img img = utils.resize_sample(img, scale=1.0/scale_factor)
def update_display(self): h, w = self.current_img.shape[:2] max_dim = float(self.display_width) if w > max_dim + 50 or h > max_dim + 50: sx = max_dim / w sy = max_dim / h self.current_scale = min(sx, sy) self.current_img = cv2.resize(self.current_img, dsize=None, fx=self.current_scale, fy=self.current_scale) clone_img = self.current_img.copy() # Draw bounding boxes for current image: for rect in self.get_image_rectangles(self.current_path): oh, ow = self.original_shape[:2] rect = rect.scale_image((ow, oh), (w, h)) if self.flipped: rect = rect.rotated_180((w, h)) tl = tuple(rect.tl) br = tuple(rect.br) cv2.rectangle(clone_img, tl, br, (255, 255, 255), 3) cv2.rectangle(clone_img, tl, br, (255, 0, 127), 2) # Draw bounding box being edited: if self.rect_tl and self.rect_br: cv2.rectangle(clone_img, self.rect_tl, self.rect_br, (255, 255, 255), 3) cv2.rectangle(clone_img, self.rect_tl, self.rect_br, (0, 255, 0), 2) rect = gm.PixelRectangle.fromCorners(self.rect_tl, self.rect_br) rect = self.convert_to_rect_in_original(rect) cropped = utils.crop_rectangle(self.original_img, rect) cw = self.current_img.shape[1] ch = self.current_img.shape[0] if rect.w > 5 and rect.h > 5: if rect.w > rect.h: preview_width = cw / 5.0 preview_height = preview_width / rect.aspect else: preview_height = ch / 4.0 preview_width = preview_height * rect.aspect new_shape = (max(2, int(round(preview_height))), max(2, int(round(preview_width)))) preview = utils.resize_sample(cropped, new_shape, use_interp=False) clone_img[0:new_shape[0], 0:new_shape[1], :] = preview cv2.imshow(self.winName, clone_img)
def update_display(self): h, w = self.current_img.shape[:2] max_dim = float(self.display_width) if w > max_dim + 50 or h > max_dim + 50: sx = max_dim / w sy = max_dim / h self.current_scale = min(sx, sy) self.current_img = cv2.resize(self.current_img, dsize=None, fx=self.current_scale, fy=self.current_scale) clone_img = self.current_img.copy() # Draw bounding boxes for current image: for rect in self.get_image_rectangles(self.current_path): oh, ow = self.original_shape[:2] rect = rect.scale_image((ow, oh), (w,h)) if self.flipped: rect = rect.rotated_180((w,h)) tl = tuple(rect.tl) br = tuple(rect.br) cv2.rectangle(clone_img, tl, br, (255, 255, 255), 3) cv2.rectangle(clone_img, tl, br, (255, 0, 127), 2) # Draw bounding box being edited: if self.rect_tl and self.rect_br: cv2.rectangle(clone_img, self.rect_tl, self.rect_br, (255, 255, 255), 3) cv2.rectangle(clone_img, self.rect_tl, self.rect_br, (0, 255, 0), 2) rect = gm.PixelRectangle.fromCorners(self.rect_tl, self.rect_br) rect = self.convert_to_rect_in_original(rect) cropped = utils.crop_rectangle(self.original_img, rect) cw = self.current_img.shape[1] ch = self.current_img.shape[0] if rect.w > 5 and rect.h > 5: if rect.w > rect.h: preview_width = cw / 5.0 preview_height = preview_width / rect.aspect else: preview_height = ch / 4.0 preview_width = preview_height * rect.aspect new_shape = (max(2, int(round(preview_height))), max(2, int(round(preview_width)))) preview = utils.resize_sample(cropped, new_shape, use_interp=False) clone_img[0:new_shape[0],0:new_shape[1],:] = preview cv2.imshow(self.winName, clone_img)