概述
最近博主参加一个小比赛,需要用到目标检测从而进行固定位置的人物经检查,看某位置是否有人,和行人检测不同,大部分的人是坐着的,而且被遮挡的比较多,所以用行人检测是行不通的。于是就想到用Tensorflow自带的object_detection模块进行二次开发,实现监测固定位置是否有人。首先安装环境的搭建,可以参考这篇博客,那么如果你不想制作数据集而且想只更改一下源码来实现自己目的,那么就往下看吧。如果要自己训练,那么就移步这里
更改代码
在object_detection文件的目录下有一个非常重要的文件”object_detection_tutorial.py“,这就是核心API,使用后效果图如下:
如果我们只想显示人,或者所想要得事物并返回他们的位置怎么做呢?
我们去看开代码这一部分
image = Image.open(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
# 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.
print(classes)
print(num_detections)
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)
plt.figure(figsize=IMAGE_SIZE)
plt.imshow(image_np)
plt.show()
通过上述代码,发现一个核心函数” vis_util.visualize_boxes_and_labels_on_image_array“在同级目录”utils“文件夹下的"visualization_utils.py"里;
下面是代码
def visualize_boxes_and_labels_on_image_array(
image,
boxes,
classes,
scores,
category_index,
instance_masks=None,
instance_boundaries=None,
keypoints=None,
use_normalized_coordinates=False,
max_boxes_to_draw=20,
min_score_thresh=.5,
agnostic_mode=False,
line_thickness=4,
groundtruth_box_visualization_color='black',
skip_scores=False,
skip_labels=False):
box_to_display_str_map = collections.defaultdict(list)
box_to_color_map = collections.defaultdict(str)
box_to_instance_masks_map = {
}
box_to_instance_boundaries_map = {
}
box_to_keypoints_map = collections.defaultdict(list)
if not max_boxes_to_draw:
max_boxes_to_draw = boxes.shape[0]
for i in range(min(max_boxes_to_draw, boxes.shape[0])):
if scores is None or scores[i] > min_score_thresh:
box = tuple(boxes[i].tolist())
if instance_masks is not None:
box_to_instance_masks_map[box] = instance_masks[i]
if instance_boundaries is not None:
box_to_instance_boundaries_map[box] = instance_boundaries[i]
if keypoints is not None:
box_to_keypoints_map[box].extend(keypoints[i])
if scores is None:
box_to_color_map[box] = groundtruth_box_visualization_color
else