-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetect.py
62 lines (51 loc) · 1.89 KB
/
detect.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
import time
import tensorflow as tf
from absl import app
from tensorflow.python.saved_model import tag_constants
from PIL import Image
import cv2
import numpy as np
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
def main():
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
input_size = 416
video_path = './test_set/video.mp4'
saved_model_loaded = tf.saved_model.load('./model/model_dir_name', tags=[tag_constants.SERVING])
infer = saved_model_loaded.signatures['serving_default']
# start video capturing process
try:
video = cv2.VideoCapture(int(video_path))
except:
video = cv2.VideoCapture(video_path)
while True:
return_value, frame = video.read()
if return_value:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
else:
print('Error during video reading')
break
image_data = cv2.resize(frame, (input_size, input_size))
image_data = image_data / 255.
image_data = image_data[np.newaxis, ...].astype(np.float32)
start_time = time.time()
batch_data = tf.constant(image_data)
pred_bbox = infer(batch_data)
print(pred_bbox)
for key, value in pred_bbox.items():
pred_conf = value[:, :, 4:]
scores = tf.reshape(pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])).numpy()
fps = 1.0 / (time.time() - start_time)
# return value in % and fps
if scores.size > 0: print(np.amax(scores))
print("FPS: %.2f" % fps)
if __name__ == '__main__':
try:
app.run(main)
except SystemExit:
pass