-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcocoGenerator.js
70 lines (60 loc) · 2 KB
/
cocoGenerator.js
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
function hash(str) {
var hash = 5381,
i = str.length;
while(i) {
hash = (hash * 33) ^ str.charCodeAt(--i);
}
/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed
* integers. Since we want the results to be always positive, convert the
* signed int to an unsigned by doing an unsigned bitshift. */
return hash >>> 0;
}
class CocoImage {
constructor({id, license, file_name, url, height, width, date_captured = '2021-05-21 00:00:00'}) {
Object.assign(this, {id, license, file_name, url, height, width, date_captured});
}
}
class CocoCategory {
constructor({supercategory, id , name}) {
Object.assign(this, {supercategory, id , name});
}
static fromSynsetsByDataSource(synsetsByDataSource, superCategory) {
return Object.keys(synsetsByDataSource).map((s, i) => {
return new CocoCategory({superCategory, name:s, id:hash(s)});
});
}
}
class CocoLicense {
constructor({id, url, name}) {
Object.assign(this, {id, url, name});
}
}
class CocoAnnotation {
constructor({id, image_id, category_id, bbox, iscrowd= 0}) {
Object.assign(this, {id, image_id, category_id, bbox, iscrowd, segmentation:[]});
}
}
class CocoGenerator {
constructor({description, version, year, date_created, images= [], annotations= [], categories = [], licenses = []}) {
Object.assign(this, {info: {description, version, year, date_created}, images, annotations, categories, licenses})
}
static fromJson(json) {
return new CocoGenerator({
description:json.info.description,
version:json.info.version,
year:json.info.year,
date_created:json.info.date_created,
images:json.images,
annotations:json.annotations,
categories:json.categories,
licenses:json.licenses
});
}
}
module.exports = {
CocoAnnotation,
CocoCategory,
CocoGenerator,
CocoImage,
CocoLicense
}