Skip to content

Commit 13744b4

Browse files
committed
Code cleanup
1 parent fe9505b commit 13744b4

File tree

1 file changed

+17
-34
lines changed

1 file changed

+17
-34
lines changed

OnePetri/Controllers/CountPlaquesViewController.swift

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ class CountPlaquesViewController: UIViewController {
2828
private var tileArray = [Tile]()
2929
private var colExtraTileArray = [Tile]()
3030
private var rowExtraTileArray = [Tile]()
31-
private var mainPlaqueArray = [Plaque]()
32-
private var colExtraPlaqueArray = [Plaque]()
33-
private var rowExtraPlaqueArray = [Plaque]()
31+
private var mergedPlaqueArray = [Plaque]()
3432

3533
private var tilesPerCol: Int!
3634
private var tilesPerRow: Int!
@@ -199,38 +197,28 @@ class CountPlaquesViewController: UIViewController {
199197

200198
let tempBox = VNImageRectForNormalizedRect(objectObservation.boundingBox, Int(currentTileWidth), Int(currentTileHeight)).applying(transformVerticalAxis)
201199

200+
var objectBounds: CGRect
201+
202202
switch currentTile.tileType {
203203
case .tile:
204-
let objectBounds = tempBox.offsetBy(dx: currentTile.locRowColumn.x * currentTileWidth, dy: currentTile.locRowColumn.y * currentTileHeight)
204+
objectBounds = tempBox.offsetBy(dx: currentTile.locRowColumn.x * currentTileWidth, dy: currentTile.locRowColumn.y * currentTileHeight)
205205
.applying(CGAffineTransform(scaleX: scaleX, y: scaleY))
206206
.applying(CGAffineTransform(translationX: (self.imageView.bounds.width-actualImageBounds.size.width)/2, y: offsetY))
207207

208-
let shapeLayer = self.createRoundedRectLayerWithBounds(objectBounds, color: [1.0, 0.0, 0.0])
209-
detectionOverlay.addSublayer(shapeLayer)
210-
211-
mainPlaqueArray.append(Plaque(petriDish: petriDish, locInLayer: shapeLayer.bounds, plaqueLayer: shapeLayer))
212-
213208
case .colExtraTile:
214-
let objectBounds = tempBox.offsetBy(dx: (currentTile.locRowColumn.x * currentTileWidth) - (currentTileWidth * 0.5), dy: currentTile.locRowColumn.y * currentTileHeight)
209+
objectBounds = tempBox.offsetBy(dx: (currentTile.locRowColumn.x * currentTileWidth) - (currentTileWidth * 0.5), dy: currentTile.locRowColumn.y * currentTileHeight)
215210
.applying(CGAffineTransform(scaleX: scaleX, y: scaleY))
216211
.applying(CGAffineTransform(translationX: offsetX, y: offsetY))
217212

218-
let shapeLayer = self.createRoundedRectLayerWithBounds(objectBounds, color: [1.0, 0.0, 0.0])
219-
detectionOverlay.addSublayer(shapeLayer)
220-
221-
colExtraPlaqueArray.append(Plaque(petriDish: petriDish, locInLayer: shapeLayer.bounds, plaqueLayer: shapeLayer))
222-
223213
case .rowExtraTile:
224-
let objectBounds = tempBox.offsetBy(dx: currentTile.locRowColumn.x * currentTileWidth, dy: (currentTile.locRowColumn.y * currentTileHeight) + (currentTileHeight * 0.5))
214+
objectBounds = tempBox.offsetBy(dx: currentTile.locRowColumn.x * currentTileWidth, dy: (currentTile.locRowColumn.y * currentTileHeight) + (currentTileHeight * 0.5))
225215
.applying(CGAffineTransform(scaleX: scaleX, y: scaleY))
226216
.applying(CGAffineTransform(translationX: (self.imageView.bounds.width-actualImageBounds.size.width)/2, y: offsetY))
227-
228-
let shapeLayer = self.createRoundedRectLayerWithBounds(objectBounds, color: [1.0, 0.0, 0.0])
229-
detectionOverlay.addSublayer(shapeLayer)
230-
231-
rowExtraPlaqueArray.append(Plaque(petriDish: petriDish, locInLayer: shapeLayer.bounds, plaqueLayer: shapeLayer))
232217
}
233218

219+
let shapeLayer = self.createRoundedRectLayerWithBounds(objectBounds, color: [1.0, 0.0, 0.0])
220+
detectionOverlay.addSublayer(shapeLayer)
221+
mergedPlaqueArray.append(Plaque(petriDish: petriDish, locInLayer: shapeLayer.bounds, plaqueLayer: shapeLayer))
234222
}
235223

236224
self.updateLayerGeometry()
@@ -302,28 +290,23 @@ class CountPlaquesViewController: UIViewController {
302290
}
303291

304292
func summarizePlaques(plaqueArray: [Plaque]? = nil) {
305-
print("===== START OF PLAQUE COUNTS =====")
306293
if let plaqueArray = plaqueArray {
307-
print("Total plaque count after NMS is: \(plaqueArray.count)")
294+
print("Total plaque count AFTER NMS is: \(plaqueArray.count)")
308295
let s = (plaqueArray.count == 1) ? "" : "s"
309296
DispatchQueue.main.async {
310297
self.textView.text = "\(plaqueArray.count) plaque\(s) detected"
311298
}
312299
} else {
313-
print("mainPlaqueArray count is: \(mainPlaqueArray.count)")
314-
print("colExtraPlaqueArray plaque array count is: \(colExtraPlaqueArray.count)")
315-
print("rowExtraPlaqueArray plaque array count is: \(rowExtraPlaqueArray.count)")
300+
print("Total plaque count BEFORE NMS is: \(mergedPlaqueArray.count)")
316301
}
317-
print("===== END OF PLAQUE COUNTS =====")
318302
}
319303

320304
func nonMaximumSuppression(finished: @escaping () -> Void) {
321305
summarizePlaques()
322306

323307
//all vs all comparison
324-
var mergedArray = mainPlaqueArray + colExtraPlaqueArray + rowExtraPlaqueArray
325-
for plaque in mergedArray {
326-
for otherPlaque in mergedArray {
308+
for plaque in mergedPlaqueArray {
309+
for otherPlaque in mergedPlaqueArray {
327310
if plaque !== otherPlaque {
328311
let plaqueLayerBds = plaque.locInLayer
329312
let otherPlaqueLayerBds = otherPlaque.locInLayer
@@ -339,17 +322,17 @@ class CountPlaquesViewController: UIViewController {
339322

340323
if (areaOverPlaque > areaOverOtherPlaque) && (areaOverPlaque >= CGFloat(iouThreshold)) {
341324
DispatchQueue.main.async{ self.detectionOverlay.sublayers!.removeAll(where: {$0 === plaque.plaqueLayer}) }
342-
mergedArray.removeAll(where: {$0 === plaque})
325+
mergedPlaqueArray.removeAll(where: {$0 === plaque})
343326
} else if (areaOverOtherPlaque > areaOverPlaque) && (areaOverOtherPlaque >= CGFloat(iouThreshold)) {
344327
DispatchQueue.main.async{ self.detectionOverlay.sublayers!.removeAll(where: {$0 === otherPlaque.plaqueLayer}) }
345-
mergedArray.removeAll(where: {$0 === otherPlaque})
328+
mergedPlaqueArray.removeAll(where: {$0 === otherPlaque})
346329
}
347330
}
348331
}
349332
}
350333
}
351-
petriDish.plaques = mergedArray
352-
summarizePlaques(plaqueArray: mergedArray)
334+
petriDish.plaques = mergedPlaqueArray
335+
summarizePlaques(plaqueArray: mergedPlaqueArray)
353336
finished()
354337
}
355338

0 commit comments

Comments
 (0)