一、对图像
import numpy as np import tensorflow as tf from PIL import Image from tensorflow.models.research.object_detection.utils import label_map_util from tensorflow.models.research.object_detection.utils import visualization_utils as vis_util ''' 1. 导入模型 2. 导入标签图像 3. 图像转换分析 4. 打印图像 ''' PB_PATH = 'ssd_inception_v2_coco_2018_01_28/frozen_inference_graph.pb' LABEL_PATH = 'data/mscoco_label_map.pbtxt' od_graph = tf.Graph() with od_graph.as_default(): od_graph_def = tf.GraphDef() # 打开文件,rb是读取二进制,同open() with tf.gfile.FastGFile(PB_PATH, 'rb') as f: od_graph_def.ParseFromString(f.read()) tf.import_graph_def(od_graph_def, name="") category_index = label_map_util.create_category_index_from_labelmap(LABEL_PATH) test_images_path = ["test_images/image{0}.jpg".format(i) for i in range(1, 4)] def image_to_np(image): (im_w, im_h) = image.size return np.array(image.getdata()).reshape(im_h, im_w, 3).astype(np.uint8) def run_inference_for_single_image(image): with od_graph.as_default(): with tf.Session() as sess: tensor_dict = {} tensor_dict['num_detections'] = tf.get_default_graph().get_tensor_by_name('num_detections:0') tensor_dict['detection_boxes'] = tf.get_default_graph().get_tensor_by_name('detection_boxes:0') tensor_dict['detection_scores'] = tf.get_default_graph().get_tensor_by_name('detection_scores:0') tensor_dict['detection_classes'] = tf.get_default_graph().get_tensor_by_name('detection_classes:0') image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0') output_dict = sess.run(tensor_dict, feed_dict={image_tensor: image}) output_dict['num_detections'] = int(output_dict['num_detections'][0]) output_dict['detection_boxes'] = output_dict['detection_boxes'][0] output_dict['detection_scores'] = output_dict['detection_scores'][0] output_dict['detection_classes'] = output_dict['detection_classes'][0].astype(np.int64) return output_dict for image_path in test_images_path: # 打开图片 转成np格式,并扩展一维 image = Image.open(image_path) image_np = image_to_np(image) image_np_dim = np.expand_dims(image_np, axis=0) output_dict = run_inference_for_single_image(image_np_dim) vis_util.visualize_boxes_and_labels_on_image_array( image_np, output_dict['detection_boxes'], output_dict['detection_classes'], output_dict['detection_scores'], category_index, use_normalized_coordinates=True) img = Image.fromarray(image_np, 'RGB') img.show()
二、对视频
import os import time import argparse import multiprocessing import numpy as np import tensorflow as tf import tarfile from matplotlib import pyplot as plt from object_detection.utils import label_map_util from object_detection.utils import visualization_utils as vis_util ''' 视频目标追踪 ''' #1.得到模型 (这里首先下载流模型并在解压在path/to/models/research/object_detection里面) MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17' PATH_TO_CKPT = os.path.join(MODEL_NAME, 'frozen_inference_graph.pb') PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt') print('Loading model...') #load frozen of tensorflow to memeory detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: #文本操作句柄,类似python里面的open() serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') #将图像从od_graph_def导入当前的默认Graph #label map to class name 如预测为5,知道它是对应飞机 NUM_CLASS = 90 print("Loading label map...") label_map = label_map_util.load_labelmap(PATH_TO_LABELS) #得到label map proto categories = label_map_util.convert_label_map_to_categories(label_map, NUM_CLASS) #得到类别 category_index = label_map_util.create_category_index(categories) #2.对视频进行物体检测 def detect_objects(image_np, sess, detection_graph): image_np_expanded = np.expand_dims(image_np, axis=0) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') boxes = detection_graph.get_tensor_by_name('detection_boxes:0') scores = detection_graph.get_tensor_by_name('detection_scores:0') classes = detection_graph.get_tensor_by_name('detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0') #Actual detection (boxes, scores, classes, num_detections) = sess.run( [boxes, scores, classes, num_detections], feed_dict={image_tensor : image_np_expanded}) #Visualization of the results of a detection vis_util.visualize_boxes_and_labels_on_image_array(image_np, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=8) return image_np from moviepy.editor import VideoFileClip from IPython.display import HTML def process_image(image): with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: image_process = detect_objects(image, sess, detection_graph) return image_process white_output = 'video/out.mp4' clip1 = VideoFileClip("video/testvideo.mp4") white_clip = clip1.fl_image(process_image) #This function expects color images! white_clip.write_videofile(white_output, audio=False)
三、电脑摄像头
import cv2 import numpy as np import tensorflow as tf from PIL import Image from tensorflow.models.research.object_detection.utils import label_map_util from tensorflow.models.research.object_detection.utils import visualization_utils as vis_util camera = cv2.VideoCapture(0) cv2.namedWindow('MyCamera') PB_PATH = 'ssd_inception_v2_coco_2018_01_28/frozen_inference_graph.pb' LABEL_PATH = 'data/mscoco_label_map.pbtxt' od_graph = tf.Graph() with od_graph.as_default(): od_graph_def = tf.GraphDef() # 打开文件,rb是读取二进制,同open() with tf.gfile.FastGFile(PB_PATH, 'rb') as f: od_graph_def.ParseFromString(f.read()) tf.import_graph_def(od_graph_def, name="") category_index = label_map_util.create_category_index_from_labelmap(LABEL_PATH) test_images_path = ["test_images/image{0}.jpg".format(i) for i in range(1, 4)] def image_to_np(image): (im_w, im_h) = image.size return np.array(image.getdata()).reshape(im_h, im_w, 3).astype(np.uint8) def run_inference_for_single_image(image): with od_graph.as_default(): with tf.Session() as sess: tensor_dict = {} tensor_dict['num_detections'] = tf.get_default_graph().get_tensor_by_name('num_detections:0') tensor_dict['detection_boxes'] = tf.get_default_graph().get_tensor_by_name('detection_boxes:0') tensor_dict['detection_scores'] = tf.get_default_graph().get_tensor_by_name('detection_scores:0') tensor_dict['detection_classes'] = tf.get_default_graph().get_tensor_by_name('detection_classes:0') image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0') output_dict = sess.run(tensor_dict, feed_dict={image_tensor: image}) output_dict['num_detections'] = int(output_dict['num_detections'][0]) output_dict['detection_boxes'] = output_dict['detection_boxes'][0] output_dict['detection_scores'] = output_dict['detection_scores'][0] output_dict['detection_classes'] = output_dict['detection_classes'][0].astype(np.int64) return output_dict while True: success, image = camera.read() image_np_dim = np.expand_dims(image, axis=0) output_dict = run_inference_for_single_image(image_np_dim) vis_util.visualize_boxes_and_labels_on_image_array( image, output_dict['detection_boxes'], output_dict['detection_classes'], output_dict['detection_scores'], category_index, use_normalized_coordinates=True) cv2.imshow('object detection', cv2.resize(image, (800,600))) if cv2.waitKey(1) & 0xff == ord('q'): break cv2.destroyWindow('MyCamera') camera.release()
如果报错,检查一下路径名是否有中文,检查一下摄像头权限是否打开。