我训练了两个tf模型,第一个是检测车牌,第二个是识别被检测车牌的号码。现在,我想将它们组合到同一个python文件中。
我曾尝试将两个模型一起加载到python文件中,但是有一些错误,这是与使用单一识别模型相比,识别结果确实很糟糕,我想原因是两个加载方法不正确冻结图,那么有谁能告诉我如何正确加载两个冻结图。
代码:
PATH_TO_FROZEN_GRAPH ='assets/plate_model/frozen_inference_graph.pb'
# List of the strings that is used to add correct label for eachbox.
PATH_TO_LABELS = 'assets/plate_model/label.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
detection_graph = tf.Graph().as_default()
od_graph_def = tf.GraphDef()
fid= tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb')
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='detection')
sess1 = tf.Session(graph=detection_graph)###detection graph
from recognition_v2 import run,recognition
sess=run()###recognition graph
# Get handles to input and output tensors
ops = tf.get_default_graph().get_operations()
all_tensor_names = {output.name for op in ops for output in op.outputs}
tensor_dict = {}
for key in [
'num_detections', 'detection_boxes', 'detection_scores',
'detection_classes', 'detection_masks'
]:
tensor_name = key + ':0'
if tensor_name in all_tensor_names:
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
tensor_name)
if 'detection_masks' in tensor_dict:
# The following processing is only for single image
detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
detection_masks, detection_boxes, image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(
tf.greater(detection_masks_reframed, 0.5), tf.uint8)
# Follow the convention by adding back the batch dimension
tensor_dict['detection_masks'] = tf.expand_dims(
detection_masks_reframed, 0)
image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
n=0
for i in [1]:
cam = cv2.VideoCapture(f'../../input/ferry_{i}.mp4')
# cam.set(cv2.CAP_PROP_FRAME_WIDTH,1920)
# cam.set(cv2.CAP_PROP_FRAME_HEIGHT,1080)
width = int(cam.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cam.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cam.get(cv2.CAP_PROP_FPS)
fps_time = 0
count = 0
has_plate_cnt=0
cc=0
fps1 = cam.get(cv2.CAP_PROP_FPS)
while True:
success, image = cam.read()
cc+=1
if not success:
break
image_h = image.shape[0]
image_w = image.shape[1]
count += 1
if has_plate_cnt > 0:
has_plate_cnt -= 1
# print(count)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# image = cv2.flip(image, 1)
# Run inference
output_dict = sess1.run(tensor_dict,
feed_dict={image_tensor: np.expand_dims(image, 0)})
# all outputs are float32 numpy arrays, so convert types as appropriate
output_dict['num_detections'] = int(output_dict['num_detections'][0])
output_dict['detection_classes'] = output_dict[
'detection_classes'][0].astype(np.uint8)
output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
output_dict['detection_scores'] = output_dict['detection_scores'][0]
if 'detection_masks' in output_dict:
output_dict['detection_masks'] = output_dict['detection_masks'][0]
plates = []
p_chars = []
for i, score in enumerate(output_dict['detection_scores']):
if score > 0.8:
char_name = category_index[output_dict['detection_classes'][i]]['name']
ymin, xmin, ymax, xmax = tuple(output_dict['detection_boxes'][i].tolist())
ymin = int(ymin * image_h)
xmin = int(xmin * image_w)
ymax = int(ymax * image_h)
xmax = int(xmax * image_w)
cv2.rectangle(image,(xmin,ymin-1),(xmax,ymax+2),(0,255,0),2)
if char_name == 'plate':
plates.append([ymin, xmin, ymax, xmax])
p_chars.append([])
if has_plate_cnt == 0:
plate_image = cv2.cvtColor(image[ymin-1:ymax+2,xmin:xmax], cv2.COLOR_BGR2RGB)
# shutil.rmtree(f'../../output/data/')
# os.mkdir(f'../../output/data/')
# cv2.imwrite(f'../../output/data/{init_time}{int(27+cc//fps1)}.jpg', plate_image)
cv2.imwrite(f'../../output/data/1.jpg', plate_image)
plate_image=cv2.imread(f'../../output/data/1.jpg')
recognition(sess,plate_image)########recognition