Compare commits

...

36 Commits
v0.0.9 ... main

Author SHA1 Message Date
Wang Xin 04f7d2d259
Create python-publish.yml (#54) 2024-05-17 20:22:09 +08:00
dependabot[bot] b151acd61c
Update pillow requirement from <10.3,>=9.2 to >=9.2,<10.4 (#52)
Updates the requirements on [pillow](https://github.com/python-pillow/Pillow) to permit the latest version.
- [Release notes](https://github.com/python-pillow/Pillow/releases)
- [Changelog](https://github.com/python-pillow/Pillow/blob/main/CHANGES.rst)
- [Commits](https://github.com/python-pillow/Pillow/compare/9.2.0...10.3.0)

---
updated-dependencies:
- dependency-name: pillow
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-04-08 18:45:16 +08:00
Wang Xin 9f21f0adc4
update version (#51) 2024-03-25 20:59:57 +08:00
Wang Xin 513dbc5609
Fix parsing error when `imageData` is empty (#50) 2024-03-24 14:59:09 +08:00
Wang Xin 94382c05f6
add humanized tip (#47) 2024-03-09 21:06:02 +08:00
Wang Xin 1ceae85190
Update README.md (#46) 2024-03-04 21:33:12 +08:00
Wang Xin 6bc4e27231
replace pool.map with pool.imap_unordered (#45) 2024-03-01 16:33:07 +08:00
Wang Xin 1e697c78e9
improve convertion speed (#44) 2024-02-29 20:28:53 +08:00
Wang Xin f777359f33
replace tqdm with rich (#43) 2024-02-28 23:53:11 +08:00
Wang Xin 94c14a6430
accelerated conversion speed (#42) 2024-02-27 18:28:36 +08:00
Wang Xin dc8d885342 update readme 2024-02-18 09:32:25 +08:00
Wang Xin efca771a3f
fix error output caused by circle shape (#40)
* show program name and version info within argparse

* lint code

* fix error output caused by circle shape
2024-02-01 16:56:40 +08:00
dependabot[bot] 7946c199cf
Update pillow requirement from <10.2,>=9.2 to >=9.2,<10.3 (#39)
Updates the requirements on [pillow](https://github.com/python-pillow/Pillow) to permit the latest version.
- [Release notes](https://github.com/python-pillow/Pillow/releases)
- [Changelog](https://github.com/python-pillow/Pillow/blob/main/CHANGES.rst)
- [Commits](https://github.com/python-pillow/Pillow/compare/9.2.0...10.2.0)

---
updated-dependencies:
- dependency-name: pillow
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-08 18:41:08 +08:00
Wang Xin cc4171e182
support recursive search dirs (#38)
* support recursive search dirs

* fix pylint error
2023-12-19 15:22:47 +08:00
dependabot[bot] dba6184a50
Update pillow requirement from <10.1,>=9.2 to >=9.2,<10.2 (#37)
Updates the requirements on [pillow](https://github.com/python-pillow/Pillow) to permit the latest version.
- [Release notes](https://github.com/python-pillow/Pillow/releases)
- [Changelog](https://github.com/python-pillow/Pillow/blob/main/CHANGES.rst)
- [Commits](https://github.com/python-pillow/Pillow/compare/9.2.0...10.1.0)

---
updated-dependencies:
- dependency-name: pillow
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-16 18:56:48 +08:00
Wang Xin acbf514894 update README 2023-10-05 23:11:21 +08:00
Wang Xin 054cfeb3ec fix pylint ci error 2023-10-04 16:40:06 +08:00
Wang Xin 9f2f443dab remove scikit-learn dependence 2023-10-04 16:28:20 +08:00
dependabot[bot] 10dacce294
Update numpy requirement from <1.26.0,>=1.23.1 to >=1.23.1,<1.27.0 (#33)
Updates the requirements on [numpy](https://github.com/numpy/numpy) to permit the latest version.
- [Release notes](https://github.com/numpy/numpy/releases)
- [Changelog](https://github.com/numpy/numpy/blob/main/doc/RELEASE_WALKTHROUGH.rst)
- [Commits](https://github.com/numpy/numpy/compare/v1.23.1...v1.26.0)

---
updated-dependencies:
- dependency-name: numpy
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-09-18 18:03:59 +08:00
dependabot[bot] 1acb4112bf
Update scikit-learn requirement from <1.3.0,>=1.1.1 to >=1.1.1,<1.4.0 (#32)
Updates the requirements on [scikit-learn](https://github.com/scikit-learn/scikit-learn) to permit the latest version.
- [Release notes](https://github.com/scikit-learn/scikit-learn/releases)
- [Commits](https://github.com/scikit-learn/scikit-learn/compare/1.1.1...1.3.0)

---
updated-dependencies:
- dependency-name: scikit-learn
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-03 18:23:17 +08:00
dependabot[bot] e32a34253f
Update pillow requirement from <9.6,>=9.2 to >=9.2,<10.1 (#31)
Updates the requirements on [pillow](https://github.com/python-pillow/Pillow) to permit the latest version.
- [Release notes](https://github.com/python-pillow/Pillow/releases)
- [Changelog](https://github.com/python-pillow/Pillow/blob/main/CHANGES.rst)
- [Commits](https://github.com/python-pillow/Pillow/compare/9.2.0...10.0.0)

---
updated-dependencies:
- dependency-name: pillow
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-03 18:19:19 +08:00
dependabot[bot] aafd10e9e6
Update numpy requirement from <1.25.0,>=1.23.1 to >=1.23.1,<1.26.0 (#30)
Updates the requirements on [numpy](https://github.com/numpy/numpy) to permit the latest version.
- [Release notes](https://github.com/numpy/numpy/releases)
- [Changelog](https://github.com/numpy/numpy/blob/main/doc/RELEASE_WALKTHROUGH.rst)
- [Commits](https://github.com/numpy/numpy/compare/v1.23.1...v1.25.0)

---
updated-dependencies:
- dependency-name: numpy
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-06-19 18:09:43 +08:00
Wang Xin 2fda40879b
cleanup code (#29)
* cleanup code

* fixed
2023-06-08 22:19:06 +08:00
Wang Xin aefad9e1ab
cleanup code (#28) 2023-06-07 20:14:46 +08:00
Wang Xin 80fbc35f51
Update README.md (#27) 2023-06-04 23:43:53 +08:00
Wang Xin 9cccb7692f
update to version 0.1.2 (#26) 2023-06-02 16:16:45 +08:00
Wang Xin 154d4e501f
Create pylint.yml (#23) 2023-05-11 16:25:00 +08:00
Wang Xin 1cd66654cb
bug fixed for output_format == bbox (#22) 2023-05-11 16:13:14 +08:00
Wang Xin df4f172722
bug fixed (#21) 2023-05-05 13:01:26 +08:00
Wang Xin 9370343e5e
add label_list argument (#20) 2023-05-04 14:24:16 +08:00
dependabot[bot] a47ee2c816
Update pillow requirement from <9.5,>=9.2 to >=9.2,<9.6 (#17)
Updates the requirements on [pillow](https://github.com/python-pillow/Pillow) to permit the latest version.
- [Release notes](https://github.com/python-pillow/Pillow/releases)
- [Changelog](https://github.com/python-pillow/Pillow/blob/main/CHANGES.rst)
- [Commits](https://github.com/python-pillow/Pillow/compare/9.2.0...9.5.0)

---
updated-dependencies:
- dependency-name: pillow
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-04-03 19:23:45 +08:00
Wang Xin fd48196639 update labelme2yolo version 2023-03-29 22:46:25 +08:00
Arshadoid a5a8f92059
Update l2y.py (#16)
Fixed _train_test_split() for correct split of test and train.
2023-03-29 22:17:02 +08:00
Wang Xin 20110cbcfa
Merge pull request #13 from GreatV/markdown_lint
markdown lint
2023-03-17 16:04:31 +08:00
Wang Xin 4af61739a4 markdown lint 2023-03-17 16:00:23 +08:00
Wang Xin c008d6c621
Merge pull request #12 from GreatV/dev
add output_format option
2023-03-16 13:51:57 +08:00
10 changed files with 1012 additions and 244 deletions

24
.github/workflows/pylint.yml vendored Normal file
View File

@ -0,0 +1,24 @@
name: Pylint
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pylint
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')

39
.github/workflows/python-publish.yml vendored Normal file
View File

@ -0,0 +1,39 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: Upload Python Package
on:
release:
types: [published]
permissions:
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}

631
.pylintrc Normal file
View File

@ -0,0 +1,631 @@
[MAIN]
# Analyse import fallback blocks. This can be used to support both Python 2 and
# 3 compatible code, which means that the block might have code that exists
# only in one or another interpreter, leading to false positives when analysed.
analyse-fallback-blocks=no
# Clear in-memory caches upon conclusion of linting. Useful if running pylint
# in a server-like mode.
clear-cache-post-run=no
# Load and enable all available extensions. Use --list-extensions to see a list
# all available extensions.
#enable-all-extensions=
# In error mode, messages with a category besides ERROR or FATAL are
# suppressed, and no reports are done by default. Error mode is compatible with
# disabling specific errors.
#errors-only=
# Always return a 0 (non-error) status code, even if lint errors are found.
# This is primarily useful in continuous integration scripts.
#exit-zero=
# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code.
extension-pkg-allow-list=
# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code. (This is an alternative name to extension-pkg-allow-list
# for backward compatibility.)
extension-pkg-whitelist=
# Return non-zero exit code if any of these messages/categories are detected,
# even if score is above --fail-under value. Syntax same as enable. Messages
# specified are enabled, while categories only check already-enabled messages.
fail-on=
# Specify a score threshold under which the program will exit with error.
fail-under=10
# Interpret the stdin as a python script, whose filename needs to be passed as
# the module_or_package argument.
#from-stdin=
# Files or directories to be skipped. They should be base names, not paths.
ignore=CVS
# Add files or directories matching the regular expressions patterns to the
# ignore-list. The regex matches against paths and can be in Posix or Windows
# format. Because '\\' represents the directory delimiter on Windows systems,
# it can't be used as an escape character.
ignore-paths=
# Files or directories matching the regular expression patterns are skipped.
# The regex matches against base names, not paths. The default value ignores
# Emacs file locks
ignore-patterns=^\.#
# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
# and thus existing member attributes cannot be deduced by static analysis). It
# supports qualified module names, as well as Unix pattern matching.
ignored-modules=
# Python code to execute, usually for sys.path manipulation such as
# pygtk.require().
#init-hook=
# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the
# number of processors available to use, and will cap the count on Windows to
# avoid hangs.
jobs=1
# Control the amount of potential inferred values when inferring a single
# object. This can help the performance when dealing with large functions or
# complex, nested conditions.
limit-inference-results=100
# List of plugins (as comma separated values of python module names) to load,
# usually to register additional checkers.
load-plugins=
# Pickle collected data for later comparisons.
persistent=yes
# Minimum Python version to use for version dependent checks. Will default to
# the version used to run pylint.
py-version=3.10
# Discover python modules and packages in the file system subtree.
recursive=no
# Add paths to the list of the source roots. Supports globbing patterns. The
# source root is an absolute path or a path relative to the current working
# directory used to determine a package namespace for modules located under the
# source root.
source-roots=
# When enabled, pylint would attempt to guess common misconfiguration and emit
# user-friendly hints instead of false-positive error messages.
suggestion-mode=yes
# Allow loading of arbitrary C extensions. Extensions are imported into the
# active Python interpreter and may run arbitrary code.
unsafe-load-any-extension=no
# In verbose mode, extra non-checker-related info will be displayed.
#verbose=
[BASIC]
# Naming style matching correct argument names.
argument-naming-style=snake_case
# Regular expression matching correct argument names. Overrides argument-
# naming-style. If left empty, argument names will be checked with the set
# naming style.
#argument-rgx=
# Naming style matching correct attribute names.
attr-naming-style=snake_case
# Regular expression matching correct attribute names. Overrides attr-naming-
# style. If left empty, attribute names will be checked with the set naming
# style.
#attr-rgx=
# Bad variable names which should always be refused, separated by a comma.
bad-names=foo,
bar,
baz,
toto,
tutu,
tata
# Bad variable names regexes, separated by a comma. If names match any regex,
# they will always be refused
bad-names-rgxs=
# Naming style matching correct class attribute names.
class-attribute-naming-style=any
# Regular expression matching correct class attribute names. Overrides class-
# attribute-naming-style. If left empty, class attribute names will be checked
# with the set naming style.
#class-attribute-rgx=
# Naming style matching correct class constant names.
class-const-naming-style=UPPER_CASE
# Regular expression matching correct class constant names. Overrides class-
# const-naming-style. If left empty, class constant names will be checked with
# the set naming style.
#class-const-rgx=
# Naming style matching correct class names.
class-naming-style=PascalCase
# Regular expression matching correct class names. Overrides class-naming-
# style. If left empty, class names will be checked with the set naming style.
#class-rgx=
# Naming style matching correct constant names.
const-naming-style=UPPER_CASE
# Regular expression matching correct constant names. Overrides const-naming-
# style. If left empty, constant names will be checked with the set naming
# style.
#const-rgx=
# Minimum line length for functions/classes that require docstrings, shorter
# ones are exempt.
docstring-min-length=-1
# Naming style matching correct function names.
function-naming-style=snake_case
# Regular expression matching correct function names. Overrides function-
# naming-style. If left empty, function names will be checked with the set
# naming style.
#function-rgx=
# Good variable names which should always be accepted, separated by a comma.
good-names=i,
j,
k,
ex,
Run,
_
# Good variable names regexes, separated by a comma. If names match any regex,
# they will always be accepted
good-names-rgxs=
# Include a hint for the correct naming format with invalid-name.
include-naming-hint=no
# Naming style matching correct inline iteration names.
inlinevar-naming-style=any
# Regular expression matching correct inline iteration names. Overrides
# inlinevar-naming-style. If left empty, inline iteration names will be checked
# with the set naming style.
#inlinevar-rgx=
# Naming style matching correct method names.
method-naming-style=snake_case
# Regular expression matching correct method names. Overrides method-naming-
# style. If left empty, method names will be checked with the set naming style.
#method-rgx=
# Naming style matching correct module names.
module-naming-style=snake_case
# Regular expression matching correct module names. Overrides module-naming-
# style. If left empty, module names will be checked with the set naming style.
#module-rgx=
# Colon-delimited sets of names that determine each other's naming style when
# the name regexes allow several styles.
name-group=
# Regular expression which should only match function or class names that do
# not require a docstring.
no-docstring-rgx=^_
# List of decorators that produce properties, such as abc.abstractproperty. Add
# to this list to register other decorators that produce valid properties.
# These decorators are taken in consideration only for invalid-name.
property-classes=abc.abstractproperty
# Regular expression matching correct type alias names. If left empty, type
# alias names will be checked with the set naming style.
#typealias-rgx=
# Regular expression matching correct type variable names. If left empty, type
# variable names will be checked with the set naming style.
#typevar-rgx=
# Naming style matching correct variable names.
variable-naming-style=snake_case
# Regular expression matching correct variable names. Overrides variable-
# naming-style. If left empty, variable names will be checked with the set
# naming style.
#variable-rgx=
[CLASSES]
# Warn about protected attribute access inside special methods
check-protected-access-in-special-methods=no
# List of method names used to declare (i.e. assign) instance attributes.
defining-attr-methods=__init__,
__new__,
setUp,
asyncSetUp,
__post_init__
# List of member names, which should be excluded from the protected access
# warning.
exclude-protected=_asdict,_fields,_replace,_source,_make,os._exit
# List of valid names for the first argument in a class method.
valid-classmethod-first-arg=cls
# List of valid names for the first argument in a metaclass class method.
valid-metaclass-classmethod-first-arg=mcs
[DESIGN]
# List of regular expressions of class ancestor names to ignore when counting
# public methods (see R0903)
exclude-too-few-public-methods=
# List of qualified class names to ignore when counting class parents (see
# R0901)
ignored-parents=
# Maximum number of arguments for function / method.
max-args=5
# Maximum number of attributes for a class (see R0902).
max-attributes=7
# Maximum number of boolean expressions in an if statement (see R0916).
max-bool-expr=5
# Maximum number of branch for function / method body.
max-branches=12
# Maximum number of locals for function / method body.
max-locals=15
# Maximum number of parents for a class (see R0901).
max-parents=7
# Maximum number of public methods for a class (see R0904).
max-public-methods=20
# Maximum number of return / yield for function / method body.
max-returns=6
# Maximum number of statements in function / method body.
max-statements=50
# Minimum number of public methods for a class (see R0903).
min-public-methods=2
[EXCEPTIONS]
# Exceptions that will emit a warning when caught.
overgeneral-exceptions=builtins.BaseException,builtins.Exception
[FORMAT]
# Expected format of line ending, e.g. empty (any line ending), LF or CRLF.
expected-line-ending-format=
# Regexp for a line that is allowed to be longer than the limit.
ignore-long-lines=^\s*(# )?<?https?://\S+>?$
# Number of spaces of indent required inside a hanging or continued line.
indent-after-paren=4
# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1
# tab).
indent-string=' '
# Maximum number of characters on a single line.
max-line-length=100
# Maximum number of lines in a module.
max-module-lines=1000
# Allow the body of a class to be on the same line as the declaration if body
# contains single statement.
single-line-class-stmt=no
# Allow the body of an if to be on the same line as the test if there is no
# else.
single-line-if-stmt=no
[IMPORTS]
# List of modules that can be imported at any level, not just the top level
# one.
allow-any-import-level=
# Allow explicit reexports by alias from a package __init__.
allow-reexport-from-package=no
# Allow wildcard imports from modules that define __all__.
allow-wildcard-with-all=no
# Deprecated modules which should not be used, separated by a comma.
deprecated-modules=
# Output a graph (.gv or any supported image format) of external dependencies
# to the given file (report RP0402 must not be disabled).
ext-import-graph=
# Output a graph (.gv or any supported image format) of all (i.e. internal and
# external) dependencies to the given file (report RP0402 must not be
# disabled).
import-graph=
# Output a graph (.gv or any supported image format) of internal dependencies
# to the given file (report RP0402 must not be disabled).
int-import-graph=
# Force import order to recognize a module as part of the standard
# compatibility libraries.
known-standard-library=
# Force import order to recognize a module as part of a third party library.
known-third-party=enchant
# Couples of modules and preferred modules, separated by a comma.
preferred-modules=
[LOGGING]
# The type of string formatting that logging methods do. `old` means using %
# formatting, `new` is for `{}` formatting.
logging-format-style=old
# Logging modules to check that the string format arguments are in logging
# function parameter format.
logging-modules=logging
[MESSAGES CONTROL]
# Only show warnings with the listed confidence levels. Leave empty to show
# all. Valid levels: HIGH, CONTROL_FLOW, INFERENCE, INFERENCE_FAILURE,
# UNDEFINED.
confidence=HIGH,
CONTROL_FLOW,
INFERENCE,
INFERENCE_FAILURE,
UNDEFINED
# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifiers separated by comma (,) or put this
# option multiple times (only on the command line, not in the configuration
# file where it should appear only once). You can also use "--disable=all" to
# disable everything first and then re-enable specific checks. For example, if
# you want to run only the similarities checker, you can use "--disable=all
# --enable=similarities". If you want to run only the classes checker, but have
# no Warning level messages displayed, use "--disable=all --enable=classes
# --disable=W".
disable=raw-checker-failed,
bad-inline-option,
locally-disabled,
file-ignored,
suppressed-message,
useless-suppression,
deprecated-pragma,
use-symbolic-message-instead
# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once). See also the "--disable" option for examples.
enable=c-extension-no-member
[METHOD_ARGS]
# List of qualified names (i.e., library.method) which require a timeout
# parameter e.g. 'requests.api.get,requests.api.post'
timeout-methods=requests.api.delete,requests.api.get,requests.api.head,requests.api.options,requests.api.patch,requests.api.post,requests.api.put,requests.api.request
[MISCELLANEOUS]
# List of note tags to take in consideration, separated by a comma.
notes=FIXME,
XXX,
TODO
# Regular expression of note tags to take in consideration.
notes-rgx=
[REFACTORING]
# Maximum number of nested blocks for function / method body
max-nested-blocks=5
# Complete name of functions that never returns. When checking for
# inconsistent-return-statements if a never returning function is called then
# it will be considered as an explicit return statement and no message will be
# printed.
never-returning-functions=sys.exit,argparse.parse_error
[REPORTS]
# Python expression which should return a score less than or equal to 10. You
# have access to the variables 'fatal', 'error', 'warning', 'refactor',
# 'convention', and 'info' which contain the number of messages in each
# category, as well as 'statement' which is the total number of statements
# analyzed. This score is used by the global evaluation report (RP0004).
evaluation=max(0, 0 if fatal else 10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10))
# Template used to display messages. This is a python new-style format string
# used to format the message information. See doc for all details.
msg-template=
# Set the output format. Available formats are text, parseable, colorized, json
# and msvs (visual studio). You can also give a reporter class, e.g.
# mypackage.mymodule.MyReporterClass.
#output-format=
# Tells whether to display a full report or only the messages.
reports=no
# Activate the evaluation score.
score=yes
[SIMILARITIES]
# Comments are removed from the similarity computation
ignore-comments=yes
# Docstrings are removed from the similarity computation
ignore-docstrings=yes
# Imports are removed from the similarity computation
ignore-imports=yes
# Signatures are removed from the similarity computation
ignore-signatures=yes
# Minimum lines number of a similarity.
min-similarity-lines=4
[SPELLING]
# Limits count of emitted suggestions for spelling mistakes.
max-spelling-suggestions=4
# Spelling dictionary name. No available dictionaries : You need to install
# both the python package and the system dependency for enchant to work..
spelling-dict=
# List of comma separated words that should be considered directives if they
# appear at the beginning of a comment and should not be checked.
spelling-ignore-comment-directives=fmt: on,fmt: off,noqa:,noqa,nosec,isort:skip,mypy:
# List of comma separated words that should not be checked.
spelling-ignore-words=
# A path to a file that contains the private dictionary; one word per line.
spelling-private-dict-file=
# Tells whether to store unknown words to the private dictionary (see the
# --spelling-private-dict-file option) instead of raising a message.
spelling-store-unknown-words=no
[STRING]
# This flag controls whether inconsistent-quotes generates a warning when the
# character used as a quote delimiter is used inconsistently within a module.
check-quote-consistency=no
# This flag controls whether the implicit-str-concat should generate a warning
# on implicit string concatenation in sequences defined over several lines.
check-str-concat-over-line-jumps=no
[TYPECHECK]
# List of decorators that produce context managers, such as
# contextlib.contextmanager. Add to this list to register other decorators that
# produce valid context managers.
contextmanager-decorators=contextlib.contextmanager
# List of members which are set dynamically and missed by pylint inference
# system, and so shouldn't trigger E1101 when accessed. Python regular
# expressions are accepted.
generated-members=cv2.*
# Tells whether to warn about missing members when the owner of the attribute
# is inferred to be None.
ignore-none=yes
# This flag controls whether pylint should warn about no-member and similar
# checks whenever an opaque object is returned when inferring. The inference
# can return multiple potential results while evaluating a Python object, but
# some branches might not be evaluated, which results in partial inference. In
# that case, it might be useful to still emit no-member and other checks for
# the rest of the inferred objects.
ignore-on-opaque-inference=yes
# List of symbolic message names to ignore for Mixin members.
ignored-checks-for-mixins=no-member,
not-async-context-manager,
not-context-manager,
attribute-defined-outside-init
# List of class names for which member attributes should not be checked (useful
# for classes with dynamically set attributes). This supports the use of
# qualified names.
ignored-classes=optparse.Values,thread._local,_thread._local,argparse.Namespace
# Show a hint with possible names when a member name was not found. The aspect
# of finding the hint is based on edit distance.
missing-member-hint=yes
# The minimum edit distance a name should have in order to be considered a
# similar match for a missing member name.
missing-member-hint-distance=1
# The total number of similar names that should be taken in consideration when
# showing a hint for a missing member.
missing-member-max-choices=1
# Regex pattern to define which classes are considered mixins.
mixin-class-rgx=.*[Mm]ixin
# List of decorators that change the signature of a decorated function.
signature-mutators=
[VARIABLES]
# List of additional names supposed to be defined in builtins. Remember that
# you should avoid defining new builtins when possible.
additional-builtins=
# Tells whether unused global variables should be treated as a violation.
allow-global-unused-variables=yes
# List of names allowed to shadow builtins
allowed-redefined-builtins=
# List of strings which can identify a callback function by name. A callback
# name must start or end with one of those strings.
callbacks=cb_,
_cb
# A regular expression matching the name of dummy variables (i.e. expected to
# not be used).
dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_
# Argument names that match this expression will be ignored.
ignored-argument-names=_.*|^ignored_|^unused_
# Tells whether we should check for unused import in __init__ files.
init-import=no
# List of qualified module names which can have objects that can redefine
# builtins.
redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io

101
README.md
View File

@ -1,89 +1,78 @@
**Forked from [rooneysh/Labelme2YOLO](https://github.com/rooneysh/Labelme2YOLO)**
# Labelme2YOLO # Labelme2YOLO
[![PyPI - Version](https://img.shields.io/pypi/v/labelme2yolo.svg)](https://pypi.org/project/labelme2yolo) [![PyPI - Version](https://img.shields.io/pypi/v/labelme2yolo.svg)](https://pypi.org/project/labelme2yolo)
![PyPI - Downloads](https://img.shields.io/pypi/dm/labelme2yolo?style=flat) ![PyPI - Downloads](https://img.shields.io/pypi/dm/labelme2yolo?style=flat)
[![PYPI - Downloads](https://static.pepy.tech/badge/labelme2yolo)](https://pepy.tech/project/labelme2yolo)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/labelme2yolo.svg)](https://pypi.org/project/labelme2yolo) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/labelme2yolo.svg)](https://pypi.org/project/labelme2yolo)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/12122fe86f8643c4aa5667c20d528f61)](https://www.codacy.com/gh/GreatV/labelme2yolo/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=GreatV/labelme2yolo&amp;utm_campaign=Badge_Grade) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/12122fe86f8643c4aa5667c20d528f61)](https://www.codacy.com/gh/GreatV/labelme2yolo/dashboard?utm_source=github.com\&utm_medium=referral\&utm_content=GreatV/labelme2yolo\&utm_campaign=Badge_Grade)
Help converting LabelMe Annotation Tool JSON format to YOLO text file format. Labelme2YOLO is a powerful tool for converting LabelMe's JSON format to [YOLOv5](https://github.com/ultralytics/yolov5) dataset format. This tool can also be used for YOLOv5/YOLOv8 segmentation datasets, if you have already made your segmentation dataset with LabelMe, it is easy to use this tool to help convert to YOLO format dataset.
If you've already marked your segmentation dataset by LabelMe, it's easy to use this tool to help converting to YOLO format dataset.
--------- ## New Features
* export data as yolo polygon annotation (for YOLOv5 & YOLOV8 segmentation)
* Now you can choose the output format of the label text. The two available alternatives are `polygon` and bounding box (`bbox`).
## New
- export data as yolo polygon annotation (for YOLOv5 v7.0 segmentation)
- Now you can choose the output format of the label text. The available options are `plygon` and `bbox`.
## Installation ## Installation
```console ```shell
pip install labelme2yolo pip install labelme2yolo
``` ```
## Parameters Explain ## Arguments
**--json_dir** LabelMe JSON files folder path.
**--val_size (Optional)** Validation dataset size, for example 0.2 means 20% for validation. **--json\_dir** LabelMe JSON files folder path.
**--test_size (Optional)** Test dataset size, for example 0.2 means 20% for Test. **--val\_size (Optional)** Validation dataset size, for example 0.2 means 20% for validation.
**--json_name (Optional)** Convert single LabelMe JSON file. **--test\_size (Optional)** Test dataset size, for example 0.1 means 10% for Test.
**--output_format (Optional)** The output format of label. **--json\_name (Optional)** Convert single LabelMe JSON file.
**--output\_format (Optional)** The output format of label.
**--label\_list (Optional)** The pre-assigned category labels.
## How to Use ## How to Use
### 1. Convert JSON files, split training, validation and test dataset by --val_size and --test_size ### 1. Converting JSON files and splitting training, validation datasets
Put all LabelMe JSON files under **labelme_json_dir**, and run this python command.
```bash You may need to place all LabelMe JSON files under **labelme\_json\_dir** and then run the following command:
```shell
labelme2yolo --json_dir /path/to/labelme_json_dir/
```
This tool will generate dataset labels and images with YOLO format in different folders, such as
```plaintext
/path/to/labelme_json_dir/YOLODataset/labels/train/
/path/to/labelme_json_dir/YOLODataset/labels/val/
/path/to/labelme_json_dir/YOLODataset/images/train/
/path/to/labelme_json_dir/YOLODataset/images/val/
/path/to/labelme_json_dir/YOLODataset/dataset.yaml
```
### 2. Converting JSON files and splitting training, validation, and test datasets with --val\_size and --test\_size
You may need to place all LabelMe JSON files under **labelme\_json\_dir** and then run the following command:
```shell
labelme2yolo --json_dir /path/to/labelme_json_dir/ --val_size 0.15 --test_size 0.15 labelme2yolo --json_dir /path/to/labelme_json_dir/ --val_size 0.15 --test_size 0.15
``` ```
Script would generate YOLO format dataset labels and images under different folders, for example,
```bash This tool will generate dataset labels and images with YOLO format in different folders, such as
```plaintext
/path/to/labelme_json_dir/YOLODataset/labels/train/ /path/to/labelme_json_dir/YOLODataset/labels/train/
/path/to/labelme_json_dir/YOLODataset/labels/test/ /path/to/labelme_json_dir/YOLODataset/labels/test/
/path/to/labelme_json_dir/YOLODataset/labels/val/ /path/to/labelme_json_dir/YOLODataset/labels/val/
/path/to/labelme_json_dir/YOLODataset/images/train/ /path/to/labelme_json_dir/YOLODataset/images/train/
/path/to/labelme_json_dir/YOLODataset/images/test/ /path/to/labelme_json_dir/YOLODataset/images/test/
/path/to/labelme_json_dir/YOLODataset/images/val/ /path/to/labelme_json_dir/YOLODataset/images/val/
/path/to/labelme_json_dir/YOLODataset/dataset.yaml /path/to/labelme_json_dir/YOLODataset/dataset.yaml
``` ```
### 2. Convert JSON files, split training and validation dataset by folder
If you already split train dataset and validation dataset for LabelMe by yourself, please put these folder under labelme_json_dir, for example,
```bash
/path/to/labelme_json_dir/train/
/path/to/labelme_json_dir/val/
```
Put all LabelMe JSON files under **labelme_json_dir**.
Script would read train and validation dataset by folder.
Run this python command.
```bash
labelme2yolo --json_dir /path/to/labelme_json_dir/
```
Script would generate YOLO format dataset labels and images under different folders, for example,
```bash
/path/to/labelme_json_dir/YOLODataset/labels/train/
/path/to/labelme_json_dir/YOLODataset/labels/val/
/path/to/labelme_json_dir/YOLODataset/images/train/
/path/to/labelme_json_dir/YOLODataset/images/val/
/path/to/labelme_json_dir/YOLODataset/dataset.yaml
```
### 3. Convert single JSON file
Put LabelMe JSON file under **labelme_json_dir**. , and run this python command.
```bash
labelme2yolo --json_dir /path/to/labelme_json_dir/ --json_name 2.json
```
Script would generate YOLO format text label and image under **labelme_json_dir**, for example,
```bash
/path/to/labelme_json_dir/2.text
/path/to/labelme_json_dir/2.png
```
## How to build package/wheel ## How to build package/wheel
1. [install hatch](https://hatch.pypa.io/latest/install/) 1. [install hatch](https://hatch.pypa.io/latest/install/)
@ -95,4 +84,6 @@ hatch build
## License ## License
**Forked from [rooneysh/Labelme2YOLO](https://github.com/rooneysh/Labelme2YOLO)**
`labelme2yolo` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license. `labelme2yolo` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.

View File

@ -6,27 +6,27 @@ build-backend = "hatchling.build"
name = "labelme2yolo" name = "labelme2yolo"
description = "This script converts the JSON format output by LabelMe to the text format required by YOLO serirs." description = "This script converts the JSON format output by LabelMe to the text format required by YOLO serirs."
readme = "README.md" readme = "README.md"
requires-python = ">=3.7" requires-python = ">=3.8"
license = "MIT" license = "MIT"
keywords = [] keywords = []
authors = [ authors = [
{ name = "GreatV(Wang Xin)", email = "xinwang614@gmail.com" }, { name = "GreatV(Wang Xin)", email = "xinwang614@gmail.com" },
] ]
classifiers = [ classifiers = [
"Development Status :: 4 - Beta", "Development Status :: 5 - Production/Stable",
"Programming Language :: Python", "Programming Language :: Python",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.10",
"Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: 3.11",
"Programming Language :: Python :: Implementation :: PyPy", "Programming Language :: Python :: 3.12",
"License :: OSI Approved :: MIT License",
] ]
dependencies = [ dependencies = [
"opencv-python>=4.1.2", "opencv-python>=4.1.2",
"Pillow>=9.2,<9.5", "Pillow>=9.2,<10.4",
"scikit-learn>=1.1.1,<1.3.0", "numpy>=1.23.1,<1.27.0",
"numpy>=1.23.1,<1.25.0" "rich"
] ]
dynamic = ["version"] dynamic = ["version"]
@ -54,7 +54,7 @@ cov = "pytest --cov-report=term-missing --cov-config=pyproject.toml --cov=labelm
no-cov = "cov --no-cov" no-cov = "cov --no-cov"
[[tool.hatch.envs.test.matrix]] [[tool.hatch.envs.test.matrix]]
python = ["37", "38", "39", "310"] python = ["38", "39", "310", "311", "312"]
[tool.coverage.run] [tool.coverage.run]
branch = true branch = true

View File

@ -1,4 +1,4 @@
opencv-python opencv-python
Pillow Pillow
scikit-learn
numpy numpy
rich

View File

@ -1,5 +1,7 @@
# SPDX-FileCopyrightText: 2023-present Wang Xin <xinwang614@gmail.com> # SPDX-FileCopyrightText: 2023-present Wang Xin <xinwang614@gmail.com>
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
"""
__version__ = '0.0.9' about version
"""
__version__ = "0.1.7"

View File

@ -1,6 +1,9 @@
# SPDX-FileCopyrightText: 2022-present Wang Xin <xinwang614@gmail.com> # SPDX-FileCopyrightText: 2022-present Wang Xin <xinwang614@gmail.com>
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
"""
main
"""
import sys import sys
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1,13 +1,22 @@
# SPDX-FileCopyrightText: 2022-present Wang Xin <xinwang614@gmail.com> # SPDX-FileCopyrightText: 2022-present Wang Xin <xinwang614@gmail.com>
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
"""
cli init
"""
import argparse import argparse
from labelme2yolo.__about__ import __version__
from labelme2yolo.l2y import Labelme2YOLO from labelme2yolo.l2y import Labelme2YOLO
def run(): def run():
"""
run cli
"""
parser = argparse.ArgumentParser("labelme2yolo") parser = argparse.ArgumentParser("labelme2yolo")
parser.add_argument(
"-v", "--version", action="version", version="%(prog)s " + __version__
)
parser.add_argument( parser.add_argument(
"--json_dir", type=str, help="Please input the path of the labelme json files." "--json_dir", type=str, help="Please input the path of the labelme json files."
) )
@ -15,14 +24,14 @@ def run():
"--val_size", "--val_size",
type=float, type=float,
nargs="?", nargs="?",
default=None, default=0.2,
help="Please input the validation dataset size, for example 0.1.", help="Please input the validation dataset size, for example 0.2.",
) )
parser.add_argument( parser.add_argument(
"--test_size", "--test_size",
type=float, type=float,
nargs="?", nargs="?",
default=None, default=0.0,
help="Please input the test dataset size, for example 0.1.", help="Please input the test dataset size, for example 0.1.",
) )
parser.add_argument( parser.add_argument(
@ -39,6 +48,14 @@ def run():
help='The default output format for labelme2yolo is "polygon".' help='The default output format for labelme2yolo is "polygon".'
' However, you can choose to output in bbox format by specifying the "bbox" option.', ' However, you can choose to output in bbox format by specifying the "bbox" option.',
) )
parser.add_argument(
"--label_list",
type=str,
nargs="+",
default=None,
help="The ordered label list, for example --label_list cat dog",
required=False,
)
args = parser.parse_args() args = parser.parse_args()
@ -46,7 +63,7 @@ def run():
parser.print_help() parser.print_help()
return 0 return 0
convertor = Labelme2YOLO(args.json_dir, args.output_format) convertor = Labelme2YOLO(args.json_dir, args.output_format, args.label_list)
if args.json_name is None: if args.json_name is None:
convertor.convert(val_size=args.val_size, test_size=args.test_size) convertor.convert(val_size=args.val_size, test_size=args.test_size)

View File

@ -5,36 +5,58 @@ Created on Aug 18, 2021
@author: GreatV(Wang Xin) @author: GreatV(Wang Xin)
""" """
import base64 import base64
import glob
import io import io
import json import json
import math import math
import os import os
import random
import shutil import shutil
from collections import OrderedDict import uuid
from multiprocessing import Pool import logging
from functools import partial
import cv2 from multiprocessing import Pool
import numpy as np
import PIL.ExifTags import PIL.ExifTags
import PIL.Image import PIL.Image
import PIL.ImageOps import PIL.ImageOps
from sklearn.model_selection import train_test_split import cv2
import numpy as np
from rich.progress import Progress
# set seed
random.seed(12345678)
random.Random().seed(12345678)
np.random.seed(12345678)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("labelme2yolo")
# number of LabelMe2YOLO multiprocessing threads def train_test_split(dataset_index, test_size=0.2):
NUM_THREADS = max(1, os.cpu_count() - 1) """Split dataset into train set and test set with test_size"""
test_size = min(max(0.0, test_size), 1.0)
total_size = len(dataset_index)
train_size = int(round(total_size * (1.0 - test_size)))
random.shuffle(dataset_index)
train_index = dataset_index[:train_size]
test_index = dataset_index[train_size:]
return train_index, test_index
# copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py # copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py
def img_data_to_pil(img_data): def img_data_to_pil(img_data):
f = io.BytesIO() """Convert img_data(byte) to PIL.Image"""
f.write(img_data) file = io.BytesIO()
img_pil = PIL.Image.open(f) file.write(img_data)
img_pil = PIL.Image.open(file)
return img_pil return img_pil
# copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py # copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py
def img_data_to_arr(img_data): def img_data_to_arr(img_data):
"""Convert img_data(byte) to numpy.ndarray"""
img_pil = img_data_to_pil(img_data) img_pil = img_data_to_pil(img_data)
img_arr = np.array(img_pil) img_arr = np.array(img_pil)
return img_arr return img_arr
@ -42,6 +64,7 @@ def img_data_to_arr(img_data):
# copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py # copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py
def img_b64_to_arr(img_b64): def img_b64_to_arr(img_b64):
"""Convert img_b64(str) to numpy.ndarray"""
img_data = base64.b64decode(img_b64) img_data = base64.b64decode(img_b64)
img_arr = img_data_to_arr(img_data) img_arr = img_data_to_arr(img_data)
return img_arr return img_arr
@ -49,27 +72,27 @@ def img_b64_to_arr(img_b64):
# copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py # copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py
def img_pil_to_data(img_pil): def img_pil_to_data(img_pil):
f = io.BytesIO() """Convert PIL.Image to img_data(byte)"""
img_pil.save(f, format="PNG") file = io.BytesIO()
img_data = f.getvalue() img_pil.save(file, format="PNG")
img_data = file.getvalue()
return img_data return img_data
# copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py # copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py
def img_arr_to_b64(img_arr): def img_arr_to_b64(img_arr):
"""Convert numpy.ndarray to img_b64(str)"""
img_pil = PIL.Image.fromarray(img_arr) img_pil = PIL.Image.fromarray(img_arr)
f = io.BytesIO() file = io.BytesIO()
img_pil.save(f, format="PNG") img_pil.save(file, format="PNG")
img_bin = f.getvalue() img_bin = file.getvalue()
if hasattr(base64, "encodebytes"): img_b64 = base64.encodebytes(img_bin)
img_b64 = base64.encodebytes(img_bin)
else:
img_b64 = base64.encodestring(img_bin)
return img_b64 return img_b64
# copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py # copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py
def img_data_to_png_data(img_data): def img_data_to_png_data(img_data):
"""Convert img_data(byte) to png_data(byte)"""
with io.BytesIO() as f_out: with io.BytesIO() as f_out:
f_out.write(img_data) f_out.write(img_data)
img = PIL.Image.open(f_out) img = PIL.Image.open(f_out)
@ -80,169 +103,186 @@ def img_data_to_png_data(img_data):
return f_in.read() return f_in.read()
def get_label_id_map(json_dir): def extend_point_list(point_list, out_format="polygon"):
label_set = set() """Extend point list to polygon or bbox"""
x_min = min(float(point) for point in point_list[::2])
x_max = max(float(point) for point in point_list[::2])
y_min = min(float(point) for point in point_list[1::2])
y_max = max(float(point) for point in point_list[1::2])
for file_name in os.listdir(json_dir): if out_format == "bbox":
if file_name.endswith("json"): x_i = x_min
json_path = os.path.join(json_dir, file_name) y_i = y_min
data = json.load(open(json_path)) w_i = x_max - x_min
for shape in data["shapes"]: h_i = y_max - y_min
label_set.add(shape["label"]) x_i = x_i + w_i / 2
y_i = y_i + h_i / 2
return np.array([x_i, y_i, w_i, h_i])
return OrderedDict([(label, label_id) for label_id, label in enumerate(label_set)]) return np.array([x_min, y_min, x_max, y_min, x_max, y_max, x_min, y_max])
def extend_point_list(point_list, format="polygon"): def save_yolo_label(obj_list, label_dir, target_dir, target_name):
xmin = min([float(point) for point in point_list[::2]]) """Save yolo label to txt file"""
xmax = max([float(point) for point in point_list[::2]]) txt_path = os.path.join(label_dir, target_dir, target_name)
ymin = min([float(point) for point in point_list[1::2]])
ymax = max([float(point) for point in point_list[1::2]])
if (format == "polygon"): with open(txt_path, "w+", encoding="utf-8") as file:
return np.array([xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax]) for label, points in obj_list:
if (format == "bbox"):
return np.array([xmin, ymin, xmax - xmin, ymax - ymin])
def save_yolo_label(json_name, label_dir_path, target_dir, yolo_obj_list):
txt_path = os.path.join(label_dir_path,
target_dir,
json_name.replace(".json", ".txt"))
with open(txt_path, "w+") as f:
for yolo_obj in yolo_obj_list:
label, points = yolo_obj
points = [str(item) for item in points] points = [str(item) for item in points]
yolo_obj_line = f"{label} {' '.join(points)}\n" line = f"{label} {' '.join(points)}\n"
f.write(yolo_obj_line) file.write(line)
def save_yolo_image(json_data, json_name, image_dir_path, target_dir): def save_yolo_image(json_data, json_dir, image_dir, target_dir, target_name):
img_name = json_name.replace(".json", ".png") """Save yolo image to image_dir_path/target_dir"""
img_path = os.path.join(image_dir_path, target_dir, img_name) img_path = os.path.join(image_dir, target_dir, target_name)
if not os.path.exists(img_path): if json_data["imageData"]:
img = img_b64_to_arr(json_data["imageData"]) img = img_b64_to_arr(json_data["imageData"])
PIL.Image.fromarray(img).save(img_path) PIL.Image.fromarray(img).save(img_path)
else:
image_name = json_data["imagePath"]
src_image_name = os.path.join(json_dir, image_name)
src_image = cv2.imread(src_image_name)
cv2.imwrite(img_path, src_image)
return img_path return img_path
class Labelme2YOLO(object): class Labelme2YOLO:
"""Labelme to YOLO format converter"""
def __init__(self, json_dir, output_format): def __init__(self, json_dir, output_format, label_list):
self._json_dir = json_dir self._json_dir = os.path.expanduser(json_dir)
self._output_format = output_format self._output_format = output_format
self._label_list = []
self._label_id_map = {}
self._label_dir_path = ""
self._image_dir_path = ""
self._label_id_map = get_label_id_map(self._json_dir) if label_list:
self._label_list = label_list
self._label_id_map = {
label: label_id for label_id, label in enumerate(label_list)
}
else:
logger.info("Searching label list from json files ...")
# get label list from json files for parallel processing
json_files = glob.glob(
os.path.join(self._json_dir, "**", "*.json"), recursive=True
)
for json_file in json_files:
with open(json_file, encoding="utf-8") as file:
json_data = json.load(file)
for shape in json_data["shapes"]:
if shape["label"] not in self._label_list:
self._label_list.append(shape["label"])
self._label_id_map = {
label: label_id for label_id, label in enumerate(self._label_list)
}
def _make_train_val_dir(self): def _update_id_map(self, label: str):
self._label_dir_path = os.path.join(self._json_dir, if label not in self._label_list:
'YOLODataset/labels/') self._label_list.append(label)
self._image_dir_path = os.path.join(self._json_dir, self._label_id_map[label] = len(self._label_id_map)
'YOLODataset/images/')
for yolo_path in (os.path.join(self._label_dir_path + 'train/'), def _make_train_val_dir(self, create_test_dir=False):
os.path.join(self._label_dir_path + 'val/'), self._label_dir_path = os.path.join(self._json_dir, "YOLODataset/labels/")
os.path.join(self._label_dir_path + 'test/'), self._image_dir_path = os.path.join(self._json_dir, "YOLODataset/images/")
os.path.join(self._image_dir_path + 'train/'),
os.path.join(self._image_dir_path + 'val/'),
os.path.join(self._image_dir_path + 'test/')):
if os.path.exists(yolo_path):
shutil.rmtree(yolo_path)
os.makedirs(yolo_path) for yolo_path in [self._label_dir_path, self._image_dir_path]:
shutil.rmtree(yolo_path, ignore_errors=True)
def _train_test_split(self, folders, json_names, val_size, test_size): parts = ["train", "val", "test"] if create_test_dir else ["train", "val"]
if len(folders) > 0 and 'train' in folders and 'val' in folders and 'test' in folders: image_dirs = [os.path.join(self._image_dir_path, part) for part in parts]
train_folder = os.path.join(self._json_dir, 'train/') label_dirs = [os.path.join(self._label_dir_path, part) for part in parts]
train_json_names = [train_sample_name + '.json' dirs = image_dirs + label_dirs
for train_sample_name in os.listdir(train_folder) for yolo_path in dirs:
if os.path.isdir(os.path.join(train_folder, train_sample_name))] os.makedirs(yolo_path, exist_ok=True)
val_folder = os.path.join(self._json_dir, 'val/') def _get_dataset_part_json_names(self, dataset_part: str):
val_json_names = [val_sample_name + '.json' """Get json names in dataset_part folder"""
for val_sample_name in os.listdir(val_folder) set_folder = os.path.join(self._json_dir, dataset_part)
if os.path.isdir(os.path.join(val_folder, val_sample_name))] json_names = []
for sample_name in os.listdir(set_folder):
set_dir = os.path.join(set_folder, sample_name)
if os.path.isdir(set_dir):
json_names.append(sample_name + ".json")
return json_names
test_folder = os.path.join(self._json_dir, 'test/') def _train_test_split(self, json_names, val_size, test_size=None):
test_json_names = [test_sample_name + '.json' """Split json names to train, val, test"""
for test_sample_name in os.listdir(test_folder) total_size = len(json_names)
if os.path.isdir(os.path.join(test_folder, test_sample_name))] dataset_index = list(range(total_size))
train_ids, val_ids = train_test_split(dataset_index, test_size=val_size)
return train_json_names, val_json_names, test_json_names test_ids = []
train_idxs, val_idxs = train_test_split(range(len(json_names)),
test_size=val_size)
tmp_train_len = len(train_idxs)
test_idxs = []
if test_size is None: if test_size is None:
test_size = 0.0 test_size = 0.0
if test_size > 1e-8: if test_size > 0.0:
train_idxs, test_idxs = train_test_split( train_ids, test_ids = train_test_split(
range(tmp_train_len), test_size=test_size / (1 - val_size)) train_ids, test_size=test_size / (1 - val_size)
train_json_names = [json_names[train_idx] for train_idx in train_idxs] )
val_json_names = [json_names[val_idx] for val_idx in val_idxs] train_json_names = [json_names[train_idx] for train_idx in train_ids]
test_json_names = [json_names[test_idx] for test_idx in test_idxs] val_json_names = [json_names[val_idx] for val_idx in val_ids]
test_json_names = [json_names[test_idx] for test_idx in test_ids]
return train_json_names, val_json_names, test_json_names return train_json_names, val_json_names, test_json_names
def convert(self, val_size, test_size): def convert(self, val_size, test_size):
json_names = [file_name for file_name in os.listdir(self._json_dir) """Convert labelme format to yolo format"""
if os.path.isfile(os.path.join(self._json_dir, file_name)) and json_names = glob.glob(
file_name.endswith('.json')] os.path.join(self._json_dir, "**", "*.json"), recursive=True
folders = [file_name for file_name in os.listdir(self._json_dir) )
if os.path.isdir(os.path.join(self._json_dir, file_name))] json_names = sorted(json_names)
train_json_names, val_json_names, test_json_names = self._train_test_split(
folders, json_names, val_size, test_size)
self._make_train_val_dir() train_json_names, val_json_names, test_json_names = self._train_test_split(
json_names, val_size, test_size
)
self._make_train_val_dir(test_size > 0.0)
# convert labelme object to yolo format object, and save them to files # convert labelme object to yolo format object, and save them to files
# also get image from labelme json file and save them under images folder # also get image from labelme json file and save them under images folder
for target_dir, json_names in zip(('train/', 'val/', 'test/'), dirs = ("train", "val", "test")
(train_json_names, val_json_names, test_json_names)): names = (train_json_names, val_json_names, test_json_names)
pool = Pool(NUM_THREADS) for target_dir, json_names in zip(dirs, names):
logger.info("Converting %s set ...", target_dir)
with Pool(os.cpu_count() - 1) as pool, Progress() as progress:
task = progress.add_task("[cyan]Converting...", total=len(json_names))
func = partial(self.covert_json_to_text, target_dir)
for _ in pool.imap_unordered(func, json_names):
progress.update(task, advance=1)
for json_name in json_names:
pool.apply_async(self.covert_json_to_text,
args=(target_dir, json_name))
pool.close()
pool.join()
print('Generating dataset.yaml file ...')
self._save_dataset_yaml() self._save_dataset_yaml()
def covert_json_to_text(self, target_dir, json_name): def covert_json_to_text(self, target_dir, json_name):
json_path = os.path.join(self._json_dir, json_name) """Convert json file to yolo format text file and save them to files"""
json_data = json.load(open(json_path)) with open(json_name, encoding="utf-8") as file:
json_data = json.load(file)
print('Converting %s for %s ...' % filename: str = uuid.UUID(int=random.Random().getrandbits(128)).hex
(json_name, target_dir.replace('/', ''))) image_name = f"{filename}.png"
label_name = f"{filename}.txt"
img_path = save_yolo_image(json_data, img_path = save_yolo_image(
json_name, json_data, self._json_dir, self._image_dir_path, target_dir, image_name
self._image_dir_path, )
target_dir)
yolo_obj_list = self._get_yolo_object_list(json_data, img_path) yolo_obj_list = self._get_yolo_object_list(json_data, img_path)
save_yolo_label(json_name, save_yolo_label(yolo_obj_list, self._label_dir_path, target_dir, label_name)
self._label_dir_path,
target_dir,
yolo_obj_list)
def convert_one(self, json_name): def convert_one(self, json_name):
"""Convert one json file to yolo format text file and save them to files"""
json_path = os.path.join(self._json_dir, json_name) json_path = os.path.join(self._json_dir, json_name)
json_data = json.load(open(json_path)) with open(json_path, encoding="utf-8") as file:
json_data = json.load(file)
print('Converting %s ...' % json_name) image_name = json_name.replace(".json", ".png")
label_name = json_name.replace(".json", ".txt")
img_path = save_yolo_image(json_data, json_name, img_path = save_yolo_image(
self._json_dir, '') json_data, self._json_dir, self._image_dir_path, "", image_name
)
yolo_obj_list = self._get_yolo_object_list(json_data, img_path) yolo_obj_list = self._get_yolo_object_list(json_data, img_path)
save_yolo_label(json_name, self._json_dir, save_yolo_label(yolo_obj_list, self._label_dir_path, "", label_name)
'', yolo_obj_list)
def _get_yolo_object_list(self, json_data, img_path): def _get_yolo_object_list(self, json_data, img_path):
yolo_obj_list = [] yolo_obj_list = []
@ -251,65 +291,86 @@ class Labelme2YOLO(object):
for shape in json_data["shapes"]: for shape in json_data["shapes"]:
# labelme circle shape is different from others # labelme circle shape is different from others
# it only has 2 points, 1st is circle center, 2nd is drag end point # it only has 2 points, 1st is circle center, 2nd is drag end point
if shape['shape_type'] == 'circle': if shape["shape_type"] == "circle":
yolo_obj = self._get_circle_shape_yolo_object( yolo_obj = self._get_circle_shape_yolo_object(shape, img_h, img_w)
shape, img_h, img_w)
else: else:
yolo_obj = self._get_other_shape_yolo_object( yolo_obj = self._get_other_shape_yolo_object(shape, img_h, img_w)
shape, img_h, img_w)
yolo_obj_list.append(yolo_obj) if yolo_obj:
yolo_obj_list.append(yolo_obj)
return yolo_obj_list return yolo_obj_list
def _get_circle_shape_yolo_object(self, shape, img_h, img_w): def _get_circle_shape_yolo_object(self, shape, img_h, img_w):
obj_center_x, obj_center_y = shape['points'][0] obj_center_x, obj_center_y = shape["points"][0]
radius = math.sqrt((obj_center_x - shape['points'][1][0]) ** 2 + radius = math.sqrt(
(obj_center_y - shape['points'][1][1]) ** 2) (obj_center_x - shape["points"][1][0]) ** 2
obj_w = 2 * radius + (obj_center_y - shape["points"][1][1]) ** 2
obj_h = 2 * radius )
num_points = 36
points = np.zeros(2 * num_points)
for i in range(num_points):
angle = 2.0 * math.pi * i / num_points
points[2 * i] = (obj_center_x + radius * math.cos(angle)) / img_w
points[2 * i + 1] = (obj_center_y + radius * math.sin(angle)) / img_h
yolo_center_x = round(float(obj_center_x / img_w), 6) if shape["label"]:
yolo_center_y = round(float(obj_center_y / img_h), 6) label = shape["label"]
yolo_w = round(float(obj_w / img_w), 6) if label not in self._label_list:
yolo_h = round(float(obj_h / img_h), 6) self._update_id_map(label)
label_id = self._label_id_map[shape["label"]]
label_id = self._label_id_map[shape['label']] return label_id, points.tolist()
return label_id, yolo_center_x, yolo_center_y, yolo_w, yolo_h return None
def _get_other_shape_yolo_object(self, shape, img_h, img_w): def _get_other_shape_yolo_object(self, shape, img_h, img_w):
point_list = shape["points"]
point_list = shape['points']
points = np.zeros(2 * len(point_list)) points = np.zeros(2 * len(point_list))
points[::2] = [float(point[0]) / img_w for point in point_list] points[::2] = [float(point[0]) / img_w for point in point_list]
points[1::2] = [float(point[1]) / img_h for point in point_list] points[1::2] = [float(point[1]) / img_h for point in point_list]
if len(points) == 4: if len(points) == 4:
if self._output_format == "polygon": if self._output_format == "polygon":
points = extend_point_list(points) points = extend_point_list(points)
if self._output_format == "bbox": if self._output_format == "bbox":
points = extend_point_list(points, "bbox") points = extend_point_list(points, "bbox")
label_id = self._label_id_map[shape['label']]
return label_id, points.tolist() if shape["label"]:
label = shape["label"]
if label not in self._label_list:
self._update_id_map(label)
label_id = self._label_id_map[shape["label"]]
return label_id, points.tolist()
return None
def _save_dataset_yaml(self): def _save_dataset_yaml(self):
yaml_path = os.path.join( yaml_path = os.path.join(self._json_dir, "YOLODataset/", "dataset.yaml")
self._json_dir, 'YOLODataset/', 'dataset.yaml')
with open(yaml_path, 'w+') as yaml_file: with open(yaml_path, "w+", encoding="utf-8") as yaml_file:
yaml_file.write('train: %s\n' % train_dir = os.path.join(self._image_dir_path, "train/")
os.path.join(self._image_dir_path, 'train/')) val_dir = os.path.join(self._image_dir_path, "val/")
yaml_file.write('val: %s\n' % test_dir = os.path.join(self._image_dir_path, "test/")
os.path.join(self._image_dir_path, 'val/'))
yaml_file.write('test: %s\n' %
os.path.join(self._image_dir_path, 'test/'))
yaml_file.write('nc: %i\n' % len(self._label_id_map))
names_str = ''
names_str = ""
for label, _ in self._label_id_map.items(): for label, _ in self._label_id_map.items():
names_str += "'%s', " % label names_str += f'"{label}", '
names_str = names_str.rstrip(", ") names_str = names_str.rstrip(", ")
yaml_file.write("names: [%s]" % names_str)
if os.path.exists(test_dir):
content = (
f"train: {train_dir}\nval: {val_dir}\ntest: {test_dir}\n"
f"nc: {len(self._label_id_map)}\n"
f"names: [{names_str}]"
)
else:
content = (
f"train: {train_dir}\nval: {val_dir}\n"
f"nc: {len(self._label_id_map)}\n"
f"names: [{names_str}]"
)
yaml_file.write(content)