Hi, I was wondering if I can easily get the exact same parameters for the YOLO conversion tool (https://tools.luxonis.com/) in an API call. I have been unsuccessful in finding how to clone the process using the github repo (luxonis/tools). Anyone know how to do this?

  • Hi @ThomasVoets,

    Here's a demo how you can call the tools using an API call:

    from typing import Union, Tuple, Literal, Dict
    import requests
    from uuid import uuid4
    import os
    
    
    STATUS_OK: int = 200
    OUTPUT_FILE_NAME: str = "output.zip"
    DEFAULT_NSHAVES: int = 6
    DEFAULT_USE_LEGACY_FRONTEND: str = 'true'
    DEFAULT_USE_RVC2: str = 'true'
    URL_V7: str = "/yolov7"
    URL_V6R3: str = "/yolov6r3"
    URL_V6R1: str = "/yolov6r1"
    DEFAULT_URL: str = "https://tools.luxonis.com"
    
    
    def convert_yolo(file_path: str, shape: Union[int, Tuple[int, int]]=416, version: Literal["v5", "v6", "v6r2", "v6r4", "v7", "v8", "v9", "v10", "v11", "goldyolo"] = "v5", 
                        url: str=DEFAULT_URL, file_name:str=OUTPUT_FILE_NAME, log: bool=False, n_shaves: int=DEFAULT_NSHAVES, use_legacy: str=DEFAULT_USE_LEGACY_FRONTEND, use_rvc2: str=DEFAULT_USE_RVC2):
        """ Uploads Yolo weights and receives zip with compiled blob.
        
        :param file_path: Path to .pt weights
        :param shape: Integer or tuple with width and height - must be divisible by 32
        :param version: Version of the Yolo model
        :param url: tools' URL
        :param file_name: Output file
        :param log: Whether to switch on logging or not
        :param n_shaves: Number of shaves
        :param use_legacy: Whether to use the legacy frontend flag while compiling to IR representation or not
        :param use_rvc2: Whether to use the RVC2 or RVC3 conversion
        :returns: Status code and Path to downloaded zip file
        """
        # Variables
        with open(file_path,'rb') as input_file:
            files = {'file': input_file}
            values = {
                'inputshape': shape if isinstance(shape, int) else " ".join(map(str,shape)), 
                'version': version, 
                'id': uuid4(),
                'nShaves': n_shaves,
                'useLegacyFrontend': use_legacy,
                "useRVC2": use_rvc2
            }
            url = f"{url}/upload"
            if log:
                print(url)
            
            # Upload files
            session = requests.Session()
            try: 
                with session.post(url, files=files, data=values, stream=True) as r:
                    r.raise_for_status()
                    if log:
                        print(f"Conversion complete. Downloading...")
                    
                    with open(file_name, 'wb') as f:
                        for chunk in r.iter_content(chunk_size=8192): 
                            # If you have chunk encoded response uncomment if
                            # and set chunk_size parameter to None.
                            #if chunk: 
                            f.write(chunk)
                    if log:
                        print(r.status_code)
                session.close()
                return r.status_code, file_name
            except Exception as e:
                print(e)
                session.close()
                return 500, ""
    
    
    if __name__ == "__main__":
        convert_yolo(file_path="yolov6nr2.pt", shape=416, version="v6r2", url=f"{DEFAULT_URL}{URL_V6R3}")  

    Best,
    Jan

Hi @ThomasVoets,

Here's a demo how you can call the tools using an API call:

from typing import Union, Tuple, Literal, Dict
import requests
from uuid import uuid4
import os


STATUS_OK: int = 200
OUTPUT_FILE_NAME: str = "output.zip"
DEFAULT_NSHAVES: int = 6
DEFAULT_USE_LEGACY_FRONTEND: str = 'true'
DEFAULT_USE_RVC2: str = 'true'
URL_V7: str = "/yolov7"
URL_V6R3: str = "/yolov6r3"
URL_V6R1: str = "/yolov6r1"
DEFAULT_URL: str = "https://tools.luxonis.com"


def convert_yolo(file_path: str, shape: Union[int, Tuple[int, int]]=416, version: Literal["v5", "v6", "v6r2", "v6r4", "v7", "v8", "v9", "v10", "v11", "goldyolo"] = "v5", 
                    url: str=DEFAULT_URL, file_name:str=OUTPUT_FILE_NAME, log: bool=False, n_shaves: int=DEFAULT_NSHAVES, use_legacy: str=DEFAULT_USE_LEGACY_FRONTEND, use_rvc2: str=DEFAULT_USE_RVC2):
    """ Uploads Yolo weights and receives zip with compiled blob.
    
    :param file_path: Path to .pt weights
    :param shape: Integer or tuple with width and height - must be divisible by 32
    :param version: Version of the Yolo model
    :param url: tools' URL
    :param file_name: Output file
    :param log: Whether to switch on logging or not
    :param n_shaves: Number of shaves
    :param use_legacy: Whether to use the legacy frontend flag while compiling to IR representation or not
    :param use_rvc2: Whether to use the RVC2 or RVC3 conversion
    :returns: Status code and Path to downloaded zip file
    """
    # Variables
    with open(file_path,'rb') as input_file:
        files = {'file': input_file}
        values = {
            'inputshape': shape if isinstance(shape, int) else " ".join(map(str,shape)), 
            'version': version, 
            'id': uuid4(),
            'nShaves': n_shaves,
            'useLegacyFrontend': use_legacy,
            "useRVC2": use_rvc2
        }
        url = f"{url}/upload"
        if log:
            print(url)
        
        # Upload files
        session = requests.Session()
        try: 
            with session.post(url, files=files, data=values, stream=True) as r:
                r.raise_for_status()
                if log:
                    print(f"Conversion complete. Downloading...")
                
                with open(file_name, 'wb') as f:
                    for chunk in r.iter_content(chunk_size=8192): 
                        # If you have chunk encoded response uncomment if
                        # and set chunk_size parameter to None.
                        #if chunk: 
                        f.write(chunk)
                if log:
                    print(r.status_code)
            session.close()
            return r.status_code, file_name
        except Exception as e:
            print(e)
            session.close()
            return 500, ""


if __name__ == "__main__":
    convert_yolo(file_path="yolov6nr2.pt", shape=416, version="v6r2", url=f"{DEFAULT_URL}{URL_V6R3}")  

Best,
Jan

Hi all, this works! Thanks a lot for this!

Hi,

that's great to hear! You're welcome!

Kind regards,
Jan