Python 如何添加具有所需点数的覆盖?
我正在尝试训练形状预测器,但遇到了一个问题,Python 如何添加具有所需点数的覆盖?,python,object-detection,dlib,Python,Object Detection,Dlib,我正在尝试训练形状预测器,但遇到了一个问题,add\u overlay功能需要68分中的5分。那么,如何添加46点的叠加? 这是代码,与文档中的代码几乎相同 #!/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 di
add\u overlay
功能需要68分中的5分。那么,如何添加46点的叠加?
这是代码,与文档中的代码几乎相同
#!/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()
输出日志:
Training with cascade depth: 10
Training with tree depth: 2
Training with 500 trees per cascade level.
Training with nu: 0.05
Training with random seed:
Training with oversampling amount: 500
Training with feature pool size: 400
Training with feature pool region padding: 0
Training with lambda_param: 0.1
Training with 20 split tests.
Fitting trees...
Training complete
Training complete, saved predictor to file predictor.dat
Training accuracy: 0.0
Showing detections and predictions on the images in the faces folder...
Processing file: img/women/women5.jpg
Number of faces detected: 1
Detection 0: Left: 290 Top: 498 Right: 646 Bottom: 676
Part 0: (317, 564), Part 1: (319, 582) ...
Traceback (most recent call last):
File "train_shape_detector.py", line 131, in <module>
win.add_overlay(shape)
RuntimeError:
Error detected at line 25.
Error detected in file /tmp/pip-build-867r6kjx/dlib/dlib/../dlib/image_processing/render_face_detections.h.
Error detected in function std::vector<dlib::image_display::overlay_line> dlib::render_face_detections(const std::vector<dlib::full_object_detection>&, dlib::rgb_pixel).
Failing expression was dets[i].num_parts() == 68 || dets[i].num_parts() == 5.
std::vector<image_window::overlay_line> render_face_detections()
You have to give either a 5 point or 68 point face landmarking output to this function.
dets[0].num_parts(): 46
级联深度训练:10
树深度训练:2
每级梯级500棵树的培训。
使用nu进行培训:0.05
随机种子训练:
过采样量的培训:500
具有功能池大小的培训:400
使用功能池区域填充进行培训:0
使用lambda_参数进行培训:0.1
通过20次分割测试进行训练。
安装树。。。
训练完成
培训完成,已将预测器保存到文件predictor.dat
训练精度:0.0
显示faces文件夹中图像的检测和预测。。。
处理文件:img/women/women5.jpg
检测到的面数:1
检测0:左:290顶:498右:646底:676
第0部分:(317564),第1部分:(319582)。。。
回溯(最近一次呼叫最后一次):
文件“train_shape_detector.py”,第131行,在
win.add_叠加(形状)
运行时错误:
在第25行检测到错误。
在文件/tmp/pip-build-867r6kjx/dlib/dlib/./dlib/image\u processing/render\u face\u detections.h中检测到错误。
在函数std::vector dlib::render_face_detections(常量std::vector&,dlib::rgb_pixel)中检测到错误。
失败的表达式为dets[i]。num_parts()==68 || dets[i]。num_parts()==5。
std::矢量渲染面检测()
您必须为此函数提供5点或68点面标记输出。
详细信息[0]。零件数量():46
您正在使用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
function的类型作为一种解决方法,我使用cv2将圆放置在点坐标上。是的,我得到了它。我只是认为有一种方法可以用dlib显示点。我已经成功地用cv2解决了这个问题。谢谢