Compare commits
36 Commits
Author | SHA1 | Date |
---|---|---|
|
04f7d2d259 | |
|
b151acd61c | |
|
9f21f0adc4 | |
|
513dbc5609 | |
|
94382c05f6 | |
|
1ceae85190 | |
|
6bc4e27231 | |
|
1e697c78e9 | |
|
f777359f33 | |
|
94c14a6430 | |
|
dc8d885342 | |
|
efca771a3f | |
|
7946c199cf | |
|
cc4171e182 | |
|
dba6184a50 | |
|
acbf514894 | |
|
054cfeb3ec | |
|
9f2f443dab | |
|
10dacce294 | |
|
1acb4112bf | |
|
e32a34253f | |
|
aafd10e9e6 | |
|
2fda40879b | |
|
aefad9e1ab | |
|
80fbc35f51 | |
|
9cccb7692f | |
|
154d4e501f | |
|
1cd66654cb | |
|
df4f172722 | |
|
9370343e5e | |
|
a47ee2c816 | |
|
fd48196639 | |
|
a5a8f92059 | |
|
20110cbcfa | |
|
4af61739a4 | |
|
c008d6c621 |
|
@ -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')
|
|
@ -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 }}
|
|
@ -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
101
README.md
|
@ -1,89 +1,78 @@
|
|||
**Forked from [rooneysh/Labelme2YOLO](https://github.com/rooneysh/Labelme2YOLO)**
|
||||
|
||||
# Labelme2YOLO
|
||||
|
||||
[](https://pypi.org/project/labelme2yolo)
|
||||

|
||||
[](https://pepy.tech/project/labelme2yolo)
|
||||
[](https://pypi.org/project/labelme2yolo)
|
||||
[](https://www.codacy.com/gh/GreatV/labelme2yolo/dashboard?utm_source=github.com&utm_medium=referral&utm_content=GreatV/labelme2yolo&utm_campaign=Badge_Grade)
|
||||
[](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.
|
||||
If you've already marked your segmentation dataset by LabelMe, it's easy to use this tool to help converting to YOLO format dataset.
|
||||
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.
|
||||
|
||||
---------
|
||||
## 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
|
||||
|
||||
```console
|
||||
```shell
|
||||
pip install labelme2yolo
|
||||
```
|
||||
|
||||
## Parameters Explain
|
||||
**--json_dir** LabelMe JSON files folder path.
|
||||
## Arguments
|
||||
|
||||
**--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
|
||||
|
||||
### 1. Convert JSON files, split training, validation and test dataset by --val_size and --test_size
|
||||
Put all LabelMe JSON files under **labelme_json_dir**, and run this python command.
|
||||
```bash
|
||||
### 1. Converting JSON files and splitting training, validation datasets
|
||||
|
||||
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
|
||||
```
|
||||
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/test/
|
||||
/path/to/labelme_json_dir/YOLODataset/labels/val/
|
||||
/path/to/labelme_json_dir/YOLODataset/images/train/
|
||||
/path/to/labelme_json_dir/YOLODataset/images/test/
|
||||
/path/to/labelme_json_dir/YOLODataset/images/val/
|
||||
|
||||
/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
|
||||
|
||||
1. [install hatch](https://hatch.pypa.io/latest/install/)
|
||||
|
@ -95,4 +84,6 @@ hatch build
|
|||
|
||||
## 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.
|
||||
|
|
|
@ -6,27 +6,27 @@ build-backend = "hatchling.build"
|
|||
name = "labelme2yolo"
|
||||
description = "This script converts the JSON format output by LabelMe to the text format required by YOLO serirs."
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.7"
|
||||
requires-python = ">=3.8"
|
||||
license = "MIT"
|
||||
keywords = []
|
||||
authors = [
|
||||
{ name = "GreatV(Wang Xin)", email = "xinwang614@gmail.com" },
|
||||
]
|
||||
classifiers = [
|
||||
"Development Status :: 4 - Beta",
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
"Programming Language :: Python",
|
||||
"Programming Language :: Python :: 3.7",
|
||||
"Programming Language :: Python :: 3.8",
|
||||
"Programming Language :: Python :: 3.9",
|
||||
"Programming Language :: Python :: 3.10",
|
||||
"Programming Language :: Python :: Implementation :: CPython",
|
||||
"Programming Language :: Python :: Implementation :: PyPy",
|
||||
"Programming Language :: Python :: 3.11",
|
||||
"Programming Language :: Python :: 3.12",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
]
|
||||
dependencies = [
|
||||
"opencv-python>=4.1.2",
|
||||
"Pillow>=9.2,<9.5",
|
||||
"scikit-learn>=1.1.1,<1.3.0",
|
||||
"numpy>=1.23.1,<1.25.0"
|
||||
"Pillow>=9.2,<10.4",
|
||||
"numpy>=1.23.1,<1.27.0",
|
||||
"rich"
|
||||
]
|
||||
dynamic = ["version"]
|
||||
|
||||
|
@ -54,7 +54,7 @@ cov = "pytest --cov-report=term-missing --cov-config=pyproject.toml --cov=labelm
|
|||
no-cov = "cov --no-cov"
|
||||
|
||||
[[tool.hatch.envs.test.matrix]]
|
||||
python = ["37", "38", "39", "310"]
|
||||
python = ["38", "39", "310", "311", "312"]
|
||||
|
||||
[tool.coverage.run]
|
||||
branch = true
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
opencv-python
|
||||
Pillow
|
||||
scikit-learn
|
||||
numpy
|
||||
rich
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
# SPDX-FileCopyrightText: 2023-present Wang Xin <xinwang614@gmail.com>
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
__version__ = '0.0.9'
|
||||
"""
|
||||
about version
|
||||
"""
|
||||
__version__ = "0.1.7"
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
# SPDX-FileCopyrightText: 2022-present Wang Xin <xinwang614@gmail.com>
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""
|
||||
main
|
||||
"""
|
||||
import sys
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -1,13 +1,22 @@
|
|||
# SPDX-FileCopyrightText: 2022-present Wang Xin <xinwang614@gmail.com>
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""
|
||||
cli init
|
||||
"""
|
||||
import argparse
|
||||
|
||||
from labelme2yolo.__about__ import __version__
|
||||
from labelme2yolo.l2y import Labelme2YOLO
|
||||
|
||||
|
||||
def run():
|
||||
"""
|
||||
run cli
|
||||
"""
|
||||
parser = argparse.ArgumentParser("labelme2yolo")
|
||||
parser.add_argument(
|
||||
"-v", "--version", action="version", version="%(prog)s " + __version__
|
||||
)
|
||||
parser.add_argument(
|
||||
"--json_dir", type=str, help="Please input the path of the labelme json files."
|
||||
)
|
||||
|
@ -15,14 +24,14 @@ def run():
|
|||
"--val_size",
|
||||
type=float,
|
||||
nargs="?",
|
||||
default=None,
|
||||
help="Please input the validation dataset size, for example 0.1.",
|
||||
default=0.2,
|
||||
help="Please input the validation dataset size, for example 0.2.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--test_size",
|
||||
type=float,
|
||||
nargs="?",
|
||||
default=None,
|
||||
default=0.0,
|
||||
help="Please input the test dataset size, for example 0.1.",
|
||||
)
|
||||
parser.add_argument(
|
||||
|
@ -39,6 +48,14 @@ def run():
|
|||
help='The default output format for labelme2yolo is "polygon".'
|
||||
' 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()
|
||||
|
||||
|
@ -46,7 +63,7 @@ def run():
|
|||
parser.print_help()
|
||||
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:
|
||||
convertor.convert(val_size=args.val_size, test_size=args.test_size)
|
||||
|
|
|
@ -5,36 +5,58 @@ Created on Aug 18, 2021
|
|||
@author: GreatV(Wang Xin)
|
||||
"""
|
||||
import base64
|
||||
import glob
|
||||
import io
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import random
|
||||
import shutil
|
||||
from collections import OrderedDict
|
||||
from multiprocessing import Pool
|
||||
import uuid
|
||||
import logging
|
||||
from functools import partial
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
from multiprocessing import Pool
|
||||
import PIL.ExifTags
|
||||
import PIL.Image
|
||||
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
|
||||
NUM_THREADS = max(1, os.cpu_count() - 1)
|
||||
def train_test_split(dataset_index, test_size=0.2):
|
||||
"""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
|
||||
def img_data_to_pil(img_data):
|
||||
f = io.BytesIO()
|
||||
f.write(img_data)
|
||||
img_pil = PIL.Image.open(f)
|
||||
"""Convert img_data(byte) to PIL.Image"""
|
||||
file = io.BytesIO()
|
||||
file.write(img_data)
|
||||
img_pil = PIL.Image.open(file)
|
||||
return img_pil
|
||||
|
||||
|
||||
# copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py
|
||||
def img_data_to_arr(img_data):
|
||||
"""Convert img_data(byte) to numpy.ndarray"""
|
||||
img_pil = img_data_to_pil(img_data)
|
||||
img_arr = np.array(img_pil)
|
||||
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
|
||||
def img_b64_to_arr(img_b64):
|
||||
"""Convert img_b64(str) to numpy.ndarray"""
|
||||
img_data = base64.b64decode(img_b64)
|
||||
img_arr = img_data_to_arr(img_data)
|
||||
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
|
||||
def img_pil_to_data(img_pil):
|
||||
f = io.BytesIO()
|
||||
img_pil.save(f, format="PNG")
|
||||
img_data = f.getvalue()
|
||||
"""Convert PIL.Image to img_data(byte)"""
|
||||
file = io.BytesIO()
|
||||
img_pil.save(file, format="PNG")
|
||||
img_data = file.getvalue()
|
||||
return img_data
|
||||
|
||||
|
||||
# copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py
|
||||
def img_arr_to_b64(img_arr):
|
||||
"""Convert numpy.ndarray to img_b64(str)"""
|
||||
img_pil = PIL.Image.fromarray(img_arr)
|
||||
f = io.BytesIO()
|
||||
img_pil.save(f, format="PNG")
|
||||
img_bin = f.getvalue()
|
||||
if hasattr(base64, "encodebytes"):
|
||||
img_b64 = base64.encodebytes(img_bin)
|
||||
else:
|
||||
img_b64 = base64.encodestring(img_bin)
|
||||
file = io.BytesIO()
|
||||
img_pil.save(file, format="PNG")
|
||||
img_bin = file.getvalue()
|
||||
img_b64 = base64.encodebytes(img_bin)
|
||||
return img_b64
|
||||
|
||||
|
||||
# copy form https://github.com/wkentaro/labelme/blob/main/labelme/utils/image.py
|
||||
def img_data_to_png_data(img_data):
|
||||
"""Convert img_data(byte) to png_data(byte)"""
|
||||
with io.BytesIO() as f_out:
|
||||
f_out.write(img_data)
|
||||
img = PIL.Image.open(f_out)
|
||||
|
@ -80,169 +103,186 @@ def img_data_to_png_data(img_data):
|
|||
return f_in.read()
|
||||
|
||||
|
||||
def get_label_id_map(json_dir):
|
||||
label_set = set()
|
||||
def extend_point_list(point_list, out_format="polygon"):
|
||||
"""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 file_name.endswith("json"):
|
||||
json_path = os.path.join(json_dir, file_name)
|
||||
data = json.load(open(json_path))
|
||||
for shape in data["shapes"]:
|
||||
label_set.add(shape["label"])
|
||||
if out_format == "bbox":
|
||||
x_i = x_min
|
||||
y_i = y_min
|
||||
w_i = x_max - x_min
|
||||
h_i = y_max - y_min
|
||||
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"):
|
||||
xmin = min([float(point) for point in point_list[::2]])
|
||||
xmax = max([float(point) for point in point_list[::2]])
|
||||
ymin = min([float(point) for point in point_list[1::2]])
|
||||
ymax = max([float(point) for point in point_list[1::2]])
|
||||
def save_yolo_label(obj_list, label_dir, target_dir, target_name):
|
||||
"""Save yolo label to txt file"""
|
||||
txt_path = os.path.join(label_dir, target_dir, target_name)
|
||||
|
||||
if (format == "polygon"):
|
||||
return np.array([xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax])
|
||||
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
|
||||
with open(txt_path, "w+", encoding="utf-8") as file:
|
||||
for label, points in obj_list:
|
||||
points = [str(item) for item in points]
|
||||
yolo_obj_line = f"{label} {' '.join(points)}\n"
|
||||
f.write(yolo_obj_line)
|
||||
line = f"{label} {' '.join(points)}\n"
|
||||
file.write(line)
|
||||
|
||||
|
||||
def save_yolo_image(json_data, json_name, image_dir_path, target_dir):
|
||||
img_name = json_name.replace(".json", ".png")
|
||||
img_path = os.path.join(image_dir_path, target_dir, img_name)
|
||||
def save_yolo_image(json_data, json_dir, image_dir, target_dir, target_name):
|
||||
"""Save yolo image to image_dir_path/target_dir"""
|
||||
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"])
|
||||
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
|
||||
|
||||
|
||||
class Labelme2YOLO(object):
|
||||
class Labelme2YOLO:
|
||||
"""Labelme to YOLO format converter"""
|
||||
|
||||
def __init__(self, json_dir, output_format):
|
||||
self._json_dir = json_dir
|
||||
def __init__(self, json_dir, output_format, label_list):
|
||||
self._json_dir = os.path.expanduser(json_dir)
|
||||
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):
|
||||
self._label_dir_path = os.path.join(self._json_dir,
|
||||
'YOLODataset/labels/')
|
||||
self._image_dir_path = os.path.join(self._json_dir,
|
||||
'YOLODataset/images/')
|
||||
def _update_id_map(self, label: str):
|
||||
if label not in self._label_list:
|
||||
self._label_list.append(label)
|
||||
self._label_id_map[label] = len(self._label_id_map)
|
||||
|
||||
for yolo_path in (os.path.join(self._label_dir_path + 'train/'),
|
||||
os.path.join(self._label_dir_path + 'val/'),
|
||||
os.path.join(self._label_dir_path + 'test/'),
|
||||
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)
|
||||
def _make_train_val_dir(self, create_test_dir=False):
|
||||
self._label_dir_path = os.path.join(self._json_dir, "YOLODataset/labels/")
|
||||
self._image_dir_path = os.path.join(self._json_dir, "YOLODataset/images/")
|
||||
|
||||
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):
|
||||
if len(folders) > 0 and 'train' in folders and 'val' in folders and 'test' in folders:
|
||||
train_folder = os.path.join(self._json_dir, 'train/')
|
||||
train_json_names = [train_sample_name + '.json'
|
||||
for train_sample_name in os.listdir(train_folder)
|
||||
if os.path.isdir(os.path.join(train_folder, train_sample_name))]
|
||||
parts = ["train", "val", "test"] if create_test_dir else ["train", "val"]
|
||||
image_dirs = [os.path.join(self._image_dir_path, part) for part in parts]
|
||||
label_dirs = [os.path.join(self._label_dir_path, part) for part in parts]
|
||||
dirs = image_dirs + label_dirs
|
||||
for yolo_path in dirs:
|
||||
os.makedirs(yolo_path, exist_ok=True)
|
||||
|
||||
val_folder = os.path.join(self._json_dir, 'val/')
|
||||
val_json_names = [val_sample_name + '.json'
|
||||
for val_sample_name in os.listdir(val_folder)
|
||||
if os.path.isdir(os.path.join(val_folder, val_sample_name))]
|
||||
def _get_dataset_part_json_names(self, dataset_part: str):
|
||||
"""Get json names in dataset_part folder"""
|
||||
set_folder = os.path.join(self._json_dir, dataset_part)
|
||||
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/')
|
||||
test_json_names = [test_sample_name + '.json'
|
||||
for test_sample_name in os.listdir(test_folder)
|
||||
if os.path.isdir(os.path.join(test_folder, test_sample_name))]
|
||||
|
||||
return train_json_names, val_json_names, test_json_names
|
||||
|
||||
train_idxs, val_idxs = train_test_split(range(len(json_names)),
|
||||
test_size=val_size)
|
||||
tmp_train_len = len(train_idxs)
|
||||
test_idxs = []
|
||||
def _train_test_split(self, json_names, val_size, test_size=None):
|
||||
"""Split json names to train, val, test"""
|
||||
total_size = len(json_names)
|
||||
dataset_index = list(range(total_size))
|
||||
train_ids, val_ids = train_test_split(dataset_index, test_size=val_size)
|
||||
test_ids = []
|
||||
if test_size is None:
|
||||
test_size = 0.0
|
||||
if test_size > 1e-8:
|
||||
train_idxs, test_idxs = train_test_split(
|
||||
range(tmp_train_len), 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]
|
||||
test_json_names = [json_names[test_idx] for test_idx in test_idxs]
|
||||
if test_size > 0.0:
|
||||
train_ids, test_ids = train_test_split(
|
||||
train_ids, test_size=test_size / (1 - val_size)
|
||||
)
|
||||
train_json_names = [json_names[train_idx] for train_idx in train_ids]
|
||||
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
|
||||
|
||||
def convert(self, val_size, test_size):
|
||||
json_names = [file_name for file_name in os.listdir(self._json_dir)
|
||||
if os.path.isfile(os.path.join(self._json_dir, file_name)) and
|
||||
file_name.endswith('.json')]
|
||||
folders = [file_name for file_name in os.listdir(self._json_dir)
|
||||
if os.path.isdir(os.path.join(self._json_dir, file_name))]
|
||||
train_json_names, val_json_names, test_json_names = self._train_test_split(
|
||||
folders, json_names, val_size, test_size)
|
||||
"""Convert labelme format to yolo format"""
|
||||
json_names = glob.glob(
|
||||
os.path.join(self._json_dir, "**", "*.json"), recursive=True
|
||||
)
|
||||
json_names = sorted(json_names)
|
||||
|
||||
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
|
||||
# also get image from labelme json file and save them under images folder
|
||||
for target_dir, json_names in zip(('train/', 'val/', 'test/'),
|
||||
(train_json_names, val_json_names, test_json_names)):
|
||||
pool = Pool(NUM_THREADS)
|
||||
dirs = ("train", "val", "test")
|
||||
names = (train_json_names, val_json_names, test_json_names)
|
||||
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()
|
||||
|
||||
def covert_json_to_text(self, target_dir, json_name):
|
||||
json_path = os.path.join(self._json_dir, json_name)
|
||||
json_data = json.load(open(json_path))
|
||||
"""Convert json file to yolo format text file and save them to files"""
|
||||
with open(json_name, encoding="utf-8") as file:
|
||||
json_data = json.load(file)
|
||||
|
||||
print('Converting %s for %s ...' %
|
||||
(json_name, target_dir.replace('/', '')))
|
||||
|
||||
img_path = save_yolo_image(json_data,
|
||||
json_name,
|
||||
self._image_dir_path,
|
||||
target_dir)
|
||||
filename: str = uuid.UUID(int=random.Random().getrandbits(128)).hex
|
||||
image_name = f"{filename}.png"
|
||||
label_name = f"{filename}.txt"
|
||||
img_path = save_yolo_image(
|
||||
json_data, self._json_dir, self._image_dir_path, target_dir, image_name
|
||||
)
|
||||
yolo_obj_list = self._get_yolo_object_list(json_data, img_path)
|
||||
save_yolo_label(json_name,
|
||||
self._label_dir_path,
|
||||
target_dir,
|
||||
yolo_obj_list)
|
||||
save_yolo_label(yolo_obj_list, self._label_dir_path, target_dir, label_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_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)
|
||||
|
||||
img_path = save_yolo_image(json_data, json_name,
|
||||
self._json_dir, '')
|
||||
image_name = json_name.replace(".json", ".png")
|
||||
label_name = json_name.replace(".json", ".txt")
|
||||
img_path = save_yolo_image(
|
||||
json_data, self._json_dir, self._image_dir_path, "", image_name
|
||||
)
|
||||
|
||||
yolo_obj_list = self._get_yolo_object_list(json_data, img_path)
|
||||
save_yolo_label(json_name, self._json_dir,
|
||||
'', yolo_obj_list)
|
||||
save_yolo_label(yolo_obj_list, self._label_dir_path, "", label_name)
|
||||
|
||||
def _get_yolo_object_list(self, json_data, img_path):
|
||||
yolo_obj_list = []
|
||||
|
@ -251,65 +291,86 @@ class Labelme2YOLO(object):
|
|||
for shape in json_data["shapes"]:
|
||||
# labelme circle shape is different from others
|
||||
# it only has 2 points, 1st is circle center, 2nd is drag end point
|
||||
if shape['shape_type'] == 'circle':
|
||||
yolo_obj = self._get_circle_shape_yolo_object(
|
||||
shape, img_h, img_w)
|
||||
if shape["shape_type"] == "circle":
|
||||
yolo_obj = self._get_circle_shape_yolo_object(shape, img_h, img_w)
|
||||
else:
|
||||
yolo_obj = self._get_other_shape_yolo_object(
|
||||
shape, img_h, img_w)
|
||||
yolo_obj = self._get_other_shape_yolo_object(shape, img_h, img_w)
|
||||
|
||||
yolo_obj_list.append(yolo_obj)
|
||||
if yolo_obj:
|
||||
yolo_obj_list.append(yolo_obj)
|
||||
|
||||
return yolo_obj_list
|
||||
|
||||
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 +
|
||||
(obj_center_y - shape['points'][1][1]) ** 2)
|
||||
obj_w = 2 * radius
|
||||
obj_h = 2 * radius
|
||||
radius = math.sqrt(
|
||||
(obj_center_x - shape["points"][1][0]) ** 2
|
||||
+ (obj_center_y - shape["points"][1][1]) ** 2
|
||||
)
|
||||
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)
|
||||
yolo_center_y = round(float(obj_center_y / img_h), 6)
|
||||
yolo_w = round(float(obj_w / img_w), 6)
|
||||
yolo_h = round(float(obj_h / img_h), 6)
|
||||
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"]]
|
||||
|
||||
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):
|
||||
|
||||
point_list = shape['points']
|
||||
point_list = shape["points"]
|
||||
points = np.zeros(2 * len(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]
|
||||
|
||||
if len(points) == 4:
|
||||
if self._output_format == "polygon":
|
||||
points = extend_point_list(points)
|
||||
if self._output_format == "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):
|
||||
yaml_path = os.path.join(
|
||||
self._json_dir, 'YOLODataset/', 'dataset.yaml')
|
||||
yaml_path = os.path.join(self._json_dir, "YOLODataset/", "dataset.yaml")
|
||||
|
||||
with open(yaml_path, 'w+') as yaml_file:
|
||||
yaml_file.write('train: %s\n' %
|
||||
os.path.join(self._image_dir_path, 'train/'))
|
||||
yaml_file.write('val: %s\n' %
|
||||
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 = ''
|
||||
with open(yaml_path, "w+", encoding="utf-8") as yaml_file:
|
||||
train_dir = os.path.join(self._image_dir_path, "train/")
|
||||
val_dir = os.path.join(self._image_dir_path, "val/")
|
||||
test_dir = os.path.join(self._image_dir_path, "test/")
|
||||
|
||||
names_str = ""
|
||||
for label, _ in self._label_id_map.items():
|
||||
names_str += "'%s', " % label
|
||||
names_str += f'"{label}", '
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue