Skip to content

Commit

Permalink
ai generated blocks (#271)
Browse files Browse the repository at this point in the history
* ai generated blocks

* updated spec
  • Loading branch information
meet-rocking authored Jan 2, 2025
1 parent d1a6054 commit c3f1a62
Show file tree
Hide file tree
Showing 36 changed files with 1,224 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3.9

WORKDIR /app

COPY . .

RUN pip install --no-cache-dir -r requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

def compute(image_path, kernel_size):
'''
Applies a Gaussian blur to an image using a specified kernel size.
Parameters:
image_path (str): The file path to the input image that will be blurred.
kernel_size (int): The size of the kernel to be used for the Gaussian blur. It must be a positive odd integer.
Returns:
dict: A dictionary with the file path to the output image that has been blurred.
'''
import cv2
import os

# Read the image from the given path
image = cv2.imread(image_path)
if image is None:
raise ValueError('The image could not be loaded. Please check the file path.')

# Apply Gaussian blur to the image
blurred_image = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)

# Create the output file path
base, ext = os.path.splitext(image_path)
output_image_path = f"{base}_blurred{ext}"

# Save the blurred image to the output path
cv2.imwrite(output_image_path, blurred_image)

return {'blurred_image_path': output_image_path}


def generate_fake_image():
import numpy as np
import cv2
# Create a fake image (e.g., 100x100 with 3 channels for RGB)
fake_image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
# Save the fake image to a temporary file
path = 'temp_fake_image.jpg'
cv2.imwrite(path, fake_image)
return path


def test_compute():
import os
# Generate a fake image file path
image_path = generate_fake_image()
try:
# Test the compute function with the generated image and a kernel size
result = compute(image_path, 3)
print(f"Test passed. Blurred image saved at: {result['blurred_image_path']}")
except ValueError as e:
print(f"Test failed with error: {e}")
# Clean up generated files after the test
os.remove(image_path)
generated_blurred_path = 'temp_fake_image_blurred.jpg'
if os.path.exists(generated_blurred_path):
os.remove(generated_blurred_path)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
opencv-python-headless==4.10.0.84
numpy==1.26.4
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"information": {
"id": "apply-gaussian-blur",
"name": "Apply gaussian blur",
"description": "Applies a Gaussian blur to an image using a specified kernel size.\n\nParameters:\nimage_path (str): The file path to the input image that will be blurred.\nkernel_size (int): The size of the kernel to be used for the Gaussian blur. It must be a positive odd integer.\n\nReturns:\ndict: A dictionary with the file path to the output image that has been blurred.",
"system_versions": [
"0.1"
],
"block_version": "1.0",
"block_source": "core/blocks/generated-blocks/apply-gaussian-blur-20241223-163019-869",
"block_type": "compute"
},
"inputs": {
"image_path": {
"type": "file",
"connections": [
{
"variable": "path",
"block": "file-af7812m8dijr"
}
],
"relays": []
},
"kernel_size": {
"type": "int",
"connections": [
{
"variable": "parameter",
"block": "parameter-rk3j8m1elpxq"
}
],
"relays": []
}
},
"outputs": {
"blurred_image_path": {
"type": "str",
"connections": [],
"relays": []
}
},
"action": {
"container": {
"image": "apply-gaussian-blur",
"version": "latest",
"command_line": [
"python",
"entrypoint.py"
]
}
},
"views": {
"node": {
"behavior": "modal",
"active": "True",
"title_bar": {
"background_color": "#228B22"
},
"preview": {},
"html": "",
"pos_x": "0",
"pos_y": "0",
"pos_z": "999"
}
},
"events": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3.9

WORKDIR /app

COPY . .

RUN pip install --no-cache-dir -r requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

def compute(image_path):
'''
Convert a colored image to a grayscale image.
Parameters:
image_path (str): The file path to the colored image that needs to be converted to grayscale.
Returns:
dict: A dictionary with the key 'grayscale_image_path' and the value as the file path to the resulting grayscale image.
'''
import cv2
import os

# Read the colored image
colored_image = cv2.imread(image_path)

# Convert the image to grayscale
grayscale_image = cv2.cvtColor(colored_image, cv2.COLOR_BGR2GRAY)

# Create the output file path
base, ext = os.path.splitext(image_path)
grayscale_image_path = f"{base}_grayscale{ext}"

# Save the grayscale image
cv2.imwrite(grayscale_image_path, grayscale_image)

return {'grayscale_image_path': grayscale_image_path}


def generate_fake_image_path():
import numpy as np
import cv2
import os

# Create a fake color image using numpy (e.g., 100x100 pixels with 3 color channels)
fake_image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)

# Define the file path where this fake image will be saved
fake_image_path = "fake_image.jpg"

