-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from zerohertzLib.util.data import MakeData | ||
from zerohertzLib.util.json import Json, JsonDir | ||
|
||
__all__ = ["Json", "JsonDir"] | ||
__all__ = ["Json", "JsonDir", "MakeData"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import os | ||
import shutil | ||
|
||
from tqdm import tqdm | ||
|
||
from .json import Json, JsonDir | ||
|
||
|
||
class MakeData: | ||
"""json νμΌ λ΄ κ°μ λ°λΌ dataλ₯Ό ꡬμΆνλ ν¨μ | ||
Args: | ||
dataPath (``str``): λͺ©ν dataκ° μ‘΄μ¬νλ directory κ²½λ‘ | ||
jsonPath (``str``): λͺ©ν json νμΌμ΄ μ‘΄μ¬νλ directory κ²½λ‘ | ||
jsonKey (``str``): ``dataPath`` μμ dataμ νμΌλͺ μ λνλ΄λ key κ° | ||
targetPath (``str``): Data κ΅¬μΆ κ²½λ‘ | ||
Attributes: | ||
gt (``zerohertzLib.util.JsonDir``): json νμΌλ€μ μ½μ΄ data κ΅¬μΆ μ νμ© | ||
daPath (``str``): ``{targetPath}/data`` | ||
gtPath (``str``): ``{targetPath}/json`` | ||
""" | ||
|
||
def __init__( | ||
self, | ||
dataPath: str, | ||
jsonPath: str, | ||
jsonKey: str, | ||
targetPath: str, | ||
) -> None: | ||
self.dataPath = dataPath | ||
self.jsonPath = jsonPath | ||
self.gt = JsonDir(jsonPath) | ||
self.jsonKey = self.gt._getKey(jsonKey) | ||
self.targetPath = targetPath | ||
self.daPath = os.path.abspath(os.path.join(self.targetPath, "data")) | ||
self.gtPath = os.path.abspath(os.path.join(self.targetPath, "json")) | ||
|
||
def condition(self, json_instance: Json) -> bool: | ||
"""Data κ΅¬μΆ μ filtering λ 쑰건 | ||
Args: | ||
json_instance (``zerohertzLib.util.Json``): ``Json`` instanceμ μ 보λ₯Ό ν΅ν΄ ꡬμΆν dataμ ν¬ν¨μν¬μ§ μ¬λΆ κ²°μ | ||
Returns: | ||
``bool``: Data ν¬ν¨ μ¬λΆ | ||
μλμ κ°μ΄ μμμ ν΅ν΄ 쑰건μ μ€μ ν μ μλ€. | ||
Examples: | ||
.. code-block:: python | ||
class MakeDataCar(MakeData): | ||
def condition(self, json_instance): | ||
key = json_instance._getKey("supercategory_name") | ||
category = json_instance._getValue(key) | ||
return category == "CityCar" or category == "Mid-size car" | ||
""" | ||
return True | ||
|
||
def make(self) -> None: | ||
"""Data κ΅¬μΆ μ€ν | ||
.. warning:: | ||
μ€ν μ ``targetPath`` μμ ν κ΅¬μΆ μ§ν | ||
Examples: | ||
>>> mdc = MakeDataCar(dataPath, jsonPath, jsonKey, targetPath) | ||
>>> mdc.make() | ||
100%|βββββββββββββ| 403559/403559 [00:54<00:00, 7369.96it/s] | ||
==================================================================================================== | ||
GT PATH: /.../data | ||
DATA PATH: /.../json | ||
==================================================================================================== | ||
100%|βββββββββββββ| 403559/403559 [01:04<00:00, 6292.39it/s] | ||
""" | ||
try: | ||
shutil.rmtree(self.targetPath) | ||
except: | ||
pass | ||
print("=" * 100) | ||
print("GT PATH:\t", self.daPath) | ||
print("DATA PATH:\t", self.gtPath) | ||
print("=" * 100) | ||
os.makedirs(self.daPath, exist_ok=True) | ||
os.makedirs(self.gtPath, exist_ok=True) | ||
for json_instance in tqdm(self.gt): | ||
dataName = json_instance._getValue(self.jsonKey) | ||
if self.condition(json_instance): | ||
try: | ||
shutil.copy( | ||
os.path.join(self.dataPath, dataName), | ||
os.path.join(self.daPath, dataName), | ||
) | ||
shutil.copy( | ||
os.path.join(self.jsonPath, json_instance.name), | ||
os.path.join(self.gtPath, json_instance.name), | ||
) | ||
except: | ||
print("Missing:\t", os.path.join(self.dataPath, dataName)) |