Skip to content

Commit

Permalink
Merge pull request #27 from naseemap47/kitti
Browse files Browse the repository at this point in the history
Kitti
  • Loading branch information
naseemap47 authored Sep 14, 2023
2 parents 30de6d7 + 293bc5c commit 583f989
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,21 @@ to convert mixed images & labels into respective directory of images and labels
**Example:**
```
python3 tools/create_yolo.py -i path_to/data Dir
```

### 9. yolo_to_kitti.py:
to convert YOLO annotations (.txt) to KITTI format

<details>
<summary>Args</summary>

`-i`, `--img` : path to image file <br>
`-t`, `--txt` : path to txt file <br>
`-c`, `--classes` : path to classes.txt

</details>

**Example:**
```
python3 tools/yolo_to_kitti.py -i path_to/image -t path_to/txt -c path_to_classes.txt
```
72 changes: 72 additions & 0 deletions tools/yolo_to_kitti.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import argparse
import os
import cv2
import glob


ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", type=str, required=True,
help="path to image/dir")
ap.add_argument("-t", "--txt", type=str, required=True,
help="path to txt/dir")
ap.add_argument("-c", "--classes", type=str, required=True,
help="path to classes.txt")

args = vars(ap.parse_args())
path_to_img = args["image"]
path_to_txt = args['txt']
path_to_class = args['classes']

os.makedirs("KITTI_"+path_to_txt, exist_ok=True)
txt_list = sorted(glob.glob(f'{path_to_txt}/*.txt'))
img_full_list = glob.glob(f'{path_to_img}/*.jpeg') + \
glob.glob(f'{path_to_img}/*.jpg') + \
glob.glob(f'{path_to_img}/*.png')

img_list = sorted(img_full_list)
class_names = open(f'{path_to_class}', 'r+').read().splitlines()
for txt, img in zip(txt_list, img_list):
img_file = cv2.imread(img)
image_height, image_width, _ = img_file.shape

with open(txt, 'r') as fh:
data=fh.readlines()

for cc, lines in enumerate(data):
lines = lines.replace('\n', '').split(" ")
lines[0] = int(lines[0])
lines[1] = float(lines[1])*image_width
lines[2] = float(lines[2])*image_height
lines[3] = float(lines[3])*image_width
lines[4] = float(lines[4])*image_height

lines[0] = class_names[lines[0]]
n_line = [0] * 15
n_line[0] = lines[0]
n_line[4] = int(lines[1]-lines[3]/2)
n_line[5] = int(lines[2]-lines[4]/2)
n_line[6] = int(lines[1]+lines[3]/2)
n_line[7] = int(lines[2]+lines[4]/2)

for c, n in enumerate(n_line):
if not c == 0:
if n < 0:
n_line[c] = 1
if n > 256:
n_line[c] = 255

for cc2, char in enumerate(n_line):
if cc2 == 1:
n_line[cc2] = float(0)
if cc2 > 7:
n_line[cc2] = 1

str1 = ' '.join(str(n_line)).replace(' ', '').replace(',', ' ').replace('[', '').replace("]","").replace("'","").replace('"',"")
data[cc] = str1+"\n"

strf = " ".join(str(x) for x in data)
strf = strf.replace("\n ","\n")
file = open("KITTI_"+txt, "w")
file.write(strf)
file.close()
print(f"[INFO] Completed {txt}")

0 comments on commit 583f989

Please sign in to comment.