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

Sample extractor that uses image annotation widget #112

Merged
merged 2 commits into from
Aug 28, 2024
Merged
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
34 changes: 34 additions & 0 deletions sample-extractors/image-annotation-extractor/extractor_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"@context": "http://clowder.ncsa.illinois.edu/contexts/extractors.jsonld",
"name": "ncsa.image_annotator",
"version": "2.0",
"description": "Saves user image annotations as metadata",
"author": "Vismayak Mohanarajan <[email protected]>",
"contributors": [],
"contexts": [
],
"repository": [
{
"repType": "git",
"repUrl": "https://opensource.ncsa.illinois.edu/stash/scm/cats/pyclowder.git"
}
],
"process": {
"file": [
"text/*",
"application/json"
]
},
"external_services": [],
"dependencies": [],
"bibtex": [],
"parameters": {
"schema": {
"IMAGE_ANNOTATIONS": {
"type": "string",
"title": "Annotate image",
"format": "ImageAnnotator"
}
}
}
}
63 changes: 63 additions & 0 deletions sample-extractors/image-annotation-extractor/image_annotation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python

"""Example extractor based on the clowder code."""

import logging
import subprocess
import json
from typing import Dict

from pyclowder.extractors import Extractor
import pyclowder.files

Vismayak marked this conversation as resolved.
Show resolved Hide resolved

class ImageAnnotator(Extractor):
"""Count the number of characters, words and lines in a text file."""
def __init__(self):
Extractor.__init__(self)

# add any additional arguments to parser
# self.parser.add_argument('--max', '-m', type=int, nargs='?', default=-1,
# help='maximum number (default=-1)')

# parse command line and load default logging configuration
self.setup()

# setup logging for the exctractor
logging.getLogger('pyclowder').setLevel(logging.DEBUG)
logging.getLogger('__main__').setLevel(logging.DEBUG)

def process_message(self, connector, host, secret_key, resource, parameters):
# Process the file and upload the results

logger = logging.getLogger(__name__)
inputfile = resource["local_paths"][0]
file_id = resource['id']
print(f"Parameters: {parameters}")

if 'parameters' in parameters:
params = None
logging.info("Received parameters")
try:
params = json.loads(parameters['parameters'])
except TypeError as e:
print(f"Failed to load parameters, it's not compatible with json.loads().\nError:{e}")
if type(parameters == Dict):
params = parameters['parameters']
if "IMAGE_ANNOTATIONS" in params:
image_annotations = params["IMAGE_ANNOTATIONS"]
logging.info(f"Image annotations: {image_annotations}")

result = json.loads(image_annotations)

metadata = self.get_metadata(result, 'file', file_id, host)

# Normal logs will appear in the extractor log, but NOT in the Clowder UI.
logger.debug(metadata)

# Upload metadata to original file
pyclowder.files.upload_metadata(connector, host, secret_key, file_id, metadata)

if __name__ == "__main__":
extractor = ImageAnnotator()
extractor.start()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyclowder==3.0.7
Loading