-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageLabelCropXml.py
More file actions
71 lines (46 loc) · 1.9 KB
/
imageLabelCropXml.py
File metadata and controls
71 lines (46 loc) · 1.9 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
65
66
67
68
69
70
71
# Improting Image class from PIL module
from PIL import Image
import pandas as pd
# Opens a image in RGB mode
import os
import numpy as np
import pandas_read_xml as pdx
def ayristir(path):
dirList=os.listdir(path)
#np.array(dirList)
fnames = []
dnames = []
for fname in dirList:
if os.path.isdir(os.path.join(path,fname)):
dnames.append(fname)
if os.path.isfile(os.path.join(path,fname)):
fnames.append(fname)
return dnames,fnames
images_path = "C:\\Users\\hsnyt\\Desktop\\trafic\\azami hız 30"
labels_path = "C:\\Users\\hsnyt\\Desktop\\trafic\\txtdosya"
#kırpılmış dosyaların olacağı klasör
new_image_path = 'C:\\Users\\hsnyt\\Desktop\\hazır\\'
(klasorler,dosyalar) = ayristir(labels_path)
# github repodaki 0,1 formatında hazırlandı
label_names = ["Sol","Sag","IleriSol","IleriSag","SolaDonulmez","SagaDonulmez","Girilmez","TrafikKapalı","Dur","ParkYapılmaz","ParkYeri","HizSiniri30","HizSiniri40","HizSiniri20Bitti","Durak"]
for i in dosyalar:
df=pdx.read_xml(os.path.join(labels_path,i))
image_name = df.loc['object'].loc['filename']
image_number = 0
xmin=int(df.loc['object'].loc['annotation'][0]['bndbox']['xmin'])
ymin=int(df.loc['object'].loc['annotation'][0]['bndbox']['ymin'])
xmax=int(df.loc['object'].loc['annotation'][0]['bndbox']['xmax'])
ymax=int(df.loc['object'].loc['annotation'][0]['bndbox']['ymax'])
crop_box = (xmin,ymin,xmax,ymax)
print(crop_box)
im = im.crop(crop_box)
newsize = (250,250)
im_crop = im.resize(newsize)
im_crop.show()
try:
os.mkdir(new_image_path+df.loc['object'].loc['annotation']['name'])
print("folder created")
except FileExistsError:
print("file already exists.")
im_crop.save(new_image_path+df.loc['object'].loc['annotation']['name']+'/'+image_name+'-'+image_number, quality=95)
image_number += 1