This repository was archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconvert_tfcsv_to_pycocodetections.py
More file actions
172 lines (145 loc) · 5.1 KB
/
Copy pathconvert_tfcsv_to_pycocodetections.py
File metadata and controls
172 lines (145 loc) · 5.1 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Convert TF2 CSV format to PyCoco detections for evaluation with PyCocoTools
License_info:
# ==============================================================================
# ISC License (ISC)
# Copyright 2021 Christian Doppler Laboratory for Embedded Machine Learning
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
# The following is a slightly modified version from the following script
# Source:
"""
# Futures
#from __future__ import print_function
# Built-in/Generic Imports
import json
import os
import warnings
# Libs
import lxml
from tqdm import tqdm
from xmltodict import unparse
import argparse
import xml.etree.ElementTree as ET
from xml.dom import minidom
import bs4
from PIL import Image
import pandas as pd
import numpy as np
# Own modules
__author__ = 'Alexander Wendt'
__copyright__ = 'Copyright 2021, Christian Doppler Laboratory for ' \
'Embedded Machine Learning'
__credits__ = ['']
__license__ = 'ISC'
__version__ = '0.5.0'
__maintainer__ = 'Alexander Wendt'
__email__ = 'alexander.wendt@tuwien.ac.at'
__status__ = 'Experiental'
parser = argparse.ArgumentParser(description='Convert Coco to VOC')
parser.add_argument("-af", '--annotation_file',
default="samples/annotations/tf2csv_format_detections.csv",
help='Annotation file.', required=False)
parser.add_argument("-o", '--output_file',
default="samples/annotations/coco_detections.json",
help='Annotation file.', required=False)
args = parser.parse_args()
# Script to convert yolo annotations to voc format from
#
# Sample format
# [
# {
# "image_id": 42,
# "category_id": 18,
# "bbox": [
# 258.15,
# 41.29,
# 348.26,
# 243.78
# ],
# "score": 0.236
# },
# {
# "image_id": 73,
# "category_id": 11,
# "bbox": [
# 61,
# 22.75,
# 504,
# 609.67
# ],
# "score": 0.318
# }
# ]
def single_detection_to_coco_detection_dict(image_id: str, category_id: int, bbox: list, score: float) -> dict:
'''
Create a dictionary from inputs for a coco detection
'''
detection = {}
detection['image_id'] = image_id
detection['category_id'] = category_id
detection['bbox'] = bbox
detection['score'] = score
return detection
def generate_detections(annotation_file, output_file):
'''
'''
ann_df = pd.read_csv(annotation_file, sep=';')
ann_df.set_index('filename', inplace=True)
detected_objects = []
for index, row in ann_df.iterrows():
print("Process ", row.name)
image_id = os.path.splitext(row.name)[-2]
width = row['width']
height = row['height']
category_id = int(row['class'])
# Round first at 2 decimals
xmin = float(row['xmin'] * width) - 1
#xmin = int(np.round(row['xmin'] * width)) - 1
if xmin<0:
warnings.warn("xmin < 0. Setting xmin=0")
xmin=0
ymin = float(row['ymin'] * height) - 1
#ymin = int(np.round(row['ymin'] * height)) - 1
if ymin<0:
warnings.warn("ymin < 0. Setting ymin=0")
ymin=0
xmax = float(row['xmax'] * width)
#xmax = int(np.round(row['xmax'] * width))
if xmax>width:
warnings.warn("xmax {} > {}. Setting xmax={}".format(xmax, width, width))
xmax=width
ymax = float(row['ymax'] * height)
#ymax = int(np.round(row['ymax'] * height))
if ymax>height:
warnings.warn("ymax {} > {}. Setting ymax={}".format(ymax, height, height))
ymax=height
assert xmax > xmin and ymax > ymin, f"Box size error !: (xmin, ymin, xmax, ymax): {xmin, ymin, xmax, ymax}"
o_width = xmax - xmin
o_height = ymax - ymin
bbox = [xmin, ymin, o_width, o_height]
score = float(row['score'])
object_dict = single_detection_to_coco_detection_dict(image_id, category_id, bbox, score)
detected_objects.append(object_dict)
# Save as json
if not os.path.isdir(os.path.dirname(output_file)):
os.makedirs(os.path.dirname(output_file))
print("Created results dir ", os.path.dirname(output_file))
with open(output_file, 'w') as outfile:
json.dump(detected_objects, outfile, indent=4)
print("Saved coco detections to ", output_file)
if __name__ == "__main__":
generate_detections(args.annotation_file, args.output_file)
print("=== Program end ===")