forked from pprp/SimpleCVReproduction
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
625 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import glob | ||
import cv2 as cv2 | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
# import random | ||
import math | ||
from tqdm import tqdm | ||
|
||
|
||
def load_images(path): | ||
image_list = [] | ||
images = glob.glob(path) | ||
for index in range(len(images)): | ||
image = cv2.cvtColor(cv2.imread(images[index]), cv2.COLOR_BGR2RGB) | ||
image_list.append(image) | ||
# image_list.append(cv2.resize(image,(1280,720))) | ||
|
||
return image_list | ||
|
||
|
||
def read_images(path): | ||
images = glob.glob(path) | ||
return images | ||
|
||
|
||
def load_images_from_path(path): | ||
image_list = [] | ||
for p in tqdm(path): | ||
image = cv2.cvtColor(cv2.imread(p), cv2.COLOR_BGR2RGB) | ||
image_list.append(image) | ||
return image_list | ||
|
||
|
||
def replace_labels(path): | ||
labelpath = [] | ||
for p in path: | ||
labelpath.append(p.replace('.jpg', '.txt')) | ||
return labelpath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Simple python implementation of Augmentation for small object detection, as described in [Augmentation for small object detection | ||
](https://arxiv.org/pdf/1902.07296.pdf). | ||
|
||
 | ||
 | ||
|
||
## Demo | ||
```shell | ||
cd $ROOT | ||
python demo.py | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
# import glob | ||
import cv2 as cv2 | ||
import numpy as np | ||
# from PIL import Image | ||
import random | ||
import math | ||
from os.path import basename, split, join, dirname | ||
from util import * | ||
|
||
|
||
def find_str(filename): | ||
if 'train' in filename: | ||
return dirname(filename[filename.find('train'):]) | ||
else: | ||
return dirname(filename[filename.find('val'):]) | ||
|
||
|
||
def convert_all_boxes(shape, anno_infos, yolo_label_txt_dir): | ||
height, width, n = shape | ||
label_file = open(yolo_label_txt_dir, 'w') | ||
for anno_info in anno_infos: | ||
target_id, x1, y1, x2, y2 = anno_info | ||
b = (float(x1), float(x2), float(y1), float(y2)) | ||
bb = convert((width, height), b) | ||
label_file.write(str(target_id) + " " + " ".join([str(a) for a in bb]) + '\n') | ||
|
||
|
||
def save_crop_image(save_crop_base_dir, image_dir, idx, roi): | ||
crop_save_dir = join(save_crop_base_dir, find_str(image_dir)) | ||
check_dir(crop_save_dir) | ||
crop_img_save_dir = join(crop_save_dir, basename(image_dir)[:-3] + '_crop_' + str(idx) + '.jpg') | ||
cv2.imwrite(crop_img_save_dir, roi) | ||
|
||
|
||
def copysmallobjects(image_dir, label_dir, save_base_dir, save_crop_base_dir=None, | ||
save_annoation_base_dir=None): | ||
image = cv2.imread(image_dir) | ||
|
||
labels = read_label_txt(label_dir) | ||
if len(labels) == 0: return | ||
rescale_labels = rescale_yolo_labels(labels, image.shape) # 转换坐标表示 | ||
all_boxes = [] | ||
|
||
for idx, rescale_label in enumerate(rescale_labels): | ||
|
||
all_boxes.append(rescale_label) | ||
# 目标的长宽 | ||
rescale_label_height, rescale_label_width = rescale_label[4] - rescale_label[2], rescale_label[3] - \ | ||
rescale_label[1] | ||
|
||
if (issmallobject((rescale_label_height, rescale_label_width), thresh=64 * 64) and rescale_label[0] == '1'): | ||
roi = image[rescale_label[2]:rescale_label[4], rescale_label[1]:rescale_label[3]] | ||
|
||
new_bboxes = random_add_patches(rescale_label, rescale_labels, image.shape, paste_number=2, iou_thresh=0.2) | ||
count = 0 | ||
|
||
# 将新生成的位置加入到label,并在相应位置画出物体 | ||
for new_bbox in new_bboxes: | ||
count += 1 | ||
all_boxes.append(new_bbox) | ||
cl, bbox_left, bbox_top, bbox_right, bbox_bottom = new_bbox[0], new_bbox[1], new_bbox[2], new_bbox[3], \ | ||
new_bbox[4] | ||
try: | ||
if (count > 1): | ||
roi = flip_bbox(roi) | ||
image[bbox_top:bbox_bottom, bbox_left:bbox_right] = roi | ||
except ValueError: | ||
continue | ||
|
||
dir_name = find_str(image_dir) | ||
save_dir = join(save_base_dir, dir_name) | ||
check_dir(save_dir) | ||
yolo_txt_dir = join(save_dir, basename(image_dir.replace('.jpg', '_augment.txt'))) | ||
cv2.imwrite(join(save_dir, basename(image_dir).replace('.jpg', '_augment.jpg')), image) | ||
convert_all_boxes(image.shape, all_boxes, yolo_txt_dir) | ||
|
||
|
||
def GaussianBlurImg(image): | ||
# 高斯模糊 | ||
ran = random.randint(0, 9) | ||
if ran % 2 == 1: | ||
image = cv2.GaussianBlur(image, ksize=(ran, ran), sigmaX=0, sigmaY=0) | ||
else: | ||
pass | ||
return image | ||
|
||
|
||
def suo_fang(image, area_max=2000, area_min=1000): | ||
# 改变图片大小 | ||
height, width, channels = image.shape | ||
|
||
while (height*width) > area_max: | ||
image = cv2.resize(image, (int(width * 0.9),int(height * 0.9))) | ||
height, width, channels = image.shape | ||
height,width = int(height*0.9),int(width*0.9) | ||
|
||
while (height*width) < area_min: | ||
image = cv2.resize(image, (int(width * 1.1),int(height * 1.1))) | ||
height, width, channels = image.shape | ||
height,width = int(height*1.1),int(width*1.1) | ||
|
||
return image | ||
|
||
|
||
def copysmallobjects2(image_dir, label_dir, save_base_dir, small_img_dir): | ||
image = cv2.imread(image_dir) | ||
labels = read_label_txt(label_dir) | ||
if len(labels) == 0: | ||
return | ||
rescale_labels = rescale_yolo_labels(labels, image.shape) # 转换坐标表示 | ||
all_boxes = [] | ||
for _, rescale_label in enumerate(rescale_labels): | ||
all_boxes.append(rescale_label) | ||
|
||
for small_img_dirs in small_img_dir: | ||
image_bbox = cv2.imread(small_img_dirs) | ||
#roi = image_bbox | ||
roi = suo_fang(image_bbox,area_max=3000,area_min=1500) | ||
|
||
new_bboxes = random_add_patches2(roi.shape, rescale_labels, image.shape, paste_number=1, iou_thresh=0) | ||
count = 0 | ||
for new_bbox in new_bboxes: | ||
count += 1 | ||
|
||
cl, bbox_left, bbox_top, bbox_right, bbox_bottom = new_bbox[0], new_bbox[1], new_bbox[2], new_bbox[3], \ | ||
new_bbox[4] | ||
#roi = GaussianBlurImg(roi) # 高斯模糊 | ||
height, width, channels = roi.shape | ||
center = (int(width / 2),int(height / 2)) | ||
#ran_point = (int((bbox_top+bbox_bottom)/2),int((bbox_left+bbox_right)/2)) | ||
mask = 255 * np.ones(roi.shape, roi.dtype) | ||
|
||
try: | ||
if count > 1: | ||
roi = flip_bbox(roi) | ||
#image[bbox_top:bbox_bottom, bbox_left:bbox_right] = roi | ||
#image[bbox_top:bbox_bottom, bbox_left:bbox_right] = cv2.addWeighted(image[bbox_top:bbox_bottom, bbox_left:bbox_right], | ||
# 0.5,roi,0.5,0) #图片融合 | ||
|
||
# 泊松融合 | ||
#image = cv2.seamlessClone(roi, image, mask, ran_point, cv2.NORMAL_CLONE) | ||
#print(str(bbox_bottom-bbox_top) + "|" + str(bbox_right-bbox_left)) | ||
#print(roi.shape) | ||
#print(mask.shape) | ||
image[bbox_top:bbox_bottom, bbox_left:bbox_right] = cv2.seamlessClone(roi, image[bbox_top:bbox_bottom, bbox_left:bbox_right], | ||
mask, center, cv2.NORMAL_CLONE) | ||
all_boxes.append(new_bbox) | ||
rescale_labels.append(new_bbox) | ||
except ValueError: | ||
print("---") | ||
continue | ||
dir_name = find_str(image_dir) | ||
save_dir = join(save_base_dir, dir_name) | ||
check_dir(save_dir) | ||
yolo_txt_dir = join(save_dir, basename(image_dir.replace('.jpg', '_augment.txt'))) | ||
cv2.imwrite(join(save_dir, basename(image_dir).replace('.jpg', '_augment.jpg')), image) | ||
convert_all_boxes(image.shape, all_boxes, yolo_txt_dir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
0 0.265364583333 0.139130434783 0.0151041666667 0.0369565217391 | ||
0 0.284114583333 0.151086956522 0.0119791666667 0.0173913043478 | ||
0 0.3515625 0.179347826087 0.015625 0.0239130434783 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
0 0.2671875 0.139130434783 0.021875 0.0434782608696 | ||
0 0.284114583333 0.154891304348 0.0119791666667 0.025 | ||
0 0.350520833333 0.179347826087 0.0177083333333 0.0239130434783 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
0 0.265364583333 0.139130434783 0.0151041666667 0.0369565217391 | ||
0 0.284114583333 0.151086956522 0.0119791666667 0.0173913043478 | ||
0 0.3515625 0.179347826087 0.015625 0.0239130434783 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
0 0.2671875 0.139130434783 0.021875 0.0434782608696 | ||
0 0.284114583333 0.154891304348 0.0119791666667 0.025 | ||
0 0.350520833333 0.179347826087 0.0177083333333 0.0239130434783 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
find ./background -maxdepth 4 -name "*.jpg" | sort > train.txt | ||
find ./crops -maxdepth 4 -name "*.jpg" | sort > small.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import aug as am | ||
import Helpers as hp | ||
from util import * | ||
import os | ||
from os.path import join | ||
from tqdm import tqdm | ||
import random | ||
|
||
base_dir = os.getcwd() | ||
|
||
save_base_dir = join(base_dir, 'save') | ||
|
||
check_dir(save_base_dir) | ||
|
||
imgs_dir = [f.strip() for f in open(join(base_dir, 'train.txt')).readlines()] | ||
labels_dir = hp.replace_labels(imgs_dir) | ||
|
||
small_imgs_dir = [f.strip() for f in open(join(base_dir, 'small.txt')).readlines()] | ||
random.shuffle(small_imgs_dir) | ||
|
||
for image_dir, label_dir in tqdm(zip(imgs_dir, labels_dir)): | ||
small_img = [] | ||
for x in range(8): | ||
if small_imgs_dir == []: | ||
#exit() | ||
small_imgs_dir = [f.strip() for f in open(join(base_dir,'small.txt')).readlines()] | ||
random.shuffle(small_imgs_dir) | ||
small_img.append(small_imgs_dir.pop()) | ||
am.copysmallobjects2(image_dir, label_dir, save_base_dir,small_img) |
7 changes: 7 additions & 0 deletions
7
SmallObjectAugmentation/figs/2018-11-03_07_45_09_1_augment.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
1 0.6 0.5898148148148148 0.0125 0.061111111111111116 | ||
1 0.346875 0.5916666666666667 0.0125 0.061111111111111116 | ||
1 0.2708333333333333 0.5750000000000001 0.0125 0.061111111111111116 | ||
0 0.3046875 0.44537037037037036 0.065625 0.1388888888888889 | ||
0 0.10625 0.8481481481481482 0.19375 0.3 | ||
0 0.6140625 0.40740740740740744 0.026041666666666668 0.037037037037037035 | ||
0 0.6359375 0.375 0.036458333333333336 0.07222222222222223 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
1 0.602083333333 0.591666666667 0.0125 0.0611111111111 | ||
0 0.306770833333 0.449074074074 0.065625 0.138888888889 | ||
0 0.108333333333 0.85 0.19375 0.3 | ||
0 0.616145833333 0.411111111111 0.0260416666667 0.037037037037 | ||
0 0.636979166667 0.376851851852 0.0364583333333 0.0722222222222 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
0 0.2643229166666667 0.13804347826086957 0.015104166666666667 0.03695652173913044 | ||
0 0.2830729166666667 0.15 0.011979166666666666 0.017391304347826087 | ||
0 0.35104166666666664 0.1782608695652174 0.015625 0.02391304347826087 | ||
1 0.8265625 0.6293478260869565 0.011458333333333333 0.06739130434782609 | ||
1 0.5752604166666666 0.7494565217391305 0.021354166666666667 0.07934782608695652 | ||
1 0.6140625 0.8445652173913044 0.014583333333333334 0.09565217391304348 | ||
1 0.72890625 0.6347826086956522 0.015104166666666667 0.09130434782608696 | ||
1 0.43515624999999997 0.7663043478260869 0.022395833333333334 0.0782608695652174 | ||
1 0.7671875 0.5396739130434782 0.017708333333333333 0.0923913043478261 | ||
1 0.5122395833333333 0.9032608695652175 0.0203125 0.08695652173913043 | ||
1 0.53046875 0.5679347826086957 0.018229166666666668 0.08586956521739131 | ||
1 0.6596354166666667 0.7711956521739131 0.021354166666666667 0.07282608695652174 | ||
1 0.490625 0.8190217391304347 0.01875 0.08804347826086957 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
0 0.26666666666666666 0.13804347826086957 0.021875 0.043478260869565216 | ||
0 0.2830729166666667 0.15380434782608696 0.011979166666666666 0.025 | ||
0 0.3494791666666667 0.1782608695652174 0.017708333333333333 0.02391304347826087 | ||
1 0.94296875 0.6923913043478261 0.013020833333333334 0.07608695652173914 | ||
1 0.47031249999999997 0.8630434782608696 0.015625 0.07173913043478261 | ||
1 0.62265625 0.5782608695652174 0.024479166666666666 0.08478260869565217 | ||
1 0.29921875 0.5173913043478261 0.0203125 0.07173913043478261 | ||
1 0.7361979166666667 0.5119565217391304 0.013020833333333334 0.06304347826086956 | ||
1 0.3963541666666667 0.6402173913043478 0.016666666666666666 0.1 | ||
1 0.4236979166666667 0.7717391304347826 0.0171875 0.08260869565217391 | ||
1 0.9356770833333333 0.8913043478260869 0.0203125 0.09782608695652174 | ||
1 0.8783854166666667 0.5608695652173913 0.015104166666666667 0.09130434782608696 | ||
1 0.8260416666666667 0.5233695652173913 0.019791666666666666 0.08369565217391305 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
./crops/0000003_crop_39.jpg | ||
./crops/0000003_crop_60.jpg | ||
./crops/0000003_crop_73.jpg | ||
./crops/0000003_crop_74.jpg | ||
./crops/0000003_crop_87.jpg | ||
./crops/0000004_crop_28.jpg | ||
./crops/0000004_crop_30.jpg | ||
./crops/0000004_crop_5.jpg | ||
./crops/0000004_crop_6.jpg | ||
./crops/0000004_crop_7.jpg | ||
./crops/0000004_crop_8.jpg | ||
./crops/0000005_crop_5.jpg | ||
./crops/0000014_crop_15.jpg | ||
./crops/0000024_crop_5.jpg | ||
./crops/0000188_crop_44.jpg | ||
./crops/0000189_crop_76.jpg | ||
./crops/0000189_crop_77.jpg | ||
./crops/0000190_crop_28.jpg | ||
./crops/0000239_crop_23.jpg | ||
./crops/0000242_crop_15.jpg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
./background/img00000.jpg | ||
./background/img00001.jpg |
Oops, something went wrong.