Skip to content

Commit

Permalink
Sample extractor that uses image annotation widget (#112)
Browse files Browse the repository at this point in the history
* First pass at creating an image annotating extractor

* Update image_annotation.py
  • Loading branch information
Vismayak authored Aug 28, 2024
1 parent c45e75d commit 69d9f71
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
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


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

0 comments on commit 69d9f71

Please sign in to comment.