如何添加具有所需点数的叠加层?

时间:2017-10-18 10:43:43

标签: python object-detection dlib

我正在尝试训练形状预测器并面临一个问题,即 public struct uHalfByte : IFormattable, IComparable<uHalfByte>, IConvertible, INumber<uHalfByte> { ... public uHalfByte ConvertGeneric<T>(T item) { if (typeof(T) == typeof(int)) { return new uHalfByte((byte)(int)Convert.ChangeType(item, typeof(int))); } else if (typeof(T) == typeof(uint)) { return new uHalfByte((byte)(uint)Convert.ChangeType(item, typeof(uint))); } else if (typeof(T) == typeof(long)) { return new uHalfByte((byte)(long)Convert.ChangeType(item, typeof(long))); } else if (typeof(T) == typeof(ulong)) { return new uHalfByte((byte)(ulong)Convert.ChangeType(item, typeof(ulong))); } else if (typeof(T) == typeof(short)) { return new uHalfByte((byte)(short)Convert.ChangeType(item, typeof(short))); } else if (typeof(T) == typeof(ushort)) { return new uHalfByte((byte)(ushort)Convert.ChangeType(item, typeof(ushort))); } else if (typeof(T) == typeof(byte)) { return new uHalfByte((byte)Convert.ChangeType(item, typeof(byte))); } else if (typeof(T) == typeof(sbyte)) { return new uHalfByte((byte)(sbyte)Convert.ChangeType(item, typeof(sbyte))); } else throw new NotSupportedException(string.Format("Type {0} is not supported, you have to write your own function!", typeof(T))); } } 功能需要68分中的5分。那么,如何添加46个叠加? 这是代码,它几乎与文档中的example相同。

add_overlay

输出日志:

#!/usr/bin/python
import os
import sys
import glob

import dlib
from skimage import io



if len(sys.argv) != 2:
    print(
        "Give the path to the examples/faces directory as the argument to this "
        "program. For example, if you are in the python_examples folder then "
        "execute this program by running:\n"
        "    ./train_shape_predictor.py ../examples/faces")
    exit()
faces_folder = sys.argv[1]

options = dlib.shape_predictor_training_options()

options.oversampling_amount = 500

options.tree_depth = 2
options.be_verbose = True

training_xml_path = os.path.join(faces_folder, "women_test.xml")
dlib.train_shape_predictor(training_xml_path, "predictor.dat", options)

print("\nTraining accuracy: {}".format(
    dlib.test_shape_predictor(training_xml_path, "predictor.dat")))

predictor = dlib.shape_predictor("predictor.dat")
detector = dlib.simple_object_detector("detector.svm")


print("Showing detections and predictions on the images in the objects folder...")
win = dlib.image_window()
for f in glob.glob(os.path.join(faces_folder, "*.jpg")):
    print("Processing file: {}".format(f))
    img = io.imread(f)

    win.clear_overlay()
    win.set_image(img)

    dets = detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))
    for k, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))
        shape = predictor(img, d)
        print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
                                                  shape.part(1)))
        win.add_overlay(shape)

    win.add_overlay(dets)
    dlib.hit_enter_to_continue()

1 个答案:

答案 0 :(得分:2)

您正在使用dlib窗口检查检测到的点数是5还是68.

在你的情况下你有46分。您需要在cv2窗口上显示图像。

def annotate_landmarks(image, landmarks):
"""
Given image and a set of landmark points, annotates the points for viewing
:param image: Input image
:type image: np.array
:param landmarks: set of facial landmark points
:type landmarks: [(float, float)]
:return: Resulting annotated image
:rtype: np.array
"""
image = image.copy()
for idx, point in enumerate(landmarks):
    pos = (point[0, 0], point[0, 1])
    cv2.putText(image, str(idx), pos,
                fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
                fontScale=0.4,
                color=(0, 0, 255))
    cv2.circle(image, pos, 3, color=(0, 255, 255))
return image

现在使用annotate函数显示结果。

new_img = img
for k, d in enumerate(dets):
    shape = predictor(new_img, d)
    new_img = annotate_landmarks(new_img, shape)

cv2.imshow(new_image)
cv2.waitkey()

注意:此功能现在可能直接插入您的要求。检查传入shape函数

annotate_landmarks类型