-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimport-annotations.py
83 lines (73 loc) · 3.29 KB
/
import-annotations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import dropbox
import requests
from modzy import ApiClient
#Initializes Modzy and Dropbox clients
mdz = ApiClient(base_url=os.getenv('MODZY_BASE_URL'), api_key=os.getenv("MODZY_API_KEY"))
dbx = dropbox.Dropbox(os.getenv("DROPBOX_ACCESS_TOKEN"))
def main():
#Loops through all images in a dropbox folder and adds the image names and URLs to a dict
jobs = []
path_urls = {}
images = dbx.files_list_folder("")
for image in images.entries:
job_ID = image.path_display[1:].split(".")[0]
jobs.append(job_ID)
path_urls[job_ID] = get_dropbox_URL(image.path_display)
#Creates a dictionary with all of the inference information that will be sent to Label Studio
results_data = {}
for i, job in enumerate(jobs):
job_details = mdz.jobs.get(job)
result = mdz.results.block_until_complete(job)
input_filename = list(result["results"].keys())[0]
results_data[f'job_{i}'] = {
"job_id": job,
"input_name": input_filename,
"model_version": job_details["model"]["version"],
"label": result["results"][input_filename]["results.json"]["data"]["result"]["classPredictions"][0]["class"],
"score": result["results"][input_filename]["results.json"]["data"]["result"]["classPredictions"][0]["score"],
"num_upvotes": result["results"][input_filename]["voting"]["up"],
"num_downvotes": result["results"][input_filename]["voting"]["down"]
}
# Creates the data object that Label Studio is expecting for our template
data = [{
"data": {
"image": path_urls[results_data[f'job_{i}']["job_id"]],
"predicted_value": predicted_value_msg(results_data[f'job_{i}']["label"], results_data[f'job_{i}']["score"], results_data[f'job_{i}']["num_upvotes"], results_data[f'job_{i}']["num_downvotes"]),
"downvotes": results_data[f'job_{i}']["num_downvotes"],
"explainable_url": explainable_url(results_data[f'job_{i}']["job_id"], results_data[f'job_{i}']["input_name"])
},
"predictions": [{
"model_version": results_data[f'job_{i}']["model_version"],
"score": results_data[f'job_{i}']["score"],
"result": [
{
"id": results_data[f'job_{i}']["job_id"],
"type": "choices",
"from_name": "location",
"to_name": "image",
"value": {
"choices": [results_data[f'job_{i}']["label"]]
}
}]
}]
} for i in range(len(results_data))]
#Posts pre-annotated image links and results to Label Studio
labelStudioURL = 'http://localhost:8080/api/projects/1/import'
authHeader = f"Token {os.getenv('LABEL_STUDIO_ACCESS_TOKEN')}"
r = requests.post(labelStudioURL, json=data, headers={'Authorization': authHeader})
#Gets shareable link from dropbox
def get_dropbox_URL(image_path):
sharedURL = dbx.sharing_create_shared_link(image_path).url
cleanURL = sharedURL.replace("dl=0","raw=1")
return cleanURL
#Formats the message included with each labeling task
def predicted_value_msg(label, score, upvotes, downvotes):
msg = f"Predicted value: {label} ({score:.1%} certainty) 👍: {upvotes} 👎: {downvotes}"
return msg
#Formats the URL to view the explanation of each prediction
def explainable_url(job_ID, input_name):
url = f"{os.getenv('MODZY_BASE_URL')}/operations/explainability/{job_ID}/{input_name}"
return url
if __name__ == '__main__':
main()