Skip to content

Commit fcae948

Browse files
committed
First pass at creating an image annotating extractor
1 parent c45e75d commit fcae948

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"@context": "http://clowder.ncsa.illinois.edu/contexts/extractors.jsonld",
3+
"name": "ncsa.image_annotator",
4+
"version": "2.0",
5+
"description": "Saves user image annotations as metadata",
6+
"author": "Vismayak Mohanarajan <[email protected]>",
7+
"contributors": [],
8+
"contexts": [
9+
],
10+
"repository": [
11+
{
12+
"repType": "git",
13+
"repUrl": "https://opensource.ncsa.illinois.edu/stash/scm/cats/pyclowder.git"
14+
}
15+
],
16+
"process": {
17+
"file": [
18+
"text/*",
19+
"application/json"
20+
]
21+
},
22+
"external_services": [],
23+
"dependencies": [],
24+
"bibtex": [],
25+
"parameters": {
26+
"schema": {
27+
"IMAGE_ANNOTATIONS": {
28+
"type": "string",
29+
"title": "Annotate image",
30+
"format": "ImageAnnotator"
31+
}
32+
}
33+
}
34+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
3+
"""Example extractor based on the clowder code."""
4+
5+
import logging
6+
import subprocess
7+
import json
8+
from typing import Dict
9+
10+
from pyclowder.extractors import Extractor
11+
import pyclowder.files
12+
13+
14+
class WordCount(Extractor):
15+
"""Count the number of characters, words and lines in a text file."""
16+
def __init__(self):
17+
Extractor.__init__(self)
18+
19+
# add any additional arguments to parser
20+
# self.parser.add_argument('--max', '-m', type=int, nargs='?', default=-1,
21+
# help='maximum number (default=-1)')
22+
23+
# parse command line and load default logging configuration
24+
self.setup()
25+
26+
# setup logging for the exctractor
27+
logging.getLogger('pyclowder').setLevel(logging.DEBUG)
28+
logging.getLogger('__main__').setLevel(logging.DEBUG)
29+
30+
def process_message(self, connector, host, secret_key, resource, parameters):
31+
# Process the file and upload the results
32+
33+
logger = logging.getLogger(__name__)
34+
inputfile = resource["local_paths"][0]
35+
file_id = resource['id']
36+
37+
if 'parameters' in parameters:
38+
params = None
39+
logging.info("Received parameters")
40+
try:
41+
params = json.loads(parameters['parameters'])
42+
except TypeError as e:
43+
print(f"Failed to load parameters, it's not compatible with json.loads().\nError:{e}")
44+
if type(parameters == Dict):
45+
params = parameters['parameters']
46+
if "IMAGE_ANNOTATIONS" in params:
47+
image_annotations = params["IMAGE_ANNOTATIONS"]
48+
logging.info(f"Image annotations: {image_annotations}")
49+
50+
result = json.loads(image_annotations)
51+
52+
metadata = self.get_metadata(result, 'file', file_id, host)
53+
54+
# Normal logs will appear in the extractor log, but NOT in the Clowder UI.
55+
logger.debug(metadata)
56+
57+
# Upload metadata to original file
58+
pyclowder.files.upload_metadata(connector, host, secret_key, file_id, metadata)
59+
60+
if __name__ == "__main__":
61+
extractor = WordCount()
62+
extractor.start()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pyclowder==3.0.7

0 commit comments

Comments
 (0)