Skip to content

Commit

Permalink
feat: init work
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianmtr committed Nov 24, 2020
0 parents commit b972714
Show file tree
Hide file tree
Showing 18 changed files with 868 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
pull_request:
branches: [ master ]

jobs:
test_hubbuilder:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Hub builder step (no push)
id: hubbuild1 # later we can refer to this step
uses: ./ # Uses an action in the root directory
continue-on-error: true
- name: Get the output
run: |
rc=0
[[ "${{ steps.hubbuild1.outputs.success_targets }}" == ".github/workflows/tests/ImageReader/" ]] || rc=1
[[ "${{ steps.hubbuild1.outputs.failed_targets }}" == ".github/workflows/tests/EmptyExecutor/" ]] || rc=1
exit $rc
- name: Hub builder step (with push)
id: hubbuild2 # later we can refer to this step
uses: ./ # Uses an action in the root directory
continue-on-error: true
with:
push: true
dockerhub_username: ${{secrets.JINAHUB_DOCKER_USER}}
dockerhub_password: ${{secrets.JINAHUB_DOCKER_PWD}}
- name: Get the output
run: |
rc=0
[[ "${{ steps.hubbuild2.outputs.success_targets }}" == ".github/workflows/tests/ImageReader/" ]] || rc=1
[[ "${{ steps.hubbuild2.outputs.failed_targets }}" == ".github/workflows/tests/EmptyExecutor/" ]] || rc=1
exit $rc
1 change: 1 addition & 0 deletions .github/workflows/tests/EmptyExecutor/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version: 0.11
13 changes: 13 additions & 0 deletions .github/workflows/tests/ImageReader/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM jinaai/jina

# setup the workspace
COPY . /workspace
WORKDIR /workspace

# install the third-party requirements
RUN pip install -r requirements.txt

# for testing the image
RUN pip install pytest && pytest

ENTRYPOINT ["jina", "pod", "--uses", "config.yml"]
3 changes: 3 additions & 0 deletions .github/workflows/tests/ImageReader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ImageReader

loads the image from the given file path and save the `ndarray` of the image in the Document.
42 changes: 42 additions & 0 deletions .github/workflows/tests/ImageReader/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import io
from typing import Dict

import numpy as np
from jina.executors.crafters import BaseCrafter


class ImageReader(BaseCrafter):
"""
:class:`ImageReader` loads the image from the given file path and save the `ndarray` of the image in the Document.
"""

def __init__(self, channel_axis: int = -1, *args, **kwargs):
"""
:class:`ImageReader` load an image file and craft into image matrix.
:param channel_axis: the axis id of the color channel, ``-1`` indicates the color channel info at the last axis
"""
super().__init__(*args, **kwargs)
self.channel_axis = channel_axis

def craft(self, buffer: bytes, uri: str, *args, **kwargs) -> Dict:
"""
Read the image from the given file path that specified in `buffer` and save the `ndarray` of the image in
the `blob` of the document.
:param buffer: the image in raw bytes
:param uri: the image file path
"""
from PIL import Image
if buffer:
raw_img = Image.open(io.BytesIO(buffer))
elif uri:
raw_img = Image.open(uri)
else:
raise ValueError('no value found in "buffer" and "uri"')
raw_img = raw_img.convert('RGB')
img = np.array(raw_img).astype('float32')
if self.channel_axis != -1:
img = np.moveaxis(img, -1, self.channel_axis)
return dict(weight=1., blob=img)
6 changes: 6 additions & 0 deletions .github/workflows/tests/ImageReader/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
!ImageReader
with:
{}
metas:
py_modules:
- __init__.py
12 changes: 12 additions & 0 deletions .github/workflows/tests/ImageReader/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
manifest_version: 1
name: ImageReader
kind: crafter
type: pod
description: loads the image from the given file path and save the `ndarray` of the image in the Document.
author: Jina AI Dev-Team (dev-team@jina.ai)
url: https://jina.ai
vendor: Jina AI Limited
documentation: https://github.com/jina-ai/jina-hub
version: 0.0.11
license: apache-2.0
keywords: [Some keywords to describe the executor, separated by commas]
2 changes: 2 additions & 0 deletions .github/workflows/tests/ImageReader/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jina
Pillow
Empty file.
37 changes: 37 additions & 0 deletions .github/workflows/tests/ImageReader/tests/test_imagereader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import io
import os

import numpy as np
from PIL import Image

from .. import ImageReader


def create_test_image(output_fn, size_width=50, size_height=50):
from PIL import Image
image = Image.new('RGB', size=(size_width, size_height), color=(155, 0, 0))
with open(output_fn, "wb") as f:
image.save(f, 'jpeg')


def test_io_uri():
crafter = ImageReader()
tmp_fn = os.path.join(crafter.current_workspace, 'test.jpeg')
img_size = 50
create_test_image(tmp_fn, size_width=img_size, size_height=img_size)
test_doc = crafter.craft(buffer=None, uri=tmp_fn)
assert test_doc['blob'].shape == (img_size, img_size, 3)


def test_io_buffer():
crafter = ImageReader()
tmp_fn = os.path.join(crafter.current_workspace, 'test.jpeg')
img_size = 50
create_test_image(tmp_fn, size_width=img_size, size_height=img_size)
image_buffer = io.BytesIO()
img = Image.open(tmp_fn)
img.save(image_buffer, format='PNG')
image_buffer.seek(0)
test_doc = crafter.craft(buffer=image_buffer.getvalue(), uri=None)
assert test_doc['blob'].shape == (img_size, img_size, 3)
np.testing.assert_almost_equal(test_doc['blob'], np.array(img).astype('float32'))
22 changes: 22 additions & 0 deletions .github/workflows/toc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CD

on:
push:
branches:
- master

jobs:
update-toc:
if: "!startsWith(github.event.head_commit.message, 'chore') && !startsWith(github.event.head_commit.message, 'build: hotfix')"
runs-on: ubuntu-latest
steps:
- uses: technote-space/toc-generator@v2
with:
MAX_HEADER_LEVEL: 2
FOLDING: false
GITHUB_TOKEN: ${{ secrets.JINA_DEV_BOT }}
COMMIT_NAME: Jina Dev Bot
COMMIT_EMAIL: dev-bot@jina.ai
TOC_TITLE: ''
TARGET_PATHS: 'README*.md,CHANGELOG.md,CONTRIBUTING.md'
COMMIT_MESSAGE: 'chore(docs): update TOC'
138 changes: 138 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Initially taken from Github's Python gitignore file

api
hub
src
status

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/
docs/api/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
docs/.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site
.python-version

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
.idea/
toy*.py
.DS_Store
post/
toy*.ipynb
data/
*.c
.nes_cache
toy*.yml
*.tmp

shell/jina-wizard.sh
/junit/
/tests/junit/
/docs/chapters/proto/docs.md
.vscode/settings.json
Loading

0 comments on commit b972714

Please sign in to comment.