---
# System prepended metadata

title: Convert dataset
tags: ['@uittogether', convert]

---

Hồ Chí Minh, 03-11-2023
[Võ Duy Nguyên](https://nguyenvd-uit.github.io/), [UIT-Together Research Group](https://uit-together.github.io/) 

 Convert dataset
===
<!-- ![downloads](https://img.shields.io/github/downloads/atom/atom/total.svg)
![build](https://img.shields.io/appveyor/ci/:user/:repo.svg)
![chat](https://img.shields.io/discord/:serverId.svg) -->

##   Mục lục

[TOC]


 Định dạng Pascal VOC
---
```
 [x1, y1, x2, y2]
```
*x1* và *y1* là tọa độ của góc trên bên trái và *x2* và *y2* là tọa độ của góc dưới bên phải của hộp giới hạn. Xem thêm ở đây: https://host.robots.ox.ac.uk/pascal/VOC/


 Định dạng MS-COCO
---
```
[x,y,width,height]
```
Các tọa độ *(x, y)* là góc trái trên cùng, với *width* và *height* của hộp giới hạn. Xem thêm ở đây: https://cocodataset.org/#format-results

 Định dạng YOLO
---
```
[x_center, y_center, width, height]
```
*x_center* và *y_center* là tọa độ chuẩn hóa của trung tâm của hộp giới hạn. 
Chiều rộng (*width*) và chiều cao (*height*) là độ dài được chuẩn hóa. 
Để chuyển đổi sang giữa YOLO phải có kích thước của hình ảnh để tính toán việc chuẩn hóa. Xem thêm ở đây: https://pjreddie.com/darknet/yolo/

##  Trực quan hoá

![bbox_example.jpeg](https://hackmd.io/_uploads/HJxJiIGmT.jpg)
> Nguồn https://albumentations.ai/docs/getting_started/bounding_boxes_augmentation/

```
PASCAL VOC:  [x1, y1, x2, y2]
             [98, 345, 420, 462]
             
MS-COCO   : [x,y,width,height]
            [98, 345, 322, 117].
            
YOLO      : [x_center, y_center, width, height]
            [0.4046875, 0.840625, 0.503125, 0.24375].
cách tính [((420 + 98) / 2) / 640, ((462 + 345) / 2) / 480, 322 / 640, 117 / 480]
```

Chuyển từ định dạng COCO sang Pascal VOC
---
```
def coco_to_pascal_voc(x1, y1, w, h):
    return [x1,y1, x1 + w, y1 + h]
```

Chuyển từ định dạng MS-COCO sang YOLO
---
```
def coco_to_yolo(x1, y1, w, h, image_w, image_h):
    return [((2*x1 + w)/(2*image_w)) , ((2*y1 + h)/(2*image_h)), w/image_w, h/image_h]
```

Chuyển từ định dạng Pascal VOC sang MS-COCO
---
```
def pascal_voc_to_coco(x1, y1, x2, y2):
    return [x1,y1, x2 - x1, y2 - y1]
```

Chuyển từ định dạng Pascal VOC sang YOLO
---
```
def pascal_voc_to_yolo(x1, y1, x2, y2, image_w, image_h):
    return [((x2 + x1)/(2*image_w)), ((y2 + y1)/(2*image_h)), (x2 - x1)/image_w, (y2 - y1)/image_h]

```
Chuyển từ định dạng YOLO sang MS-COCO
---
```
def yolo_to_coco(x_center, y_center, w, h,  image_w, image_h):
    w = w * image_w
    h = h * image_h
    x1 = ((2 * x_center * image_w) - w)/2
    y1 = ((2 * y_center * image_h) - h)/2
    return [x1, y1, w, h]
```

 Chuyển từ định dạng YOLO sang Pascal
---
```
def yolo_to_pascal_voc(x_center, y_center, w, h,  image_w, image_h):
    w = w * image_w
    h = h * image_h
    x1 = ((2 * x_center * image_w) - w)/2
    y1 = ((2 * y_center * image_h) - h)/2
    x2 = x1 + w
    y2 = y1 + h
    return [x1, y1, x2, y2]
```

Github:
https://github.com/UIT-Together-Research-Group/OD_ConvertDataset

Tài liệu hướng dẫn dùng cho nhóm [UIT-Together Research Group](https://uit-together.github.io/) 
