프로젝트/컴퓨터비전

#04. [알약검출] EDA

upstack 2026. 10. 6. 18:17

01. 현재 EDA 실행 경로 확인

import os
print("현재 실행 경로:", os.getcwd())

02. 최상위 탐색 폴더 지정

# 최상위 탐색 폴더 지정
base_dir = Path("../datasets/sprint_ai_project1_data/train_annotations")
json_paths = list(base_dir.rglob("*.json"))
print(f"총 {len(json_paths)}개의 JSON 파일을 찾았습니다.")

# 데이터 로드 예시
all_data = []
for path in json_paths: # 경로를 10개만 테스트로 불러오기: json_paths[:10]
    with open(path, 'r', encoding='utf-8') as f:
        data = json.load(f)
        all_data.append(data)

03. 데이터 갯수 확인하기 

def count_files(directory: Path, ext_pattern:str, file_desc: str):
    if not directory.exists():
        print(f"❌ 폴더를 찾을 수 없습니다: {directory.resolve()}")
        return []
    file_paths = list(directory.rglob(ext_pattern))
    print(f"[{directory.name}] 총 {len(file_paths):,}개의 {file_desc}파일을 찾았습니다.")
    return file_paths

# 최상위 탐색 폴더 지정
base_dir = Path("../datasets/sprint_ai_project1_data")
train_anno_dir = base_dir / "train_annotations"
train_img_dir = base_dir / "train_images"
test_img_dir = base_dir / "test_images"

# 함수 호출
json_paths = count_files(train_anno_dir, "*.json", "JSON")
img_paths = count_files(train_img_dir, "*.png", "이미지(PNG)")
test_img_paths = count_files(test_img_dir, "*.png", "이미지(PNG)")

 

04. 메타데이터(JSON) 분석

# 파일명(확장자 제외)만 추출하여 Set(집합)으로 변환
json_stems = set(p.stem for p in json_paths)
img_stems = set(p.stem for p in img_paths)

# 교집합 및 누락 데이터 확인
common_files = json_stems.intersection(img_stems)
only_in_json = json_stems - img_stems
only_in_img = img_stems - json_stems

print(f"매칭된 데이터 쌍: {len(common_files):,}개")
print(f"이미지가 없는 JSON: {len(only_in_json):,}개")
print(f"JSON이 없는 이미지: {len(only_in_img):,}개")

 

  • set : 집합에 관련된 것을 처리하기 위한 자료형
    • 형태 : set({1, 2, 3})
    • 합집합, 차집합, 교집합 등으로 활용함
  • stem : pathlib 모듈에서 파일 경로를 다룰 때 "확장자를 제외한 순수 파일명"을 추출하는 속성(Attribute)
    • 예시 
      • p.stem ➡️ 'K-001900_70_000' (확장자를 뺀 순수 파일명)
      • p.name ➡️ 'K-001900_70_000.png' (파일명 + 확장자 전체)
      • p.suffix ➡️ '.png' (확장자만)