|
|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
import itertools
|
|
|
|
|
from .model_loader import ModelLoader
|
|
|
|
|
from cvat.apps.engine.utils import import_modules, execute_python_code
|
|
|
|
|
import itertools
|
|
|
|
|
|
|
|
|
|
def _process_detections(detections, path_to_conv_script, restricted=True):
|
|
|
|
|
results = Results()
|
|
|
|
|
@ -31,6 +31,17 @@ def _process_detections(detections, path_to_conv_script, restricted=True):
|
|
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
def _process_attributes(shape_attributes, label_attr_spec):
|
|
|
|
|
attributes = []
|
|
|
|
|
for attr_text, attr_value in shape_attributes.items():
|
|
|
|
|
if attr_text in label_attr_spec:
|
|
|
|
|
attributes.append({
|
|
|
|
|
"spec_id": label_attr_spec[attr_text],
|
|
|
|
|
"value": attr_value,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return attributes
|
|
|
|
|
|
|
|
|
|
class Results():
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self._results = {
|
|
|
|
|
@ -84,25 +95,62 @@ class Results():
|
|
|
|
|
"attributes": attributes or {},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def run_inference_engine_annotation(data, model_file, weights_file,
|
|
|
|
|
labels_mapping, attribute_spec, convertation_file, job=None, update_progress=None, restricted=True):
|
|
|
|
|
def process_attributes(shape_attributes, label_attr_spec):
|
|
|
|
|
attributes = []
|
|
|
|
|
for attr_text, attr_value in shape_attributes.items():
|
|
|
|
|
if attr_text in label_attr_spec:
|
|
|
|
|
attributes.append({
|
|
|
|
|
"spec_id": label_attr_spec[attr_text],
|
|
|
|
|
"value": attr_value,
|
|
|
|
|
})
|
|
|
|
|
class InferenceAnnotationRunner:
|
|
|
|
|
def __init__(self, data, model_file, weights_file, labels_mapping,
|
|
|
|
|
attribute_spec, convertation_file):
|
|
|
|
|
self.data = iter(data)
|
|
|
|
|
self.data_len = len(data)
|
|
|
|
|
self.model = ModelLoader(model=model_file, weights=weights_file)
|
|
|
|
|
self.frame_counter = 0
|
|
|
|
|
self.attribute_spec = attribute_spec
|
|
|
|
|
self.convertation_file = convertation_file
|
|
|
|
|
self.iteration_size = 128
|
|
|
|
|
self.labels_mapping = labels_mapping
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(self, job=None, update_progress=None, restricted=True):
|
|
|
|
|
result = {
|
|
|
|
|
"shapes": [],
|
|
|
|
|
"tracks": [],
|
|
|
|
|
"tags": [],
|
|
|
|
|
"version": 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
detections = []
|
|
|
|
|
for _ in range(self.iteration_size):
|
|
|
|
|
try:
|
|
|
|
|
frame = next(self.data)
|
|
|
|
|
except StopIteration:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
orig_rows, orig_cols = frame.shape[:2]
|
|
|
|
|
|
|
|
|
|
detections.append({
|
|
|
|
|
"frame_id": self.frame_counter,
|
|
|
|
|
"frame_height": orig_rows,
|
|
|
|
|
"frame_width": orig_cols,
|
|
|
|
|
"detections": self.model.infer(frame),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
self.frame_counter += 1
|
|
|
|
|
if job and update_progress and not update_progress(job, self.frame_counter * 100 / self.data_len):
|
|
|
|
|
return None, False
|
|
|
|
|
|
|
|
|
|
processed_detections = _process_detections(detections, self.convertation_file, restricted=restricted)
|
|
|
|
|
|
|
|
|
|
return attributes
|
|
|
|
|
self._add_shapes(processed_detections.get_shapes(), result["shapes"])
|
|
|
|
|
|
|
|
|
|
def add_shapes(shapes, target_container):
|
|
|
|
|
more_items = self.frame_counter != self.data_len
|
|
|
|
|
|
|
|
|
|
return result, more_items
|
|
|
|
|
|
|
|
|
|
def _add_shapes(self, shapes, target_container):
|
|
|
|
|
for shape in shapes:
|
|
|
|
|
if shape["label"] not in labels_mapping:
|
|
|
|
|
if shape["label"] not in self.labels_mapping:
|
|
|
|
|
continue
|
|
|
|
|
db_label = labels_mapping[shape["label"]]
|
|
|
|
|
label_attr_spec = attribute_spec.get(db_label)
|
|
|
|
|
|
|
|
|
|
db_label = self.labels_mapping[shape["label"]]
|
|
|
|
|
label_attr_spec = self.attribute_spec.get(db_label)
|
|
|
|
|
target_container.append({
|
|
|
|
|
"label_id": db_label,
|
|
|
|
|
"frame": shape["frame"],
|
|
|
|
|
@ -111,38 +159,5 @@ def run_inference_engine_annotation(data, model_file, weights_file,
|
|
|
|
|
"z_order": 0,
|
|
|
|
|
"group": None,
|
|
|
|
|
"occluded": False,
|
|
|
|
|
"attributes": process_attributes(shape["attributes"], label_attr_spec),
|
|
|
|
|
"attributes": _process_attributes(shape["attributes"], label_attr_spec),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
result = {
|
|
|
|
|
"shapes": [],
|
|
|
|
|
"tracks": [],
|
|
|
|
|
"tags": [],
|
|
|
|
|
"version": 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data_len = len(data)
|
|
|
|
|
model = ModelLoader(model=model_file, weights=weights_file)
|
|
|
|
|
|
|
|
|
|
frame_counter = 0
|
|
|
|
|
|
|
|
|
|
detections = []
|
|
|
|
|
for frame in data:
|
|
|
|
|
orig_rows, orig_cols = frame.shape[:2]
|
|
|
|
|
|
|
|
|
|
detections.append({
|
|
|
|
|
"frame_id": frame_counter,
|
|
|
|
|
"frame_height": orig_rows,
|
|
|
|
|
"frame_width": orig_cols,
|
|
|
|
|
"detections": model.infer(frame),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
frame_counter += 1
|
|
|
|
|
if job and update_progress and not update_progress(job, frame_counter * 100 / data_len):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
processed_detections = _process_detections(detections, convertation_file, restricted=restricted)
|
|
|
|
|
|
|
|
|
|
add_shapes(processed_detections.get_shapes(), result["shapes"])
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|