Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New bbm #1131

Open
wants to merge 21 commits into
base: reports-caching
Choose a base branch
from
Open

New bbm #1131

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 24 additions & 58 deletions backend/projects/annotation_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@
"type": "labels",
},
},
"OCRTextlineSegmentation": {
"ocr_transcribed_json": {
"to_name": "image_url",
"from_name": [
"annotation_bboxes",
],
"type": ["rectangle"],
},
},
"OCRTranscription": {
"ocr_transcribed_json": {
"to_name": "image_url",
Expand Down Expand Up @@ -190,59 +199,6 @@
}


def convert_prediction_json_to_annotation_result(
prediction_json, speakers_json, audio_duration, index, is_acoustic=False
):
"""
Convert prediction_json and transcribed_json to annotation_result
"""

result = []
if prediction_json == None:
return result

for idx, val in enumerate(prediction_json):
label_dict = {
"origin": "manual",
"to_name": "audio_url",
"from_name": "labels",
"original_length": audio_duration,
}
text_dict = {
"origin": "manual",
"to_name": "audio_url",
"from_name": "transcribed_json",
"original_length": audio_duration,
}
if is_acoustic:
text_dict["from_name"] = "verbatim_transcribed_json"
id = f"shoonya_{index}s{idx}s{generate_random_string(13-len(str(idx)))}"
label_dict["id"] = id
text_dict["id"] = id
label_dict["type"] = "labels"
text_dict["type"] = "textarea"

value_labels = {
"start": val["start"],
"end": val["end"],
"labels": [
next(
speaker
for speaker in speakers_json
if speaker["speaker_id"] == val["speaker_id"]
)["name"]
],
}
value_text = {"start": val["start"], "end": val["end"], "text": [val["text"]]}

label_dict["value"] = value_labels
text_dict["value"] = value_text
result.append(label_dict)
result.append(text_dict)

return result


def convert_conversation_json_to_annotation_result(conversation_json, idx):
result = []
for i in range(len(conversation_json)):
Expand All @@ -262,12 +218,15 @@ def convert_conversation_json_to_annotation_result(conversation_json, idx):


def draft_data_json_to_annotation_result(draft_data_json, project_type, pk=None):
from projects.views import convert_prediction_json_to_annotation_result

registry_helper = ProjectRegistry.get_instance()
input_dataset_info = registry_helper.get_input_dataset_and_fields(project_type)
dataset_model = getattr(dataset_models, input_dataset_info["dataset_type"])
try:
dataset_item = dataset_model.objects.get(pk=pk)
except:
dataset_item = None
pass
result = []
idx = 0
Expand All @@ -286,13 +245,20 @@ def draft_data_json_to_annotation_result(draft_data_json, project_type, pk=None)
if field == "conversation_json":
ans = convert_conversation_json_to_annotation_result(value, idx)
elif field == "transcribed_json" or field == "prediction_json":
ans = convert_prediction_json_to_annotation_result(
assert type(value) in [
list,
dict,
], f"Something wrong is there in the type of {value}"
if isinstance(value, list):
value = {"verbatim_transcribed_json": value}
sub_ans = convert_prediction_json_to_annotation_result(
None,
project_type,
dataset_item,
value,
dataset_item.speakers_json,
dataset_item.audio_duration,
idx,
project_type == "AcousticNormalisedTranscriptionEditing",
True,
)
ans.extend(sub_ans)
else:
if field_type == "textarea":
field_dict["value"] = {"text": [value]}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<View>
<Style>.ant-input { font-size: large; }</Style>
<Image name="image_url" value="$image_url"/>
<Rectangle name="annotation_bboxes" toName="image_url" strokeWidth="3" className="ignore_assertion"/>
</View>
18 changes: 18 additions & 0 deletions backend/projects/project_registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ OCR:
fields:
annotations:
- ocr_transcribed_json
OCRTextlineSegmentation:
project_mode: "Annotation"
label_studio_jsx_file: "ocr/ocr_textline_segmentation.jsx"
input_dataset:
class: OCRDocument
fields:
- image_url
- page_number
display_fields:
- image_url
- page_number
prediction: ocr_prediction_json
output_dataset:
class: OCRDocument
save_type: in_place
fields:
annotations:
- ocr_transcribed_json
OCRTranscriptionEditing:
project_mode: "Annotation"
label_studio_jsx_file: "ocr/ocr_transcription.jsx"
Expand Down
25 changes: 24 additions & 1 deletion backend/projects/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import yaml
from yaml.loader import SafeLoader
from jiwer import wer
from dataset import models as dataset_models
from users.utils import generate_random_string
from utils.convert_result_to_chitralekha_format import (
create_memory,
Expand Down Expand Up @@ -367,6 +368,7 @@ def process_ocr_tasks(
task,
is_OCRSegmentCategorization,
is_OCRSegmentCategorizationEditing,
is_OCRTextlineSegmentation,
is_OCRSegmentCategorisationRelationMappingEditing,
):
annotation_result = process_annotation_result(task)
Expand All @@ -375,6 +377,7 @@ def process_ocr_tasks(
annotation_result,
is_OCRSegmentCategorization,
is_OCRSegmentCategorizationEditing,
is_OCRTextlineSegmentation,
is_OCRSegmentCategorisationRelationMappingEditing,
)

Expand Down Expand Up @@ -474,6 +477,7 @@ def process_ocr_results(
annotation_result,
is_OCRSegmentCategorization,
is_OCRSegmentCategorizationEditing,
is_OCRTextlineSegmentation,
is_OCRSegmentCategorisationRelationMappingEditing,
):
from projects.views import convert_annotation_result_to_formatted_json
Expand All @@ -482,12 +486,15 @@ def process_ocr_results(
annotation_result,
None,
False,
is_OCRSegmentCategorization or is_OCRSegmentCategorizationEditing,
is_OCRSegmentCategorization
or is_OCRSegmentCategorizationEditing
or is_OCRTextlineSegmentation,
False,
)
if (
is_OCRSegmentCategorization
or is_OCRSegmentCategorizationEditing
or is_OCRTextlineSegmentation
or is_OCRSegmentCategorisationRelationMappingEditing
):
bboxes_relation_json = []
Expand All @@ -513,6 +520,7 @@ def process_task(
include_input_data_metadata_json,
dataset_model,
is_audio_project_type,
fetch_parent_data_field,
):
task_dict = model_to_dict(task)
if export_type != "JSON":
Expand Down Expand Up @@ -547,6 +555,21 @@ def process_task(
task_dict["data"]["input_data_metadata_json"] = dataset_model.objects.get(
pk=task_dict["input_data"]
).metadata_json
try:
if fetch_parent_data_field and dataset_model:
parent_data_item = dataset_model.objects.get(
pk=task_dict["input_data"]
).parent_data
if parent_data_item:
dataset_model = getattr(
dataset_models, parent_data_item.instance_id.dataset_type
)
parent_dataset_model = dataset_model.objects.get(pk=parent_data_item.id)
task_dict["data"]["fetch_parent_data_field"] = getattr(
parent_dataset_model, fetch_parent_data_field, None
)
except Exception as e:
pass

del task_dict["annotation_users"]
del task_dict["review_user"]
Expand Down
Loading
Loading