forked from yinguobing/image_utility
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_split.py
More file actions
64 lines (50 loc) · 2.1 KB
/
Copy pathrandom_split.py
File metadata and controls
64 lines (50 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import sys
import cv2
from random import randrange
from subprocess import call
train_share = 857
total_share = 1000
current_path = "/home/robin/Desktop/hand_images/00_rps_image_set/scissor"
image_format_list = ["jpeg", "jpg", "gif", "png", "bmp", "tiff", "ppm", "pgm", "pbm", "pnm"]
count_images = 0
count_images_train = 0
count_images_test = 0
total_files = 0
cv2.namedWindow("Preview", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Preview", 120, 120)
def generate_path(path, path_type="_generated"):
dir_list = path.split()
dir_list[-1] = dir_list[-1]+path_type
separator = "/"
return separator.join(dir_list)
train_path = generate_path(current_path, "_train")
test_path = generate_path(current_path, "_test")
os.makedirs(train_path)
os.makedirs(test_path)
for file_path, _, file_names in os.walk(current_path, followlinks=False):
for file_name in file_names:
if file_name.split(".")[-1] in image_format_list:
count_images += 1
old_file = os.path.join(file_path, file_name)
# Generate random number range 1~total_share, which helps decide current image's belonging.
if randrange(1, total_share) <= train_share:
new_file = os.path.join(train_path, file_name)
count_images_train += 1
else:
new_file = os.path.join(test_path, file_name)
count_images_test += 1
call(["cp", old_file, new_file])
# Uncomment following code to preview images during counting.
# print(file_name)
# image = cv2.imread(old_file)
# cv2.imshow("Preview", image)
# cv2.waitKey(10)
else:
print("Not a image file: ", file_name)
total_files += 1
sys.stdout.write("\r>> Files : %2d, Images: %2d, Train images: %2d, Test images: %2d"
% (total_files, count_images, count_images_train, count_images_test))
sys.stdout.flush()
print("\n Total files: %2d, Images: %2d, Train images: %2d, Test images: %2d"
% (total_files, count_images, count_images_train, count_images_test))