-
Notifications
You must be signed in to change notification settings - Fork 1
/
image.jl
139 lines (113 loc) · 4.24 KB
/
image.jl
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
@with_kw_noshow struct ImageListRequest
limit::Int = 50; @assert limit > 0
offset::Int = 0; @assert offset >= 0
sort::Symbol = :lowerName
sortdir::Int = 1; @assert sortdir == 1 || sortdir == -1
datasetId::String
end
ImageListRequest(ds::Union{ListEntry,DatasetMetadata}; kw...) = ImageListRequest(datasetId = ds.id; kw...)
function Base.get(req::ImageListRequest)
query = "https://isic-archive.com/api/v1/image?limit=$(req.limit)&offset=$(req.offset)&sort=$(req.sort)&sortdir=$(req.sortdir)&datasetId=$(req.datasetId)"
[ListEntry(o) for o in clean_json(get(query))]
end
show(io::IO, req::ImageListRequest) =
print(io, "$(typeof(req))\nlimit: $(req.limit)\noffset: $(req.offset)\nsort: $(req.sort)\nsortdir: $(req.sortdir)\ndatasetId: $(req.datasetId)\n")
# ====================================================================
mutable struct ImageMetadata
id::String
modelType::String
created::DateTime
#creatorId::String
#description::String
name::String
updated::DateTime
class::Symbol
meta::Dict{String,Any}
end
function ImageMetadata(o::Dict)
ImageMetadata(
o["_id"],
o["_modelType"],
parse_datetime(o["created"]),
#o["creatorId"],
#o["description"],
o["name"],
parse_datetime(o["updated"]),
extract_class(o["meta"]),
o["meta"])
end
function JSON.json(o::ImageMetadata)
dict = Dict{String, Any}()
dict["_id"] = o.id
dict["_modelType"] = o.modelType
dict["created"] = tostring(o.created)
#dict["creatorId"] = o.creatorId
#dict["description"] = o.description
dict["name"] = o.name
dict["updated"] = tostring(o.updated)
dict["meta"] = o.meta
json(dict)
end
save(file, o::ImageMetadata) = writeall(file, json(o))
load_imagemeta(file) = ImageMetadata(JSON.parse(readall(file)))
for op = (:<, :>, :(==), :(!=), :(<=), :(>=))
@eval function Base.$op(i1::ImageMetadata, i2::ImageMetadata)
Base.$op(i1.updated, i2.updated)
end
end
function extract_class(o::Dict)
if haskey(o, "clinical")
c = o["clinical"]
if haskey(c, "benign_malignant")
return Symbol(c["benign_malignant"])
elseif haskey(c, "ben_mal")
return Symbol(c["ben_mal"])
end
end
:none
end
function Base.show(io::IO, o::ImageMetadata)
printstyled(io, string(typeof(o)), "\n", color = :white)
print(io, " .name: ")
printstyled(io, o.name, "\n", color = :blue)
println(io, " .id: ", o.id)
println(io, " .modelType: ", o.modelType)
#println(io, " .creatorId: ", o.creatorId)
println(io, " .created: ", o.created)
println(io, " .updated: ", o.updated)
println(io, " .class: ", o.class)
#println(io, " .description: ", o.description)
println(io, "")
print(io, " .meta: parsed JSON as ")
show(io, o.meta)
end
# ====================================================================
@with_kw_noshow struct ImageMetadataRequest
id::String
end
ImageMetadataRequest(le::Union{ListEntry,ImageMetadata}) = ImageMetadataRequest(id = le.id)
function Base.get(req::ImageMetadataRequest)
query = "https://isic-archive.com/api/v1/image/$(req.id)"
ImageMetadata(clean_json(get(query)))
end
show(io::IO, req::ImageMetadataRequest) = print(io, "$(typeof(req))\nid: $(req.id)")
# ====================================================================
@with_kw_noshow struct ImageDownloadRequest
id::String
end
ImageDownloadRequest(le::Union{ListEntry,ImageMetadata}) = ImageDownloadRequest(id = le.id)
function Base.get(req::ImageDownloadRequest)
query = "https://isic-archive.com/api/v1/image/$(req.id)/download"
ImageMagick.readblob(get(query).body)
end
show(io::IO, req::ImageDownloadRequest) = print(io, "$(typeof(req))\nid: $(req.id)")
# ====================================================================
@with_kw_noshow struct ImageThumbnailRequest
id::String
end
ImageThumbnailRequest(le::Union{ListEntry,ImageMetadata}) = ImageThumbnailRequest(id = le.id)
function Base.get(req::ImageThumbnailRequest)
query = "https://isic-archive.com/api/v1/image/$(req.id)/thumbnail"
ImageMagick.readblob(get(query).body)
end
show(io::IO, req::ImageThumbnailRequest) = print(io, "$(typeof(req))\nid: $(req.id)")