towhee

towhee.api = <towhee.hparam.hyperparameter._api object>

Create an API input, for building RestFul API or application API.

Returns:

DataFrame containing the APi.

Return type:

DataFrame

Examples:

>>> from fastapi import FastAPI
>>> from fastapi.testclient import TestClient
>>> app = FastAPI()
>>> import towhee
>>> with towhee.api() as api:
...     app1 = (
...         api.map(lambda x: x+' -> 1')
...            .map(lambda x: x+' => 1')
...            .serve('/app1', app)
...     )
>>> with towhee.api['x']() as api:
...     app2 = (
...         api.runas_op['x', 'x_plus_1'](func=lambda x: x+' -> 2')
...            .runas_op['x_plus_1', 'y'](func=lambda x: x+' => 2')
...            .select['y']()
...            .serve('/app2', app)
...     )
>>> with towhee.api() as api:
...     app2 = (
...         api.parse_json()
...            .runas_op['x', 'x_plus_1'](func=lambda x: x+' -> 3')
...            .runas_op['x_plus_1', 'y'](func=lambda x: x+' => 3')
...            .select['y']()
...            .serve('/app3', app)
...     )
>>> client = TestClient(app)
>>> client.post('/app1', '1').text
'"1 -> 1 => 1"'
>>> client.post('/app2', '2').text
'{"y":"2 -> 2 => 2"}'
>>> client.post('/app3', '{"x": "3"}').text
'{"y":"3 -> 3 => 3"}'
towhee.dc = <towhee.hparam.hyperparameter._dc object>

Return a DataCollection.

Parameters:

iterable (iterable) – The iterable to wrap in a DataCollection.

Returns:

The wrapped iterable.

Return type:

DataCollection or DataFrame

Examples

  1. create a simple data collection;

>>> import towhee
>>> towhee.dc([0, 1, 2]).to_list()
[0, 1, 2]
  1. create a data collection of structural data.

>>> towhee.dc['column']([0, 1, 2]).to_list()
[<Entity dict_keys(['column'])>, <Entity dict_keys(['column'])>, <Entity dict_keys(['column'])>]
>>> towhee.dc['string', 'int']([['a', 1], ['b', 2], ['c', 3]]).to_list()
[<Entity dict_keys(['string', 'int'])>, <Entity dict_keys(['string', 'int'])>, <Entity dict_keys(['string', 'int'])>]
towhee.ops = <towhee.hparam.hyperparameter.ops object>

Create operator instance.

Entry point for creating operator instances, for example:

>>> op_instance = ops.my_namespace.my_repo_name(init_arg1=xxx, init_arg2=xxx)
towhee.register(name: Optional[str] = None, input_schema=None, output_schema=None, flag=None)

Register a class, function, or callable as a towhee operator.

Examples:

  1. register a function as operator

>>> from towhee import register
>>> @register
... def foo(x, y):
...     return x+y
  1. register a class as operator

>>> @register
... class foo_cls():
...     def __init__(self, x):
...         self.x = x
...     def __call__(self, y):
...         return self.x + y

By default, function/class name is used as operator name, which is used by the operator factory towhee.ops to invoke the operator.

>>> from towhee import ops
>>> op = ops.foo()
>>> op(1, 2)
3
>>> op = ops.foo_cls(x=2)
>>> op(3)
5
  1. register operator with an alternative name:

>>> @register(name='my_foo')
... def foo(x, y):
...     return x+y
>>> ops.my_foo()(1,2)
3

Operator URI and Namespace: The URI (unique reference identifier) of an operator has two parts: namespace and name. The namespace helps identify one operator and group the operators into various kinds. We can specific the namespace when create an operator:

>>> ops.anon.my_foo()(1,2)
3

anon is the default namespace to which an operator is registered if no namespace is specified. And it’s also the default searching namespace for the operator factory.

You can also specific the fullname, including namespace when register an operator:

>>> @register(name='my_namespace/my_foo')
... def foo(x, y):
...     return x+y
>>> ops.my_namespace.my_foo()(1,2)
3

Output Schema:

>>> @register(name='my_foo', output_schema='value')
... def foo(x, y):
...     return x+y
>>> from towhee.hparam import param_scope
>>> with param_scope('towhee.need_schema=1'):
...     ops.my_foo()(1,2)
Output(value=3)

Flag: Each operator type, for example: NNOperator and PyOperator, has their own default flag:

>>> from towhee.operator.base import Operator, NNOperator, PyOperator
>>> from towhee.operator.base import OperatorFlag
>>> @register
... class foo(NNOperator):
...     pass
>>> foo().flag
<OperatorFlag.REUSEABLE|STATELESS: 6>

The default flag can be override by register(flag=someflag):

>>> @register(flag=OperatorFlag.EMPTYFLAG)
... class foo(NNOperator):
...     pass
>>> foo().flag
<OperatorFlag.EMPTYFLAG: 1>
Parameters:
  • name (str, optional) – operator name, will use the class/function name if None.

  • input_schema (NamedTuple, optional) – input schema for the operator. Defaults to None.

  • output_schema (NamedTuple, optional) – output schema, will convert the operator output to NamedTuple if not None.

  • flag ([OperatorFlag], optional) – operator flag. Defaults to OperatorFlag.EMPTYFLAG.

Returns:

[description]

Return type:

[type]

towhee.read_camera(device_id=0, limit=-1)

Read images from a camera.

Parameters:
  • device_id (int, optional) – The camera device ID. Defaults to 0.

  • limit (int, optional) – The amount of images to capture. Defaults to -1.

Returns:

Collection with images.

Return type:

DataCollection

towhee.read_csv(csv_path: Union[str, Path], encoding: str = 'utf-8-sig')
towhee.read_json(json_path: Union[str, Path], encoding: str = 'utf-8')
towhee.read_zip(url, pattern, mode='r')

Load files from url/path.

Parameters:
  • zip_src (Union[str, path]) – The path leads to the image.

  • pattern (str) – The filename pattern to extract.

  • mode (str) – file open mode.

Returns:

The file handler for file in the zip file.

Return type:

(File)

towhee.glob = <towhee.hparam.hyperparameter._glob object>

Return a DataCollection of paths matching a pathname pattern.

Returns:

DataCollection holding paths.

Return type:

DataCollection or DataFrame

Examples

  1. create a simple data collection;

>>> import towhee
>>> towhee.glob('*.jpg', '*.png').to_list() 
['a.jpg', 'b.jpg', 'x.png']
  1. create a data collection of structural data.

>>> towhee.glob['path']('*.jpg').to_list() 
[<Entity dict_keys(['path'])>, <Entity dict_keys(['path'])>]
towhee.glob_zip = <towhee.hparam.hyperparameter._glob_zip object>

Return a DataCollection of files matching a pathname pattern from a zip archive.

Parameters:
  • uri (str or Path) – The path to zip.

  • pattern (str) – The pattern to match.

Returns:

DataCollection holding paths.

Return type:

DataCollection or DataFrame

Examples

  1. create a simple data collection;

>>> import towhee
>>> towhee.glob_zip('somefile.zip', '*.jpg').to_list() 
['a.jpg', 'b.jpg]
  1. create a data collection of structural data.

>>> towhee.glob_zip['path']('somefile.zip', '*.jpg').to_list() 
[<Entity dict_keys(['path'])>, <Entity dict_keys(['path'])>]