我在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
打印时的数据框如下所示:
image x y w h brand
0 001.jpg 100 200 100 200 test1
1 001.jpg 100 200 100 200 test1
2 001.jpg 100 200 100 200 test2
description
image: image name
x,y,w,h : crop coordinates
brand : destination folder
第一列中列出的图像存储在特定目录中。我将其称为源目录。
src_path = '/var/www/html/projects/images/'
我必须遍历数据框,从第一列获取每个图像,根据坐标裁剪子集,并将裁剪后的图像保存在使用“文件夹”列创建的文件夹中。
以下是目前的代码:
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下的实际目录。
答案 0 :(得分:2)
在尝试移动图像之前,您必须保存它。
cv2.imwrite(os.path.join(dest_path, rows['brand']),brand)
答案 1 :(得分:1)
我解决了这个问题。我没有使用shutil.move
,而是使用了cv2.imwrite。我的代码的最后几行现在看起来像这样:
counter = 0
for index, rows in df.iterrows():
filename = basename(rows['imagename'])
image = cv2.imread(src_path+rows['imagename'])
brand = image[rows['y']:rows['y']+rows['h'], rows['x']:rows['x']+rows['w']]
counter=counter+1
fold = rows['brand']+"/"
dest_fold = dest_path+fold
cv2.imwrite(dest_fold+"/"+filename+ "_" +str(counter)+".jpg", brand)