def __init__(self, sess): self.sess = sess self.class_names = read_classes(_CLASSNAME) self.anchors = np.array(_ANCHORS).reshape(-1, 2) self.score_threshold = _SCORE_THRESHOLD self.iou_threshold = _IOU_THRESHOLD self.input_size = (_SIZE, _SIZE) self.ratio = tf.placeholder(tf.float32, [2], name='ratio') self.inputs, self.boxes, self.scores = self.init_opt()
def main(): s_list = utils.read_s_list("test_files/s_test.pli") p_list, variables_dict = utils.read_p_list_and_dict( "test_files/p_test.pli") description_list = utils.read_classes("test_files/isomorphism_test.pli") temp = utils.sort_p_list(description_list[0].p_list) flag, result = algorithm.IMA_first_full(description_list[0].s_list, description_list[4].p_list, description_list[4].variables_dict) print(flag)
def __init__(self, model_path=None, anchors_path=None, classes_path=None, dims=None): if model_path is None or anchors_path is None or classes_path is None or dims is None or len( dims) != 2: raise ValueError('Arguments do not match the specification.') self._model = keras.models.load_model(model_path, compile=False) self._anchors = read_anchors(anchors_path) self._class_names = read_classes(classes_path) self._dims = dims self._image_shape = list(reversed([int(x) for x in dims])) self._model_input_dims = (608, 608) self._colors = generate_colors(self._class_names) self._sess = K.get_session() self._construct_graph()
import tensorflow as tf from keras import backend as K from bounding_box import Box from yad2k.models.keras_yolo import yolo_boxes_to_corners from utils import read_classes, read_anchors, preprocess_image, scale_boxes class_names = read_classes("model_data/coco_classes.txt") anchors = read_anchors("model_data/yolo_anchors.txt") def filter_anchor_boxes(box_confidence, boxes, box_class_probs, threshold=.6): """ Filters YOLO boxes by thresholding on object and class confidence Filter any box for which the class "score" is less than a chosen threshold. Arguments: box_confidence -- tensor of shape (19, 19, 5, 1) - confidence probability that there's some object for each of the 5 boxes boxes -- tensor of shape (19, 19, 5, 4) - (bx,by,bh,bw) for each of the 5 boxes per cell. box_class_probs -- tensor of shape (19, 19, 5, 80)- detection probabilities (c1,c2,...c80) for each of the 80 classes for each of the 5 boxes per cell. threshold -- real value, if [ highest class probability score < threshold], then get rid of the corresponding box Returns: scores -- tensor of shape (None,), containing the class probability score for selected boxes boxes -- tensor of shape (None, 4), containing (b_x, b_y, b_h, b_w) coordinates of selected boxes classes -- tensor of shape (None,), containing the index of the class detected by the selected boxes """ box_scores = box_confidence * box_class_probs
def show_ground_truth(model, dataset, idx, device='cpu', classes='../../../Data/SS/ss.names', overlap_threshold=1., black=False, clean=False): lst_classes = read_classes(classes) colors = create_pascal_label_colormap(len(lst_classes)) data = dataset[idx] images, image_info, targets = data model.set_image_size(images.shape[-2:]) images = images[None] image_info = {k: [v] for k, v in image_info.items()} image_info['padding'] = list(map(lambda x: [x], image_info['padding'][0])) image_info['scale'] = list(map(lambda x: [x], image_info['scale'][0])) targets = [t[None].to(device) for t in targets] bboxes, classes, confidences, image_idx = model.process_bboxes( targets, image_info, 1e-5, overlap_threshold, nms=False) for i, data in enumerate(zip(image_info['id'], images)): idx, image = data image = Image.open( os.path.join(dataset.root_dir[0], 'JPEGImages', image_info['id'][0] + '.jpg')) if black: for images, images_info, targets in dataset: if images_info['id'] == image_info['id'][0]: image = Image.fromarray( np.uint8(cm.binary_r(images[0].numpy()) * 255)) break image = image.resize(model.default_image_size) image = gizeh.ImagePattern(np.array(image)) image = gizeh.rectangle(2. * model.default_image_size[0], 2. * model.default_image_size[1], xy=(0, 0), fill=image) pdf = gizeh.PDFSurface('detections/yolo_gt_{}.pdf'.format(idx), model.default_image_size[0], model.default_image_size[1]) image.draw(pdf) mask = np.array(image_idx) == idx if not clean: for bb, cl, co in zip(bboxes[mask], classes[mask], confidences[mask]): rect = [[int(bb[0]), int(bb[1])], [int(bb[2]), int(bb[1])], [int(bb[2]), int(bb[3])], [int(bb[0]), int(bb[3])]] rect = gizeh.polyline(rect, close_path=True, stroke_width=4, stroke=colors[cl]) rect.draw(pdf) for bb, cl, co in zip(bboxes[mask], classes[mask], confidences[mask]): w, h = len(dataset.classes[cl]) * 8.5 + 8, 15 rect = gizeh.rectangle( w, h, xy=(int(bb[0] + w / 2 - 2), max((int(bb[1] - h / 2 + 7)), 7)), # - 2)), # fill=colors[cl]) fill=(1, 1, 1, 0.5)) rect.draw(pdf) txt = gizeh.text( lst_classes[cl], # 'Helvetica', 'monospace', fontsize=16, xy=(int(bb[0]), max((int(bb[1]), 10))), # - 12), fill=(0., 0., 0.), v_align='center', # 'bottom', h_align='left') txt.draw(pdf) pdf.flush() pdf.finish()
def __init__(self, anchors, class_file, root_dir, mu, sigma, mode, dataset, batch_size=1, skip_truncated=False, do_transforms=False, image_size=(512, 512), skip_difficult=True, strides=32, return_targets=True): self.classes = read_classes(class_file) self.num_classes = len(self.classes) self.num_features = 5 + self.num_classes if isinstance(root_dir, str): root_dir = [root_dir] if isinstance(dataset, str): dataset = [dataset] assert len(root_dir) == len(dataset) self.root_dir = root_dir self.raw_dir = [os.path.join(r, 'Raw') for r in self.root_dir] self.annotations_dir = [ os.path.join(r, 'Annotations') for r in self.root_dir ] self.sets_dir = [ os.path.join(r, 'ImageSets', 'Main') for r in self.root_dir ] self.dataset = dataset self.data = [] self.skip_truncated = skip_truncated self.skip_difficult = skip_difficult self.anchors = [a.clone().detach().cpu() for a in anchors] self.num_anchors = [len(a) for a in self.anchors] self.do_transforms = do_transforms self.return_targets = return_targets self.batch_size = batch_size if isinstance(strides, int): strides = [strides] self.strides = strides assert len(anchors) == len(strides) self.num_detectors = len(anchors) self.default_image_size = image_size self.mode = mode self.mu = mu self.sigma = sigma for d in range(len(dataset)): for cls in self.classes: file = os.path.join(self.sets_dir[d], '{}_{}.txt'.format(cls, dataset[d])) with open(file) as f: for line in f: image_desc = line.split() if image_desc[1] == '1': self.data.append((d, image_desc[0])) self.data = list(set(self.data)) # Remove duplicates. self.data.sort() self.n = len(self.data) self.image_size = np.repeat(self.default_image_size, self.n).reshape(-1, 2) self.grid_sizes = [] for ss in self.strides: self.grid_sizes.append( np.repeat([s // ss for s in self.default_image_size], self.n).reshape(-1, 2))
tracker = cv2.TrackerMIL_create() if tracker_type == 'KCF': tracker = cv2.TrackerKCF_create() if tracker_type == 'TLD': tracker = cv2.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': tracker = cv2.TrackerMedianFlow_create() if tracker_type == 'CSRT': tracker = cv2.TrackerCSRT_create() if tracker_type == 'MOSSE': tracker = cv2.TrackerMOSSE_create() # Open our model device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = alexnet(pretrained=True).eval().cuda() classes = read_classes() # параметр 0 для камеры, 'traffic.mp4' для видео video = cv2.VideoCapture('doge.mp4') # read 1st frame ok, frame = video.read() fourcc = cv2.VideoWriter_fourcc(*'XVID') vw = frame.shape[1] vh = frame.shape[0] print("Video size", vw, vh) outvideo = cv2.VideoWriter("out.mp4", fourcc, 30.0, (vw, vh)) # frame counter index = 0
import cv2 import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from utils import read_classes ROOT_DIR = os.getcwd() DATA_PATH = os.path.join(ROOT_DIR, "data") DATA_SET_PATH = os.path.join(DATA_PATH, "VOCdevkit/VOC2012") ANNOTATIONS_PATH = os.path.join(DATA_SET_PATH, "Annotations") IMAGES_PATH = os.path.join(DATA_SET_PATH, "JPEGImages") # Classes that you want to detect. CLASSES = read_classes("./model_data/voc2012_classes.txt") def process_image(image_file): """Decode image at given path.""" # Method 1: return <class 'tf.Tensor'> image_string = tf.io.read_file(image_file) # Method 2: return <class 'bytes'> #with open(image_file, 'rb') as f: # image_string = f.read() # binary-string try: image_data = tf.image.decode_jpeg(image_string, channels=3) #image_data = tf.image.resize(image_data, [300, 300]) #image_data /= 255.0 # normalize to [0, 1] range
def __init__(self, anchors, class_file, root_dir, dataset=['train'], batch_size=1, skip_truncated=False, do_transforms=False, skip_difficult=True, image_size=(416, 416), strides=32, return_targets=True, multi_scale=False): """ Initialise the dataset object with some network and dataset specific parameters. Parameters ---------- anchors : Tensor A Tensor object of N anchors given by (x1, y1, x2, y2). class_file : str The path to a text file containing the names of the different classes that should be loaded. root_dir : str or list The root directory where the PASCAL VOC images and annotations are stored. dataset : list, optional The specific subset of the PASCAL VOC challenge which should be loaded. skip_truncated : bool, optional A boolean value to specify whether bounding boxes should be skipped or returned for objects that are truncated. do_transforms : bool, optional A boolean value to determine whether default image augmentation transforms should be randomly applied to images. skip_difficult : bool, optional A boolean value to specify whether bounding boxes should be skipped or returned for objects that have been labeled as 'difficult'. image_size : tuple of int, optional A tuple (w, h) describing the desired width and height of the images to be returned. grid_size : tuple of int, optional A tuple (x, y) describing how many horizontal and vertical grids comprise the YOLO model that will load images from this dataset. """ assert image_size[0] == image_size[ 1], 'This implementation has only been validated for square images.' self.classes = read_classes(class_file) self.num_classes = len(self.classes) self.num_features = 5 + self.num_classes if isinstance(root_dir, str): root_dir = [root_dir] if isinstance(dataset, str): dataset = [dataset] assert len(root_dir) == len(dataset) self.root_dir = root_dir self.images_dir = [ os.path.join(r, 'JPEGImages') for r in self.root_dir ] self.annotations_dir = [ os.path.join(r, 'Annotations') for r in self.root_dir ] self.sets_dir = [ os.path.join(r, 'ImageSets', 'Main') for r in self.root_dir ] self.dataset = dataset self.images = [] self.skip_truncated = skip_truncated self.skip_difficult = skip_difficult self.anchors = [a.clone().detach().cpu() for a in anchors] self.num_anchors = [len(a) for a in self.anchors] self.do_transforms = do_transforms self.return_targets = return_targets self.batch_size = batch_size self.multi_scale = multi_scale if isinstance(strides, int): strides = [strides] self.strides = strides assert len(anchors) == len(strides) self.num_detectors = len(anchors) self.default_image_size = image_size for d in range(len(dataset)): for cls in self.classes: file = os.path.join(self.sets_dir[d], '{}_{}.txt'.format(cls, dataset[d])) with open(file) as f: for line in f: image_desc = line.split() if image_desc[1] == '1': self.images.append((d, image_desc[0])) self.images = list(set(self.images)) # Remove duplicates. self.images.sort() # self.images = [(0, str(3000+i)) for i in range(10)] self.n = len(self.images) self.image_size = None self.grid_sizes = None if self.multi_scale: self.step() else: self.disable_multiscale()
def __init__(self, classes, mu, sigma, train=False, root_dir='data/VOC2012/', dataset='train', skip_truncated=True, do_transforms=False, skip_difficult=True, return_targets=False): """ Initialise the dataset object with some network and dataset specific parameters. Parameters ---------- classes : str The path to a text file containing the names of the different classes that should be loaded. root_dir : str, optional The root directory where the PASCAL VOC images and annotations are stored. dataset : {'train', 'val', 'trainval', 'test}, optional The specific subset of the PASCAL VOC challenge which should be loaded. skip_truncated : bool, optional A boolean value to specify whether bounding boxes should be skipped or returned for objects that are truncated. do_transforms : bool, optional A boolean value to determine whether default image augmentation transforms should be randomly applied to images. skip_difficult : bool, optional A boolean value to specify whether bounding boxes should be skipped or returned for objects that have been labeled as 'difficult'. """ self.classes = read_classes(classes) self.classes.insert(0, '__background__') self.num_classes = len(self.classes) if isinstance(root_dir, str): root_dir = [root_dir] if isinstance(dataset, str): dataset = [dataset] assert len(root_dir) == len(dataset) self.root_dir = root_dir self.images_dir = [ os.path.join(r, 'JPEGImages') for r in self.root_dir ] self.annotations_dir = [ os.path.join(r, 'Annotations') for r in self.root_dir ] self.sets_dir = [ os.path.join(r, 'ImageSets', 'Main') for r in self.root_dir ] self.dataset = dataset self.train = train self.mu = mu self.sigma = sigma self.images = [] self.skip_truncated = skip_truncated self.skip_difficult = skip_difficult self.do_transforms = do_transforms self.return_targets = return_targets for d in range(len(dataset)): for cls in self.classes[1:]: file = os.path.join(self.sets_dir[d], '{}_{}.txt'.format(cls, dataset[d])) with open(file) as f: for line in f: image_desc = line.split() if image_desc[1] == '1': self.images.append((d, image_desc[0])) self.images = list(set(self.images)) # Remove duplicates. self.images.sort() # random.shuffle(self.images) self.n = len(self.images)