Пример #1
0
def evaluate():
    img = Image.open(FLAGS.photo_file)
    gray_img = img.convert('L')
    img = [int(e) / 255.0 for e in resize(gray_img).tobytes()]
    x = tf.placeholder(tf.float32, [None, 784])
    kb = tf.placeholder(tf.float32, [])
    convolution_op = convolution(x, kb)
    with tf.Session() as sess:
        saver = tf.train.Saver()
        load_checkpoint(sess, saver)
        logits = sess.run([convolution_op], feed_dict={x: [img], kb: 1.0})
        print(LABEL2NAME[np.argmax(logits[0])])
Пример #2
0
 def __init__(self, file_index, path, img_stats, height, width, multi,
              whitelist_path):
     NamedDB.__init__(self, whitelist_path, "FlipDB")
     DB.__init__(self, "unaltered", img_stats, multi)
     self.image_path = path
     with open(file_index, "r") as f:
         self.index = np.load(f)
     with open(whitelist_path, "r") as f:
         self.whitelist = np.load(f)
     with tf.variable_scope("jpeg_distort"):
         self.input, image = image_util.add_jpeg_decoding(img_stats.depth)
         resized = image_util.resize(image, img_stats.size, img_stats.size)
         norm_l = image_util.normalize(image_util.brighten(resized, 5),
                                       img_stats.mean, img_stats.std)
         norm_r = image_util.normalize(image_util.brighten(resized, 5),
                                       img_stats.mean, img_stats.std)
         self.l_output = tf.expand_dims(tf.squeeze(norm_l), 0)
         self.r_output = tf.expand_dims(
             tf.image.flip_left_right(tf.squeeze(norm_r)), 0)
     self.image_whitelist = np.unique(self.whitelist)
def image_to_array(img, size):
    #show_image(img)
    side = img.shape[:1]
    side = side[0] / 9
    result = [[0 for i in range(9)] for j in range(9)]
    for i in range(9):
        for j in range(9):
            tl = (i * side, j * side)  # Top left corner
            br = ((i + 1) * side, (j + 1) * side)  # Bottom right corner
            digit = crop_digit(img, (tl, br), size)
            if (digit is not None):
                digit = resize(digit, size, 4)
                #show_image(digit)
                feature = image_to_feature(digit)
                if len(feature) != 0:
                    predicted = model.predict([feature])[0]
                    #print(predicted)
                    result[j][i] = int(predicted)
                else:
                    result[j][i] = int(0)
    return result
Пример #4
0
def create_mosaic_lines(files_rgb: List[FileInfo], img_target, thumb_width,
                        thumb_height, thumb_width_count, thumb_height_count,
                        img_part_width, img_part_height):
    lines = []
    for i in range(0, thumb_height_count - 1):
        line = None
        for j in range(0, thumb_width_count - 1):
            x = j * img_part_width
            y = i * img_part_height

            rect_rgb = get_int_from_rgb(
                rect_median_color(img_target, x, y, img_part_width,
                                  img_part_height))
            rect_best_fit = find_best_fit(rect_rgb, files_rgb)
            resized = resize(cv2.imread(rect_best_fit), thumb_width,
                             thumb_height)
            if line is None:
                line = resized
            else:
                line = np.concatenate((line, resized), axis=1)
        lines.append(line)
    return lines
Пример #5
0
 def add_resizer(self, ratio):
     resizer = lambda im: image_util.resize(im, ratio)
     self.ops.append(resizer)
