Interaction-without-Interac.../server/detector.py

98 lines
3.9 KiB
Python
Raw Normal View History

2019-04-01 22:30:26 +00:00
# Code adapted from Tensorflow Object Detection Framework
# https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb
# Tensorflow Object Detection Detector
import numpy as np
import tensorflow as tf
import cv2
2019-07-18 12:46:16 +00:00
# Detector API can be changed out given the I/O remains the same
# this way you can use a different N-Net if you like to
2019-04-01 22:30:26 +00:00
class DetectorAPI:
def __init__(self, path_to_ckpt):
self.path_to_ckpt = path_to_ckpt
self.detection_graph = tf.Graph()
with self.detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(self.path_to_ckpt, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
self.default_graph = self.detection_graph.as_default()
self.sess = tf.Session(graph=self.detection_graph)
# Definite input and output Tensors for detection_graph
self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
self.detection_boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
self.detection_scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
self.num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')
def process_frame(self, image):
2019-04-01 22:30:26 +00:00
# Expand dimensions since the trained_model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image, axis=0)
# Actual detection.
2019-04-09 20:05:04 +00:00
2019-04-01 22:30:26 +00:00
(boxes, scores, classes, num) = self.sess.run(
[self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections],
feed_dict={self.image_tensor: image_np_expanded})
im_height, im_width,_ = image.shape
boxes_list = [None for i in range(boxes.shape[1])]
for i in range(boxes.shape[1]):
2019-04-09 20:05:04 +00:00
boxes_list[i] = (
int(boxes[0, i, 0] * im_height),
int(boxes[0, i, 1] * im_width),
int(boxes[0, i, 2] * im_height),
int(boxes[0, i, 3] * im_width)
2019-04-09 20:05:04 +00:00
)
2019-04-01 22:30:26 +00:00
return boxes_list, scores[0].tolist(), [int(x) for x in classes[0].tolist()], int(num[0])
def close(self):
self.sess.close()
self.default_graph.close()
2019-04-09 20:05:04 +00:00
class Detector:
2019-06-10 10:50:21 +00:00
def __init__(self):
2019-06-13 14:40:33 +00:00
self.model_path = "./model.pb"
2019-04-09 20:05:04 +00:00
self.odapi = DetectorAPI(path_to_ckpt=self.model_path)
2019-06-27 17:55:59 +00:00
self.threshold = 0.6
2019-04-09 20:05:04 +00:00
2019-06-10 10:50:21 +00:00
def detect(self, stream):
cap = cv2.VideoCapture(stream)
2019-06-10 11:54:58 +00:00
img = None
2019-04-01 22:30:26 +00:00
r, img = cap.read()
2019-06-06 16:46:25 +00:00
if img is None:
return img
2019-07-18 12:46:16 +00:00
# scale the image down for faster processing
2019-06-27 17:55:59 +00:00
scale_percent = 60 # percent of original size
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
img = cv2.resize(img, dim)
2019-07-18 12:46:16 +00:00
# get the results from the net
boxes, scores, classes, num = self.odapi.process_frame(img)
2019-06-13 14:40:33 +00:00
res = False
2019-04-01 22:30:26 +00:00
for i in range(len(boxes)):
# Class 1 represents human
2019-07-18 12:46:16 +00:00
# draw recogniction boxes and return resulting image + true/false
if classes[i] == 1:
2019-04-09 20:05:04 +00:00
if scores[i] > self.threshold:
box = boxes[i]
cv2.rectangle(img, (box[1], box[0]), (box[3], box[2]), (255, 0, 0), 2)
2019-06-13 14:40:33 +00:00
res = True
2019-06-27 17:55:59 +00:00
return img, res
else:
2019-06-13 14:40:33 +00:00
res = False
2019-06-27 17:55:59 +00:00
return img, res