aist_hdf5 / fix_image.py
dhlee3000's picture
Upload root file fix_image.py
ea38e27 verified
import os
import cv2
def center_crop(img):
h, w = img.shape[:2]
size = min(h, w) # ์ •์‚ฌ๊ฐํ˜• ๋ณ€์˜ ๊ธธ์ด
start_x = (w - size) // 2
start_y = (h - size) // 2
return img[start_y:start_y+size, start_x:start_x+size]
def process_folder(base_path, sub_folder, crop_folder):
input_dir = os.path.join(base_path, sub_folder)
output_dir = os.path.join(base_path, crop_folder)
if not os.path.exists(input_dir):
print(f"ํด๋” ์—†์Œ: {input_dir}")
return
os.makedirs(output_dir, exist_ok=True)
for fname in os.listdir(input_dir):
if fname.lower().endswith(('.jpg', '.png', '.jpeg')):
fpath = os.path.join(input_dir, fname)
img = cv2.imread(fpath)
if img is None:
print(f"์ด๋ฏธ์ง€ ์ฝ๊ธฐ ์‹คํŒจ: {fpath}")
continue
cropped = center_crop(img)
out_path = os.path.join(output_dir, fname)
cv2.imwrite(out_path, cropped)
def main():
txt_path = "/root/aist_hdf5/full_list.txt"
with open(txt_path, "r") as f:
folders = [line.strip() for line in f.readlines() if line.strip()]
for folder in folders:
base_path = os.path.join("/root/aist_hdf5/rec", folder)
process_folder(base_path, "faces", "cropped_faces")
process_folder(base_path, "images", "cropped_images")
print(f"์ฒ˜๋ฆฌ ์™„๋ฃŒ: {folder}")
if __name__ == "__main__":
main()