Commit 48cd6ed71771db6a09d8a919d29fe1caa4203b5a
1 parent
b797194f
Exists in
master
and in
1 other branch
staged.
Showing
1 changed file
with
21 additions
and
7 deletions
Show diff stats
mdata/crop.py
| ... | ... | @@ -5,6 +5,10 @@ from PIL import Image |
| 5 | 5 | from ..common import * |
| 6 | 6 | import random |
| 7 | 7 | |
| 8 | +import cv2 | |
| 9 | +import numpy as np | |
| 10 | + | |
| 11 | + | |
| 8 | 12 | base_dir = '/data/hadoop/ImageNet/ILSVRC/ILSVRC2013_DET_val/' |
| 9 | 13 | category = 'Test' |
| 10 | 14 | |
| ... | ... | @@ -14,14 +18,24 @@ def crop_Test(): |
| 14 | 18 | for name in files: |
| 15 | 19 | image = os.path.join(path, name) |
| 16 | 20 | print image |
| 21 | + | |
| 22 | + # try: | |
| 23 | + # im = Image.open(image) | |
| 24 | + # w, h = im.size | |
| 25 | + # if w < 300 or h < 300: | |
| 26 | + # continue | |
| 27 | + # left, upper = random.randint(0, w - 300), random.randint(0, h - 300) | |
| 28 | + # im = im.crop((left, upper, left + 300, upper + 300)) | |
| 29 | + # im.save(os.path.join(base_dir, category + '_crop_pil', name)) | |
| 30 | + # except: | |
| 31 | + # pass | |
| 32 | + | |
| 17 | 33 | try: |
| 18 | - im = Image.open(image) | |
| 19 | - w, h = im.size | |
| 20 | - if w < 300 or h < 300: | |
| 21 | - continue | |
| 22 | - left, upper = random.randint(0, w - 300), random.randint(0, h - 300) | |
| 23 | - im = im.crop((left, upper, left + 300, upper + 300)) | |
| 24 | - im.save(os.path.join(base_dir, category + '_crop', name)) | |
| 34 | + img = cv2.imread(image, cv2.CV_LOAD_IMAGE_UNCHANGED) | |
| 35 | + h, w = img.shape[:2] | |
| 36 | + left, upper = np.random.randint(w - 300), np.random.randint(h - 300) | |
| 37 | + img_crop = img[upper:upper + 300, left:left + 300] | |
| 38 | + cv2.imwrite(os.path.join(base_dir, category + '_crop_cv', name)) | |
| 25 | 39 | except: |
| 26 | 40 | pass |
| 27 | 41 | ... | ... |