import cv2
import numpy as np
import os
import xml.dom.minidom
from PIL import Image
import random
def write_xml(info,width,height,xml_dir='new/'):
info1=info[0][:-4]+'.jpg'
#write in xml file
xml_file = open((xml_dir + info1[:-4]+'.xml'),'w')
xml_file.write('<annotation>\n')
xml_file.write(' <folder>VOC2007</folder>\n')
xml_file.write(' <filename>' +info1 + '</filename>\n')
xml_file.write(' <size>\n')
xml_file.write(' <width>' + str(width) + '</width>\n')
xml_file.write(' <height>' + str(height) + '</height>\n')
xml_file.write(' <depth>3</depth>\n')
xml_file.write(' </size>\n')
for img_each_label in info[1:]:
spt=img_each_label[4]
xml_file.write(' <object>\n')
xml_file.write(' <name>' + spt + '</name>\n')
xml_file.write(' <pose>Unspecified</pose>\n')
xml_file.write(' <truncated>0</truncated>\n')
xml_file.write(' <difficult>0</difficult>\n')
xml_file.write(' <bndbox>\n')
xml_file.write(' <xmin>' + str(img_each_label[0]) + '</xmin>\n')
xml_file.write(' <ymin>' + str(img_each_label[1]) + '</ymin>\n')
xml_file.write(' <xmax>' + str(img_each_label[2]) + '</xmax>\n')
xml_file.write(' <ymax>' + str(img_each_label[3]) + '</ymax>\n')
xml_file.write(' </bndbox>\n')
xml_file.write(' </object>\n')
xml_file.write('</annotation>')
def xml_read(filename):
# print(filename)
if os.path.exists(filename) is False:
return None
dom = xml.dom.minidom.parse(filename)
#得到文档元素对象
root = dom.documentElement
object_ = root.getElementsByTagName('object')
info=[]
# filename = root.getElementsByTagName('filename')[0].firstChild.data
for object_1 in object_:
name=object_1.getElementsByTagName("name")[0].firstChild.data
bndbox=object_1.getElementsByTagName("bndbox")[0]
xmin=int(bndbox.getElementsByTagName("xmin")[0].firstChild.data)
ymin=int(bndbox.getElementsByTagName("ymin")[0].firstChild.data)
xmax=int(bndbox.getElementsByTagName("xmax")[0].firstChild.data)
ymax=int(bndbox.getElementsByTagName("ymax")[0].firstChild.data)
# if name=='40\'':
# print(filename,xmin,ymin,xmax,ymax)
# if 'limitRate'==name or 'limit'==name:
info.append([xmin,ymin,xmax,ymax,name])
return info
file_dir='./'
name=[]
for files in os.scandir('.'):
if files.name[-1]=='l':
name.append(files.name)
num=227
del_num=0
# for ii in range(len(name)):
while 1:
num+=1
file=name[num]
print(file,num-del_num)
locations=xml_read(file_dir+file)
img=cv2.imread(file[:-4]+'.jpg')
i_h,i_w,_=img.shape
img=cv2.resize(img,(1080,720))
img55=img.copy()
for i in range(len(locations)):
locations[i][0]=int(locations[i][0]*1080/i_w)
locations[i][1]=int(locations[i][1]*720/i_h)
locations[i][2]=int(locations[i][2]*1080/i_w)
locations[i][3]=int(locations[i][3]*720/i_h)
for i in locations:
cv2.rectangle(img55,(i[0],i[1]),(i[2],i[3]),(255,0,0),3)
cv2.imshow('sszs',img55)
para=[[0,0],[0,220],[480,0],[480,220]]
for i in range(len(para)):
a=para[i]
img1=img[a[1]:a[1]+500,a[0]:a[0]+600].copy()
ix1=a[0]
ix2=a[0]+600
iy1=a[1]
iy2=a[1]+500
locate=[]
flag=0
for l in locations:
l=l.copy()
x1=l[0]
x2=l[2]
y1=l[1]
y2=l[3]
if ix1<=x1 and ix2>=x2 and iy1<=y1 and iy2>=y2:
locate.append(l)
elif ix1>x2 or iy1>y2 or ix2<x1 or iy2<y1:
pass
else:
flag+=1
if flag==0 and len(locate)>0:
print(i)
for k in range(len(locate)):
locate[k][0]=locate[k][0]-a[0]
locate[k][1]=locate[k][1]-a[1]
locate[k][2]=locate[k][2]-a[0]
locate[k][3]=locate[k][3]-a[1]
# print((k[0]-a[0],k[1]-a[1]),(k[2]-a[0],k[3]-a[1]))
# for io in locate:
# cv2.rectangle(img1,(io[0],io[1]),(io[2],io[3]),(255,0,0),3)
cv2.imwrite('new/'+file[:-4]+'_%d.jpg'%i,img1)
aa=[]
aa.append(file[:-4]+'_%d.jpg'%i)
aa=aa+locate
print(aa)
write_xml(aa,600,500)
原图
裁剪后的图片,只要xml标记区域超过局部边界就将子图丢弃(如图3目标超界则被丢弃)
最后记得点赞
xml局部裁剪
import cv2
import numpy as np
import os
import xml.dom.minidom
from PIL import Image
import random
def write_xml(info,width,height,xml_dir='12/'):
info1=info[0][:-4]+'.jpg'
#write in xml file
xml_file = open((xml_dir + info1[:-4]+'.xml'),'w')
xml_file.write('<annotation>\n')
xml_file.write(' <folder>VOC2007</folder>\n')
xml_file.write(' <filename>' +info1 + '</filename>\n')
xml_file.write(' <size>\n')
xml_file.write(' <width>' + str(width) + '</width>\n')
xml_file.write(' <height>' + str(height) + '</height>\n')
xml_file.write(' <depth>3</depth>\n')
xml_file.write(' </size>\n')
for img_each_label in info[1:]:
spt=img_each_label[4]
xml_file.write(' <object>\n')
xml_file.write(' <name>' + spt + '</name>\n')
xml_file.write(' <pose>Unspecified</pose>\n')
xml_file.write(' <truncated>0</truncated>\n')
xml_file.write(' <difficult>0</difficult>\n')
xml_file.write(' <bndbox>\n')
xml_file.write(' <xmin>' + str(img_each_label[0]) + '</xmin>\n')
xml_file.write(' <ymin>' + str(img_each_label[1]) + '</ymin>\n')
xml_file.write(' <xmax>' + str(img_each_label[2]) + '</xmax>\n')
xml_file.write(' <ymax>' + str(img_each_label[3]) + '</ymax>\n')
xml_file.write(' </bndbox>\n')
xml_file.write(' </object>\n')
xml_file.write('</annotation>')
def xml_read(filename):
# print(filename)
if os.path.exists(filename) is False:
return None
dom = xml.dom.minidom.parse(filename)
#得到文档元素对象
root = dom.documentElement
object_ = root.getElementsByTagName('object')
info=[]
# filename = root.getElementsByTagName('filename')[0].firstChild.data
for object_1 in object_:
name=object_1.getElementsByTagName("name")[0].firstChild.data
bndbox=object_1.getElementsByTagName("bndbox")[0]
xmin=int(bndbox.getElementsByTagName("xmin")[0].firstChild.data)
ymin=int(bndbox.getElementsByTagName("ymin")[0].firstChild.data)
xmax=int(bndbox.getElementsByTagName("xmax")[0].firstChild.data)
ymax=int(bndbox.getElementsByTagName("ymax")[0].firstChild.data)
# if name=='40\'':
# print(filename,xmin,ymin,xmax,ymax)
# if 'limitRate'==name or 'limit'==name:
info.append([xmin,ymin,xmax,ymax,name])
return info
file_dir='./'
name=[]
for files in os.scandir('.'):
if files.name[-1]=='l':
name.append(files.name)
num=0
for ii in range(len(name)):
file=name[ii-num]
# file="002446.xml"
# print(file)
print(file[:-4],' kkkk',end='')
locations=xml_read(file_dir+file)
img=cv2.imread(file[:-4]+'.jpg')
img2=img.copy()
h,w,_=img.shape
aa=0
xmax=locations[0][2]
xmin=locations[0][0]
ymax=locations[0][3]
ymin=locations[0][1]
for i in locations:
xmin=min(xmin,i[0])
xmax=max(xmax,i[2])
ymin=min(ymin,i[1])
ymax=max(ymax,i[3])
ymin=max(0,ymin-10)
ymax=min(h,ymax+10)
xmin=max(0,xmin-10)
xmax=min(w,xmax+10)
lena=random.randint(300,700)
if (xmax-xmin)<lena:
xmin=max(0,(xmax+xmin)//2-lena//2)
xmax=min(w,(xmax+xmin)//2+lena//2)
if (ymax-ymin)<lena:
ymin=max(0,(ymax+ymin)//2-lena//2)
ymax=min(h,(ymax+ymin)//2+lena//2)
print(xmin,ymin,xmax,ymax)
img2=img2[ymin:ymax,xmin:xmax,:]
h,w,_=img2.shape
if h/w>2 or w/h>2:
continue
aa=[]
aa.append(file[:-4]+'.jpg')
for i in locations:
aa.append([i[0]-xmin,i[1]-ymin,i[2]-xmin,i[3]-ymin,i[4]])
# cv2.rectangle(img2,(i[0]-xmin,i[1]-ymin),(i[2]-xmin,i[3]-ymin),(255,0,0),3)
cv2.imwrite('12/'+file[:-4]+'.jpg',img2)
write_xml(aa,w,h)
# for i in locations:
# cv2.rectangle(img2,(i[0]-xmin,i[1]-ymin),(i[2]-xmin,i[3]-ymin),(255,0,0),3)
# print('')
# img2=cv2.resize(img2,(300,300))
# cv2.imshow('sss',img)
# cv2.imshow('img2',img2)
# # cv2.waitKey(0)
# if cv2.waitKey(0) & 0xFF == ord('3'):
# num+=2
# import cv2
# import numpy as np
# import os
# file_dir='./'
# name=[]
# for root, dirs, files in os.walk(file_dir):
# for file in files:
# if file[-1]=='g':
# name.append(file)
# for i in name:
# img=cv2.imread(i)
# h,l,_=img.shape
# img1=cv2.resize(img,(300,300))
# if h/l>2.1:
# new_len=int(l*1.9)
# cut_len=(h-new_len)//2
# img=img[cut_len:cut_len+new_len,:,:]
# print(h/l)
# elif l/h>2.1:
# new_len=int(h*1.9)
# cut_len=(l-new_len)//2
# img=img[:,cut_len:cut_len+new_len,:]
# print(l/h)
# else:
# continue
# cv2.imshow('ss',img1)
# # print(img.shape[1]/img.shape[0])
# img1=cv2.resize(img,(300,300))
# cv2.imshow('ss3',img1)
# cv2.waitKey(0)
# file_dir='.'
# name=[]
# for root, dirs, files in os.walk(file_dir):
# for file in files:
# if file[-1]=='S':
# name.append(file)
# num=0
# write_flag=0
# for vedio in name:
# cap = cv2.VideoCapture(vedio)
# while 1:
# ret, frame = cap.read()
# if ret==False:
# break
# frame=cv2.resize(frame,(600,600))
# if write_flag==3:
# write_flag=0
# cv2.imwrite('dataset//%06d.jpg'%num,frame)
# print(num)
# num+=1
# write_flag+=1
# cap.release()
# cap = cv2.VideoCapture('11.TS')
# while 1:
# ret, frame = cap.read()
# if ret==False:
# break
# cv2.imshow('frame',frame)
# cv2.waitKey(1)
# cap.release()
# cv2.destroyAllWindows()