我正在尝试使用tensorflow对象检测api和raspberry pi 2在视频帧中检测人。视频输入来自ip摄像机。 运行代码时出现以下错误。
2019-04-07 13:57:17.996374: W
tensorflow/core/framework/allocator.cc:124] Allocation of 5660928
exceeds 10% of system memory.
2019-04-07 13:57:18.096145: W
tensorflow/core/framework/allocator.cc:124] Allocation of 25159680
exceeds 10% of system memory.
2019-04-07 13:57:18.208663: W
tensorflow/core/framework/allocator.cc:124] Allocation of 10063872
exceeds 10% of system memory.
2019-04-07 13:57:18.260229: W
tensorflow/core/framework/allocator.cc:124] Allocation of 5031936
exceeds 10% of system memory.
2019-04-07 13:57:18.293027: W
tensorflow/core/framework/allocator.cc:124] Allocation of 5031936
exceeds 10% of system memory.
Killed
我已按照本教程在pi上安装tensorflow和opencv
https://www.youtube.com/watch?v=npZ-8Nj1YwY
这是我正在使用的代码
import numpy as np
import tensorflow as tf
import cv2
import time
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 processFrame(self, image):
# 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.
start_time = time.time()
(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})
end_time = time.time()
print("Elapsed Time:", end_time-start_time)
im_height, im_width,_ = image.shape
boxes_list = [None for i in range(boxes.shape[1])]
for i in range(boxes.shape[1]):
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))
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()
if __name__ == "__main__":
model_path ='/home/pi/tensorflow1/models/research/object_detection/ssd_mobilenet_v2_coco_2018_03_29/frozen_inference_graph.pb'
odapi = DetectorAPI(path_to_ckpt=model_path)
threshold = 0.7
cap = cv2.VideoCapture('rtsp://vamshi@123:vikas123@192.168.0.5:88/videoMain')
while True:
r, img = cap.read()
img = cv2.resize(img, (500, 400))
cv2.line(img,(0,400 // 2),(500 ,400 // 2),(0,255,255),2)
cv2.line(img,(500 // 2,0),(500 // 2,400),(0,255,255),2)
boxes, scores, classes, num = odapi.processFrame(img)
# Visualization of the results of a detection.
for i in range(len(boxes)):
# Class 1 represents human
if classes[i] == 1 and scores[i] > threshold:
box = boxes[i]
cv2.rectangle(img,(box[1],box[0]),(box[3],box[2]),(255,255,0),2)
cX=int((box[1] + box[3]) / 2.0)
cY=int((box[0] + box[2]) / 2.0)
cv2.circle(img,(cX,cY),4,(0,255,0),-1)
if cX < 500 // 2 and cY < 400 //2:
print ('Quadrant 1')
elif cX < 500 // 2 and cY > 400 //2:
print ('Quadrant 3')
elif cX > 500 // 2 and cY < 400 //2:
print ('Quadrant 2')
else:
print ('Quadrant 4')
cv2.imshow("Human Detection", img)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
Tensorflow版本:1.13.1
OpenCV版本:3.4.4