-
Notifications
You must be signed in to change notification settings - Fork 175
/
test.py
74 lines (55 loc) · 2.09 KB
/
test.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
71
72
73
74
import os
import colorsys
import numpy as np
from keras import backend as K
from keras.models import load_model
from keras.layers import Input
from yolo4.model import yolo_eval, yolo4_body
from yolo4.utils import letterbox_image
from PIL import Image, ImageFont, ImageDraw
from timeit import default_timer as timer
import cv2
from decode_np import Decode
def get_class(classes_path):
classes_path = os.path.expanduser(classes_path)
with open(classes_path) as f:
class_names = f.readlines()
class_names = [c.strip() for c in class_names]
return class_names
def get_anchors(anchors_path):
anchors_path = os.path.expanduser(anchors_path)
with open(anchors_path) as f:
anchors = f.readline()
anchors = [float(x) for x in anchors.split(',')]
return np.array(anchors).reshape(-1, 2)
if __name__ == '__main__':
print('Please visit https://github.com/miemie2013/Keras-YOLOv4 for more complete model!')
model_path = 'ep073-loss11.905.h5'
anchors_path = 'model_data/yolo4_anchors.txt'
classes_path = 'model_data/voc_classes.txt'
class_names = get_class(classes_path)
anchors = get_anchors(anchors_path)
num_anchors = len(anchors)
num_classes = len(class_names)
model_image_size = (608, 608)
# 分数阈值和nms_iou阈值
conf_thresh = 0.2
nms_thresh = 0.45
yolo4_model = yolo4_body(Input(shape=model_image_size+(3,)), num_anchors//3, num_classes)
model_path = os.path.expanduser(model_path)
assert model_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.'
yolo4_model.load_weights(model_path)
_decode = Decode(conf_thresh, nms_thresh, model_image_size, yolo4_model, class_names)
while True:
img = input('Input image filename:')
try:
image = cv2.imread(img)
except:
print('Open Error! Try again!')
continue
else:
image, boxes, scores, classes = _decode.detect_image(image, True)
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
yolo4_model.close_session()