Commit 6ea4ee20 authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): add hand detection

parent f3e7cfa7
Loading
Loading
Loading
Loading

zoo/detection/hand.py

0 → 100644
+26 −0
Original line number Diff line number Diff line
import os.path

from ultralytics import YOLO

from ..base import _TRAIN_DIR as _GLOBAL_TRAIN_DIR

_TRAIN_DIR = os.path.join(_GLOBAL_TRAIN_DIR, 'hand_detect')


def train(train_cfg: str, session_name: str, level: str = 's',
          max_epochs: int = 200, **kwargs):
    # Load a pretrained YOLO model (recommended for training)
    _last_pt = os.path.join(_TRAIN_DIR, session_name, 'weights', 'last.pt')
    if os.path.exists(_last_pt):
        model, resume = YOLO(_last_pt), True
    else:
        model, resume = YOLO(f'yolov8{level}.pt'), False

    # Train the model using the 'coco128.yaml' dataset for 3 epochs
    model.train(
        data=train_cfg, epochs=max_epochs,
        name=session_name, project=_TRAIN_DIR,
        save=True, plots=True,
        exist_ok=True, resume=resume,
        **kwargs
    )

zoo/face_detect.py

0 → 100644
+44 −0
Original line number Diff line number Diff line
import os.path
from functools import partial
from typing import List

import click
from huggingface_hub import hf_hub_download
from tqdm.auto import tqdm
from ultralytics import YOLO

from .detection.onnx import export_yolo_to_onnx
from .utils import GLOBAL_CONTEXT_SETTINGS
from .utils import print_version as _origin_print_version

print_version = partial(_origin_print_version, 'zoo.face_detect')


@click.group(context_settings={**GLOBAL_CONTEXT_SETTINGS})
@click.option('-v', '--version', is_flag=True,
              callback=print_version, expose_value=False, is_eager=True,
              help="Show version information.")
def cli():
    pass  # pragma: no cover


_KNOWN_CKPTS: List[str] = [
    'face_detect_best_s.pt',
    'face_detect_best_n.pt',
]


@cli.command('export', help='Export all models as onnx.',
             context_settings={**GLOBAL_CONTEXT_SETTINGS})
@click.option('--output_dir', '-O', 'output_dir', type=click.Path(file_okay=False), required=True,
              help='Output directory of all models.', show_default=True)
def export(output_dir: str):
    for ckpt in tqdm(_KNOWN_CKPTS):
        yolo = YOLO(hf_hub_download('deepghs/imgutils-models', f'face_detect/{ckpt}'))
        filebody, _ = os.path.splitext(ckpt)
        output_file = os.path.join(output_dir, f'{filebody}.onnx')
        export_yolo_to_onnx(yolo, output_file)


if __name__ == '__main__':
    cli()

zoo/hand_detect.py

0 → 100644
+44 −0
Original line number Diff line number Diff line
import os.path
from functools import partial
from typing import List

import click
from huggingface_hub import hf_hub_download
from tqdm.auto import tqdm
from ultralytics import YOLO

from .detection.onnx import export_yolo_to_onnx
from .utils import GLOBAL_CONTEXT_SETTINGS
from .utils import print_version as _origin_print_version

print_version = partial(_origin_print_version, 'zoo.hand_detect')


@click.group(context_settings={**GLOBAL_CONTEXT_SETTINGS})
@click.option('-v', '--version', is_flag=True,
              callback=print_version, expose_value=False, is_eager=True,
              help="Show version information.")
def cli():
    pass  # pragma: no cover


_KNOWN_CKPTS: List[str] = [
    'hand_detect_best_s.pt',
    'hand_detect_best_n.pt',
]


@cli.command('export', help='Export all models as onnx.',
             context_settings={**GLOBAL_CONTEXT_SETTINGS})
@click.option('--output_dir', '-O', 'output_dir', type=click.Path(file_okay=False), required=True,
              help='Output directory of all models.', show_default=True)
def export(output_dir: str):
    for ckpt in tqdm(_KNOWN_CKPTS):
        yolo = YOLO(hf_hub_download('deepghs/imgutils-models', f'hand_detect/{ckpt}'))
        filebody, _ = os.path.splitext(ckpt)
        output_file = os.path.join(output_dir, f'{filebody}.onnx')
        export_yolo_to_onnx(yolo, output_file)


if __name__ == '__main__':
    cli()