yolov8数据集预处理代码
时间: 2023-10-05 22:09:46 浏览: 134
以下是yolov8数据集预处理代码的示例:
```python
import cv2
import numpy as np
def letterbox_resize(image, target_size):
height, width = image.shape[:2]
target_height, target_width = target_size
scale = min(target_width / width, target_height / height)
resized_width = int(scale * width)
resized_height = int(scale * height)
image_resized = cv2.resize(image, (resized_width, resized_height))
delta_width = target_width - resized_width
delta_height = target_height - resized_height
top = delta_height // 2
bottom = delta_height - top
left = delta_width // 2
right = delta_width - left
image_letterboxed = cv2.copyMakeBorder(image_resized, top, bottom, left, right, cv2.BORDER_CONSTANT, value=[128, 128, 128])
return image_letterboxed
def normalize_image(image):
return image / 255.0
def preprocess(image, target_size):
image_resized = letterbox_resize(image, target_size)
image_normalized = normalize_image(image_resized)
image_expanded = np.expand_dims(image_normalized, axis=0)
return image_expanded
image = cv2.imread('image.jpg')
target_size = (416, 416)
preprocessed_image = preprocess(image, target_size)
```