Skip to content

Commit

Permalink
Update : convert coco data format to FLIR, KAIST dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
maengjemo committed Jan 23, 2025
1 parent a41c32c commit 5d08c18
Show file tree
Hide file tree
Showing 42 changed files with 2,268 additions and 71 deletions.
100 changes: 100 additions & 0 deletions MISC/_check_flir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Description: Check the FLIR dataset with visualize

from pycocotools.coco import COCO
import cv2
import os
import numpy as np


COLOR_PALETTE = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0], [255, 0, 255], [0, 255, 255], [255, 255, 255]])

def get_category_names(coco):
"""Get category names from COCO annotation file"""
cats = coco.loadCats(coco.getCatIds())
return {cat['id']: cat['name'] for cat in cats}

def draw_bbox(image, bbox, category_id, color, category_names):
x, y, w, h = [int(b) for b in bbox]
cv2.rectangle(image, (x, y), (x+w, y+h), color.tolist(), 2)
# Add category label with name
label = f"{category_names.get(category_id, f'unknown-{category_id}')}"
# Get text size for better positioning
(text_width, text_height), baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)
# Draw background rectangle for text
cv2.rectangle(image, (x, y-text_height-baseline-5), (x+text_width, y), color.tolist(), -1)
# Draw text
cv2.putText(image, label, (x, y-baseline-3), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)
return image


def visualize(coco_path, root, flag):
rgb_root = os.path.join(root, flag + '_RGB')
thermal_root = os.path.join(root, flag + '_thermal')
coco = COCO(coco_path)

# Get category names from annotation file
category_names = get_category_names(coco)
print("Categories in dataset:", category_names)

img_ids = coco.getImgIds()
current_idx = 0

while True:
# Get current image info
img_info = coco.loadImgs(img_ids[current_idx])[0]
img_name = img_info['file_name_RGB']
thermal_name = img_info['file_name_IR']

# Load RGB and thermal images
rgb_path = os.path.join(rgb_root, img_name)
thermal_path = os.path.join(thermal_root, thermal_name)

rgb_img = cv2.imread(rgb_path)
thermal_img = cv2.imread(thermal_path)

if rgb_img is None or thermal_img is None:
print(f"Error loading images: {img_name}")
current_idx = (current_idx + 1) % len(img_ids)
continue

# Get annotations for current image
ann_ids = coco.getAnnIds(imgIds=img_info['id'])
anns = coco.loadAnns(ann_ids)

# Draw bounding boxes
rgb_vis = rgb_img.copy()
thermal_vis = thermal_img.copy()

for ann in anns:
bbox = ann['bbox']
category_id = ann['category_id']
color = COLOR_PALETTE[category_id % len(COLOR_PALETTE)]

rgb_vis = draw_bbox(rgb_vis, bbox, category_id, color, category_names)
thermal_vis = draw_bbox(thermal_vis, bbox, category_id, color, category_names)

# Display images side by side
combined_img = np.hstack((rgb_vis, thermal_vis))
cv2.imshow('FLIR Dataset Visualization (Press q to quit, <- or -> to navigate)', combined_img)

# Handle keyboard input
key = cv2.waitKey(0) & 0xFF
if key == ord('q'): # Quit
break
elif key == 83 or key == ord('d'): # Right arrow or 'd'
current_idx = (current_idx + 1) % len(img_ids)
elif key == 81 or key == ord('a'): # Left arrow or 'a'
current_idx = (current_idx - 1) % len(img_ids)

cv2.destroyAllWindows()


def main():
coco_path = '/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/FLIR-align/annotations/val.json'
root = '/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/FLIR-align/'
flag = os.path.basename(coco_path).split('.')[0]
visualize(coco_path, root, flag)


if __name__ == "__main__":
main()
Empty file added MISC/_check_kaist.py
Empty file.
101 changes: 101 additions & 0 deletions MISC/_check_llvip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Description: Check the FLIR dataset with visualize

from pycocotools.coco import COCO
import cv2
import os
import numpy as np

from check_flir import get_category_names, draw_bbox
COLOR_PALETTE = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0], [255, 0, 255], [0, 255, 255], [255, 255, 255]])

# def get_category_names(coco):
# """Get category names from COCO annotation file"""
# cats = coco.loadCats(coco.getCatIds())
# return {cat['id']: cat['name'] for cat in cats}

# def draw_bbox(image, bbox, category_id, color, category_names):
# x, y, w, h = [int(b) for b in bbox]
# cv2.rectangle(image, (x, y), (x+w, y+h), color.tolist(), 2)
# # Add category label with name
# label = f"{category_names.get(category_id, f'unknown-{category_id}')}"
# # Get text size for better positioning
# (text_width, text_height), baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)
# # Draw background rectangle for text
# cv2.rectangle(image, (x, y-text_height-baseline-5), (x+text_width, y), color.tolist(), -1)
# # Draw text
# cv2.putText(image, label, (x, y-baseline-3), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)
# return image


