Skip to content

Commit 69d9f71

Browse files
authored
Sample extractor that uses image annotation widget (#112)
* First pass at creating an image annotating extractor * Update image_annotation.py
1 parent c45e75d commit 69d9f71

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 ImageAnnotator(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+
print(f"Parameters: {parameters}")
37+
38+
if 'parameters' in parameters:
39+
params = None
40+
logging.info("Received parameters")
41+
try:
42+
params = json.loads(parameters['parameters'])
43+
except TypeError as e:
44+
print(f"Failed to load parameters, it's not compatible with json.loads().\nError:{e}")
45+
if type(parameters == Dict):
46+
params = parameters['parameters']
47+
if "IMAGE_ANNOTATIONS" in params:
48+
image_annotations = params["IMAGE_ANNOTATIONS"]
49+
logging.info(f"Image annotations: {image_annotations}")
50+
51+
result = json.loads(image_annotations)
52+
53+
metadata = self.get_metadata(result, 'file', file_id, host)
54+
55+
# Normal logs will appear in the extractor log, but NOT in the Clowder UI.
56+
logger.debug(metadata)
57+
58+
# Upload metadata to original file
59+
pyclowder.files.upload_metadata(connector, host, secret_key, file_id, metadata)
60+
61+
if __name__ == "__main__":
62+
extractor = ImageAnnotator()
63+
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)