-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_img.py
62 lines (48 loc) · 1.54 KB
/
find_img.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
# -*- coding:utf-8 -*-
import xml.etree.ElementTree as ET
import os
import shutil
import sys
NO = int( sys.argv[1])
clses = ['bg',]
nos = [0,]
path = "Annotations"
Opth = "selected"
if not os.path.exists(Opth):
os.mkdir(Opth)
files= os.listdir(path)
for file in files:
if not os.path.isdir(file):
tree = ET.parse(path+"/"+file)
root = tree.getroot()
objects = root.findall('.//object')
if len(objects) == 1:
object = objects[0]
name = object.find('name')
cls = name.text
if cls not in clses:
clses.append(cls)
nos.append(0)
if nos[clses.index(cls)]<(NO):
shutil.copyfile("JPEGImages/"+file[:-4]+'.jpg', Opth+"/"+cls+'-'+file[:-4]+'.jpg')
nos[clses.index(cls)] = nos[clses.index(cls)] + 1
# if NO > 0:
# continue
# NO = NO - 1
else:
continue
for file in files:
if not os.path.isdir(file):
tree = ET.parse(path+"/"+file)
root = tree.getroot()
objects = root.findall('.//object')
for object in objects:
name = object.find('name')
cls = name.text
if cls not in clses:
clses.append(cls)
nos.append(0)
if nos[clses.index(cls)]<(NO):
shutil.copyfile("JPEGImages/"+file[:-4]+'.jpg', Opth+"/"+cls+'-'+file[:-4]+'.jpg')
nos[clses.index(cls)] = nos[clses.index(cls)] + 1
print(clses)