Remove use of tensorflow to reduce dependencies.

This commit is contained in:
Michael Pivato
2019-02-07 07:01:14 +10:30
parent 5b24c98ad7
commit 9fbb4310ac

View File

@@ -1,7 +1,8 @@
from GestureRecognition.handrecogniser import HandRecogniser from GestureRecognition.handrecogniser import HandRecogniser
import numpy as np import numpy as np
import cv2 import cv2
import tensorflow as tf # import tensorflow as tf
import multiprocessing as mp
class SimpleHandRecogniser(HandRecogniser): class SimpleHandRecogniser(HandRecogniser):
def __init__(self, frame): def __init__(self, frame):
@@ -95,25 +96,25 @@ class SimpleHandRecogniser(HandRecogniser):
# Taken the code straight from his example, as it works perfectly. This is specifically # Taken the code straight from his example, as it works perfectly. This is specifically
# from the load_inference_graph method that he wrote, and will load the graph into # from the load_inference_graph method that he wrote, and will load the graph into
# memory if one has not already been loaded for this object. # memory if one has not already been loaded for this object.
def load_inference_graph(self): # def load_inference_graph(self):
"""Loads a tensorflow model checkpoint into memory""" # """Loads a tensorflow model checkpoint into memory"""
if self.graph != None and self.sess != None: # if self.graph != None and self.sess != None:
# Don't load more than once, to save time... # # Don't load more than once, to save time...
return # return
PATH_TO_CKPT = '/Users/piv/Documents/Projects/car/GestureRecognition/frozen_inference_graph.pb' # PATH_TO_CKPT = '/Users/piv/Documents/Projects/car/GestureRecognition/frozen_inference_graph.pb'
# load frozen tensorflow model into memory # # load frozen tensorflow model into memory
detection_graph = tf.Graph() # detection_graph = tf.Graph()
with detection_graph.as_default(): # with detection_graph.as_default():
od_graph_def = tf.GraphDef() # od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: # with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read() # serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph) # od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='') # tf.import_graph_def(od_graph_def, name='')
sess = tf.Session(graph=detection_graph) # sess = tf.Session(graph=detection_graph)
self.graph = detection_graph # self.graph = detection_graph
self.sess = sess # self.sess = sess
# Source: Victor Dibia # Source: Victor Dibia
@@ -121,64 +122,73 @@ class SimpleHandRecogniser(HandRecogniser):
# Taken the code straight from his example, as it works perfectly. This is specifically # Taken the code straight from his example, as it works perfectly. This is specifically
# from the detect_hand method that he wrote, as other processing is required for the # from the detect_hand method that he wrote, as other processing is required for the
# hand recognition to work correctly. # hand recognition to work correctly.
def detect_hand_tensorflow(self, detection_graph, sess): # def detect_hand_tensorflow(self, detection_graph, sess):
""" Detects hands in a frame using a CNN # """ Detects hands in a frame using a CNN
detection_graph -- The CNN to use to detect the hand.
sess -- THe tensorflow session for the given graph
"""
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
img_expanded = np.expand_dims(self.img, axis=0)
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: img_expanded})
print('finished detection')
return np.squeeze(boxes), np.squeeze(scores)
# def detect_hand_opencv(self, detection_graph, sess):
# """Performs hand detection using a CNN from tensorflow using opencv.
# detection_graph -- The CNN to use to detect the hand. # detection_graph -- The CNN to use to detect the hand.
# sess -- THe tensorflow session for the given graph # sess -- THe tensorflow session for the given graph
# """ # """
# if self.img is None:
# return
# height = self.img.shape[0] # image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# width = self.img.shape[1]
# scale = 0.5 # detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# classes = None # detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
# net = cv2.dnn.readNetFromTensorflow(detection_graph, sess) # detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
# # width is scaled weirdly to ensure we keep tbe same ratio as the original image. # num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# net.setInput(cv2.dnn.blobFromImage(self.img, scale, size=(300, 300 * (width/height)), swapRB=True, crop=False))
# netOut = net.forward()
# # Format output to look same as tensorflow output. # img_expanded = np.expand_dims(self.img, axis=0)
# scores = []
# boxes = []
# for out in netOut: # (boxes, scores, classes, num) = sess.run(
# for detection in out[0,0]: # [detection_boxes, detection_scores, detection_classes, num_detections],
# scores.append(detection[2]) # feed_dict={image_tensor: img_expanded})
# boxes.append(detection[3], detection[4], detection[5], detection[6]) # print('finished detection')
# # Only doing first class as only trying to find the hand. # return np.squeeze(boxes), np.squeeze(scores)
# break
# return np.array(boxes), np.array(scores) def load_cv_net(self, graph_path, names_path):
"""Loads a tensorflow neural object detection network using openCV
Arguments
graph_path: Path to the tensorflow frozen inference graph (something.pb)
names_path: Path to the tensorflow (something.pbtext) file.
"""
self.net = cv2.dnn.readNetFromTensorflow(graph_path, names_path)
def detect_hand_opencv(self, detection_graph, sess):
"""Performs hand detection using a CNN from tensorflow using opencv.
detection_graph -- The CNN to use to detect the hand.
sess -- THe tensorflow session for the given graph
"""
if self.img is None:
return
height = self.img.shape[0]
width = self.img.shape[1]
scale = 0.5
classes = None
net = cv2.dnn.readNetFromTensorflow(detection_graph, sess)
# width is scaled weirdly to ensure we keep tbe same ratio as the original image.
net.setInput(cv2.dnn.blobFromImage(self.img, scale, size=(300, 300 * (width/height)), swapRB=True, crop=False))
netOut = net.forward()
# Format output to look same as tensorflow output.
scores = []
boxes = []
for out in netOut:
for detection in out[0,0]:
scores.append(detection[2])
boxes.append(detection[3], detection[4], detection[5], detection[6])
# Only doing first class as only trying to find the hand.
break
return np.array(boxes), np.array(scores)
def get_best_hand(self, boxes, scores, conf_thresh, nms_thresh): def get_best_hand(self, boxes, scores, conf_thresh, nms_thresh):
""" """
@@ -236,14 +246,14 @@ class SimpleHandRecogniser(HandRecogniser):
print('There is no image') print('There is no image')
return -1 return -1
# First cut out the frame using the neural network. # First cut out the frame using the neural network.
self.load_inference_graph() # self.load_inference_graph()
print("loaded inference graph") # print("loaded inference graph")
detections, scores = self.detect_hand_tensorflow(self.graph, self.sess) # detections, scores = self.detect_hand_tensorflow(self.graph, self.sess)
print("Getting best hand") # print("Getting best hand")
best_hand = self.get_best_hand(detections, scores, 0.7, 0.5) # best_hand = self.get_best_hand(detections, scores, 0.7, 0.5)
if best_hand is not None: # if best_hand is not None:
self.img = self.img[best_hand[0] - 30:best_hand[2] + 30, best_hand[1] - 30:best_hand[3] + 30] # self.img = self.img[best_hand[0] - 30:best_hand[2] + 30, best_hand[1] - 30:best_hand[3] + 30]
print('Attempting to use pure hand recognition') print('Attempting to use pure hand recognition')
self.img_hsv = cv2.cvtColor(self.img, cv2.COLOR_BGR2HSV) self.img_hsv = cv2.cvtColor(self.img, cv2.COLOR_BGR2HSV)
@@ -275,6 +285,13 @@ class SimpleHandRecogniser(HandRecogniser):
if x_end >= self.mask.shape[1]: if x_end >= self.mask.shape[1]:
x_end = self.mask.shape[1] - 1 x_end = self.mask.shape[1] - 1
# Could batch this function to execute on multiple cores? # Could batch this function to execute on multiple cores?
# Calc num CPUS.
# num_cores = mp.cpu_count()
# # Calc batch size:
# batch_size = x_end // num_cores
# for b in range(0, num_cores - 1):
# pass
for x in range(x_start, x_end): for x in range(x_start, x_end):
# Need to check circle is inside the bounds. # Need to check circle is inside the bounds.
ypos = self.__calc_pos_y(x, radius, centre) ypos = self.__calc_pos_y(x, radius, centre)