From c54025a86b04886c6907628c6fa47320a2260878 Mon Sep 17 00:00:00 2001 From: vinicvaz Date: Mon, 27 Nov 2023 14:25:08 -0300 Subject: [PATCH] basic cv2 piece --- dependencies/Dockerfile_opencv | 15 +++++++++ pieces/OpenCVSegmentationPiece/metadata.json | 19 +++++++++++ pieces/OpenCVSegmentationPiece/models.py | 25 +++++++++++++++ pieces/OpenCVSegmentationPiece/piece.py | 33 ++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 dependencies/Dockerfile_opencv create mode 100644 pieces/OpenCVSegmentationPiece/metadata.json create mode 100644 pieces/OpenCVSegmentationPiece/models.py create mode 100644 pieces/OpenCVSegmentationPiece/piece.py diff --git a/dependencies/Dockerfile_opencv b/dependencies/Dockerfile_opencv new file mode 100644 index 0000000..a9fa384 --- /dev/null +++ b/dependencies/Dockerfile_opencv @@ -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 diff --git a/pieces/OpenCVSegmentationPiece/metadata.json b/pieces/OpenCVSegmentationPiece/metadata.json new file mode 100644 index 0000000..4fb6cef --- /dev/null +++ b/pieces/OpenCVSegmentationPiece/metadata.json @@ -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" + } +} \ No newline at end of file diff --git a/pieces/OpenCVSegmentationPiece/models.py b/pieces/OpenCVSegmentationPiece/models.py new file mode 100644 index 0000000..ff3b3b5 --- /dev/null +++ b/pieces/OpenCVSegmentationPiece/models.py @@ -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.", + ) diff --git a/pieces/OpenCVSegmentationPiece/piece.py b/pieces/OpenCVSegmentationPiece/piece.py new file mode 100644 index 0000000..49aa25a --- /dev/null +++ b/pieces/OpenCVSegmentationPiece/piece.py @@ -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) + ) + +