forked from RonaldsonBellande/ML_object_classification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
executable file
·63 lines (45 loc) · 1.94 KB
/
convert.py
File metadata and controls
executable file
·63 lines (45 loc) · 1.94 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
from PIL import Image
import os
import cv2
def convertImages2PNG(directory):
imagesDir = os.path.join(directory, 'JPEGImages')
pngDir = os.path.join(directory, 'PNGImages')
if not os.path.exists(pngDir):
os.makedirs(pngDir)
for directory in os.listdir(os.path.join(imagesDir)):
currentDir = os.path.join(imagesDir, directory)
for filename in os.listdir(os.path.join(imagesDir, directory)):
jpgFileName = os.path.join(currentDir, filename)
filenameNoType = os.path.splitext(filename)[0]
print(jpgFileName)
image = Image.open(jpgFileName)
classDir = os.path.join(pngDir, directory)
if not os.path.exists(classDir):
os.makedirs(classDir)
pngFilePath = os.path.join(classDir, (filenameNoType + ".png"))
print(pngFilePath)
image.save(pngFilePath)
def resizeImages(directory):
pngDir = os.path.join(directory, 'PNGImages')
resizeDir = os.path.join(directory, 'ResizedPNGImagesSmall')
if not os.path.exists(resizeDir):
os.makedirs(resizeDir)
for directory in os.listdir(os.path.join(pngDir)):
currentDir = os.path.join(pngDir, directory)
for filename in os.listdir(currentDir):
pngFileName = os.path.join(currentDir, filename)
print(pngFileName)
image = cv2.imread(pngFileName)
res = cv2.resize(image, dsize=(128, 128), interpolation=cv2.INTER_CUBIC)
classDir = os.path.join(resizeDir, directory)
if not os.path.exists(classDir):
os.makedirs(classDir)
pngFilePath = os.path.join(classDir, filename)
print(pngFilePath)
cv2.imwrite(pngFilePath, res)
directory1 = r'PascalVOC2012/voc2012/VOC2012'
directory2 = r'PascalVOC2012/VOC2012'
#convertImages2PNG(directory1)
#convertImages2PNG(directory2)
resizeImages(directory1)
resizeImages(directory2)