Skip to content

⚡️ Speed up function images_to_cv2 by 23% #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Feb 3, 2025

📄 23% (0.23x) speedup for images_to_cv2 in supervision/utils/conversion.py

⏱️ Runtime : 343 milliseconds 278 milliseconds (best of 21 runs)

📝 Explanation and details

o3-mini
We can speed up the code by replacing the explicit for‐loop with a list comprehension, using a faster isinstance check (which is more idiomatic and slightly more efficient), and using np.asarray instead of np.array to avoid unnecessary copies when possible.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 8 Passed
🌀 Generated Regression Tests 22 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
⚙️ Existing Unit Tests Details
- utils/test_conversion.py
🌀 Generated Regression Tests Details
from typing import List, TypeVar

import cv2
import numpy as np
# imports
import pytest  # used for our unit tests
from PIL import Image
from supervision.annotators.base import ImageType
from supervision.utils.conversion import images_to_cv2

# function to test
ImageType = TypeVar("ImageType", np.ndarray, Image.Image)
from supervision.utils.conversion import images_to_cv2

# unit tests

def test_single_pillow_image():
    # Create a single Pillow image
    image = Image.new('RGB', (100, 100), color='red')
    # Convert using the function
    codeflash_output = images_to_cv2([image])

def test_single_opencv_image():
    # Create a single OpenCV image
    image = np.zeros((100, 100, 3), dtype=np.uint8)
    # Convert using the function
    codeflash_output = images_to_cv2([image])

def test_mixed_image_types():
    # Create a Pillow image and an OpenCV image
    pillow_image = Image.new('RGB', (100, 100), color='red')
    opencv_image = np.zeros((100, 100, 3), dtype=np.uint8)
    # Convert using the function
    codeflash_output = images_to_cv2([pillow_image, opencv_image])

def test_empty_list():
    # Convert an empty list
    codeflash_output = images_to_cv2([])


def test_large_image_size():
    # Create a very large Pillow image
    pillow_image = Image.new('RGB', (8000, 8000), color='red')
    # Convert using the function
    codeflash_output = images_to_cv2([pillow_image])


def test_hdr_image():
    # Create a fake HDR image (high dynamic range)
    hdr_image = np.random.rand(100, 100, 3).astype(np.float32)
    # Convert using the function
    codeflash_output = images_to_cv2([hdr_image])

def test_image_with_alpha_channel():
    # Create a Pillow image with an alpha channel (RGBA)
    pillow_image = Image.new('RGBA', (100, 100), color=(255, 0, 0, 128))
    # Convert using the function
    codeflash_output = images_to_cv2([pillow_image])

def test_consistent_output():
    # Create a Pillow image
    pillow_image = Image.new('RGB', (100, 100), color='red')
    # Convert using the function multiple times
    codeflash_output = images_to_cv2([pillow_image])
    codeflash_output = images_to_cv2([pillow_image])
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

from typing import List, TypeVar

import cv2
import numpy as np
# imports
import pytest  # used for our unit tests
from PIL import Image
from supervision.annotators.base import ImageType
from supervision.utils.conversion import images_to_cv2

# function to test
ImageType = TypeVar("ImageType", np.ndarray, Image.Image)
from supervision.utils.conversion import images_to_cv2


# unit tests
def test_empty_list():
    codeflash_output = images_to_cv2([])

def test_single_pillow_image():
    pillow_image = Image.new('RGB', (10, 10), color='red')
    codeflash_output = images_to_cv2([pillow_image])

def test_single_opencv_image():
    opencv_image = np.zeros((10, 10, 3), dtype=np.uint8)
    codeflash_output = images_to_cv2([opencv_image])

def test_mixed_list():
    pillow_image = Image.new('RGB', (10, 10), color='red')
    opencv_image = np.zeros((10, 10, 3), dtype=np.uint8)
    codeflash_output = images_to_cv2([pillow_image, opencv_image])


def test_corrupted_pillow_image():
    corrupted_image = Image.new('RGB', (10, 10))
    corrupted_image = corrupted_image.crop((0, 0, 5, 5))  # Simulate corruption
    codeflash_output = images_to_cv2([corrupted_image])


def test_large_image_sizes():
    pillow_image = Image.new('RGB', (1000, 1000), color='red')
    opencv_image = np.zeros((1000, 1000, 3), dtype=np.uint8)
    codeflash_output = images_to_cv2([pillow_image, opencv_image])

def test_high_resolution_images():
    pillow_image = Image.new('RGB', (4000, 4000), color='red')
    opencv_image = np.zeros((4000, 4000, 3), dtype=np.uint8)
    codeflash_output = images_to_cv2([pillow_image, opencv_image])

def test_different_color_spaces():
    pillow_image_rgba = Image.new('RGBA', (10, 10), color='red')
    pillow_image_cmyk = Image.new('CMYK', (10, 10), color='cyan')
    codeflash_output = images_to_cv2([pillow_image_rgba.convert('RGB'), pillow_image_cmyk.convert('RGB')])

def test_images_with_alpha_channel():
    pillow_image = Image.new('RGBA', (10, 10), color='red')
    opencv_image = np.zeros((10, 10, 4), dtype=np.uint8)
    codeflash_output = images_to_cv2([pillow_image.convert('RGB'), opencv_image[:, :, :3]])


def test_unsupported_formats():
    pillow_image = Image.new('P', (10, 10), color='red')  # 'P' mode is not directly supported
    codeflash_output = images_to_cv2([pillow_image.convert('RGB')])

def test_consistent_output_format():
    pillow_image = Image.new('RGB', (10, 10), color='red')
    codeflash_output = images_to_cv2([pillow_image])

def test_input_integrity():
    pillow_image = Image.new('RGB', (10, 10), color='red')
    images = [pillow_image]
    images_to_cv2(images)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

Codeflash

o3-mini
We can speed up the code by replacing the explicit for‐loop with a list comprehension, using a faster isinstance check (which is more idiomatic and slightly more efficient), and using np.asarray instead of np.array to avoid unnecessary copies when possible.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Feb 3, 2025
@codeflash-ai codeflash-ai bot requested a review from misrasaurabh1 February 3, 2025 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚡️ codeflash Optimization PR opened by Codeflash AI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants