-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectDetectorLite.py
63 lines (53 loc) · 2.3 KB
/
ObjectDetectorLite.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
import cv2
import tensorflow as tf
import sys
import numpy as np
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
from ObjectDetectorDetectionAPI import ObjectDetectorDetectionAPI
class ObjectDetectorLite(ObjectDetectorDetectionAPI):
def __init__(self, PATH_TO_LITE_MODEL, PATH_TO_LABELS, NUM_CLASSES, THRESHOLD):
model_path = PATH_TO_LITE_MODEL
self.THRESHOLD = THRESHOLD
"""
Builds Tensorflow graph, load model and labels
"""
# Load lebel_map
self._load_label(PATH_TO_LABELS, NUM_CLASSES, use_disp_name=True)
# Define lite graph and Load Tensorflow Lite model into memory
self.interpreter = tf.lite.Interpreter(
model_path=model_path)
self.interpreter.allocate_tensors()
self.input_details = self.interpreter.get_input_details()
self.output_details = self.interpreter.get_output_details()
def detect(self, image, threshold):
"""
Predicts person in frame with threshold level of confidence
Returns list with top-left, bottom-right coordinates and list with labels, confidence in %
"""
threshold = self.THRESHOLD
# Resize and normalize image for network input
frame = cv2.resize(image, (300, 300))
frame = np.expand_dims(frame, axis=0)
frame = (2.0 / 255.0) * frame - 1.0
frame = frame.astype('float32')
# run model
self.interpreter.set_tensor(self.input_details[0]['index'], frame)
self.interpreter.invoke()
# get results
boxes = self.interpreter.get_tensor(
self.output_details[0]['index'])
classes = self.interpreter.get_tensor(
self.output_details[1]['index'])
scores = self.interpreter.get_tensor(
self.output_details[2]['index'])
num = self.interpreter.get_tensor(
self.output_details[3]['index'])
# Find detected boxes coordinates
return self._boxes_coordinates(image,
np.squeeze(boxes[0]),
np.squeeze(classes[0]+1).astype(np.int32),
np.squeeze(scores[0]),
min_score_thresh=threshold)
def close(self):
pass