# Save the generated fake image to the file path
cv2.imwrite(fake_image_path, fake_image)

return fake_image_path


def test_compute():
# Retrieve the generated fake image path
image_path = generate_fake_image_path()

# Run the compute function with the fake image path
result = compute(image_path)

# Print the result to verify that the function ran successfully
print(result)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
opencv-python-headless==4.10.0.84
numpy==1.26.4
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"information": {
"id": "convert-colored-image-to-grayscale",
"name": "Convert colored image to grayscale",
"description": "Convert a colored image to a grayscale image.\n\nParameters:\nimage_path (str): The file path to the colored image that needs to be converted to grayscale.\n\nReturns:\ndict: A dictionary with the key 'grayscale_image_path' and the value as the file path to the resulting grayscale image.",
"system_versions": [
"0.1"
],
"block_version": "1.0",
"block_source": "core/blocks/generated-blocks/convert-colored-image-to-grayscale-20241223-163032-427",
"block_type": "compute"
},
"inputs": {
"image_path": {
"type": "file",
"connections": [
{
"variable": "path",
"block": "file-af181m72dija"
}
],
"relays": []
}
},
"outputs": {
"grayscale_image_path": {
"type": "str",
"connections": [],
"relays": []
}
},
"action": {
"container": {
"image": "convert-colored-image-to-grayscale",
"version": "latest",
"command_line": [
"python",
"entrypoint.py"
]
}
},
"views": {
"node": {
"behavior": "modal",
"active": "True",
"title_bar": {
"background_color": "#228B22"
},
"preview": {},
"html": "",
"pos_x": "0",
"pos_y": "0",
"pos_z": "999"
}
},
"events": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3.9

WORKDIR /app

COPY . .

RUN pip install --no-cache-dir -r requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
def compute(image_path, threshold):
'''
Convert a grayscale image to a binary image using a specified threshold.
Parameters:
image_path (str): The file path to the grayscale image that needs to be converted to binary.
threshold (int): The threshold value used to convert the image to binary. Pixel values above this threshold will be set to 255, and those below will be set to 0.
Returns:
dict: A dictionary with the key 'binary_image_path' and the value as the file path to the resulting binary image after conversion.
'''
import cv2
import os

# Read the grayscale image
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

# Apply the threshold to convert the image to binary
_, binary_image = cv2.threshold(image, threshold, 255, cv2.THRESH_BINARY)

# Define the output path for the binary image
base, ext = os.path.splitext(image_path)
binary_image_path = f"{base}_binary{ext}"

# Save the binary image
cv2.imwrite(binary_image_path, binary_image)

return {'binary_image_path': binary_image_path}

def generate_fake_image():
import numpy as np
import cv2

# Create a fake grayscale image using a numpy array
fake_image = np.random.randint(0, 256, (100, 100), dtype=np.uint8)
fake_image_path = 'fake_image.png'
cv2.imwrite(fake_image_path, fake_image)
return fake_image_path

def test_compute():
image_path = generate_fake_image()
threshold = 128
output = compute(image_path, threshold)
print(output)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
opencv-python-headless==4.10.0.84
numpy==1.26.4
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"information": {
"id": "convert-image-to-binary",
"name": "Convert image to binary",
"description": "Convert a grayscale image to a binary image using a specified threshold.\n\nParameters:\nimage_path (str): The file path to the grayscale image that needs to be converted to binary.\nthreshold (int): The threshold value used to convert the image to binary. Pixel values above this threshold will be set to 255, and those below will be set to 0.\n\nReturns:\ndict: A dictionary with the key 'binary_image_path' and the value as the file path to the resulting binary image after conversion.",
"system_versions": [
"0.1"
],
"block_version": "1.0",
"block_source": "core/blocks/generated-blocks/convert-image-to-binary-20241223-163041-154",
"block_type": "compute"
},
"inputs": {
"image_path": {
"type": "file",
"connections": [
{
"variable": "path",
"block": "file-af181m72dija"
}
],
"relays": []
},
"threshold": {
"type": "int",
"connections": [
{
"variable": "parameter",
"block": "parameter-adzakalp9mlc"
}
],
"relays": []
}
},
"outputs": {
"binary_image_path": {
"type": "str",
"connections": [],
"relays": []
}
},
"action": {
"container": {
"image": "convert-image-to-binary",
"version": "latest",
"command_line": [
"python",
"entrypoint.py"
]
}
},
"views": {
"node": {
"behavior": "modal",
"active": "True",
"title_bar": {
"background_color": "#228B22"
},
"preview": {},
"html": "",
"pos_x": "0",
"pos_y": "0",
"pos_z": "999"
}
},
"events": {}
}
Loading

0 comments on commit c3f1a62

Please sign in to comment.