Пример #6
0
    def convert(self, img_path, objs):
        """
        Input:
          img_path: 画像ファイルのパス
          objs: オブジェクトbounding boxの情報を含んだ配列。各オブジェクトは(xmin, ymin, xmax, ymax, class_id)
        Output:
          (3, target_size, target_size)の画像, bboxの配列
          bboxの配列は長さ n_grid**2 で、各要素は
          (中心点のgrid内x, 中心点のgrid内y, 幅, 高さ, class_id)
        """
        orig_img = Image.open(img_path)
        w, h = orig_img.size

        if self.augment:
            if random() > 0.5:
                t_img, t_boxes = orig_img, objs
            else:
                t_img, t_boxes = horizontal_flip(orig_img, objs)
            max_noise = min(w, h) * 0.2
            w_noise = int(max_noise * (random() - 0.5))
            h_noise = int(max_noise * (random() - 0.5))

            t_img, t_boxes = resize(t_img,
                                    t_boxes,
                                    self.target_size,
                                    w_noise,
                                    h_noise,
                                    min_bbox_pixel=3)
            np_img = self.color_noise(
                numpy.asarray(t_img.convert('RGB'),
                              dtype=numpy.float32)).transpose(2, 0, 1)
        else:
            t_img, t_boxes = resize(orig_img,
                                    objs,
                                    self.target_size,
                                    min_bbox_pixel=3)
            np_img = numpy.asarray(t_img.convert('RGB'),
                                   dtype=numpy.float32).transpose(2, 0, 1)

        np_boxes = numpy.zeros((self.n_grid**2, 5 + self.n_classes),
                               dtype=numpy.float32)
        np_boxes[:, 5:] = self.class_weights
        for box in t_boxes:
            try:
                w_min, h_min, w_max, h_max, cls_id = box
                grid_idx, x_grid_index, y_grid_index, grid_center_x, grid_center_y, relative_w, relative_h = self.grid_retriever.grid_position(
                    w_min, h_min, w_max, h_max)
                np_boxes[grid_idx][0] = grid_center_x
                np_boxes[grid_idx][1] = grid_center_y
                np_boxes[grid_idx][2] = relative_w
                np_boxes[grid_idx][3] = relative_h
                np_boxes[grid_idx][4] = cls_id

                if not self.rescore_neighbour:
                    continue

                neighbours = []
                if grid_center_x < 0.5 and x_grid_index > 0:
                    neighbours.append((grid_idx - 1, grid_center_x / 0.5))
                    if grid_center_y < 0.5 and y_grid_index > 0:
                        _distance = (grid_center_x**2 + grid_center_y**2)**0.5
                        neighbours.append((grid_idx - self.n_grid - 1,
                                           min(_distance / 0.5, 1.0)))
                    elif grid_center_y > 0.5 and y_grid_index < self.n_grid - 1:
                        _distance = (grid_center_x**2 +
                                     (1.0 - grid_center_y)**2)**0.5
                        neighbours.append((grid_idx + self.n_grid - 1,
                                           min(_distance / 0.5, 1.0)))
                elif grid_center_x > 0.5 and x_grid_index < self.n_grid - 1:
                    neighbours.append(
                        (grid_idx + 1, (1.0 - grid_center_x) / 0.5))
                    if grid_center_y < 0.5 and y_grid_index > 0:
                        _distance = ((1.0 - grid_center_x)**2 +
                                     grid_center_y**2)**0.5
                        neighbours.append((grid_idx - self.n_grid + 1,
                                           min(_distance / 0.5, 1.0)))
                    elif grid_center_y > 0.5 and y_grid_index < self.n_grid - 1:
                        _distance = ((1.0 - grid_center_x)**2 +
                                     (1.0 - grid_center_y)**2)**0.5
                        neighbours.append((grid_idx + self.n_grid + 1,
                                           min(_distance / 0.5, 1.0)))

                if grid_center_y < 0.5 and y_grid_index > 0:
                    neighbours.append(
                        (grid_idx - self.n_grid, grid_center_y / 0.5))
                elif grid_center_y > 0.5 and y_grid_index < self.n_grid - 1:
                    neighbours.append(
                        (grid_idx + self.n_grid, (1.0 - grid_center_y) / 0.5))

                for grid_i, w in neighbours:
                    np_boxes[grid_i,
                             5 + cls_id] = min(np_boxes[grid_i, 5 + cls_id], w)

            except Exception as e:
                print(e)
                print(box)
                print(
                    self.grid_retriever.grid_position(w_min, h_min, w_max,
                                                      h_max))

        return np_img, np_boxes