-
Notifications
You must be signed in to change notification settings - Fork 34
/
voc_demo.py
66 lines (59 loc) · 2.65 KB
/
voc_demo.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
# -*-coding: utf-8 -*-
"""
@Project: PythonAPI
@File : vocDemo.py
@Author : panjq
@E-mail : pan_jinquan@163.com
@Date : 2019-05-09 19:10:16
"""
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
from utils import file_processing, image_processing
from voctools import pascal_voc
# for SSD label,the first label is BACKGROUND:
# classes = ["BACKGROUND", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow",
# "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
# for YOLO label,ignore the BACKGROUND
# classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog",
# "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
# for wall
# classes = ["PCwall"]
# classes=["BACKGROUND",'PCwall']
classes=["BACKGROUND",'face']
print("class_name:{}".format(classes))
def pascal_voc_test(annotations_dir, image_dir, class_names, coordinatesType="SSD", show=True):
'''
:param annotations_dir:
:param image_dir:
:param class_names:
:param coordinatesType:
:param show:
:return:
'''
annotations_list = file_processing.get_files_list(annotations_dir, postfix=["*.xml"])
print("have {} annotations files".format(len(annotations_list)))
for i, annotations_file in enumerate(annotations_list):
name_id = os.path.basename(annotations_file)[:-len(".xml")]
image_name = name_id + ".jpg"
image_path = os.path.join(image_dir, image_name)
if not os.path.exists(image_path):
print("no image_dict:{}".format(image_path))
continue
if not os.path.exists(annotations_file):
print("no annotations:{}".format(annotations_file))
continue
rects, class_name, class_id = pascal_voc.get_annotation(annotations_file, class_names, coordinatesType)
if len(rects) == 0 or len(class_name) == 0 or len(class_id) == 0:
print("no class in annotations:{}".format(annotations_file))
if show:
image = image_processing.read_image(image_path)
image_processing.show_image_rects_text("image_dict", image, rects, class_name)
if __name__ == "__main__":
# annotations_dir = './dataset/VOC/Annotations'
# image_dir = "./dataset/VOC/JPEGImages"
annotations_dir = '/media/dm/dm2/project/dataset/face/Annotations'
image_dir = "/media/dm/dm2/project/dataset/face/JPEGImages"
pascal_voc_test(annotations_dir, image_dir, classes, coordinatesType="SSD", show=True)