# Some image augmentation tools for yolov5 (and probably other models)
## Before you start:
### install Pytorch
### install https://github.com/ultralytics/yolov5
### install https://pypi.org/project/rembg/
## pip install tools4yolo
#### Tested against Windows 10 / Python 3.10 / Anaconda
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1489.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1016.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1085.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1180.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1232.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1244.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1290.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1428.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1434.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1440.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1441.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1442.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1444.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1455.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1474.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1486.jpg?raw=true)
## Formating the needle images
```python
# Get some images from somewhere
# Important! The pictures need to have a transparent background
# The function remove_background_and_resize removes most of the background. If there is some background left, remove it using GIMP or Photoshop
from tools4yolo import remove_background_and_resize
bands = [
r"C:\pics\aerosmith",
r"C:\pics\anthrax",
r"C:\pics\black_sabbath",
r"C:\pics\iron_maiden",
r"C:\pics\judas_priest",
r"C:\pics\kiss",
r"C:\pics\krokus",
r"C:\pics\led_zeppelin",
r"C:\pics\manowar",
r"C:\pics\metalchurch",
r"C:\pics\misfits",
r"C:\pics\motorhead",
r"C:\pics\ozzy",
r"C:\pics\pantera",
r"C:\pics\saint_vitus",
r"C:\pics\saxon",
r"C:\pics\scorpions",
r"C:\pics\slayer",
r"C:\pics\whitesnake",
r"C:\pics\accept",
r"C:\pics\acdc",
]
for folder in bands:
folderband = folder.replace(r"C:\pics", r"C:\pics2")
remove_background_and_resize(
folder=folder, folderout=folderband, maxwidth=640, maxheight=640
)
```
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/acdclogos.png?raw=true)
## Augmenting the needle images
```python
# To get some variations of the needle images, use:
from tools4yolo import augment_needle_images
augment_needle_images(
folder="c:\\pics2",
outputfolder="c:\\pics3",
width=640,
height=640,
)
# Make sure that there is no background after calling augment_needle_images
```
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/acdcaugm.png?raw=true)
## Generating the configuration file
```python
import os
from tools4yolo import generate_ini_file
allbands = [
r"C:\pics3\metallica",
r"C:\pics3\acdc",
r"C:\pics3\aerosmith",
r"C:\pics3\anthrax",
r"C:\pics3\black_sabbath",
r"C:\pics3\iron_maiden",
r"C:\pics3\judas_priest",
r"C:\pics3\kiss",
r"C:\pics3\krokus",
r"C:\pics3\led_zeppelin",
r"C:\pics3\manowar",
r"C:\pics3\metalchurch",
r"C:\pics3\misfits",
r"C:\pics3\motorhead",
r"C:\pics3\ozzy",
r"C:\pics3\pantera",
r"C:\pics3\saint_vitus",
r"C:\pics3\saxon",
r"C:\pics3\scorpions",
r"C:\pics3\slayer",
r"C:\pics3\whitesnake",
r"C:\pics3\accept",
]
allclasses = []
backgroundfolder = r"C:\Backgrounds"
personal_yaml_file = "bandlogos.yaml" # name it however you want
outputfolder = "c:\\bandlogos1\\pics2" # folder will be created if it does not exist
outputinifile = 'c:\\bandlogos1\\training2.ini' # file will be created if it does not exist
os.makedirs(outputfolder,exist_ok=True)
for i, k in enumerate(allbands):
allclasses.append(
{
"classnumber": i,
"classname": k.split(os.sep)[-1],
"random_background_folder": backgroundfolder,
"class_pictures": k,
"personal_yaml_file": personal_yaml_file,
"outputfolder": outputfolder,
"howmany": 1000,
"background_qty": 100,
"processes": 2,
"image_size_width": 640,
"image_size_height": 640,
"needle_size_percentage_min": 0.20,
"needle_size_percentage_max": 0.9,
"blur_image_kernel_min": 1,
"blur_image_kernel_max": 5,
"blur_image_frequency": 10,
"sharpen_image_kernel_min": 1,
"sharpen_image_kernel_max": 6,
"sharpen_image_frequency": 10,
"distorted_resizing_add_min_x": 0.01,
"distorted_resizing_add_max_x": 0.15,
"distorted_resizing_add_min_y": 0.01,
"distorted_resizing_add_max_y": 0.15,
"distorted_resizing_frequency": 10,
"blur_borders_min_x0": 0.01,
"blur_borders_max_x0": 0.1,
"blur_borders_min_x1": 0.01,
"blur_borders_max_x1": 0.1,
"blur_borders_min_y0": 0.01,
"blur_borders_max_y0": 0.1,
"blur_borders_min_y1": 0.01,
"blur_borders_max_y1": 0.1,
"blur_borders_kernel_min": 1,
"blur_borders_kernel_max": 6,
"blur_borders_frequency": 30,
"pixelborder_min": 1,
"pixelborder_max": 20,
"pixelborder_loop_min": 1,
"pixelborder_loop_max": 2,
"pixelborder_frequency": 30,
"perspective_distortion_min_x": 0.01,
"perspective_distortion_max_x": 0.15,
"perspective_distortion_min_y": 0.01,
"perspective_distortion_max_y": 0.15,
"perspective_distortion_percentage": 15,
"transparency_distortion_min": 140,
"transparency_distortion_max": 255,
"transparency_distortion_frequency": 40,
"canny_edge_blur_thresh_lower_min": 10,
"canny_edge_blur_thresh_lower_max": 20,
"canny_edge_blur_thresh_upper_min": 80,
"canny_edge_blur_thresh_upper_max": 90,
"canny_edge_blur_kernel_min": 1,
"canny_edge_blur_kernel_max": 6,
"canny_edge_blur_frequency": 10,
"random_crop_min_x": 0.01,
"random_crop_max_x": 0.10,
"random_crop_min_y": 0.01,
"random_crop_max_y": 0.10,
"random_crop_frequency": 30,
"hue_shift_min": 1,
"hue_shift_max": 180,
"hue_shift_frequency": 90,
"change_contrast_min": 0.8,
"change_contrast_max": 1.2,
"change_contrast_frequency": 30,
"rotate_image_min": 2,
"rotate_image_max": 359,
"rotate_image_frequency": 90,
"colors_to_change_percentage_max": 75,
"colors_to_change_percentage_min": 1,
"colors_to_change_frequency": 90,
"colors_to_change_r_min": 5,
"colors_to_change_r_max": 250,
"colors_to_change_g_min": 5,
"colors_to_change_g_max": 250,
"colors_to_change_b_min": 5,
"colors_to_change_b_max": 250,
"flip_image_left_right_frequency": 2,
"flip_image_up_down_frequency": 2,
"verbose": True,
"bloom_kernel_min": 1,
"bloom_kernel_max": 25,
"bloom_sigmaX_min": 140,
"bloom_sigmaX_max": 240,
"bloom_intensity_min": 4.5,
"bloom_intensity_max": 20.5,
"bloom_frequency": 30,
"fish_distortion_min1": 0.01,
"fish_distortion_max1": 0.4,
"fish_distortion_min2": 0.01,
"fish_distortion_max2": 0.4,
"fish_distortion_min3": 0.01,
"fish_distortion_max3": 0.4,
"fish_distortion_min4": 0.01,
"fish_distortion_max4": 0.03,
"fish_divider_1_min": 1,
"fish_divider_1_max": 2,
"fish_divider_2_min": 1,
"fish_divider_2_max": 4,
"fish_divider_3_min": 1,
"fish_divider_3_max": 2,
"fish_divider_4_min": 2,
"fish_divider_4_max": 4,
"fish_border_add": 0.1,
"fish_frequency": 50,
}
)
inifile=generate_ini_file(allclasses)
print(inifile)
with open(outputinifile,mode='w',encoding='utf-8') as f:
f.write(inifile)
# Example of a generated config file.
[class0]
classnumber:0
classname:metallica
random_background_folder:C:\Backgrounds
class_pictures:C:\pics2\metallica
personal_yaml_file:bandlogos.yaml
outputfolder:c:\bandlogos1\pics
howmany:1400
background_qty:100
processes:4
image_size_width:640
image_size_height:640
needle_size_percentage_min:0.2
needle_size_percentage_max:0.9
blur_image_kernel_min:1
blur_image_kernel_max:5
blur_image_frequency:10
sharpen_image_kernel_min:1
sharpen_image_kernel_max:6
sharpen_image_frequency:10
distorted_resizing_add_min_x:0.01
distorted_resizing_add_max_x:0.15
distorted_resizing_add_min_y:0.01
distorted_resizing_add_max_y:0.15
distorted_resizing_frequency:10
blur_borders_min_x0:0.01
blur_borders_max_x0:0.1
blur_borders_min_x1:0.01
blur_borders_max_x1:0.1
blur_borders_min_y0:0.01
blur_borders_max_y0:0.1
blur_borders_min_y1:0.01
blur_borders_max_y1:0.1
blur_borders_kernel_min:1
blur_borders_kernel_max:6
blur_borders_frequency:30
pixelborder_min:1
pixelborder_max:20
pixelborder_loop_min:1
pixelborder_loop_max:2
pixelborder_frequency:30
perspective_distortion_min_x:0.01
perspective_distortion_max_x:0.15
perspective_distortion_min_y:0.01
perspective_distortion_max_y:0.15
perspective_distortion_percentage:15
transparency_distortion_min:140
transparency_distortion_max:255
transparency_distortion_frequency:40
canny_edge_blur_thresh_lower_min:10
canny_edge_blur_thresh_lower_max:20
canny_edge_blur_thresh_upper_min:80
canny_edge_blur_thresh_upper_max:90
canny_edge_blur_kernel_min:1
canny_edge_blur_kernel_max:6
canny_edge_blur_frequency:10
random_crop_min_x:0.01
random_crop_max_x:0.1
random_crop_min_y:0.01
random_crop_max_y:0.1
random_crop_frequency:30
hue_shift_min:1
hue_shift_max:180
hue_shift_frequency:90
change_contrast_min:0.8
change_contrast_max:1.2
change_contrast_frequency:30
rotate_image_min:2
rotate_image_max:359
rotate_image_frequency:90
colors_to_change_percentage_max:75
colors_to_change_percentage_min:1
colors_to_change_frequency:90
colors_to_change_r_min:5
colors_to_change_r_max:250
colors_to_change_g_min:5
colors_to_change_g_max:250
colors_to_change_b_min:5
colors_to_change_b_max:250
flip_image_left_right_frequency:2
flip_image_up_down_frequency:2
verbose:True
bloom_kernel_min:1
bloom_kernel_max:25
bloom_sigmaX_min:140
bloom_sigmaX_max:240
bloom_intensity_min:4.5
bloom_intensity_max:20.5
bloom_frequency:30
fish_distortion_min1:0.01
fish_distortion_max1:0.4
fish_distortion_min2:0.01
fish_distortion_max2:0.4
fish_distortion_min3:0.01
fish_distortion_max3:0.4
fish_distortion_min4:0.01
fish_distortion_max4:0.03
fish_divider_1_min:1
fish_divider_1_max:2
fish_divider_2_min:1
fish_divider_2_max:4
fish_divider_3_min:1
fish_divider_3_max:2
fish_divider_4_min:2
fish_divider_4_max:4
fish_border_add:0.1
fish_frequency:50
[class1]
classnumber:1
classname:acdc
random_background_folder:C:\Backgrounds
class_pictures:C:\pics2\acdc
personal_yaml_file:bandlogos.yaml
outputfolder:c:\bandlogos1\pics
howmany:1400
background_qty:100
processes:4
image_size_width:640
image_size_height:640
needle_size_percentage_min:0.2
needle_size_percentage_max:0.9
blur_image_kernel_min:1
blur_image_kernel_max:5
blur_image_frequency:10
sharpen_image_kernel_min:1
sharpen_image_kernel_max:6
sharpen_image_frequency:10
distorted_resizing_add_min_x:0.01
distorted_resizing_add_max_x:0.15
distorted_resizing_add_min_y:0.01
distorted_resizing_add_max_y:0.15
distorted_resizing_frequency:10
blur_borders_min_x0:0.01
blur_borders_max_x0:0.1
blur_borders_min_x1:0.01
blur_borders_max_x1:0.1
blur_borders_min_y0:0.01
blur_borders_max_y0:0.1
blur_borders_min_y1:0.01
blur_borders_max_y1:0.1
blur_borders_kernel_min:1
blur_borders_kernel_max:6
blur_borders_frequency:30
pixelborder_min:1
pixelborder_max:20
pixelborder_loop_min:1
pixelborder_loop_max:2
pixelborder_frequency:30
perspective_distortion_min_x:0.01
perspective_distortion_max_x:0.15
perspective_distortion_min_y:0.01
perspective_distortion_max_y:0.15
perspective_distortion_percentage:15
transparency_distortion_min:140
transparency_distortion_max:255
transparency_distortion_frequency:40
canny_edge_blur_thresh_lower_min:10
canny_edge_blur_thresh_lower_max:20
canny_edge_blur_thresh_upper_min:80
canny_edge_blur_thresh_upper_max:90
canny_edge_blur_kernel_min:1
canny_edge_blur_kernel_max:6
canny_edge_blur_frequency:10
random_crop_min_x:0.01
random_crop_max_x:0.1
random_crop_min_y:0.01
random_crop_max_y:0.1
random_crop_frequency:30
hue_shift_min:1
hue_shift_max:180
hue_shift_frequency:90
change_contrast_min:0.8
change_contrast_max:1.2
change_contrast_frequency:30
rotate_image_min:2
rotate_image_max:359
rotate_image_frequency:90
colors_to_change_percentage_max:75
colors_to_change_percentage_min:1
colors_to_change_frequency:90
colors_to_change_r_min:5
colors_to_change_r_max:250
colors_to_change_g_min:5
colors_to_change_g_max:250
colors_to_change_b_min:5
colors_to_change_b_max:250
flip_image_left_right_frequency:2
flip_image_up_down_frequency:2
verbose:True
bloom_kernel_min:1
bloom_kernel_max:25
bloom_sigmaX_min:140
bloom_sigmaX_max:240
bloom_intensity_min:4.5
bloom_intensity_max:20.5
bloom_frequency:30
fish_distortion_min1:0.01
fish_distortion_max1:0.4
fish_distortion_min2:0.01
fish_distortion_max2:0.4
fish_distortion_min3:0.01
fish_distortion_max3:0.4
fish_distortion_min4:0.01
fish_distortion_max4:0.03
fish_divider_1_min:1
fish_divider_1_max:2
fish_divider_2_min:1
fish_divider_2_max:4
fish_divider_3_min:1
fish_divider_3_max:2
fish_divider_4_min:2
fish_divider_4_max:4
fish_border_add:0.1
fish_frequency:50
...
```
## Generating the data set, and training the model
```python
from tools4yolo import start_yolov5_training
if __name__ == "__main__": # necessary - multiprocessing
start_yolov5_training(
cfgfile=r"C:\bandlogos1\training2.ini",
ptfile=r"C:\bandlogos1\best1.pt",
generate_images=True,
train_model=True,
model_file="yolov5m.yaml",
hypfile="hyp.scratch-low.yaml",
batch=16,
epochs=35,
workers=3,
save_period=1,
cache="disk",
)
```
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1386.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1387.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1389.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1392.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1393.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1396.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1398.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1399.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1401.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1402.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1403.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1408.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1421.jpg?raw=true)
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1424.jpg?raw=true)
# Using the trained model
```python
from cv2imshow.cv2imshow import cv2_imshow_multi
from tools4yolo import Yolov5Detect
from fast_ctypes_screenshots import (
ScreenshotOfOneMonitor,
)
ptfile = [r"C:\bandlogos1\pics1\dataset\splitset\bandlogos\weights\best.pt"]
yv = Yolov5Detect(
modelfiles=ptfile, repo_or_dir="./yolov5", model="custom", source="local"
)
try:
with ScreenshotOfOneMonitor(
monitor=0, ascontiguousarray=False
) as screenshots_monitor:
while True:
img5 = screenshots_monitor.screenshot_one_monitor()
li = yv.detect(
images=[
img5
],
confidence_thresh=.4,
bgr_to_rgb=False,
draw_output=True,
#save_folder="c:\\outputfolderyolo3v",
)
cv2_imshow_multi(
title="pic3",
image=li[0][-1],
killkeys="ctrl+alt+h", # switch on/off
)
except KeyboardInterrupt:
pass
```
![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/acdcresults.png?raw=true)
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/tools4yolo",
"name": "tools4yolo",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "image,augmentation,yolov5,yolo",
"author": "Johannes Fischer",
"author_email": "aulasparticularesdealemaosp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/b7/57/6e659feaf2f404a6f57eb9d52fce435355319b70619e930a2653228bbc3d/tools4yolo-0.11.tar.gz",
"platform": null,
"description": "\r\n# Some image augmentation tools for yolov5 (and probably other models)\r\n\r\n## Before you start:\r\n\r\n### install Pytorch\r\n\r\n### install https://github.com/ultralytics/yolov5\r\n\r\n### install https://pypi.org/project/rembg/\r\n\r\n## pip install tools4yolo \r\n\r\n#### Tested against Windows 10 / Python 3.10 / Anaconda \r\n\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1489.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1016.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1085.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1180.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1232.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1244.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1290.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1428.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1434.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1440.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1441.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1442.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1444.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1455.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1474.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1486.jpg?raw=true)\r\n\r\n## Formating the needle images \r\n\r\n```python\r\n\r\n# Get some images from somewhere \r\n# Important! The pictures need to have a transparent background\r\n\r\n# The function remove_background_and_resize removes most of the background. If there is some background left, remove it using GIMP or Photoshop \r\n\r\n\r\nfrom tools4yolo import remove_background_and_resize\r\n\r\n\r\nbands = [\r\n r\"C:\\pics\\aerosmith\",\r\n r\"C:\\pics\\anthrax\",\r\n r\"C:\\pics\\black_sabbath\",\r\n r\"C:\\pics\\iron_maiden\",\r\n r\"C:\\pics\\judas_priest\",\r\n r\"C:\\pics\\kiss\",\r\n r\"C:\\pics\\krokus\",\r\n r\"C:\\pics\\led_zeppelin\",\r\n r\"C:\\pics\\manowar\",\r\n r\"C:\\pics\\metalchurch\",\r\n r\"C:\\pics\\misfits\",\r\n r\"C:\\pics\\motorhead\",\r\n r\"C:\\pics\\ozzy\",\r\n r\"C:\\pics\\pantera\",\r\n r\"C:\\pics\\saint_vitus\",\r\n r\"C:\\pics\\saxon\",\r\n r\"C:\\pics\\scorpions\",\r\n r\"C:\\pics\\slayer\",\r\n r\"C:\\pics\\whitesnake\",\r\n r\"C:\\pics\\accept\",\r\n r\"C:\\pics\\acdc\",\r\n]\r\n\r\nfor folder in bands:\r\n folderband = folder.replace(r\"C:\\pics\", r\"C:\\pics2\")\r\n remove_background_and_resize(\r\n folder=folder, folderout=folderband, maxwidth=640, maxheight=640\r\n )\r\n``` \r\n\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/acdclogos.png?raw=true)\r\n\r\n## Augmenting the needle images \r\n\r\n```python\r\n# To get some variations of the needle images, use: \r\n\r\nfrom tools4yolo import augment_needle_images\r\n\r\naugment_needle_images(\r\n folder=\"c:\\\\pics2\",\r\n outputfolder=\"c:\\\\pics3\",\r\n width=640,\r\n height=640,\r\n)\r\n\r\n# Make sure that there is no background after calling augment_needle_images\r\n\r\n\r\n``` \r\n\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/acdcaugm.png?raw=true)\r\n\r\n## Generating the configuration file \r\n\r\n\r\n```python\r\n\r\nimport os\r\nfrom tools4yolo import generate_ini_file\r\n\r\nallbands = [\r\n r\"C:\\pics3\\metallica\",\r\n r\"C:\\pics3\\acdc\",\r\n r\"C:\\pics3\\aerosmith\",\r\n r\"C:\\pics3\\anthrax\",\r\n r\"C:\\pics3\\black_sabbath\",\r\n r\"C:\\pics3\\iron_maiden\",\r\n r\"C:\\pics3\\judas_priest\",\r\n r\"C:\\pics3\\kiss\",\r\n r\"C:\\pics3\\krokus\",\r\n r\"C:\\pics3\\led_zeppelin\",\r\n r\"C:\\pics3\\manowar\",\r\n r\"C:\\pics3\\metalchurch\",\r\n r\"C:\\pics3\\misfits\",\r\n r\"C:\\pics3\\motorhead\",\r\n r\"C:\\pics3\\ozzy\",\r\n r\"C:\\pics3\\pantera\",\r\n r\"C:\\pics3\\saint_vitus\",\r\n r\"C:\\pics3\\saxon\",\r\n r\"C:\\pics3\\scorpions\",\r\n r\"C:\\pics3\\slayer\",\r\n r\"C:\\pics3\\whitesnake\",\r\n r\"C:\\pics3\\accept\",\r\n]\r\nallclasses = []\r\nbackgroundfolder = r\"C:\\Backgrounds\"\r\npersonal_yaml_file = \"bandlogos.yaml\" # name it however you want \r\noutputfolder = \"c:\\\\bandlogos1\\\\pics2\" # folder will be created if it does not exist\r\noutputinifile = 'c:\\\\bandlogos1\\\\training2.ini' # file will be created if it does not exist\r\nos.makedirs(outputfolder,exist_ok=True)\r\nfor i, k in enumerate(allbands):\r\n allclasses.append(\r\n {\r\n \"classnumber\": i,\r\n \"classname\": k.split(os.sep)[-1],\r\n \"random_background_folder\": backgroundfolder,\r\n \"class_pictures\": k,\r\n \"personal_yaml_file\": personal_yaml_file,\r\n \"outputfolder\": outputfolder,\r\n \"howmany\": 1000,\r\n \"background_qty\": 100,\r\n \"processes\": 2,\r\n \"image_size_width\": 640,\r\n \"image_size_height\": 640,\r\n \"needle_size_percentage_min\": 0.20,\r\n \"needle_size_percentage_max\": 0.9,\r\n \"blur_image_kernel_min\": 1,\r\n \"blur_image_kernel_max\": 5,\r\n \"blur_image_frequency\": 10,\r\n \"sharpen_image_kernel_min\": 1,\r\n \"sharpen_image_kernel_max\": 6,\r\n \"sharpen_image_frequency\": 10,\r\n \"distorted_resizing_add_min_x\": 0.01,\r\n \"distorted_resizing_add_max_x\": 0.15,\r\n \"distorted_resizing_add_min_y\": 0.01,\r\n \"distorted_resizing_add_max_y\": 0.15,\r\n \"distorted_resizing_frequency\": 10,\r\n \"blur_borders_min_x0\": 0.01,\r\n \"blur_borders_max_x0\": 0.1,\r\n \"blur_borders_min_x1\": 0.01,\r\n \"blur_borders_max_x1\": 0.1,\r\n \"blur_borders_min_y0\": 0.01,\r\n \"blur_borders_max_y0\": 0.1,\r\n \"blur_borders_min_y1\": 0.01,\r\n \"blur_borders_max_y1\": 0.1,\r\n \"blur_borders_kernel_min\": 1,\r\n \"blur_borders_kernel_max\": 6,\r\n \"blur_borders_frequency\": 30,\r\n \"pixelborder_min\": 1,\r\n \"pixelborder_max\": 20,\r\n \"pixelborder_loop_min\": 1,\r\n \"pixelborder_loop_max\": 2,\r\n \"pixelborder_frequency\": 30,\r\n \"perspective_distortion_min_x\": 0.01,\r\n \"perspective_distortion_max_x\": 0.15,\r\n \"perspective_distortion_min_y\": 0.01,\r\n \"perspective_distortion_max_y\": 0.15,\r\n \"perspective_distortion_percentage\": 15,\r\n \"transparency_distortion_min\": 140,\r\n \"transparency_distortion_max\": 255,\r\n \"transparency_distortion_frequency\": 40,\r\n \"canny_edge_blur_thresh_lower_min\": 10,\r\n \"canny_edge_blur_thresh_lower_max\": 20,\r\n \"canny_edge_blur_thresh_upper_min\": 80,\r\n \"canny_edge_blur_thresh_upper_max\": 90,\r\n \"canny_edge_blur_kernel_min\": 1,\r\n \"canny_edge_blur_kernel_max\": 6,\r\n \"canny_edge_blur_frequency\": 10,\r\n \"random_crop_min_x\": 0.01,\r\n \"random_crop_max_x\": 0.10,\r\n \"random_crop_min_y\": 0.01,\r\n \"random_crop_max_y\": 0.10,\r\n \"random_crop_frequency\": 30,\r\n \"hue_shift_min\": 1,\r\n \"hue_shift_max\": 180,\r\n \"hue_shift_frequency\": 90,\r\n \"change_contrast_min\": 0.8,\r\n \"change_contrast_max\": 1.2,\r\n \"change_contrast_frequency\": 30,\r\n \"rotate_image_min\": 2,\r\n \"rotate_image_max\": 359,\r\n \"rotate_image_frequency\": 90,\r\n \"colors_to_change_percentage_max\": 75,\r\n \"colors_to_change_percentage_min\": 1,\r\n \"colors_to_change_frequency\": 90,\r\n \"colors_to_change_r_min\": 5,\r\n \"colors_to_change_r_max\": 250,\r\n \"colors_to_change_g_min\": 5,\r\n \"colors_to_change_g_max\": 250,\r\n \"colors_to_change_b_min\": 5,\r\n \"colors_to_change_b_max\": 250,\r\n \"flip_image_left_right_frequency\": 2,\r\n \"flip_image_up_down_frequency\": 2,\r\n \"verbose\": True,\r\n \"bloom_kernel_min\": 1,\r\n \"bloom_kernel_max\": 25,\r\n \"bloom_sigmaX_min\": 140,\r\n \"bloom_sigmaX_max\": 240,\r\n \"bloom_intensity_min\": 4.5,\r\n \"bloom_intensity_max\": 20.5,\r\n \"bloom_frequency\": 30,\r\n \"fish_distortion_min1\": 0.01,\r\n \"fish_distortion_max1\": 0.4,\r\n \"fish_distortion_min2\": 0.01,\r\n \"fish_distortion_max2\": 0.4,\r\n \"fish_distortion_min3\": 0.01,\r\n \"fish_distortion_max3\": 0.4,\r\n \"fish_distortion_min4\": 0.01,\r\n \"fish_distortion_max4\": 0.03,\r\n \"fish_divider_1_min\": 1,\r\n \"fish_divider_1_max\": 2,\r\n \"fish_divider_2_min\": 1,\r\n \"fish_divider_2_max\": 4,\r\n \"fish_divider_3_min\": 1,\r\n \"fish_divider_3_max\": 2,\r\n \"fish_divider_4_min\": 2,\r\n \"fish_divider_4_max\": 4,\r\n \"fish_border_add\": 0.1,\r\n \"fish_frequency\": 50,\r\n }\r\n )\r\ninifile=generate_ini_file(allclasses)\r\nprint(inifile)\r\n\r\nwith open(outputinifile,mode='w',encoding='utf-8') as f:\r\n f.write(inifile)\r\n\r\n\r\n# Example of a generated config file.\r\n\r\n[class0]\r\nclassnumber:0\r\nclassname:metallica\r\nrandom_background_folder:C:\\Backgrounds\r\nclass_pictures:C:\\pics2\\metallica\r\npersonal_yaml_file:bandlogos.yaml\r\noutputfolder:c:\\bandlogos1\\pics\r\nhowmany:1400\r\nbackground_qty:100\r\nprocesses:4\r\nimage_size_width:640\r\nimage_size_height:640\r\nneedle_size_percentage_min:0.2\r\nneedle_size_percentage_max:0.9\r\nblur_image_kernel_min:1\r\nblur_image_kernel_max:5\r\nblur_image_frequency:10\r\nsharpen_image_kernel_min:1\r\nsharpen_image_kernel_max:6\r\nsharpen_image_frequency:10\r\ndistorted_resizing_add_min_x:0.01\r\ndistorted_resizing_add_max_x:0.15\r\ndistorted_resizing_add_min_y:0.01\r\ndistorted_resizing_add_max_y:0.15\r\ndistorted_resizing_frequency:10\r\nblur_borders_min_x0:0.01\r\nblur_borders_max_x0:0.1\r\nblur_borders_min_x1:0.01\r\nblur_borders_max_x1:0.1\r\nblur_borders_min_y0:0.01\r\nblur_borders_max_y0:0.1\r\nblur_borders_min_y1:0.01\r\nblur_borders_max_y1:0.1\r\nblur_borders_kernel_min:1\r\nblur_borders_kernel_max:6\r\nblur_borders_frequency:30\r\npixelborder_min:1\r\npixelborder_max:20\r\npixelborder_loop_min:1\r\npixelborder_loop_max:2\r\npixelborder_frequency:30\r\nperspective_distortion_min_x:0.01\r\nperspective_distortion_max_x:0.15\r\nperspective_distortion_min_y:0.01\r\nperspective_distortion_max_y:0.15\r\nperspective_distortion_percentage:15\r\ntransparency_distortion_min:140\r\ntransparency_distortion_max:255\r\ntransparency_distortion_frequency:40\r\ncanny_edge_blur_thresh_lower_min:10\r\ncanny_edge_blur_thresh_lower_max:20\r\ncanny_edge_blur_thresh_upper_min:80\r\ncanny_edge_blur_thresh_upper_max:90\r\ncanny_edge_blur_kernel_min:1\r\ncanny_edge_blur_kernel_max:6\r\ncanny_edge_blur_frequency:10\r\nrandom_crop_min_x:0.01\r\nrandom_crop_max_x:0.1\r\nrandom_crop_min_y:0.01\r\nrandom_crop_max_y:0.1\r\nrandom_crop_frequency:30\r\nhue_shift_min:1\r\nhue_shift_max:180\r\nhue_shift_frequency:90\r\nchange_contrast_min:0.8\r\nchange_contrast_max:1.2\r\nchange_contrast_frequency:30\r\nrotate_image_min:2\r\nrotate_image_max:359\r\nrotate_image_frequency:90\r\ncolors_to_change_percentage_max:75\r\ncolors_to_change_percentage_min:1\r\ncolors_to_change_frequency:90\r\ncolors_to_change_r_min:5\r\ncolors_to_change_r_max:250\r\ncolors_to_change_g_min:5\r\ncolors_to_change_g_max:250\r\ncolors_to_change_b_min:5\r\ncolors_to_change_b_max:250\r\nflip_image_left_right_frequency:2\r\nflip_image_up_down_frequency:2\r\nverbose:True\r\nbloom_kernel_min:1\r\nbloom_kernel_max:25\r\nbloom_sigmaX_min:140\r\nbloom_sigmaX_max:240\r\nbloom_intensity_min:4.5\r\nbloom_intensity_max:20.5\r\nbloom_frequency:30\r\nfish_distortion_min1:0.01\r\nfish_distortion_max1:0.4\r\nfish_distortion_min2:0.01\r\nfish_distortion_max2:0.4\r\nfish_distortion_min3:0.01\r\nfish_distortion_max3:0.4\r\nfish_distortion_min4:0.01\r\nfish_distortion_max4:0.03\r\nfish_divider_1_min:1\r\nfish_divider_1_max:2\r\nfish_divider_2_min:1\r\nfish_divider_2_max:4\r\nfish_divider_3_min:1\r\nfish_divider_3_max:2\r\nfish_divider_4_min:2\r\nfish_divider_4_max:4\r\nfish_border_add:0.1\r\nfish_frequency:50\r\n\r\n\r\n[class1]\r\nclassnumber:1\r\nclassname:acdc\r\nrandom_background_folder:C:\\Backgrounds\r\nclass_pictures:C:\\pics2\\acdc\r\npersonal_yaml_file:bandlogos.yaml\r\noutputfolder:c:\\bandlogos1\\pics\r\nhowmany:1400\r\nbackground_qty:100\r\nprocesses:4\r\nimage_size_width:640\r\nimage_size_height:640\r\nneedle_size_percentage_min:0.2\r\nneedle_size_percentage_max:0.9\r\nblur_image_kernel_min:1\r\nblur_image_kernel_max:5\r\nblur_image_frequency:10\r\nsharpen_image_kernel_min:1\r\nsharpen_image_kernel_max:6\r\nsharpen_image_frequency:10\r\ndistorted_resizing_add_min_x:0.01\r\ndistorted_resizing_add_max_x:0.15\r\ndistorted_resizing_add_min_y:0.01\r\ndistorted_resizing_add_max_y:0.15\r\ndistorted_resizing_frequency:10\r\nblur_borders_min_x0:0.01\r\nblur_borders_max_x0:0.1\r\nblur_borders_min_x1:0.01\r\nblur_borders_max_x1:0.1\r\nblur_borders_min_y0:0.01\r\nblur_borders_max_y0:0.1\r\nblur_borders_min_y1:0.01\r\nblur_borders_max_y1:0.1\r\nblur_borders_kernel_min:1\r\nblur_borders_kernel_max:6\r\nblur_borders_frequency:30\r\npixelborder_min:1\r\npixelborder_max:20\r\npixelborder_loop_min:1\r\npixelborder_loop_max:2\r\npixelborder_frequency:30\r\nperspective_distortion_min_x:0.01\r\nperspective_distortion_max_x:0.15\r\nperspective_distortion_min_y:0.01\r\nperspective_distortion_max_y:0.15\r\nperspective_distortion_percentage:15\r\ntransparency_distortion_min:140\r\ntransparency_distortion_max:255\r\ntransparency_distortion_frequency:40\r\ncanny_edge_blur_thresh_lower_min:10\r\ncanny_edge_blur_thresh_lower_max:20\r\ncanny_edge_blur_thresh_upper_min:80\r\ncanny_edge_blur_thresh_upper_max:90\r\ncanny_edge_blur_kernel_min:1\r\ncanny_edge_blur_kernel_max:6\r\ncanny_edge_blur_frequency:10\r\nrandom_crop_min_x:0.01\r\nrandom_crop_max_x:0.1\r\nrandom_crop_min_y:0.01\r\nrandom_crop_max_y:0.1\r\nrandom_crop_frequency:30\r\nhue_shift_min:1\r\nhue_shift_max:180\r\nhue_shift_frequency:90\r\nchange_contrast_min:0.8\r\nchange_contrast_max:1.2\r\nchange_contrast_frequency:30\r\nrotate_image_min:2\r\nrotate_image_max:359\r\nrotate_image_frequency:90\r\ncolors_to_change_percentage_max:75\r\ncolors_to_change_percentage_min:1\r\ncolors_to_change_frequency:90\r\ncolors_to_change_r_min:5\r\ncolors_to_change_r_max:250\r\ncolors_to_change_g_min:5\r\ncolors_to_change_g_max:250\r\ncolors_to_change_b_min:5\r\ncolors_to_change_b_max:250\r\nflip_image_left_right_frequency:2\r\nflip_image_up_down_frequency:2\r\nverbose:True\r\nbloom_kernel_min:1\r\nbloom_kernel_max:25\r\nbloom_sigmaX_min:140\r\nbloom_sigmaX_max:240\r\nbloom_intensity_min:4.5\r\nbloom_intensity_max:20.5\r\nbloom_frequency:30\r\nfish_distortion_min1:0.01\r\nfish_distortion_max1:0.4\r\nfish_distortion_min2:0.01\r\nfish_distortion_max2:0.4\r\nfish_distortion_min3:0.01\r\nfish_distortion_max3:0.4\r\nfish_distortion_min4:0.01\r\nfish_distortion_max4:0.03\r\nfish_divider_1_min:1\r\nfish_divider_1_max:2\r\nfish_divider_2_min:1\r\nfish_divider_2_max:4\r\nfish_divider_3_min:1\r\nfish_divider_3_max:2\r\nfish_divider_4_min:2\r\nfish_divider_4_max:4\r\nfish_border_add:0.1\r\nfish_frequency:50\r\n\r\n...\r\n``` \r\n\r\n## Generating the data set, and training the model \r\n\r\n```python\r\n\r\nfrom tools4yolo import start_yolov5_training\r\n\r\nif __name__ == \"__main__\": # necessary - multiprocessing\r\n start_yolov5_training(\r\n\r\n cfgfile=r\"C:\\bandlogos1\\training2.ini\",\r\n ptfile=r\"C:\\bandlogos1\\best1.pt\",\r\n generate_images=True,\r\n train_model=True,\r\n model_file=\"yolov5m.yaml\",\r\n hypfile=\"hyp.scratch-low.yaml\",\r\n batch=16,\r\n epochs=35,\r\n workers=3,\r\n save_period=1,\r\n cache=\"disk\",\r\n )\r\n\r\n```\r\n\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1386.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1387.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1389.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1392.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1393.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1396.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1398.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1399.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1401.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1402.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1403.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1408.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1421.jpg?raw=true)\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/image1424.jpg?raw=true)\r\n\r\n# Using the trained model\r\n\r\n```python\r\n\r\nfrom cv2imshow.cv2imshow import cv2_imshow_multi\r\n\r\nfrom tools4yolo import Yolov5Detect\r\nfrom fast_ctypes_screenshots import (\r\n ScreenshotOfOneMonitor,\r\n\r\n)\r\nptfile = [r\"C:\\bandlogos1\\pics1\\dataset\\splitset\\bandlogos\\weights\\best.pt\"]\r\n\r\nyv = Yolov5Detect(\r\n modelfiles=ptfile, repo_or_dir=\"./yolov5\", model=\"custom\", source=\"local\"\r\n)\r\ntry:\r\n with ScreenshotOfOneMonitor(\r\n monitor=0, ascontiguousarray=False\r\n ) as screenshots_monitor:\r\n while True:\r\n\r\n img5 = screenshots_monitor.screenshot_one_monitor()\r\n li = yv.detect(\r\n images=[\r\n img5\r\n ],\r\n confidence_thresh=.4,\r\n bgr_to_rgb=False,\r\n draw_output=True,\r\n #save_folder=\"c:\\\\outputfolderyolo3v\",\r\n )\r\n cv2_imshow_multi(\r\n title=\"pic3\",\r\n image=li[0][-1],\r\n killkeys=\"ctrl+alt+h\", # switch on/off\r\n )\r\nexcept KeyboardInterrupt:\r\n pass\r\n\r\n\r\n```\r\n\r\n![](https://github.com/hansalemaos/screenshots/blob/main/yolov5augmentation/acdcresults.png?raw=true)\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Some image augmentation tools for yolov5 (and probably other models)",
"version": "0.11",
"project_urls": {
"Homepage": "https://github.com/hansalemaos/tools4yolo"
},
"split_keywords": [
"image",
"augmentation",
"yolov5",
"yolo"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a43b699241e8d45ef6122251526cb0e5c732706dc59fe6e93e9f56eba88bc745",
"md5": "90ab9325df473f4c0aaf3c8c07518b87",
"sha256": "10c66f1053a0e2d5962f72afca17921eb1ec788c48c8fbb826604810b2adf88e"
},
"downloads": -1,
"filename": "tools4yolo-0.11-py3-none-any.whl",
"has_sig": false,
"md5_digest": "90ab9325df473f4c0aaf3c8c07518b87",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 128912,
"upload_time": "2023-08-30T03:12:31",
"upload_time_iso_8601": "2023-08-30T03:12:31.721874Z",
"url": "https://files.pythonhosted.org/packages/a4/3b/699241e8d45ef6122251526cb0e5c732706dc59fe6e93e9f56eba88bc745/tools4yolo-0.11-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b7576e659feaf2f404a6f57eb9d52fce435355319b70619e930a2653228bbc3d",
"md5": "a010f63790bed1025176d3b8eb304d54",
"sha256": "c1abd6bc59bb946ad45e0a321d33fc074238b32a35a3b135c2ece72872d3f12f"
},
"downloads": -1,
"filename": "tools4yolo-0.11.tar.gz",
"has_sig": false,
"md5_digest": "a010f63790bed1025176d3b8eb304d54",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 128620,
"upload_time": "2023-08-30T03:12:34",
"upload_time_iso_8601": "2023-08-30T03:12:34.261573Z",
"url": "https://files.pythonhosted.org/packages/b7/57/6e659feaf2f404a6f57eb9d52fce435355319b70619e930a2653228bbc3d/tools4yolo-0.11.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-30 03:12:34",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hansalemaos",
"github_project": "tools4yolo",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "tools4yolo"
}