Skip to content

Commit

Permalink
ai-blocks cv
Browse files Browse the repository at this point in the history
  • Loading branch information
meet-rocking committed Jan 15, 2025
1 parent 7a14fc3 commit da610c8
Show file tree
Hide file tree
Showing 52 changed files with 667 additions and 635 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
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.
dict: A dictionary with the key 'blurred_image_path' containing the file path to the output image that has been blurred.
'''
import cv2
import os
Expand All @@ -30,30 +30,33 @@ def compute(image_path, kernel_size):

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

# Create a 100x100 pixel image with 3 color channels (R, G, B)
fake_image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
fake_image_path = 'fake_image.png'
cv2.imwrite(fake_image_path, fake_image)
return fake_image_path

def test_compute():
# Import necessary modules
import os
# Generate a fake image file path

# Generate a fake image for testing
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
kernel_size = 3 # An odd integer for the kernel size

# Call the compute function
result = compute(image_path, kernel_size)

# Check the output
blurred_image_path = result['blurred_image_path']

# Assert if output image exists
assert os.path.exists(blurred_image_path), "The blurred image file was not created."

# Clean up created image files
os.remove(image_path)
generated_blurred_path = 'temp_fake_image_blurred.jpg'
if os.path.exists(generated_blurred_path):
os.remove(generated_blurred_path)
os.remove(blurred_image_path)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy==1.26.4
opencv-python-headless==4.10.0.84
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,23 @@
"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.",
"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 key 'blurred_image_path' containing 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_source": "core/blocks/apply-gaussian-blur-20250115-142408-732",
"block_type": "compute"
},
"inputs": {
"image_path": {
"type": "file",
"connections": [
{
"variable": "path",
"block": "file-af7812m8dijr"
}
],
"connections": [],
"relays": []
},
"kernel_size": {
"type": "int",
"connections": [
{
"variable": "parameter",
"block": "parameter-rk3j8m1elpxq"
}
],
"connections": [],
"relays": []
}
},
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

def compute(image_path):
'''
Convert a colored image to a grayscale image.
Expand Down Expand Up @@ -27,30 +26,35 @@ def compute(image_path):

return {'grayscale_image_path': grayscale_image_path}


def generate_fake_image_path():
import numpy as np
import cv2
import numpy as np
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

# Create a fake image
fake_image = np.zeros((100, 100, 3), dtype=np.uint8)

# Define a temporary file path
temp_image_path = 'temp_colored_image.jpg'

# Write the fake image to this path
cv2.imwrite(temp_image_path, fake_image)

return temp_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
# Import necessary module
import os

# Generate a fake image path
fake_image_path = generate_fake_image_path()

# Call the function
result = compute(fake_image_path)

# Print the result
print(result)

# Remove the temporary images created
os.remove(fake_image_path)
os.remove(result['grayscale_image_path'])
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy==1.26.4
opencv-python-headless==4.10.0.84
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
"0.1"
],
"block_version": "1.0",
"block_source": "core/blocks/generated-blocks/convert-colored-image-to-grayscale-20241223-163032-427",
"block_source": "core/blocks/convert-colored-image-to-grayscale-20250115-142427-862",
"block_type": "compute"
},
"inputs": {
"image_path": {
"type": "file",
"connections": [
{
"variable": "path",
"block": "file-af181m72dija"
}
],
"connections": [],
"relays": []
}
},
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,44 +1,52 @@

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.
threshold (int): The threshold value used to convert the grayscale image to binary. Pixel values greater than or equal to this threshold will be set to white, otherwise black.
Returns:
dict: A dictionary with the key 'binary_image_path' and the value as the file path to the resulting binary image after conversion.
dict: A dictionary with the key 'binary_image_path' and the value being 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
# Check if image is loaded
if image is None:
raise ValueError('Image not found or unable to load.')

# Convert the image to binary using the threshold
_, binary_image = cv2.threshold(image, threshold, 255, cv2.THRESH_BINARY)
# Define the output path for the binary image

# Create the output file path
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)

# Create a 10x10 grayscale image with random values
fake_image = np.random.randint(0, 256, (10, 10), dtype=np.uint8)

# Save it to a temporary location
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)
result = compute(image_path, threshold)
print(result) # To verify the output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy==1.26.4
opencv-python-headless==4.10.0.84
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,23 @@
"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.",
"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 grayscale image to binary. Pixel values greater than or equal to this threshold will be set to white, otherwise black.\n\nReturns:\ndict: A dictionary with the key 'binary_image_path' and the value being 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_source": "core/blocks/convert-image-to-binary-20250115-142445-780",
"block_type": "compute"
},
"inputs": {
"image_path": {
"type": "file",
"connections": [
{
"variable": "path",
"block": "file-af181m72dija"
}
],
"connections": [],
"relays": []
},
"threshold": {
"type": "int",
"connections": [
{
"variable": "parameter",
"block": "parameter-adzakalp9mlc"
}
],
"connections": [],
"relays": []
}
},
Expand Down

This file was deleted.

Loading

0 comments on commit da610c8

Please sign in to comment.