You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
48 lines
1.3 KiB
import cv2
|
|
import numpy as np
|
|
import os
|
|
|
|
def capture_images_from_video(video_path, save_dir, frame_interval=30):
|
|
cap = cv2.VideoCapture(video_path)
|
|
frame_count = 0
|
|
while cap.isOpened():
|
|
ret, frame = cap.read()
|
|
if not ret:
|
|
break
|
|
if frame_count % frame_interval == 0:
|
|
file_path = os.path.join(save_dir, f'frame_{frame_count}.jpg')
|
|
cv2.imwrite(file_path, frame)
|
|
frame_count += 1
|
|
cap.release()
|
|
|
|
def augment_image(image):
|
|
# 图像旋转
|
|
rows, cols, _ = image.shape
|
|
rotation_matrix = cv2.getRotationMatrix2D((cols/2, rows/2), 10, 1)
|
|
image = cv2.warpAffine(image, rotation_matrix, (cols, rows))
|
|
# 图像翻转
|
|
image = cv2.flip(image, 1)
|
|
return image
|
|
|
|
def preprocess_image(image_path):
|
|
# 读取图像
|
|
image = cv2.imread(image_path)
|
|
# 图像缩放
|
|
image = cv2.resize(image, (640, 640))
|
|
# 图像归一化
|
|
image = image / 255.0
|
|
# 去噪处理(例如高斯模糊)
|
|
image = cv2.GaussianBlur(image, (5, 5), 0)
|
|
# 图像增强
|
|
image = augment_image(image)
|
|
return image
|
|
|
|
# 示例
|
|
video_path = 'path/to/video.mp4'
|
|
save_dir = 'path/to/save/images'
|
|
capture_images_from_video(video_path, save_dir)
|
|
|
|
image_path = 'path/to/traffic_sign_image.jpg'
|
|
preprocessed_image = preprocess_image(image_path)
|
|
|