-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_counter_detection.py
71 lines (54 loc) · 2.08 KB
/
test_counter_detection.py
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
from __future__ import generators
import glob
import sys
from yolo_lib.darknet import *
from yolo_lib.non_maximal_suppression import *
from collections import namedtuple
from yolo_lib.annotation import *
from PIL import Image
from yolo_lib.non_maximal_suppression import *
from yolo_lib.annotation import *
import PIL
if __name__ == '__main__':
PIL.Image.MAX_IMAGE_PIXELS = None
if len(sys.argv) < 2:
print("Incorrect arguments. Please run python predict.py path/to/file")
sys.exit(1)
folder = sys.argv[1]
folder = folder.rstrip('/') + '/'
images = glob.glob( folder + "*.jpg")
output = sys.argv[2]
output = output.rstrip('/') + '/'
matches = 0
total = 0
for filename in images:
print(filename)
base = os.path.basename(os.path.splitext(filename)[0])
model = YoloModel("./cfg/spark-counter-yolov3-tiny.cfg", "weights/spark-counter-yolov3-tiny_best.weights", "./cfg/spark-counter.data");
results = model.detect(filename, 0.25, True, True)
predictions = list(map(lambda o: Prediction(
o[0].decode('utf-8'),
o[1],
o[2][0] - o[2][2]/2,
o[2][1] - o[2][3]/2,
o[2][2],
o[2][3]
), results['detections']))
print("{0} predictions".format(len(predictions)))
print("output " + output + base )
im = Image.fromarray(results['image'])
im.save(output + base + "-prediction.jpg")
if(len(predictions) == 0) :
continue
predictions.sort(key=lambda prediction: prediction.confidence, reverse=True)
prediction = predictions[0]
im = Image.open(filename)
width, height = im.size
ratio = 1.2
croppedImage = im.crop((
prediction.leftx - (prediction.width * (ratio -1)),
prediction.topy - (prediction.height * (ratio-1)),
prediction.leftx + prediction.width * ratio,
prediction.topy + prediction.height * ratio
))
croppedImage.save(output + base + "-scaled-cropped.jpg")