boostcampaitech3 / level2-object-detection-level2-cv-18

level2-object-detection-level2-cv-18 created by GitHub Classroom
1 stars 5 forks source link

Bounding Box 확인하기 #6

Open YJ0522771 opened 2 years ago

YJ0522771 commented 2 years ago

이미지에 bounding box를 그려서 예측 결과를 눈으로 확인해 볼 수 있도록 만들어 보았습니다.

코드

import numpy as np
import pandas as pd
import cv2
import os
import random
import matplotlib.pyplot as plt

# class 별 bbox 색
colors = [
    (255, 0, 0), 
    (0, 255, 0),
    (0, 0, 255),
    (127, 127, 0),
    (127, 0, 127),
    (0, 127, 127),
    (200, 200, 200),
    (50, 150, 200),
    (200, 155, 50),
    (130, 198, 20),
]
labels = {0: 'General trash', 1: 'Paper', 2: 'Paper pack', 3: 'Metal', 4: 'Glass', 5: 'Plastic', 6: 'Styrofoam', 7: 'Plastic bag', 8: 'Battery', 9: 'Clothing'}

# 이미지 경로
img_dir = './detection/dataset'
# output 파일 경로
output_dir = './detection/baseline/faster_rcnn/faster_rcnn_torchvision_epochs_30.csv'

test_result = pd.read_csv(output_dir)

file_names = test_result['image_id'].values.tolist()
bboxes = test_result['PredictionString'].values.tolist()

idx = random.choice(range(len(file_names)))
file_name = file_names[idx]
bbox = bboxes[idx].split()

image = cv2.imread(os.path.join(img_dir, file_name)).astype(np.uint8)
for i in range(0, len(bbox), 6):
    label = int(bbox[i])
    x_min = int(bbox[i + 2].split('.')[0])
    y_min = int(bbox[i + 3].split('.')[0])
    x_max = int(bbox[i + 4].split('.')[0])
    y_max = int(bbox[i + 5].split('.')[0])

    # bounding box 그리기
    image = cv2.rectangle(image, pt1=(x_min, y_min), pt2=(x_max, y_max), color=colors[label], thickness=5)
    # label text 넣기
    cv2.putText(image, labels[label], (x_min, y_min - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, colors[label], 3)

plt.figure(figsize=(10,10))
plt.imshow(image)

예시

image

ed-kyu commented 2 years ago

감사합니다!

kimkihoon0515 commented 2 years ago

EDA에 좋은 자료네요 감사합니다.

ysw2946 commented 2 years ago

감사합니다!