def draw_circles(img_name, dst_name, points): img = cv2.imread(npath(img_name)) font = cv2.FONT_HERSHEY_SIMPLEX for i, point in enumerate(points): img = cv2.circle(img, point, 3, (0,255,0), -1) img = cv2.putText(img, str(i), point, font, 0.5, (255,255,255,50), 1, cv2.LINE_AA) cv2.imwrite(npath(dst_name), img)
def __init__(self, template_path): if not os.path.exists(template_path): raise ImproperlyConfigured("Config path '%s' does not exist." % npath(template_path)) self.template_path = template_path self._config = self.__load_template()
def blend(img_name, dst_name, datalist, pallet=None, alpha=0.4, beta=0.6): """ Blends the raw image with a mask which segmented according to the datalist. `img_name` source file path `dst_name` dstination file path `datalist` a tuple of two-element-tuple contains `label` and `points`, such as "( ("label1", [[x1, y1], [x2, y2] ... [xn, yn]]), ("label2", [[x1, y1], [x2, y2] ... [xn, yn]]), )" `pallet` a dict represents label and corresponding color to draw, such as "{ 'label1': (255, 0, 0), 'label2': (0, 255, 0), } " `alpha, beta` weights for the proportion of raw image and mask """ image = cv2.imread(npath(img_name)) # constructs the pallet if not defined before if not pallet: pallet = create_pallet(datalist, is_global=True) default = pallet.get('default', None) # draw mask = np.zeros(image.shape, np.uint8) for label, points in datalist: pts = np.array(points, np.int32) cv2.fillPoly(mask, [pts], (get_label_color(label, pallet, default))) blended = cv2.addWeighted(image, alpha, mask, beta, 0) cv2.imwrite(npath(dst_name), blended) return pallet
def blend(self, filename, alpha=0.7, gamma=0.0): if alpha < 1.0 and alpha > 0.0: beta = 1.0 - alpha else: raise PaintingFailed( "Parameter `alpha` is ought to be in the range of 0 and 1.0") mask = np.zeros(self.im.shape, np.uint8) mask = self.render(mask) blended_img = cv2.addWeighted(self.im, alpha, mask, beta, gamma) cv2.imwrite(npath(filename), blended_img)
def __init__(self, video_path): if not os.path.exists(video_path): raise IOError("No such file or directory: %s" % video_path) self.video_name = os.path.basename(video_path) self.cap = cv2.VideoCapture(npath(video_path)) self.nframes = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT)) self.fps = int(self.cap.get(cv2.CAP_PROP_FPS)) self.width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)) self.height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) self.seconds = self.nframes / self.fps + ( 1 if self.nframes % self.fps > 0 else 0)
def find_commands(management_dir): """ Given a path to a management directory, returns a list of all the command names that are available. Returns an empty list if no commands are defined. """ command_dir = os.path.join(management_dir, 'commands') return [ name for _, name, is_pkg in pkgutil.iter_modules([npath(command_dir)]) if not is_pkg and not name.startswith('_') ]
def draw(self, data_model): image_path = data_model.dest_filepath image_prefix, _ = os.path.splitext(image_path) try: # no mask or blend images if setting labels None if self.mask_label: mask_path = image_prefix + self.mask_label + self.image_suffix draw.draw_polygons(image_path, mask_path, data_model.datalist, self.pallet) if self.blend_label: blend_path = image_prefix + self.blend_label + self.image_suffix draw.blend(image_path, blend_path, data_model.datalist, self.pallet) except AttributeError as e: logger.error("Unable to read {}.".format(npath(image_path)))
def __init__(self, conf_path): if not os.path.exists(conf_path): raise DoesNotExist("Config path does not exist: '%s'." % npath(conf_path)) self.path = conf_path # checks and updates the codecs of config file if necessary # which must be done before setting attribute mtime. self.__update_codec() # Reference to the Argvs registry that holds this ArgvConfig. self._config = self._parse() # gets posix stat result at last config_stat = os.stat(conf_path) # time of last access/modification/file-status-change self.atime = config_stat.st_atime self.mtime = config_stat.st_mtime self.ctime = config_stat.st_ctime
def __init__(self, image_path, pallet=None, autofill=False, use_default=False, persistent=True): if not os.path.exists(image_path): logger.warning( "Unable to find the image specified: {}".format(image_path)) raise IOError( "Unable to find the image specified: {}".format(image_path)) self.image_path = image_path self.im = cv2.imread(npath(image_path)) # add colors automatically self._autofill = autofill self._use_default = use_default self._persistent = persistent # reset pallet if not set persistent True self._pallet = self.persistent_pallet if persistent else {} # update pallet if a new one was given if pallet: self.update_pallet(pallet) self._shapes = []
def write(self, frame, dst_file): if os.path.exists(dst_file): raise IOError("File is already existed.") cv2.imwrite(npath(dst_file), frame)
def draw_polygons(img_name, dst_name, datalist, pallet=None, border=False, grayscale=False): """ Draws a mask on a black background. `img_name` source file path `dst_name` dstination file path `datalist` a tuple of two-element-tuple contains `label` and `points`, such as "( ("label1", [[x1, y1], [x2, y2] ... [xn, yn]]), ("label2", [[x1, y1], [x2, y2] ... [xn, yn]]), )" `pallet` a dict represents label and corresponding color to draw, such as "{ 'label1': (255, 0, 0), 'label2': (0, 255, 0), } " `border` False or a tuple indicating the color to use (such as `(255, 255, 255)`) `grayscale` a boolean indicates whether the image was grayscale or rgb """ image = cv2.imread(npath(img_name)) # constructs the pallet if not defined before if not pallet: labels = set() for label, _ in datalist: labels.add(label) nlabel = len(labels) if grayscale: colors = range(0, 256, 256 / nlabel)[1:] # do not use color black pallet = {k: v for k, v in zip(list(labels), colors)} else: pallet = create_pallet(datalist, is_global=True) default = pallet.get('default', None) # constructs a canvas with different storage ability: 8 or 24 bit/pixel if grayscale: canvas = np.zeros(image.shape[:2] + (1, ), np.uint8) else: canvas = np.zeros(image.shape, np.uint8) # draw for label, points in datalist: pts = np.array(points, np.int32) if border: cv2.polylines(canvas, [pts], True, border, 3) cv2.fillPoly(canvas, [pts], (get_label_color(label, pallet, default))) cv2.imwrite(npath(dst_name), canvas) return pallet
def masking(self, filename): mask = np.zeros(self.im.shape, np.uint8) mask = self.render(mask) cv2.imwrite(npath(filename), mask)
def draw(self, filename): # Do not draw on the original image img = copy.deepcopy(self.im) img = self.render(img) cv2.imwrite(npath(filename), img)