diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c803cef..b140c87 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -63,5 +63,7 @@ jobs: pip list - name: Unittest + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} run: | - pytest \ No newline at end of file + pytest -s \ No newline at end of file diff --git a/.github/workflows/package_ci.yml b/.github/workflows/package_ci.yml index 4fc1b02..987519d 100644 --- a/.github/workflows/package_ci.yml +++ b/.github/workflows/package_ci.yml @@ -60,5 +60,7 @@ jobs: pip list - name: Unittest + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} run: | - pytest \ No newline at end of file + pytest -s \ No newline at end of file diff --git a/tests/test_cli.py b/tests/test_cli.py index 4ee3d1b..2aecac8 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3,7 +3,7 @@ import subprocess from pathlib import Path -from ultralytics.yolo.utils import ROOT, SETTINGS +from ultralytics.yolo.utils import SETTINGS MODEL = Path(SETTINGS['weights_dir']) / 'yolov8n' CFG = 'yolov8n' diff --git a/tests/test_hf.py b/tests/test_hf.py index e7aeba7..b803e90 100644 --- a/tests/test_hf.py +++ b/tests/test_hf.py @@ -3,7 +3,10 @@ download_from_hub, postprocess_classify_output, render_result, + push_to_hfhub, ) +import subprocess +import os hub_id = "ultralyticsplus/yolov8s" @@ -56,3 +59,36 @@ def test_classification_inference(): results = model(image, imgsz=640) name_to_probs = postprocess_classify_output(model=model, result=results[0]) name_to_probs + + +def run(cmd): + # Run a subprocess command with check=True + subprocess.run(cmd.split(), check=True) + + +def test_detection_upload(): + import platform + from packaging.version import Version + + # run following lines if linux and python major == 3 and python minor == 10 (python micor can be anything) + print(f'platform.system(): {platform.system()}') + print(f'platform.python_version(): {platform.python_version()}') + print(f'os.getenv("RUNNER_OS"): {os.getenv("RUNNER_OS")}') + print(f'Version(platform.python_version()) >= Version("3.10"): {Version(platform.python_version()) >= Version("3.10")}') + if platform.system() == 'Linux' and Version(platform.python_version()) >= Version("3.10"): + print('training started') + run('yolo train detect model=yolov8n.pt data=coco8.yaml imgsz=32 epochs=1') + print('training ended') + hf_token = os.getenv('HF_TOKEN') + if hf_token is None: + raise ValueError('Please set HF_TOKEN environment variable to your HuggingFace token.') + print('push to hub started') + push_to_hfhub( + hf_model_id="fcakyon/yolov8n-test", + exp_dir='runs/detect/train', + hf_token=os.getenv('HF_TOKEN'), + hf_private=True, + hf_dataset_id="fcakyon/football-detection", + thumbnail_text='YOLOv8s Football Detection' + ) + print('push to hub ended') \ No newline at end of file diff --git a/ultralyticsplus/__init__.py b/ultralyticsplus/__init__.py index ea43ff2..6905d00 100644 --- a/ultralyticsplus/__init__.py +++ b/ultralyticsplus/__init__.py @@ -1,4 +1,4 @@ from .hf_utils import download_from_hub, push_to_hfhub from .ultralytics_utils import YOLO, postprocess_classify_output, render_result -__version__ = "0.0.16" +__version__ = "0.0.17" diff --git a/ultralyticsplus/hf_utils.py b/ultralyticsplus/hf_utils.py index 188ae4d..96739f3 100644 --- a/ultralyticsplus/hf_utils.py +++ b/ultralyticsplus/hf_utils.py @@ -204,7 +204,7 @@ def generate_thumbnail( text=thumbnail_text, pil_image=read_image_as_pil(image_path_or_url), brightness=0.60, - text_font=65, + text_font_size=65, crop_margin=None, ) @@ -508,14 +508,14 @@ def push_to_hfhub( score_top5_acc = None if task in ["object-detection", "instance-segmentation"]: # read the largest value in metrics/mAP50(B) column from csv file named results.csv - score_map50 = results_df["metrics/mAP50(B)"].max() + score_map50 = results_df["metrics/mAP50(B)"].max().item() if task == "instance-segmentation": # read the largest value in metrics/mAP50(B) metrics/mAP50(M) columns from csv file named results.csv - score_map50_mask = results_df["metrics/mAP50(M)"].max() + score_map50_mask = results_df["metrics/mAP50(M)"].max().item() if task == "image-classification": # read the largest value in metrics/accuracy_top1 metrics/accuracy_top5 columns from csv file named results.csv - score_top1_acc = results_df["metrics/accuracy_top1"].max() - score_top5_acc = results_df["metrics/accuracy_top5"].max() + score_top1_acc = results_df["metrics/accuracy_top1"].max().item() + score_top5_acc = results_df["metrics/accuracy_top5"].max().item() _push_to_hfhub( hf_model_id=hf_model_id, diff --git a/ultralyticsplus/other_utils.py b/ultralyticsplus/other_utils.py index cbf9a6c..90bfd36 100644 --- a/ultralyticsplus/other_utils.py +++ b/ultralyticsplus/other_utils.py @@ -1,11 +1,32 @@ +import os +from PIL.ImageFont import FreeTypeFont + +FONT_URL = "https://github.com/fcakyon/ultralyticsplus/releases/download/0.0.16/OpenSans-Bold.ttf" + + +def get_font(size: int) -> FreeTypeFont: + from PIL import ImageFont + + try: + font = ImageFont.truetype("OpenSans-Bold.ttf", size) + except OSError: + from sahi.utils.file import download_from_url + + package_dir = os.path.dirname(os.path.abspath(__file__)) + font_path = os.path.join(package_dir, "OpenSans-Bold.ttf") + download_from_url(from_url=FONT_URL, to_path=font_path) + font = ImageFont.truetype(font_path, size) + return font + + def add_text_to_image( text, pil_image, brightness: float = 0.75, - text_font: int = 50, + text_font_size: int = 50, crop_margin: int = None, ): - from PIL import ImageDraw, ImageFont, ImageEnhance + from PIL import ImageDraw, ImageEnhance RESIZE_WIDTH_TO = 1280 MAX_TEXT_WIDTH = 860 @@ -31,7 +52,7 @@ def add_text_to_image( draw = ImageDraw.Draw(pil_image) # Define the font and text to be written - font = ImageFont.truetype("OpenSans-Bold.ttf", text_font) + font = get_font(size=text_font_size) # Get the bounding box of the text bbox = draw.textbbox((0, 0), text, font=font) @@ -42,8 +63,8 @@ def add_text_to_image( # update font size if text_width > MAX_TEXT_WIDTH: - text_font = int(MAX_TEXT_WIDTH / text_width * text_font) - font = ImageFont.truetype("OpenSans-Bold.ttf", text_font) + text_font_size = int(MAX_TEXT_WIDTH / text_width * text_font_size) + font = get_font(size=text_font_size) bbox = draw.textbbox((0, 0), text, font=font) text_width = bbox[2] - bbox[0] text_height = bbox[3] - bbox[1]