-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdet_webcam.py
54 lines (40 loc) · 1.38 KB
/
det_webcam.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
import argparse
import sys
import cv2
import tensorflow as tf
import numpy as np
parser = argparse.ArgumentParser(
description='convert model')
parser.add_argument('--net_type', default="slim", type=str,
help='The network architecture ,optional: RFB (higher precision) or slim (faster)')
args = parser.parse_args()
def main():
if args.net_type == 'slim':
model_path = "export_models/slim/"
elif args.net_type == 'RFB':
model_path = "export_models/RFB/"
else:
print("The net type is wrong!")
sys.exit(1)
model = tf.keras.models.load_model(model_path)
cap = cv2.VideoCapture(0)
ret = True
while ret:
ret, origin_img = cap.read()
h, w, _ = origin_img.shape
img = cv2.resize(origin_img, (320, 240))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img - 127.0
img = img / 128.0
results = model.predict(np.expand_dims(img, axis=0))
for result in results:
start_x = int(result[-4] * w)
start_y = int(result[-3] * h)
end_x = int(result[-2] * w)
end_y = int(result[-1] * h)
cv2.rectangle(origin_img, (start_x, start_y), (end_x, end_y), (0, 255, 0), 0)
cv2.imshow('frame', origin_img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if __name__ == '__main__':
main()