2up1down commited on
Commit
de9a9d0
1 Parent(s): ee3edc6

Upload 2 files

Browse files
Files changed (2) hide show
  1. README.md +15 -3
  2. make-dataset.py +116 -0
README.md CHANGED
@@ -1,3 +1,15 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dataset for Reading Analog Dial
2
+
3
+ dataset found at [Synanthropic/reading-analog-dial](https://huggingface.co/datasets/Synanthropic/reading-analog-dial)
4
+
5
+ This is the dataset used to train the model for reading analog dial found at: [demo](https://huggingface.co/spaces/Synanthropic/reading-analog-dial)
6
+
7
+ # Setup dataset for model training
8
+ - there are two datasets, corner and keypoint
9
+ 1. download dataset
10
+ 2. unzip one of (corner, keypoint)
11
+ 3. edit make-dataset.py and set variable *srcdir* to one of corner or keypoint
12
+ - run script
13
+ 4. train your model
14
+
15
+
make-dataset.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1.unzip file
2
+ # 2. change *srcdir*
3
+ # 3. run this script
4
+ # do training
5
+
6
+ import numpy as np
7
+ from pathlib import Path
8
+ import os
9
+
10
+ outdir = "datasets/data/"
11
+
12
+ # setwhich one
13
+ # srcdir = "corners"
14
+ # srcdir = "keypoints"
15
+
16
+ if srcdir=="keypoints":
17
+ _KEYS = "start_kp center end_kp tip".split()
18
+ DIM = 640
19
+ elif srcdir=="corners":
20
+ _KEYS = "topLeft topRight bottomRight bottomLeft".split()
21
+ DIM = 1280
22
+
23
+ F = os.listdir(srcdir)
24
+
25
+ import random
26
+ random.seed(4)
27
+ random.shuffle(F)
28
+ F[0]
29
+
30
+ # 80% training
31
+ n = int(0.8*len(F))
32
+ n
33
+ train,val = F[:n], F[n:]
34
+
35
+ import shutil
36
+
37
+ for d in "images labels".split():
38
+ for dd in "train val test".split():
39
+ if not os.path.exists(f"{outdir}/{d}/{dd}"):
40
+ os.makedirs(f"{outdir}/{d}/{dd}")
41
+
42
+ for trgd,F in [(f"{outdir}/images/train", train), (f"{outdir}/images/val", val), (f"{outdir}/images/test", val[:100])]:
43
+ for srcf in F:
44
+ shutil.move(os.path.join(srcdir, srcf), os.path.join(trgd, srcf))
45
+
46
+
47
+
48
+
49
+ def filename2keypoints(fn:str):
50
+ kp = {}
51
+ i = 0
52
+ for sfn in fn.split("_"):
53
+ if "-" in sfn:
54
+ a,b = sfn.split("-")
55
+ kp[_KEYS[i]] = (int(a),int(b))
56
+ i+=1
57
+ return kp
58
+
59
+
60
+ # <class-index> <x> <y> <width> <height> <px1> <py1> <px2> <py2> ... <pxn> <pyn>
61
+ def formatLabel(cx,cy, w,h, kp):
62
+ pxs =""
63
+ for k in _KEYS:
64
+ x,y = kp[k]
65
+ pxs += f"{x/DIM} {y/DIM} "
66
+ return f"0 {cx/DIM} {cy/DIM} {w/DIM} {h/DIM} " +pxs.strip()
67
+
68
+ if srcdir=="keypoints":
69
+ def toCenterCoordinates(kp,dim = DIM):
70
+ # trying to determine bounding box via cv isnt reliable so we assume, dial face will always appears nearly same dim. and determine box from center point
71
+ w, h = 560,560
72
+ x,y = kp["center"]
73
+ wh = w/2
74
+ hh = h/2
75
+
76
+ cx = x-wh
77
+ cy = y - hh # left top corner
78
+
79
+ cx,cy, x,y
80
+ _w,_h = 0,0
81
+ # though this is not really correct, we will adjust dim. if bounding box is outside viewport
82
+ if cx<0:
83
+ _w = cx
84
+ elif x+wh > dim:
85
+ _w = (x+wh) - dim
86
+
87
+ if cy < 0:
88
+ _h = cy
89
+ elif (y+hh)>dim:
90
+ _h = (y+hh) - dim
91
+
92
+ return x,y, w-_w,h-_h
93
+ else:
94
+ def toCenterCoordinates(kp,dim = DIM):
95
+ # bounding corners
96
+ c = np.array(list(kp.values()))
97
+ margin = 10
98
+ topLeft,bottomRight = c.min(axis=0)-margin, c.max(axis=0)+margin
99
+ topLeft,bottomRight = np.clip(topLeft, 0, dim),np.clip(bottomRight, 0, dim)
100
+ cx,cy = 0.5*(bottomRight + topLeft)
101
+ w,h = bottomRight-topLeft
102
+ return cx,cy, w,h
103
+
104
+
105
+ # make annotation file
106
+ for d,F in [(f"{outdir}/images/train", train), (f"{outdir}/images/val", val), (f"{outdir}/images/test", val[:100])]:
107
+ for f in F:
108
+ _,ext = os.path.splitext(f)
109
+ antfn = os.path.join(outdir, d.replace("images/", "labels/"), _+".txt")
110
+ kp = filename2keypoints(f)
111
+ cx,cy,w,h = toCenterCoordinates(kp)
112
+ assert cx>0
113
+ assert cy>0
114
+ L = formatLabel(cx,cy,w,h, kp)
115
+ with open(antfn, "w") as fn:
116
+ fn.write(L+"\n")