|
26 | 26 | from lib.core.repositories import BaseManageableRepository |
27 | 27 | from lib.core.service_types import UploadAnnotationAuthData |
28 | 28 | from lib.core.serviceproviders import SuerannotateServiceProvider |
| 29 | +from lib.core.types import PriorityScore |
29 | 30 | from lib.core.usecases.base import BaseReportableUseCae |
30 | 31 | from lib.core.usecases.images import GetBulkImages |
31 | 32 | from lib.core.usecases.images import ValidateAnnotationUseCase |
32 | 33 | from lib.core.video_convertor import VideoFrameGenerator |
33 | 34 | from superannotate.logger import get_default_logger |
34 | 35 | from superannotate_schemas.validators import AnnotationValidators |
35 | 36 |
|
| 37 | + |
36 | 38 | logger = get_default_logger() |
37 | 39 |
|
38 | 40 |
|
@@ -622,3 +624,72 @@ def execute(self): |
622 | 624 | else: |
623 | 625 | self._response.errors = "Couldn't get annotations." |
624 | 626 | return self._response |
| 627 | + |
| 628 | + |
| 629 | +class UploadPriorityScoresUseCase(BaseReportableUseCae): |
| 630 | + |
| 631 | + CHUNK_SIZE = 100 |
| 632 | + |
| 633 | + def __init__( |
| 634 | + self, |
| 635 | + reporter, |
| 636 | + project: ProjectEntity, |
| 637 | + folder: FolderEntity, |
| 638 | + scores: List[PriorityScore], |
| 639 | + project_folder_name: str, |
| 640 | + backend_service_provider: SuerannotateServiceProvider |
| 641 | + ): |
| 642 | + super().__init__(reporter) |
| 643 | + self._project = project |
| 644 | + self._folder = folder |
| 645 | + self._scores = scores |
| 646 | + self._client = backend_service_provider |
| 647 | + self._project_folder_name = project_folder_name |
| 648 | + |
| 649 | + @staticmethod |
| 650 | + def get_clean_priority(priority): |
| 651 | + if len(str(priority)) > 8: |
| 652 | + priority = float(str(priority)[:8]) |
| 653 | + if priority > 1000000: |
| 654 | + priority = 1000000 |
| 655 | + if priority < 0: |
| 656 | + priority = 0 |
| 657 | + if str(float(priority)).split('.')[1:2]: |
| 658 | + if len(str(float(priority)).split('.')[1]) > 5: |
| 659 | + priority = float(str(float(priority)).split('.')[0] + '.' + str(float(priority)).split('.')[1][:5]) |
| 660 | + return priority |
| 661 | + |
| 662 | + @property |
| 663 | + def folder_path(self): |
| 664 | + return f"{self._project.name}{f'/{self._folder.name}'if self._folder.name != 'root' else ''}" |
| 665 | + |
| 666 | + def execute(self): |
| 667 | + if self.is_valid(): |
| 668 | + priorities = [] |
| 669 | + initial_scores = [] |
| 670 | + for i in self._scores: |
| 671 | + priorities.append({ |
| 672 | + "name": i.name, |
| 673 | + "entropy_value": self.get_clean_priority(i.priority) |
| 674 | + }) |
| 675 | + initial_scores.append(i.name) |
| 676 | + uploaded_score_names = [] |
| 677 | + self.reporter.log_info(f"Uploading priority scores for {len(priorities)} item(s) from {self.folder_path}.") |
| 678 | + iterations = range(0, len(priorities), self.CHUNK_SIZE) |
| 679 | + self.reporter.start_progress(iterations, "Uploading priority scores") |
| 680 | + if iterations: |
| 681 | + for i in iterations: |
| 682 | + priorities_to_upload = priorities[i : i + self.CHUNK_SIZE] # noqa: E203 |
| 683 | + res = self._client.upload_priority_scores( |
| 684 | + team_id=self._project.team_id, |
| 685 | + project_id=self._project.uuid, |
| 686 | + folder_id=self._folder.uuid, |
| 687 | + priorities=priorities_to_upload |
| 688 | + ) |
| 689 | + self.reporter.update_progress(len(priorities_to_upload)) |
| 690 | + uploaded_score_names.extend(list(map(lambda x: x["name"], res.get("data", [])))) |
| 691 | + skipped_score_names = list(set(initial_scores) - set(uploaded_score_names)) |
| 692 | + self._response.data = (uploaded_score_names, skipped_score_names) |
| 693 | + else: |
| 694 | + self.reporter.warning_messages("Empty scores.") |
| 695 | + return self._response |
0 commit comments