forked from guchengxi1994/mask2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize.py
87 lines (72 loc) · 2.68 KB
/
resize.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'''
lanhuage: python
Descripttion:
version: beta
Author: xiaoshuyui
Date: 2020-10-26 08:31:13
LastEditors: xiaoshuyui
LastEditTime: 2020-11-20 14:12:40
'''
import os
import xml.etree.ElementTree as ET
import numpy as np
import skimage
from convertmask.utils.methods.logger import logger
from convertmask.utils.xml2yolo.xml2yolo import convert as x2yVert
from convertmask.utils.yolo2xml.yolo2xml import convert as y2xVert
def resize_img(img: np.ndarray, heightFactor=1, widthFactor=1):
if isinstance(img,str):
img = skimage.io.imread(img)
imgShape = img.shape
resizedImg = skimage.transform.resize(
img, (int(heightFactor * imgShape[0]), int(widthFactor * imgShape[1])))
return np.array(resizedImg * 255).astype(np.uint8)
# def resize_xml(xmlpath:str, heightFactor=1, widthFactor=1):
# pass
def resizeScript(img, xmlpath: str, heightFactor=1, widthFactor=1,flag=True):
if isinstance(img, str) and os.path.exists(img):
oriImg = skimage.io.imread(img)
elif isinstance(img, np.ndarray):
oriImg = img
else:
logger.error('Input error!')
return
in_file = open(xmlpath)
tree = ET.parse(in_file)
root = tree.getroot()
parentPath, xmlFilename = os.path.split(xmlpath)
xf, _ = os.path.splitext(xmlFilename)
savePath = parentPath + os.sep + xf + '_reshape.xml'
root.find('filename').text = xf + '_reshape.jpg'
root.find('path').text = parentPath + os.sep + xf + '_reshape.jpg'
resizeImg = resize_img(oriImg, heightFactor, widthFactor)
resizeImgShape = resizeImg.shape
width = int(resizeImgShape[1])
height = int(resizeImgShape[0])
size = root.find('size')
size.find('width').text = str(width)
size.find('height').text = str(height)
for obj in root.iter('object'):
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text),
float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
# print('===========================')
# print(b)
bb = x2yVert((oriImg.shape[1], oriImg.shape[0]), b)
x, y, w, h = bb[0], bb[1], bb[2], bb[3]
# print(x, y, w, h)
# w = w
# h = h
bbox = y2xVert((resizeImgShape[1],resizeImgShape[0]), x, y, w, h)
# print(bbox)
# print('===========================')
xmlbox.find('xmin').text = str(int(bbox[0]))
xmlbox.find('ymin').text = str(int(bbox[2]))
xmlbox.find('xmax').text = str(int(bbox[1]))
xmlbox.find('ymax').text = str(int(bbox[3]))
if flag: # save file
tree.write(savePath)
in_file.close()
return resizeImg, savePath
else:
return tree