Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
cospectrum committed Dec 14, 2024
1 parent 29e8850 commit bbfd529
Show file tree
Hide file tree
Showing 17 changed files with 962 additions and 156 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.onnx filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
160 changes: 5 additions & 155 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,162 +1,12 @@
# Byte-compiled / optimized / DLL files
# Python-generated files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
*.py[oc]
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-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
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

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

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
*.egg-info

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

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

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

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

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.DS_Store
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# microwink
# microwink
Lightweight instance segmentation model for card IDs

## Usage

```python
from microwink import SegModel
from microwink.common import draw_mask
from PIL import Image

seg_model = SegModel.from_path("./models/seg_model.onnx")

img = Image.open("...").convert("RGB")
cards = seg_model.apply(img)

for card in cards:
print(f"{card.score=}, {card.box=}")
img = draw_mask(img, card.mask > 0.5)
img.save("result.png")
```
3 changes: 3 additions & 0 deletions models/seg_model.onnx
Git LFS file not shown
24 changes: 24 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[project]
name = "microwink"
version = "0.0.1"
description = "Lightweight instance segmentation model for card IDs"
readme = "README.md"
license = { text = "Apache-2.0" }
authors = [{ name = "cospectrum", email = "[email protected]" }]
requires-python = ">=3.10"
dependencies = [
"numpy>=2.2.0",
"onnxruntime>=1.20.1",
"opencv-python>=4.10.0.84",
"pillow>=11.0.0",
]

[project.urls]
Repository = "https://github.com/cospectrum/microwink"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[dependency-groups]
dev = ["mypy>=1.13.0", "pytest>=8.3.4", "ruff>=0.8.3"]
5 changes: 5 additions & 0 deletions src/microwink/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .seg import (
Threshold as Threshold,
SegModel as SegModel,
SegResult as SegResult,
)
60 changes: 60 additions & 0 deletions src/microwink/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import numpy as np

from dataclasses import dataclass
from typing import Any, Iterable, Sequence
from PIL import Image, ImageDraw
from PIL.Image import Image as PILImage


@dataclass
class Box:
x: float
y: float
h: float
w: float

@staticmethod
def from_xyxy(box: Iterable[Any]) -> "Box":
x1, y1, x2, y2 = [float(t) for t in box]
h = y2 - y1
w = x2 - x1
assert h >= 0.0
assert w >= 0.0
return Box(x=x1, y=y1, w=w, h=h)


def sigmoid(x: np.ndarray) -> np.ndarray:
return 1 / (1 + np.exp(-x))


def draw_box(
image: PILImage,
box: Box,
*,
color: tuple[int, ...] | str | float = (255, 0, 0),
width: int = 4,
) -> PILImage:
image = image.copy()
draw = ImageDraw.Draw(image)
points = [(box.x, box.y), (box.x + box.w, box.y + box.h)]
draw.rectangle(points, outline=color, width=width)
return image


def draw_mask(
image: PILImage,
binary_mask: np.ndarray,
*,
color: Sequence[Any] = (0, 255, 0),
alpha: float = 0.5,
) -> PILImage:
assert 0.0 <= alpha <= 1.0
assert (image.height, image.width) == binary_mask.shape
img = np.array(image)
assert len(img.shape) == len(color)
overlay = np.zeros_like(img)
overlay[binary_mask] = color
assert overlay.shape == img.shape

img[binary_mask] = (1.0 - alpha) * img[binary_mask] + alpha * overlay[binary_mask]
return Image.fromarray(img)
Empty file added src/microwink/py.typed
Empty file.
Loading

0 comments on commit bbfd529

Please sign in to comment.