-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenerator.py
More file actions
268 lines (205 loc) · 8.55 KB
/
Generator.py
File metadata and controls
268 lines (205 loc) · 8.55 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import numpy as np
import scipy as sp
from Util import *
import datetime
import time
import csv
from detection import detectionResult
from datetime import datetime
import csv
from config import *
import sys
class detGenerator(object):
def __init__(self, resolution = (1080,720), minObj=5, maxObj=10, framenum=100):
self.width = resolution[0]
self.height = resolution[1]
self.wh = Point(resolution[0],resolution[1])
self.maxObj = maxObj
self.minObj = minObj
self.imgId = 0
self.framenum = framenum
#for validation
self.objId = 0
self.objCatagories = {1:0,2:0,3:0,4:0,5:0}
self.lastCreateTime = 0
self.objectHolds = {}
self.leftMost = resolution[0]
self.initObjectCount = np.random.randint(minObj,maxObj+1)
self.objectCount = self.initObjectCount
#result to return
self.detectionResultList = []
self.statOfFrame = np.zeros(100)
self.countResult = {}
self.totalObj = 0
#background image
self.bg = rect_(Point(self.width/2, self.height/2), self.wh)
self.validGenAreaY = np.ones(5)
#init first frame
self.batchAddObject(self.initObjectCount)
def addObj(self, det:detectionResult):
if self.imgId != 0:
occupiedY = int(det.center.y // 0.2)
self.validGenAreaY[occupiedY] = 0
self.objectHolds[self.objId] = det
self.objId += 1
self.statOfFrame[self.imgId-1]+=1
def genObject(self):
now = datetime.now()
currentTimestamp = int(datetime.timestamp(now))
catagory = np.random.randint(1,6)
wVar, hVar = [i+0.99 for i in np.random.rand(2)*0.2]
#Object always appear from left except first frame,
#each object might appear 3~5 frame
#gen object width and height
w, h = np.random.randint(100,250), np.random.randint(70, 170)
if self.imgId == 0:
absCenterX = np.random.rand()*self.width
else:
xMid = self.leftMost/2
absCenterX = min((0.5-np.random.rand())*w + xMid, self.width/5)
#Object won't directly has large overlap, it might continuously appear
#but not overlap
possibleIdx, = np.nonzero(self.validGenAreaY)
possibleY = np.random.randint(5) if possibleIdx.size == 0 else np.random.choice(possibleIdx)
centerRangeY = 0.2*possibleY+0.1+(np.random.rand()-0.5)*0.1
absCenterY = centerRangeY*self.height
#make variation of detection bot
absWH = Point(w*wVar, h*hVar)
absC = Point(absCenterX, absCenterY)
return detectionResult(currentTimestamp, catagory, self.objId, absWH, absC, resolution)
def move(self, det: detectionResult):
#each move has 5% variation
variation = 0.95 + np.random.rand()*0.1
#w and h might change due to detection
variationW, variationH = [i+0.99 for i in np.random.rand(2)*0.2]
stride = det.xStride*variation
#x move horizontal, with same speed and 5% variation
Cx = det.absCenter.x + stride
#theoretically y won't move but i give it a same variation range
#corespond to x
Cy = det.originAbsY + (det.xStride-stride)
newW = det.orW*variationW
newH = det.orH*variationH
now = datetime.now()
currentTimestamp = int(datetime.timestamp(now))
ncr, nwhr = Point(Cx, Cy), Point(newW, newH)
det.updatePosition(currentTimestamp, ncr, nwhr)
def createNewObject(self, genCount:int):
self.batchAddObject(genCount)
self.lastCreateTime = 0
self.leftMost = self.width
def batchAddObject(self, num):
for i in range(num):
tempDet = self.genObject()
while self.checkDetOverLap(tempDet, 0.3) is not True:
tempDet = self.genObject()
self.addObj(tempDet)
def updateLeftMostPoint(self):
for key, det in self.objectHolds.items():
self.leftMost = min(det.LU.x, self.leftMost)
def checkDetOverLap(self, det:detectionResult, thresh):
if len(self.objectHolds) == 0:
return True
for k, holdDet in self.objectHolds.items():
if IOU(det.rect, holdDet.rect) > thresh:
return False
return True
def populateFrame(self):
#each Frame reset the valid y range
self.validGenAreaY = np.ones(5)
toRemove = []
self.countResult[self.imgId] = []
for key, det in self.objectHolds.items():
self.move(det)
if det.existTime == det.lifespan :
toRemove.append(key)
self.countResult[self.imgId].append(det.catagory)
for k in toRemove:
del self.objectHolds[k]
self.updateLeftMostPoint()
self.objectCount = len(self.objectHolds)
l = abs(self.minObj - self.objectCount)
complement = self.maxObj - self.objectCount
#print("lower bound {}".format(l))
#print("object num {} and complement {}".format(self.objectCount, complement))
#must create new object
if self.objectCount < self.minObj:
objToGen = np.random.randint(l, complement+1)
else:
objToGen = np.random.randint(complement+1)
#print("Current object hold are {} and generating {}".format(self.objectCount, objToGen))
self.createNewObject(objToGen)
self.lastCreateTime +=1
self.imgId +=1
def constructDet(self, det:detectionResult):
return ('{},{},{},{:.4f},{:.3f},{:.3f},{:.3f},{:.3f}'.format(det.timestamp, self.imgId, det.catagory, det.confidence\
, det.center.x, det.center.y, det.wh.x, det.wh.y))
def reset(self):
self.imgId = 0
self.objId = 0
self.objCatagories = {1:0,2:0,3:0,4:0,5:0}
self.lastCreateTime = 0
self.objectHolds.clear()
self.leftMost = self.width
self.initObjectCount = np.random.randint(self.minObj,self.maxObj+1)
self.objectCount = self.initObjectCount
#result to return
self.detectionResultList.clear()
self.statOfFrame = np.zeros(100)
self.countResult.clear()
self.batchAddObject(self.initObjectCount)
'''
below are method for output
'''
def printCurrentFrame(self, Output=True):
if not Output :
return
for key, det in self.objectHolds.items():
print(self.constructDet(det))
def getDetectionRes(self, useObj=False):
while(self.imgId < self.framenum):
for key, det in self.objectHolds.items():
if useObj:
self.detectionResultList.append(det)
else:
self.detectionResultList.append(self.constructDet(det))
self.populateFrame()
return self.detectionResultList
def toCsv(self, filename="detResult.csv"):
with open(filename, 'a') as f:
while(self.imgId < self.framenum):
for key, det in self.objectHolds.items():
f.write("{}\n".format(self.constructDet(det)))
self.populateFrame()
for key, det in self.objectHolds.items():
f.write("{}\n" .format(self.constructDet(det)))
def display(self):
while(self.imgId < self.framenum):
backGround = np.zeros((720, 1080, 3), np.uint8)
backGround[:,:,:] = (255,255,255)
self.populateFrame()
for key, det in self.objectHolds.items():
renderRect(det.rect, backGround, det.catagory-1)
cv2.imshow("background", backGround)
k = cv2.waitKey(0)
if k == 'n':
continue
elif k == 27: #escape key
break
cv2.destroyAllWindows()
def run(self, Output=True):
while(self.imgId <= self.framenum):
self.printCurrentFrame(Output)
self.populateFrame()
def getGroundTruth(self):
return self.countResult
if __name__=="__main__":
arg = str(sys.argv)
detGen = detGenerator(minObj=5,maxObj=10, framenum = 100)
if "display" in arg:
detGen.display()
elif "save" in arg:
detGen.toCsv()
else:
detGen.run()
print(detGen.getGroundTruth())