From a0c6326ad6e1104d7bc6fdbee1fba878e3128d36 Mon Sep 17 00:00:00 2001 From: Niklas Neugebauer <68709968+NiklasNeugebauer@users.noreply.github.com> Date: Wed, 13 Nov 2024 17:01:09 +0100 Subject: [PATCH] Fix custom creation date (#43) Co-authored-by: Dr. Dennis Wittich --- learning_loop_node/data_classes/image_metadata.py | 2 +- learning_loop_node/detector/detector_node.py | 5 +++-- learning_loop_node/detector/outbox.py | 13 ++++++++----- learning_loop_node/loop_communication.py | 6 +++--- mock_detector/app_code/tests/test_detector.py | 2 +- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/learning_loop_node/data_classes/image_metadata.py b/learning_loop_node/data_classes/image_metadata.py index 98496a2d..af95d00c 100644 --- a/learning_loop_node/data_classes/image_metadata.py +++ b/learning_loop_node/data_classes/image_metadata.py @@ -28,7 +28,7 @@ class ImageMetadata(): tags: List[str] = field(default_factory=list, metadata={ 'description': 'List of tags'}) - date: Optional[str] = field(default_factory=current_datetime, metadata={ + created: Optional[str] = field(default_factory=current_datetime, metadata={ 'description': 'Creation date of the image'}) source: Optional[str] = field(default=None, metadata={ 'description': 'Source of the image'}) diff --git a/learning_loop_node/detector/detector_node.py b/learning_loop_node/detector/detector_node.py index 51604251..07252ac2 100644 --- a/learning_loop_node/detector/detector_node.py +++ b/learning_loop_node/detector/detector_node.py @@ -140,7 +140,6 @@ def setup_sio_server(self) -> None: @self.sio.event async def detect(sid, data: Dict) -> Dict: - self.log.debug('running detect via socketio') try: np_image = np.frombuffer(data['image'], np.uint8) det = await self.get_detections( @@ -153,7 +152,6 @@ async def detect(sid, data: Dict) -> Dict: if det is None: return {'error': 'no model loaded'} detection_dict = jsonable_encoder(asdict(det)) - self.log.debug('detect via socketio finished') return detection_dict except Exception as e: self.log.exception('could not detect via socketio') @@ -188,6 +186,9 @@ async def upload(sid, data: Dict) -> Optional[Dict]: source = data.get('source', None) creation_date = data.get('creation_date', None) + self.log.debug('running upload via socketio. tags: %s, source: %s, creation_date: %s', + tags, source, creation_date) + loop = asyncio.get_event_loop() try: await loop.run_in_executor(None, self.outbox.save, data['image'], image_metadata, tags, source, creation_date) diff --git a/learning_loop_node/detector/outbox.py b/learning_loop_node/detector/outbox.py index c29cc456..201b2457 100644 --- a/learning_loop_node/detector/outbox.py +++ b/learning_loop_node/detector/outbox.py @@ -76,10 +76,11 @@ def save(self, return tmp = f'{GLOBALS.data_folder}/tmp/{identifier}' image_metadata.tags = tags - if creation_date and self._is_valid_isoformat(creation_date): - image_metadata.date = creation_date + if self._is_valid_isoformat(creation_date): + image_metadata.created = creation_date else: - image_metadata.date = identifier + image_metadata.created = identifier + image_metadata.source = source or 'unknown' os.makedirs(tmp, exist_ok=True) @@ -94,7 +95,9 @@ def save(self, else: self.log.error('Could not rename %s to %s', tmp, self.path + '/' + identifier) - def _is_valid_isoformat(self, date: str) -> bool: + def _is_valid_isoformat(self, date: Optional[str]) -> bool: + if date is None: + return False try: datetime.fromisoformat(date) return True @@ -153,7 +156,7 @@ async def _upload_batch(self, items: List[str]): self.log.exception('Could not upload images') return finally: - self.log.info('Closing files') + self.log.debug('Closing files') for _, file in data: file.close() diff --git a/learning_loop_node/loop_communication.py b/learning_loop_node/loop_communication.py index 13505e09..7f44cb26 100644 --- a/learning_loop_node/loop_communication.py +++ b/learning_loop_node/loop_communication.py @@ -35,7 +35,7 @@ def __init__(self) -> None: else: self.async_client = httpx.AsyncClient(base_url=self.base_url, timeout=Timeout(60.0)) - logging.info(f'Loop interface initialized with base_url: {self.base_url} / user: {self.username}') + logging.info('Loop interface initialized with base_url: %s / user: %s', self.base_url, self.username) def websocket_url(self) -> str: return f'ws{"s" if "learning-loop.ai" in self.host else ""}://' + self.host @@ -48,7 +48,7 @@ async def ensure_login(self, relogin=False) -> None: self.async_client.cookies.clear() response = await self.async_client.post('/api/login', data={'username': self.username, 'password': self.password}) if response.status_code != 200: - logging.info(f'Login failed with response: {response}') + logging.info('Login failed with response: %s', response) raise LoopCommunicationException('Login failed with response: ' + str(response)) self.async_client.cookies.update(response.cookies) @@ -57,7 +57,7 @@ async def logout(self) -> None: response = await self.async_client.post('/api/logout') if response.status_code != 200: - logging.info(f'Logout failed with response: {response}') + logging.info('Logout failed with response: %s', response) raise LoopCommunicationException('Logout failed with response: ' + str(response)) self.async_client.cookies.clear() diff --git a/mock_detector/app_code/tests/test_detector.py b/mock_detector/app_code/tests/test_detector.py index a10c8efc..19719fc9 100644 --- a/mock_detector/app_code/tests/test_detector.py +++ b/mock_detector/app_code/tests/test_detector.py @@ -38,4 +38,4 @@ async def test_sio_detect(test_detector_node: DetectorNode, sio): assert response['point_detections'] == [] assert response['segmentation_detections'] == [] assert response['tags'] == [] - assert 'date' in response + assert 'created' in response