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.
    rect_points, class_names, class_colors = draw_boxes_and_labels(
        boxes=np.squeeze(boxes),
        classes=np.squeeze(classes).astype(np.int32),
        scores=np.squeeze(scores),
        category_index=category_index,
        min_score_thresh=.5
    )
    return dict(rect_points=rect_points, class_names=class_names, class_colors=class_colors)
Пример #2
0
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.
    rect_points, class_names, class_colors = draw_boxes_and_labels(
        boxes=np.squeeze(boxes),
        classes=np.squeeze(classes).astype(np.int32),
        scores=np.squeeze(scores),
        category_index=category_index,
        min_score_thresh=.5)
    return dict(rect_points=rect_points,
                class_names=class_names,
                class_colors=class_colors)
def detect_objects(image_np, sess, detection_graph, category_index):
	# 扩展图像的尺寸, 因为模型要求图像具有形状: [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')
	# 检测结果,每个框表示检测到特定对象的图像的一部分
	boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
	# scores表示识别出的物体的置信度 是一个概率值,他最后会与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')

	# 运行会话开始检测并返回结果,feed为传入的图像张量
	(boxes, scores, classes, num_detections) = sess.run(
		[boxes, scores, classes, num_detections],
		feed_dict={image_tensor: image_np_expanded})

	# 检测结果的可视化,也就是将返回的框框画在图像上
	# 返回框坐标、类名和颜色
	rect_points, class_names, class_colors = draw_boxes_and_labels(
		boxes=np.squeeze(boxes),
		classes=np.squeeze(classes).astype(np.int32),
		scores=np.squeeze(scores),
		category_index=category_index,
		min_score_thresh=.3  # 可视化的最低分数阈值
	)

	return dict(rect_points=rect_points, class_names=class_names, class_colors=class_colors)
Пример #4
0
def detect_objects(image_np, sess, detection_graph, width, height, one_width,
                   one_height, index, rect_points, class_names, class_colors):
    #print('1'+str(rect_points))
    if index % 3 == 0:
        # 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.
        rect_points, class_names, class_colors = draw_boxes_and_labels(
            boxes=np.squeeze(boxes),
            classes=np.squeeze(classes).astype(np.int32),
            scores=np.squeeze(scores),
            category_index=category_index,
            min_score_thresh=.5)

    #print('2'+str(rect_points))
    font = cv2.FONT_HERSHEY_SIMPLEX
    for point, name, color in zip(rect_points, class_names, class_colors):
        if point['xmax'] - point['xmin'] < 0.4:
            cv2.rectangle(
                image_np,
                (int(point['xmin'] * width), int(point['ymin'] * height)),
                (int(point['xmax'] * width), int(point['ymax'] * height)),
                color, 3)
            cv2.rectangle(
                image_np,
                (int(point['xmin'] * width), int(point['ymin'] * height)),
                (int(point['xmin'] * width) + len(name[0]) * 6,
                 int(point['ymin'] * height) - 10), color, -1, cv2.LINE_AA)
            cv2.putText(
                image_np, name[0],
                (int(point['xmin'] * width), int(point['ymin'] * height)),
                font, 0.3, (0, 0, 0), 1)
    return cv2.resize(
        image_np,
        (one_width, one_height)), rect_points, class_names, class_colors
Пример #5
0
    def get_CNN_output(self, image_np):
        # 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 = self.detection_graph.get_tensor_by_name(
            'image_tensor:0')

        boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
        scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
        classes = self.detection_graph.get_tensor_by_name(
            'detection_classes:0')
        num_detections = self.detection_graph.get_tensor_by_name(
            'num_detections:0')
        masks = self.detection_graph.get_tensor_by_name('detection_masks:0')

        # Actual detection.
        (boxes, scores, classes, num_detections, masks) = \
            self.sess.run([boxes, scores, classes, num_detections, masks], feed_dict={image_tensor: image_np_expanded})

        # Visualization of the results of a detection.
        rect_points, class_scores, class_colors = draw_boxes_and_labels(
            boxes=np.squeeze(boxes),
            classes=np.squeeze(classes).astype(np.int32),
            scores=np.squeeze(scores),
            min_score_thresh=.3)
        ma_alpha = 0.1
        for rp, cs, ma in zip(rect_points, class_scores, masks[0]):
            abs_xmin = int(rp['xmin'] * self.slice_size[1] + self.offset[1])
            abs_ymin = int(rp['ymin'] * self.slice_size[0] + self.offset[0])
            abs_xmax = np.min(
                (int(rp['xmax'] * self.slice_size[1] + self.offset[1]),
                 self.frame_shape[1]))
            abs_ymax = np.min(
                (int(rp['ymax'] * self.slice_size[0] + self.offset[0]),
                 self.frame_shape[0]))
            if abs_xmax <= abs_xmin or abs_ymax <= abs_ymin:
                continue
            ma_overlay = np.zeros(self.frame_shape[:2], dtype=np.uint8)
            ma_overlay[abs_ymin:abs_ymax, abs_xmin:abs_xmax] = cs[0]
            self.moving_avg_image = cv2.addWeighted(ma_overlay, ma_alpha,
                                                    self.moving_avg_image,
                                                    1 - ma_alpha, 0)
            self.detections.append(
                dict(rel_rect=rp,
                     score=cs,
                     abs_rect=(abs_xmin, abs_ymin, abs_xmax, abs_ymax),
                     mask=ma))
Пример #6
0
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')

    (boxes, scores, classes,
     num_detections) = sess.run([boxes, scores, classes, num_detections],
                                feed_dict={image_tensor: image_np_expanded})

    rect_points, class_names, class_colors = draw_boxes_and_labels(
        boxes=np.squeeze(boxes),
        classes=np.squeeze(classes).astype(np.int32),
        scores=np.squeeze(scores),
        category_index=category_index,
        min_score_thresh=.5)
    return dict(rect_points=rect_points,
                class_names=class_names,
                class_colors=class_colors)
Пример #7
0
def detect_objects(image_np, sess, detection_graph):
    global detectioncounter
    global emptyframecounter
    global pause
    # 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})

    #comine results form cv to be processed later
    combined = list(
        map(lambda x, y, z: [float(x), float(y),
                             z.tolist()], scores[0], classes[0], boxes[0]))
    bigger_than_threshold = list(
        filter(lambda x: x[0] >= MIN_THRESHOLD, combined))

    #when objects are recognized, it is counted which objects are there.
    # also detectioncounter is counted up. If this 10 was overruled, we got the object counter.
    #If no object is selected and it expires
    #the objectCounter and detectioncounter are set to 0.
    if pause > 0:  #pause detection
        pause -= 1
    else:
        if len(bigger_than_threshold) != 0:
            countObjects(bigger_than_threshold)
            detectioncounter += 1
            emptyframecounter = 0
            if detectioncounter > TRIGGER_TIME:
                evaluateObjectcounter()
                detectioncounter = 0

        else:
            emptyframecounter += 1
            if emptyframecounter > RESET_TIME:
                for i in range(0, 11):
                    objectCounter[i][0] = 0
                detectioncounter = 0
                emptyframecounter = 0

    # Visualization of the results of a detection.
    rect_points, class_names, class_colors = draw_boxes_and_labels(
        boxes=np.squeeze(boxes),
        classes=np.squeeze(classes).astype(np.int32),
        scores=np.squeeze(scores),
        category_index=category_index,
        min_score_thresh=MIN_THRESHOLD)
    return dict(rect_points=rect_points,
                class_names=class_names,
                class_colors=class_colors)