YOLO 실습 코드 : visualize_yolo_results.py > 자료실

본문 바로가기

YOLO 실습 코드 : visualize_yolo_results.py

필기자
2025-06-26 18:32 9 0

본문


import os
import cv2

# 불러올 이미지,라벨 경로 설정
image_dir = 'results/images'
label_dir = 'results/labels'

# 결과 이미지 리스트 정렬
image_files = sorted([f for f in os.listdir(image_dir) if f.endswith('.jpg')])
label_files = sorted([f for f in os.listdir(label_dir) if f.endswith('.txt')])

for img_name, label_name in zip(image_files, label_files):
    img_path = os.path.join(image_dir, img_name)
    label_path = os.path.join(label_dir, label_name)

    img = cv2.imread(img_path) # 이미지 불러오기
    H, W = img.shape[:2] # 이미지 높이, 넓이 추출

    with open(label_path, 'r') as f:
        lines = f.read().strip().split('\n') # 라벨.txt 에서 객체 좌표값 불러오기

    for line in lines: # 좌표값 픽셀 좌표로 환산
        if not line:
            continue
        class_id, xc, yc, w, h = map(float, line.split())
        x1 = int((xc - w/2) * W)
        y1 = int((yc - h/2) * H)
        x2 = int((xc + w/2) * W)
        y2 = int((yc + h/2) * H)

        # 클래스 번호(text), 바운딩박스(좌표값) 원본 이미지에 삽입
        color = (0, 255, 0)
        cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
        cv2.putText(img, f'Class {int(class_id)}', (x1, y1 - 5),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)

    cv2.imshow('YOLO Annotation', img) # 결과 출력

    key = cv2.waitKey(0)
    if key == 27:  # 프로그램 종료 : ESC
        break

cv2.destroyAllWindows()

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색