Skip to content

Commit

Permalink
basic cv2 piece
Browse files Browse the repository at this point in the history
  • Loading branch information
vinicvaz committed Nov 27, 2023
1 parent 7eeb739 commit c54025a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
15 changes: 15 additions & 0 deletions dependencies/Dockerfile_opencv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Use the base image
FROM ghcr.io/tauffer-consulting/domino-base-piece:latest

# Update package lists and install necessary dependencies
RUN apt-get update && \
apt-get install -y \
python3-dev \
python3-pip \
libopencv-dev

# Install OpenCV using pip (Python package manager)
RUN pip3 install opencv-python-headless

# Set working directory (optional)
WORKDIR /app
19 changes: 19 additions & 0 deletions pieces/OpenCVSegmentationPiece/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "OpenCVFilterPiece",
"description": "Piece that uses OpenCV filter on an Image",
"dependency": {
"dockerfile": "Dockerfile_opencv"
},
"tags": [
"image",
"processing",
"filter"
],
"style": {
"node_label": "OpenCV Filter Piece",
"node_style": {
"backgroundColor": "#38b2be"
},
"icon_class_name": "material-symbols:image-outline"
}
}
25 changes: 25 additions & 0 deletions pieces/OpenCVSegmentationPiece/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pydantic import BaseModel, Field
from enum import Enum

class FilterNaem(str, Enum):
canny = "canny"
binary = "binary"

class InputModel(BaseModel):
image_path: str = Field(
title="Image Path",
description="The path to the image to apply filter.",
json_schema_extra={"from_upstream": "always"}
)
filter_name: FilterNaem = Field(
title="Filter Name",
description="The name of the filter to apply.",
default="canny",
)


class OutputModel(BaseModel):
image_path: str = Field(
title="Output image Path",
description="The path to the image with the filter applied.",
)
33 changes: 33 additions & 0 deletions pieces/OpenCVSegmentationPiece/piece.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from domino.base_piece import BasePiece
from .models import InputModel, OutputModel
import pandas as pd
from pathlib import Path
from plotly.subplots import make_subplots
import cv2
from pathlib import Path


class OpenCVFilterPiece(BasePiece):

def piece_function(self, input_data: InputModel):
image = cv2.imread(input_data.image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
if input_data.filter_name == "canny":
edged = cv2.Canny(gray, 50, 100)
elif input_data.filter_name == "binary":
_, edged = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)
else:
raise ValueError(f"Unknown filter name: {input_data.filter_name}")

output_image_path = Path(self.results_path) / "opencv_image.png"
cv2.imwrite(str(output_image_path), edged)
self.display_result = {
"file_type": "png",
"file_path": str(output_image_path)
}

return OutputModel(
image_path=str(output_image_path)
)


0 comments on commit c54025a

Please sign in to comment.