我在Python中有一个数据帧。在import pandas as pd
inp = [{'image':'001.jpg', 'x':100, 'y':200,'w':100, 'h':200, 'brand':'test1'}, {'image':'001.jpg', 'x':100, 'y':200,'w':100, 'h':200, 'brand': 'test1'}, {'image':'001.jpg', 'x':100, 'y':200, 'w':100, 'h':200, 'brand': 'test2'}]
df = pd.DataFrame(inp)
print df
打印时的数据框如下所示:
^{pr2}$
第一列中列出的图像存储在特定目录中。我将其称为源目录。在src_path = '/var/www/html/projects/images/'
我必须遍历数据帧,从第一列中获取每个图像,根据坐标裁剪子集,并将裁剪后的图像保存在使用“folder”列创建的文件夹中。在
以下是目前为止的代码:import pandas as pd
import os
import shutil
import cv2
#read the data frame
df = pd.read_csv('annotations.csv')
src_path = '/var/www/html/projects/images/'
# create a master folder to store all cropped images in separate sub-directories created basis the name in brand column
if not os.path.exists(os.path.join(src_path,'imageProcessDir')):
os.mkdir(os.path.join(src_path,'imageProcessDir'))
dest_path = src_path+'imageProcessDir'
#create sub-directories for each brand
ub = df.brand.unique()
for u in ub:
os.mkdir(os.path.join(dest_path,u))
for index, rows in df.iterrows():
#read each image
image = cv2.imread(src_path+rows['image'])
#crop image
brand = image[rows['y']:rows['y']+rows['h'], rows['x']:rows['x']+rows['w']]
#save the cropped image in specific directories as listed in folder column
if not os.path.exists(os.path.join(dest_path, rows['brand'])):
shutil.move(brand, os.mkdir(os.path.join(dest_path, rows['brand'])));
我被困在最后一步,我需要在品牌子目录中保存裁剪后的图像。以下是所需的最终目录结构:dest_path
|
imageProcessDir
|__test1
|_001_1.jpg #brand = image[rows....will go here
|_001_2.jpg
|__test2
|_001_1.jpg
我可以重新命名最终输出。在
但是,我无法匹配df列下的目录名和imageProcessDir下的实际目录名。在