js在自定义对象中添加数据

    科技2025-03-23  24

    js在自定义对象中添加数据

    So, If you are here then you might be enthusiast towards learning data augmentation, Object detection, machine learning, deep learning or image processing. And, you might have worked on image classification task where you might have done the data augmentation steps.

    因此,如果您在这里,那么您可能会更热衷于学习数据增强,对象检测,机器学习,深度学习或图像处理。 并且,您可能已经完成了图像分类任务,其中您可能已经完成了数据扩充步骤。

    But, In Case of object detection, We have to draw bounding boxes for all the images. And, If we will apply the data augmentation steps then the number of images will increase and then again we need to do the labeling for those images. These is a method I will cover in this article how you can automate the labeling steps for augmented images.

    但是,在物体检测的情况下,我们必须为所有图像绘制边界框。 而且,如果我们将应用数据扩充步骤,那么图像的数量将会增加,然后我们需要再次为这些图像做标签。 这些是我将在本文中介绍的方法,您可以如何自动执行增强图像的标记步骤。

    什么是“数据增强”? (What is “Data Augmentation” ?)

    Data augmentation is a strategy that enables practitioners to significantly increase the diversity of data available for training models, without actually collecting new data. Data augmentation techniques such as cropping, padding, and horizontal flipping are commonly used to train large neural networks.

    数据扩充 是一种策略,使从业人员可以显着增加 可用于训练模型 的 数据 的多样性 ,而无需实际收集新 数据 。 诸如裁剪,填充和水平翻转之类的 数据增强 技术通常用于训练大型神经网络。

    We now have idea what is data augmentation.

    现在我们知道什么是数据扩充。

    If you have worked on data augmentation in Image Classification problems you might aware of some data augmentation steps like:

    如果您曾从事图像分类问题中的数据增强工作,则可能会注意到一些数据增强步骤,例如:

    Image Rotation

    影像旋转 Add Noise

    增加噪音 Image Flipping

    图像翻转 And, many others are there

    还有很多其他的

    Now, you might have thought how we can use these techniques for Object detection?? hmm The challenge is :

    现在,您可能已经想到了如何使用这些技术进行对象检测? 嗯,挑战是:

    Yes, we can try these mentioned methods and then we can again do the image labeling for all the newly created image. But, Seriously? are you going to repeat those labeling process for all the same but altered images? Big NO

    是的,我们可以尝试这些提到的方法,然后我们可以再次为所有新创建的图像进行图像标记。 但是, 认真吗? 您要为所有相同但已更改的图像重复这些标记过程吗? 大NO

    So, here is a way:

    因此,这是一种方法:

    Why not we can just apply transformation for both the images and labels together? BOOM!! Yes, we will do the same transformation for image and label.

    为什么不将图像和标签一起应用变换呢? 繁荣!! 是的,我们将对图像和标签进行相同的转换。

    怎么样 (How)

    In this tutorial, we will use Jupyter Notebook to train our model. In the Jupyter Notebook, we can run a set of code snippets and get the output we want.

    在本教程中,我们将使用Jupyter Notebook训练模型。 在Jupyter Notebook中,我们可以运行一组代码片段并获取所需的输出。

    安装Jupyter Notebook (Installing Jupyter Notebook)

    Jupyter Notebooks are a part of Anaconda distribution and are open-source Use this link for installation. During installation it will ask for adding notebook to environment variable, I would suggest to check that checkbox.

    Jupyter Notebooks是Anaconda发行版的一部分,并且是开源的。使用此链接进行安装。 在安装过程中,它将要求将笔记本添加到环境变量中,我建议选中该复选框。

    After installation is complete, open the Anaconda prompt and type “Jupyter notebook”, and it will launch a Jupyter Notebook in your browser.

    安装完成后,打开Anaconda提示符并键入“ Jupyter notebook”,它将在您的浏览器中启动Jupyter Notebook。

    Jupyter Notebook Jupyter笔记本

    Click on “new” to open your new Python Notebook.

    单击“新建”以打开新的Python笔记本。

    Converting yolo format to opencv

    将yolo格式转换为opencv

    Below function will help us converting Yolo format label to opencv, It will help in rotating bounding boxes.

    下面的功能将帮助我们将Yolo格式标签转换为opencv,这将有助于旋转边界框。

    # Importing Libraries import os import numpy as np import cv2 import argparse import time from tqdm import tqdm #convert from Yolo_mark to opencv format def yoloFormattocv(x1, y1, x2, y2, H, W): bbox_width = x2 * W bbox_height = y2 * H center_x = x1 * W center_y = y1 * H voc = [] voc.append(center_x - (bbox_width / 2)) voc.append(center_y - (bbox_height / 2)) voc.append(center_x + (bbox_width / 2)) voc.append(center_y + (bbox_height / 2)) return [int(v) for v in voc]

    Converting opencv format to yolo

    将opencv格式转换为yolo

    After rotating the bounding boxed, we need to return back the yolo formatted label for our object detection model training. We will use the below function for it.

    旋转边界框后,我们需要返回yolo格式的标签以进行对象检测模型训练。 我们将使用以下功能。

    # Convert from opencv format to yolo format # H,W is the image height and width def cvFormattoYolo(corner, H, W): bbox_W = corner[3] - corner[1] bbox_H = corner[4] - corner[2] center_bbox_x = (corner[1] + corner[3]) / 2 center_bbox_y = (corner[2] + corner[4]) / 2 return corner[0], round(center_bbox_x / W, 6), round(center_bbox_y / H, 6), round(bbox_W / W, 6), round(bbox_H / H, 6)

    Rotating Images with Their Bounding Boxes

    旋转带有边界框的图像

    The below functions will help in rotating both image and bounding boxes.

    以下功能将有助于旋转图像框和边界框。

    class yoloRotatebbox: def __init__(self, filename, image_ext, angle): assert os.path.isfile(filename + image_ext) assert os.path.isfile(filename + '.txt') self.filename = filename self.image_ext = image_ext self.angle = angle # Read image using cv2 self.image = cv2.imread(self.filename + self.image_ext, 1) rotation_angle = self.angle * np.pi / 180 self.rot_matrix = np.array( [[np.cos(rotation_angle), -np.sin(rotation_angle)], [np.sin(rotation_angle), np.cos(rotation_angle)]]) def rotateYolobbox(self): new_height, new_width = self.rotate_image().shape[:2] f = open(self.filename + '.txt', 'r') f1 = f.readlines() new_bbox = [] H, W = self.image.shape[:2] for x in f1: bbox = x.strip('\n').split(' ') if len(bbox) > 1: (center_x, center_y, bbox_width, bbox_height) = yoloFormattocv(float(bbox[1]), float(bbox[2]), float(bbox[3]), float(bbox[4]), H, W) upper_left_corner_shift = (center_x - W / 2, -H / 2 + center_y) upper_right_corner_shift = (bbox_width - W / 2, -H / 2 + center_y) lower_left_corner_shift = (center_x - W / 2, -H / 2 + bbox_height) lower_right_corner_shift = (bbox_width - W / 2, -H / 2 + bbox_height) new_lower_right_corner = [-1, -1] new_upper_left_corner = [] for i in (upper_left_corner_shift, upper_right_corner_shift, lower_left_corner_shift, lower_right_corner_shift): new_coords = np.matmul(self.rot_matrix, np.array((i[0], -i[1]))) x_prime, y_prime = new_width / 2 + new_coords[0], new_height / 2 - new_coords[1] if new_lower_right_corner[0] < x_prime: new_lower_right_corner[0] = x_prime if new_lower_right_corner[1] < y_prime: new_lower_right_corner[1] = y_prime if len(new_upper_left_corner) > 0: if new_upper_left_corner[0] > x_prime: new_upper_left_corner[0] = x_prime if new_upper_left_corner[1] > y_prime: new_upper_left_corner[1] = y_prime else: new_upper_left_corner.append(x_prime) new_upper_left_corner.append(y_prime) # print(x_prime, y_prime) new_bbox.append([bbox[0], new_upper_left_corner[0], new_upper_left_corner[1], new_lower_right_corner[0], new_lower_right_corner[1]]) return new_bbox def rotate_image(self): """ Rotates an image (angle in degrees) and expands image to avoid cropping """ height, width = self.image.shape[:2] # image shape has 3 dimensions image_center = (width / 2, height / 2) # getRotationMatrix2D needs coordinates in reverse order (width, height) compared to shape rotation_mat = cv2.getRotationMatrix2D(image_center, self.angle, 1.) # rotation calculates the cos and sin, taking absolutes of those. abs_cos = abs(rotation_mat[0, 0]) abs_sin = abs(rotation_mat[0, 1]) # find the new width and height bounds bound_w = int(height * abs_sin + width * abs_cos) bound_h = int(height * abs_cos + width * abs_sin) # subtract old image center (bringing image back to origin) and adding the new image center coordinates rotation_mat[0, 2] += bound_w / 2 - image_center[0] rotation_mat[1, 2] += bound_h / 2 - image_center[1] # rotate image with the new bounds and translated rotation matrix rotated_mat = cv2.warpAffine(self.image, rotation_mat, (bound_w, bound_h)) return rotated_mat

    Running all the defined functions and class

    运行所有定义的函数和类

    Now, we will call out methods to do the task. Here, I am using Image Rotation. Other augmentation methods can be as usual.

    现在,我们将调出方法来完成任务。 在这里,我正在使用图像旋转。 其他增强方法可以像往常一样。

    if __name__ == "__main__": angels=[45,90,135,180,225,270,315] for filename in tqdm(os.listdir()): file =filename.split(".") if(file[-1]=="jpg"): image_name=file[0] image_ext="."+file[1] else: continue for angle in angels: im = yoloRotatebbox(image_name, image_ext, angle) bbox = im.rotateYolobbox() image = im.rotate_image() # to write rotateed image to disk cv2.imwrite(image_name+'_' + str(angle) + '.jpg', image) file_name = image_name+'_' + str(angle) + '.txt' #print("For angle "+str(angle)) if os.path.exists(file_name): os.remove(file_name) # to write the new rotated bboxes to file for i in bbox: with open(file_name, 'a') as fout: fout.writelines( ' '.join(map(str, cvFormattoYolo(i, im.rotate_image().shape[0], im.rotate_image().shape[1]))) + '\n')

    Demo Video

    示范影片

    演示地址

    Conclusion

    结论

    So, here it is we have used 250 images to create 1400 images for Yolov4 without creating bounding box for all the augmented images. :) You can use your custom images and increase the image count with their labels for better accuracy.

    因此,这里我们使用250张图像为Yolov4创建1400张图像,而没有为所有增强图像创建边界框。 :)您可以使用自定义图像,并使用其标签增加图像数量,以提高准确性。

    翻译自: https://medium.com/predict/data-augmentation-for-custom-object-detection-15674966e0c8

    js在自定义对象中添加数据

    相关资源:微信小程序源码-合集6.rar
    Processed: 0.008, SQL: 8