-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import argparse | ||
import os | ||
import sys | ||
|
||
import numpy | ||
from PIL import Image | ||
import six.moves.cPickle as pickle | ||
|
||
train_image_dir = "data/bbox_image/" | ||
|
||
# --------------------------------------------------- | ||
# create train.txt | ||
import targets | ||
targets = targets.getTargets() | ||
if os.path.exists("train.txt"): | ||
os.remove("train.txt") | ||
f = open("train.txt","w") | ||
for fileName in os.listdir(train_image_dir): | ||
image_id = fileName[:fileName.index(".")] | ||
if image_id in targets: | ||
classIdx = targets.index(image_id) | ||
f.write((train_image_dir + "%s %d") % (fileName, classIdx) + "\n") | ||
f.close() | ||
# --------------------------------------------------- | ||
|
||
# --------------------------------------------------- | ||
# resize | ||
print "=====================================" | ||
print "start resize!" | ||
print "=====================================" | ||
for i, imgpath in enumerate(os.listdir(train_image_dir)): | ||
if not imgpath.endswith(".jpg"): | ||
print "not image file!:%s" % imgpath | ||
continue | ||
print i | ||
img = Image.open(train_image_dir + imgpath) | ||
size = min(img.size) # img.size は、(width, height)というタプルを返す。PILのバージョンによっては、img.width, img.heightも使えるが。 | ||
start_x = img.size[0] / 2 - size / 2 | ||
start_y = img.size[1] / 2 - size / 2 | ||
box = (start_x, start_y, start_x + size, start_y + size) # box is a 4-tuple defining the left, upper, right, and lower pixel coordinate. | ||
img2 = img.crop(box).resize((256, 256), Image.ANTIALIAS) | ||
img2.save(train_image_dir + imgpath, 'JPEG') | ||
|
||
# ----------------------------------------------------- | ||
|
||
parser = argparse.ArgumentParser(description='Compute images mean array') | ||
parser.add_argument('--dataset', '-d', help='Path to training image-label list file', default='train.txt') | ||
parser.add_argument('--root', '-r', default='.', | ||
help='Root directory path of image files') | ||
parser.add_argument('--output', '-o', default='mean.npy', | ||
help='path to output mean array') | ||
args = parser.parse_args() | ||
|
||
sum_image = None | ||
count = 0 | ||
for i, line in enumerate(open(args.dataset)): | ||
filepath = os.path.join(args.root, line.strip().split()[0]) | ||
if not filepath.endswith(".jpg"): | ||
print "not jpg file. skip..." | ||
continue | ||
try: | ||
image = numpy.asarray(Image.open(filepath)).transpose(2, 0, 1) | ||
except: | ||
print "idx:%d" % i | ||
print "file:%s" % filepath | ||
print sys.exc_info()[0] | ||
continue | ||
|
||
if sum_image is None: | ||
sum_image = numpy.ndarray(image.shape, dtype=numpy.float32) | ||
sum_image[:] = image | ||
else: | ||
# ここで、operands could not be broadcast together with shapes エラーになった | ||
# broadcast ... サイズ/形状の異なる配列同士の演算のこと | ||
sum_image += image | ||
count += 1 | ||
# sys.stderr.write('\r{}'.format(count)) | ||
# sys.stderr.flush() | ||
|
||
sys.stderr.write('\n') | ||
|
||
mean = sum_image / count | ||
pickle.dump(mean, open(args.output, 'wb'), -1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def getTargets(): | ||
return [ | ||
# "n07722217", # onion | ||
# "n07730207", # carrot | ||
"n07735510", # pumpkin | ||
"n07734017", # tomato | ||
"n07747607", # orange | ||
"n07739125", # apple | ||
] |