-
Notifications
You must be signed in to change notification settings - Fork 4
/
cropBoundingBox.py
60 lines (41 loc) · 1.84 KB
/
cropBoundingBox.py
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
# coding: UTF-8
import cv2
import os
from xml.etree import ElementTree
for gov in ['Adachi', 'Muroran', 'Chiba', 'Ichihara', 'Nagakute', 'Sumida', 'Numazu']:
file_list = os.listdir('/home/ubuntu/py3envtf/IEEE_paper/RoadDamageDataset/' + gov + '/Annotations')
for file in file_list:
im_name = file.split('.')[0] + '.jpg'
full_impath = 'RoadDamageDataset/' + gov + '/JPEGImages/' + im_name
infile_xml = open('RoadDamageDataset/' + gov + '/Annotations/' + file, encoding='utf-8')
tree = ElementTree.parse(infile_xml)
root = tree.getroot()
cnt = 0
for obj in root.iter('object'):
cls_name = obj.find('name').text
xmlbox = obj.find('bndbox')
xmin = int(xmlbox.find('xmin').text)
xmax = int(xmlbox.find('xmax').text)
ymin = int(xmlbox.find('ymin').text)
ymax = int(xmlbox.find('ymax').text)
if xmin>xmax:
xmin = int(xmlbox.find('xmax').text)
xmax = int(xmlbox.find('xmin').text)
if ymin>ymax:
ymin = int(xmlbox.find('ymax').text)
ymax = int(xmlbox.find('ymin').text)
# open image
img = cv2.imread(full_impath)
# crop bounding box
roi = img[ymin:ymax, xmin:xmax]
if roi.sum() != 0:
# resize image
h = 128
w = 128
roiResized = cv2.resize(roi, (h, w))
im_name = im_name.split('.')[0] + '_' + str(cnt) + '.png'
# im_name = im_name.split('.')[0] + '.png'
# save resized image
cv2.imwrite('data/' + cls_name + '/' + im_name, roiResized)
cnt = cnt + 1
im_name = file.split('.')[0] + '.jpg'