Skip to content

Commit

Permalink
Add missing docstrings (#87)
Browse files Browse the repository at this point in the history
* Add docstrings

* Auto-format by https://ultralytics.com/actions

---------

Co-authored-by: UltralyticsAssistant <[email protected]>
  • Loading branch information
glenn-jocher and UltralyticsAssistant authored Apr 28, 2024
1 parent 5013314 commit 5d6b35f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions general_json2yolo.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import contextlib
import json
from collections import defaultdict

import cv2
import pandas as pd
from PIL import Image
from collections import defaultdict

from utils import *

Expand Down Expand Up @@ -138,7 +138,7 @@ def convert_vott_json(name, files, img_path):

# Convert ath JSON file into YOLO-format labels --------------------------------
def convert_ath_json(json_dir): # dir contains json annotations and images
# Create folders
"""Converts ath JSON annotations to YOLO-format labels, resizes images, and organizes data for training."""
dir = make_dirs() # output directory

jsons = []
Expand Down
17 changes: 12 additions & 5 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def exif_size(img):


def split_rows_simple(file="../data/sm4/out.txt"): # from utils import *; split_rows_simple()
# splits one textfile into 3 smaller ones based upon train, test, val ratios
"""Splits a text file into train, test, and val files based on specified ratios; expects a file path as input."""
with open(file) as f:
lines = f.readlines()

Expand All @@ -46,6 +46,7 @@ def split_rows_simple(file="../data/sm4/out.txt"): # from utils import *; split


def split_files(out_path, file_name, prefix_path=""): # split training data
"""Splits file names into separate train, test, and val datasets and writes them to prefixed paths."""
file_name = list(filter(lambda x: len(x) > 0, file_name))
file_name = sorted(file_name)
i, j, k = split_indices(file_name, train=0.9, test=0.1, validate=0.0)
Expand All @@ -58,6 +59,7 @@ def split_files(out_path, file_name, prefix_path=""): # split training data


def split_indices(x, train=0.9, test=0.1, validate=0.0, shuffle=True): # split training data
"""Splits array indices for train, test, and validate datasets according to specified ratios."""
n = len(x)
v = np.arange(n)
if shuffle:
Expand Down Expand Up @@ -95,15 +97,17 @@ def write_data_data(fname="data.data", nc=80):


def image_folder2file(folder="images/"): # from utils import *; image_folder2file()
# write a txt file listing all imaged in folder
"""Generates a txt file listing all images in a specified folder; usage: `image_folder2file('path/to/folder/')`."""
s = glob.glob(f"{folder}*.*")
with open(f"{folder[:-1]}.txt", "w") as file:
for l in s:
file.write(l + "\n") # write image list


def add_coco_background(path="../data/sm4/", n=1000): # from utils import *; add_coco_background()
# add coco background to sm4 in outb.txt
"""Adds COCO dataset background images to a specified folder and lists them in outb.txt; usage:
`add_coco_background('path/', 1000)`.
"""
p = f"{path}background"
if os.path.exists(p):
shutil.rmtree(p) # delete output folder
Expand All @@ -123,12 +127,14 @@ def add_coco_background(path="../data/sm4/", n=1000): # from utils import *; ad


def create_single_class_dataset(path="../data/sm3"): # from utils import *; create_single_class_dataset('../data/sm3/')
# creates a single-class version of an existing dataset
"""Creates a single-class version of an existing dataset in the specified path."""
os.system(f"mkdir {path}_1cls")


def flatten_recursive_folders(path="../../Downloads/data/sm4/"): # from utils import *; flatten_recursive_folders()
# flattens nested folders in path/images and path/JSON into single folders
"""Flattens nested folders in 'path/images' and 'path/json' into single 'images_flat' and 'json_flat'
directories.
"""
idir, jdir = f"{path}images/", f"{path}json/"
nidir, njdir = Path(f"{path}images_flat/"), Path(f"{path}json_flat/")
n = 0
Expand Down Expand Up @@ -160,6 +166,7 @@ def flatten_recursive_folders(path="../../Downloads/data/sm4/"): # from utils i


def coco91_to_coco80_class(): # converts 80-index (val2014) to 91-index (paper)
"""Converts COCO 91-class index (paper) to 80-class index (2014 challenge)."""
return [
0,
1,
Expand Down

0 comments on commit 5d6b35f

Please sign in to comment.