Skip to content

Commit

Permalink
✨ Feat: Make Data based on Json
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerohertz committed Nov 17, 2023
1 parent 2f9eaff commit 85e3220
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
3 changes: 2 additions & 1 deletion zerohertzLib/util/__init__.py
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"]
101 changes: 101 additions & 0 deletions zerohertzLib/util/data.py
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))

0 comments on commit 85e3220

Please sign in to comment.