2022年度第7回卒研セミナー(2022/06/02)

関連サイトと資料

学習用データの画像サイズを必要なサイズに縮小する

画像の縮小

import cv2
import glob
import os
  
# データがあるディレクトリ
DATA_DIR = 'forest-path-movie-dataset-main/forest-path-movie-dataset-main/'
    

INPUT_DIR = DATA_DIR + 'scenes/scene-001'
OUTPUT_DIR = DATA_DIR + 'scenes2/scene-001'
  
if not os.path.exists(OUTPUT_DIR):
    os.makedirs(OUTPUT_DIR)
    

def createSavePath(input_path, output_dir):
    fn = os.path.basename(input_path)
    fn2 = os.path.splitext(fn)[0] + '.png'
    return output_dir + '/' + fn2
    

input_path = INPUT_DIR + '/000.jpg'
output_path = createSavePath(input_path, OUTPUT_DIR)
  
print(f'input_path = {input_path}')
print(f'output_path = {output_path}')
    

img = cv2.imread(input_path, cv2.IMREAD_COLOR)
print(img.shape)
  
img2 = cv2.resize(img, dsize=(224, 224))
print(img2.shape)
  
cv2.imwrite(output_path, img2) 
    

scenes/scene-001/000.jpg

scenes2/scene-001/000.png

os.remove(output_path)
    

for input_path in glob.glob(INPUT_DIR + '/*.jpg'):
    print(input_path)
    

for input_path in glob.glob(INPUT_DIR + '/*.jpg'):
    output_path = createSavePath(input_path, OUTPUT_DIR)
      
    img = cv2.imread(input_path, cv2.IMREAD_COLOR)
    img2 = cv2.resize(img, dsize=(224, 224))
      
    cv2.imwrite(output_path, img2)
    





for i in range(2, 100+1):
    INPUT_DIR = DATA_DIR + f'scenes/scene-{i:03}'
    OUTPUT_DIR = DATA_DIR + f'scenes2/scene-{i:03}'
    print(INPUT_DIR)
    print(OUTPUT_DIR)
    

for i in range(2, 100+1):
    print(i)
    INPUT_DIR = DATA_DIR + f'scenes/scene-{i:03}'
    OUTPUT_DIR = DATA_DIR + f'scenes2/scene-{i:03}'
    
    if not os.path.exists(OUTPUT_DIR):
        os.mkdir(OUTPUT_DIR)
    
    for input_path in glob.glob(INPUT_DIR + '/*.jpg'):
        output_path = createSavePath(input_path, OUTPUT_DIR)
        
        img = cv2.imread(input_path, cv2.IMREAD_COLOR)
        img2 = cv2.resize(img, dsize=(224, 224))
  
        cv2.imwrite(output_path, img2)
    

csvファイルの書き換え

import pandas as pd
  
df = pd.read_csv('sample.csv')
print(df['file'])
    

v = df['file'][0]
print(v)
  
v = v.replace('scenes', 'scenes2')
v = v.replace('.jpg', '.png')
print(v)
    

for i in range(len(df['file'])):
    v = df['file'][i]
    v = v.replace('scenes', 'scenes2')
    v = v.replace('.jpg', '.png')
    df['file'][i] = v
  
print(df['file'])
    

df.to_csv('sample2.csv', index=False)
    

INPUT_DIR = 'forest-path-movie-dataset-main/forest-path-movie-dataset-main/'
df = pd.read_csv(INPUT_DIR+'all_file.csv')
  
for i in range(len(df['file'])):
    v = df['file'][i]
    v = v.replace('scenes', 'scenes2')
    v = v.replace('.jpg', '.png')
    df['file'][i] = v
  
df.to_csv(INPUT_DIR+'all_file2.csv', index=False)