Skip to content

Instantly share code, notes, and snippets.

@yinguobing
Created July 20, 2017 02:15
Show Gist options
  • Save yinguobing/421f66147c1d8a51ef107cf263afa527 to your computer and use it in GitHub Desktop.
Save yinguobing/421f66147c1d8a51ef107cf263afa527 to your computer and use it in GitHub Desktop.
这段代码用来对Cambridge Hand Gesture Data set进行整理,将所有的图像重命名并按照类别放置在预定的文件夹中,以便生成TensorFlow需要的TFRecord文件。
import os
import numpy
from random import randint
from PIL import Image
# Get all the image files' path.
homePath = "./images"
# Define locations for different type of data.
for dir_parent in ["train", "test"]:
os.mkdir(dir_parent)
for subdir in ["rock", "paper", "scissor", "others"]:
os.mkdir(dir_parent + "/" + subdir)
# Define code list for classification.
codePaper = ["0000", "0001", "0003", "0004", "0005"]
codeRock = ["0002"]
codeScissor = ["0006", "0007", "0008"]
# Define counter for images
counter = numpy.zeros([2, 4], int)
count = 0
# Step1, rename and split images according to gesture type.
for filePath, _, fileNames in os.walk(homePath):
for fileName in fileNames:
# Only process jpeg files
if ".jpg" in fileName or ".jpeg" in fileName:
# Use directory names as image ID, so we could tell which gesture it is from the name.
train_or_test = ""
if randint(1, 10) <= 8:
train_or_test = "train"
else:
train_or_test = "test"
typeCode = filePath[-9:-5]
category = ""
if typeCode in codePaper:
category = "paper"
elif typeCode in codeScissor:
category = "scissor"
elif typeCode in codeRock:
if int(fileName[-8:-4]) >= 30:
category = "rock"
else:
category = "others"
newPath = os.path.join(os.getcwd(), train_or_test, category)
# Resize and move the file now
oldFile = os.path.join(filePath, fileName)
newFileName = filePath[-14:-10] + "-" + filePath[-9:-5] + "-" + filePath[-4:] + "-" + fileName
newFile = os.path.join(newPath, newFileName)
im = Image.open(oldFile)
im = im.resize((32, 32), Image.NEAREST)
im.save(newFile)
if train_or_test == "train":
if category == "rock":
counter[0, 0] += 1
elif category == "paper":
counter[0, 1] += 1
elif category == "scissor":
counter[0, 2] += 1
else:
counter[0, 3] += 1
else:
if category == "rock":
counter[1, 0] += 1
elif category == "paper":
counter[1, 1] += 1
elif category == "scissor":
counter[1, 2] += 1
else:
counter[1, 3] += 1
count += 1
if count % 10 == 0:
print("Images processed: %d" % count)
else:
print("Only image files needed, file " + fileName + " ignored.")
# All done, summary up.
print("Total images: %d" % count)
print("Categories: ", counter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment