无法多次使用tensorflow会话执行函数



我有一个python文件,它从另一个使用tensorflow会话的python文件中导入一个函数,如下所示:

counting.py

import requests
import time
import os
import tensorflow as tf
# Object detection imports
from utils import backbone
from api import object_counting_api
def count_object():
input_video = "img.jpg"
detection_graph, category_index = backbone.set_model('My_graph', 'detection.pbtxt')
is_color_recognition_enabled = 0
tf.reset_default_graph()
result = object_counting_api.single_image_object_counting(input_video, detection_graph, category_index, is_color_recognition_enabled)
print(result)
time.sleep(2.4)
while True:
count_object()

结果是通过python文件object_coutning_api中的函数-single_image_object_counting计算的功能如下所示:

import tensorflow as tf
import csv
import cv2
import numpy as np
from utils import visualization_utils as vis_util
def single_image_object_counting(input_video, detection_graph, category_index, is_color_recognition_enabled):     
counting_mode = "..."
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
# Definite input and output Tensors for detection_graph
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
detection_boxes = 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.
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')            

input_frame = cv2.imread(input_video)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(input_frame, axis=0)
# Actual detection.
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# insert information text to video frame
font = cv2.FONT_HERSHEY_SIMPLEX
# Visualization of the results of a detection.        
counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_single_image_array(1,input_frame,
                        1,
                        is_color_recognition_enabled,
                        np.squeeze(boxes),
                        np.squeeze(classes).astype(np.int32),
                        np.squeeze(scores),
                        category_index,
                        use_normalized_coordinates=True,
                        line_thickness=4)
if(len(counting_mode) == 0):
cv2.putText(input_frame, "...", (10, 35), font, 0.8, (0,255,255),2,cv2.FONT_HERSHEY_SIMPLEX)                       
else:
cv2.putText(input_frame, counting_mode, (10, 35), font, 0.8, (0,255,255),2,cv2.FONT_HERSHEY_SIMPLEX)
#cv2.imshow('tensorflow_object counting_api',input_frame)        
cv2.waitKey(0)
return counting_mode
sess.close()

计数模式第一次返回计数,但未能再次执行。我正在监视一个不时覆盖其映像的目录。我需要执行counting.py文件一次,并更快地获得结果,因为重新启动python文件将花费更多时间,因为它将一次又一次地启动Tensorflow。如果有人能给出一个多次运行函数的解决方案,那将是一个很大的帮助。

其输出为:

'defected:': 1, 'perfect:': 1
(program doesn't end)

我试图在single_image_counting函数结束时关闭会话,但输出保持不变。

关键是在初始化张量、运行会话以及每次运行时重新初始化所有变量时使用xrange的for循环。

with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
tf.initialize_all_variables().run()
# Definite input and output Tensors for detection_graph
for time in xrange(1,2):
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
detection_boxes = 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.
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')            

input_frame = cv2.imread(input_video)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(input_frame, axis=0)
# Actual detection.
for num in xrange(1,3):
print(num)
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
font = cv2.FONT_HERSHEY_SIMPLEX

相关内容

  • 没有找到相关文章