update,
This commit is contained in:
114
vinniesniper-54816/task1/_tools/crawer/import_img.py
Normal file
114
vinniesniper-54816/task1/_tools/crawer/import_img.py
Normal file
@@ -0,0 +1,114 @@
|
||||
import glob
|
||||
import os
|
||||
import shutil
|
||||
import cv2
|
||||
|
||||
import random
|
||||
import string
|
||||
|
||||
beach_src_path = "p/beach"
|
||||
beach_target_path = "/home/logic/_wsl_workspace/comission-playlist-2024/vinniesniper-54816/src/data/1xx_Beach"
|
||||
building_src_path = "p/building"
|
||||
building_target_path = "/home/logic/_wsl_workspace/comission-playlist-2024/vinniesniper-54816/src/data/2xx_Building"
|
||||
|
||||
bus_src_path = "p/bus"
|
||||
bus_target_path = "/home/logic/_wsl_workspace/comission-playlist-2024/vinniesniper-54816/src/data/3xx_Bus"
|
||||
|
||||
dinosaur_src_path = "p/dinosaur"
|
||||
dinosaur_target_path = "/home/logic/_wsl_workspace/comission-playlist-2024/vinniesniper-54816/src/data/4xx_Dinosaur"
|
||||
|
||||
elephant_src_path = "p/elephant"
|
||||
elephant_target_path = "/home/logic/_wsl_workspace/comission-playlist-2024/vinniesniper-54816/src/data/5xx_Elephant"
|
||||
|
||||
flower_src_path = "p/flower"
|
||||
flower_target_path = "/home/logic/_wsl_workspace/comission-playlist-2024/vinniesniper-54816/src/data/6xx_Flower"
|
||||
|
||||
horse_src_path = "p/horse"
|
||||
horse_target_path = "/home/logic/_wsl_workspace/comission-playlist-2024/vinniesniper-54816/src/data/7xx_Horse"
|
||||
|
||||
mountain_src_path = "p/mountain"
|
||||
mountain_target_path = "/home/logic/_wsl_workspace/comission-playlist-2024/vinniesniper-54816/src/data/8xx_Mountain"
|
||||
|
||||
dish_src_path = "p/dish"
|
||||
dish_target_path = "/home/logic/_wsl_workspace/comission-playlist-2024/vinniesniper-54816/src/data/9xx_Dish"
|
||||
|
||||
def merge_images(input_folder, output_folder):
|
||||
if not os.path.exists(output_folder):
|
||||
os.mkdir(output_folder)
|
||||
|
||||
count = 0
|
||||
for root, _, filenames in os.walk(input_folder):
|
||||
for fn in list(sorted(filenames)):
|
||||
if (count < 10000):
|
||||
if os.path.getsize(os.path.join(root, fn)) == 0:
|
||||
os.remove(os.path.join(root, fn))
|
||||
continue
|
||||
|
||||
img = cv2.imread(os.path.join(root, fn))
|
||||
if img is None:
|
||||
os.remove(os.path.join(root, fn))
|
||||
continue
|
||||
|
||||
if fn.endswith('.jpg'):
|
||||
count += 1
|
||||
shutil.copy(os.path.join(root, fn), os.path.join(output_folder, 'c_{:010d}.jpg'.format(count)))
|
||||
|
||||
def copy_img(src_path, target_path):
|
||||
random_str = ''.join(random.choices(string.ascii_letters + string.digits, k=5))
|
||||
tmp_dir = '_tmp/_tmp_' + random_str
|
||||
os.mkdir(tmp_dir)
|
||||
|
||||
for f in glob.glob(os.path.join(target_path, 'c_*.jpg')):
|
||||
os.remove(f)
|
||||
|
||||
merge_images(src_path, tmp_dir)
|
||||
|
||||
for f in sorted(glob.glob(tmp_dir+'/*.jpg')):
|
||||
shutil.copy(f, target_path)
|
||||
|
||||
shutil.rmtree(tmp_dir)
|
||||
print(' img add done' )
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
def add_img(src_path, target_path):
|
||||
print('add to ' + target_path, end="")
|
||||
copy_img(src_path, target_path)
|
||||
|
||||
with ThreadPoolExecutor() as executor:
|
||||
executor.submit(add_img, beach_src_path, beach_target_path)
|
||||
executor.submit(add_img, building_src_path, building_target_path)
|
||||
executor.submit(add_img, bus_src_path, bus_target_path)
|
||||
executor.submit(add_img, dinosaur_src_path, dinosaur_target_path)
|
||||
executor.submit(add_img, elephant_src_path, elephant_target_path)
|
||||
executor.submit(add_img, flower_src_path, flower_target_path)
|
||||
executor.submit(add_img, horse_src_path, horse_target_path)
|
||||
executor.submit(add_img, mountain_src_path, mountain_target_path)
|
||||
executor.submit(add_img, dish_src_path, dish_target_path)
|
||||
|
||||
|
||||
# print('add to bus', end="")
|
||||
# copy_img(bus_src_path, bus_target_path)
|
||||
|
||||
# print('add to dinosaur', end="")
|
||||
# copy_img(dinosaur_src_path, dinosaur_target_path)
|
||||
|
||||
# print('add to elephant', end="")
|
||||
# copy_img(elephant_src_path, elephant_target_path)
|
||||
|
||||
# print('add to flower', end="")
|
||||
# copy_img(flower_src_path, flower_target_path)
|
||||
|
||||
# print('add to horse', end="")
|
||||
# copy_img(horse_src_path, horse_target_path)
|
||||
|
||||
# print('add to mountain', end="")
|
||||
# copy_img(mountain_src_path, mountain_target_path)
|
||||
|
||||
# print('add to dish', end="")
|
||||
# copy_img(dish_src_path, dish_target_path)
|
||||
|
||||
print('done')
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user