google開(kāi)源了基于深度學(xué)習(xí)的物體識(shí)別模型和python API啤它。
- 模型 Tensorflow detection model zoo :不同的模型在效率與準(zhǔn)確性上有區(qū)別,訓(xùn)練數(shù)據(jù)集市微軟的COCO
- python api: Tensorflow Object Detection API
Tensorflow Object Detection API 效果圖片
google的api是用于圖片物體識(shí)別的岛啸,但是只需要做三項(xiàng)修改就可以完成實(shí)時(shí)物體檢測(cè)。更詳細(xì)請(qǐng)參考 Dat Tran的文章
- API結(jié)構(gòu)微調(diào)娃殖;
- 多線(xiàn)程值戳,讀取視頻流议谷;
- 多進(jìn)程炉爆,加載物體識(shí)別模型;
API結(jié)構(gòu)微調(diào)
import os
import cv2
import numpy as np
import multiprocessing
from multiprocessing import Queue, Pool
# tensorflow api 接口相關(guān)函數(shù)
import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
# 模型路徑
PATH_TO_CKPT = '../object_detection/ssd_mobilenet_v1_coco_11_06_2017/frozen_inference_graph.pb')
# label字典路徑卧晓,用于識(shí)別出物品后展示類(lèi)別名
PATH_TO_LABELS = '../object_detection/data/mscoco_label_map.pbtxt'
NUM_CLASSES = 90 # 最大分類(lèi)數(shù)量
label_map = label_map_util.load_labelmap(PATH_TO_LABELS) # 獲得類(lèi)別字典
categories = label_map_util.convert_label_map_to_categories(
label_map,
max_num_classes=NUM_CLASSES,
use_display_name=True)
category_index = label_map_util.create_category_index(categories)
# 物體識(shí)別神經(jīng)網(wǎng)絡(luò)芬首,向前傳播獲得識(shí)別結(jié)果
def detect_objects(image_np, sess, detection_graph):
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
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.
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.
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=3)
return image_np
多線(xiàn)程,讀取視頻流
更多資料參考 Increasing webcam FPS with Python and OpenCV
import cv2
from threading import Thread
# 多線(xiàn)程逼裆,高效讀視頻
class WebcamVideoStream:
def __init__(self, src, width, height):
# initialize the video camera stream and read the first frame
# from the stream
self.stream = cv2.VideoCapture(src)
self.stream.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
(self.grabbed, self.frame) = self.stream.read()
# initialize the variable used to indicate if the thread should
# be stopped
self.stopped = False
def start(self):
# start the thread to read frames from the video stream
Thread(target=self.update, args=()).start()
return self
def update(self):
# keep looping infinitely until the thread is stopped
while True:
# if the thread indicator variable is set, stop the thread
if self.stopped:
return
# otherwise, read the next frame from the stream
(self.grabbed, self.frame) = self.stream.read()
def read(self):
# return the frame most recently read
return self.frame
def stop(self):
# indicate that the thread should be stopped
self.stopped = True
# 使用方法
video_capture = WebcamVideoStream(src=video_source,
width=width,
height=height).start()
frame = video_capture.read()
多進(jìn)程郁稍,加載物體識(shí)別模型
- 配置參數(shù)
class configs(object): def __init__(self): self.num_workers = 2 # worker數(shù)量 self.queue_size = 5 # 多進(jìn)程,輸入輸出胜宇,隊(duì)列長(zhǎng)度 self.video_source = 0 # 0代表從攝像頭讀取視頻流 self.width = 720 # 圖片寬 self.height = 490 # 圖片高 args = configs()
- 定義用于多進(jìn)程執(zhí)行的函數(shù)word耀怜,每個(gè)進(jìn)程執(zhí)行work函數(shù),都會(huì)加載一次模型
def worker(input_q, output_q): detection_graph = tf.Graph() with detection_graph.as_default(): # 加載模型 od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') sess = tf.Session(graph=detection_graph) while True: # 全局變量input_q與output_q定義桐愉,請(qǐng)看下文 frame = input_q.get() # 從多進(jìn)程輸入隊(duì)列财破,取值 output_q.put(detect_objects(frame, sess, detection_graph)) # detect_objects函數(shù) 返回一張圖片,標(biāo)記所有被發(fā)現(xiàn)的物品 sess.close()
- 多進(jìn)程 Queue 文檔 (Exchanging objects between processes)
import multiprocessing input_q = Queue(maxsize=args.queue_size) # 多進(jìn)程輸入隊(duì)列 output_q = Queue(maxsize=args.queue_size) # 多進(jìn)程輸出隊(duì)列 pool = Pool(args.num_workers, worker, (input_q, output_q)) # 多進(jìn)程加載模型 video_capture = WebcamVideoStream(src=args.video_source, width=args.width, height=args.height).start() while True: frame = video_capture.read() # video_capture多線(xiàn)程讀取視頻流 input_q.put(frame) # 視頻幀放入多進(jìn)程輸入隊(duì)列 frame = output_q.get() # 多進(jìn)程輸出隊(duì)列取出標(biāo)記好物體的圖片 cv2.imshow('Video', frame) # 展示已標(biāo)記物體的圖片 if cv2.waitKey(1) & 0xFF == ord('q'): break pool.terminate() # 關(guān)閉多進(jìn)程 video_capture.stop() # 關(guān)閉視頻流 cv2.destroyAllWindows() # opencv窗口關(guān)閉
簡(jiǎn)單測(cè)試