def visualize(coco_path, root, flag):
if flag == 'val':
flag = 'test'
rgb_root = os.path.join(root, 'visible')
thermal_root = os.path.join(root,'infrared')
coco = COCO(coco_path)

# Get category names from annotation file
category_names = get_category_names(coco)
print("Categories in dataset:", category_names)

img_ids = coco.getImgIds()
current_idx = 0

while True:
# Get current image info
img_info = coco.loadImgs(img_ids[current_idx])[0]
img_name = img_info['file_name']
thermal_name = img_info['file_name']

# Load RGB and thermal images
rgb_path = os.path.join(rgb_root,flag, img_name)
thermal_path = os.path.join(thermal_root,flag, thermal_name)

rgb_img = cv2.imread(rgb_path)
thermal_img = cv2.imread(thermal_path)

if rgb_img is None or thermal_img is None:
print(f"Error loading images: {img_name}")
current_idx = (current_idx + 1) % len(img_ids)
continue

# Get annotations for current image
ann_ids = coco.getAnnIds(imgIds=img_info['id'])
anns = coco.loadAnns(ann_ids)

# Draw bounding boxes
rgb_vis = rgb_img.copy()
thermal_vis = thermal_img.copy()

for ann in anns:
bbox = ann['bbox']
category_id = ann['category_id']
color = COLOR_PALETTE[category_id % len(COLOR_PALETTE)]
rgb_vis = draw_bbox(rgb_vis, bbox, category_id, color, category_names)
thermal_vis = draw_bbox(thermal_vis, bbox, category_id, color, category_names)

# Display images side by side
combined_img = np.hstack((rgb_vis, thermal_vis))
cv2.imshow('FLIR Dataset Visualization (Press q to quit, <- or -> to navigate)', combined_img)

# Handle keyboard input
key = cv2.waitKey(0) & 0xFF
if key == ord('q'): # Quit
break
elif key == 83 or key == ord('d'): # Right arrow or 'd'
current_idx = (current_idx + 1) % len(img_ids)
elif key == 81 or key == ord('a'): # Left arrow or 'a'
current_idx = (current_idx - 1) % len(img_ids)

cv2.destroyAllWindows()


def main():
coco_path = '/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/LLVIP/coco_annotations/train.json'
root = '/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/LLVIP/'
flag = os.path.basename(coco_path).split('.')[0]
visualize(coco_path, root, flag)


if __name__ == "__main__":
main()
54 changes: 54 additions & 0 deletions MISC/_process_flir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

from pycocotools.coco import COCO
# val_coco = COCO('/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/Annotations/FLIR_coco/val.json')
# num_images = len(coco_annotation.getImgIds())
# print("Number of images:", num_images)
# # %%
# train_coco = COCO('/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/Annotations/FLIR_coco/train.json')
# num_imgs = len(train_coco.getImgIds())
# print("Number of images:", num_imgs)
# # %%
# import glob

# search_root = '/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/FLIR-align'
# id = train_coco.getImgIds()[0]
# img = train_coco.loadImgs(id)[0]

# # %%
# rgb_name = img['file_name_RGB']
# thermal_name = img['file_name_IR']
# rgb_files = glob.glob(search_root + '/*/' + rgb_name)
# thermal_files = glob.glob(search_root + '/*/' + thermal_name)

# %%

# %%
import shutil
import glob
def copy_files(coco_path, isTrain):
search_root = '/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/FLIR-align'
coco = COCO(coco_path)
if isTrain:
rgb_root = '/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/FLIR-align/train_RGB'
thermal_root = '/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/FLIR-align/train_thermal'
else:
rgb_root = '/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/FLIR-align/val_RGB'
thermal_root = '/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/FLIR-align/val_thermal'

for id in coco.getImgIds():
img = coco.loadImgs(id)[0]
rgb_name = img['file_name_RGB']
thermal_name = img['file_name_IR']
rgb_file = glob.glob(search_root + '/JPEGImages/' + rgb_name)[0]
thermal_file = glob.glob(search_root + '/JPEGImages/' + thermal_name)[0]
rgb_filename = rgb_file.split('/')[-1]
thermal_filename = thermal_file.split('/')[-1]

shutil.copy2(rgb_file, rgb_root + '/' + rgb_filename)
shutil.copy2(thermal_file, thermal_root + '/' + thermal_filename)
print(f"Copy {rgb_file} to {rgb_root + '/' + rgb_filename} | {thermal_file} to {thermal_root + '/' + thermal_filename}")



# copy_files('/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/Annotations/FLIR_coco/val.json', False)
copy_files('/media/ailab/HDD1/Workspace/dset/Drone-Detection-Benchmark/Source/Annotations/FLIR_coco/train.json', True)
Loading

0 comments on commit 5d08c18

Please sign in to comment.