【问题标题】:How to save image with detection box in Dlib?如何在 Dlib 中保存带有检测框的图像?
【发布时间】:2015-12-19 11:08:00
【问题描述】:

我正在尝试修改 Dlib 的 train_object_detector 示例以将带有检测框的图像保存到文件中。如何使用叠加层保存图像。看完这个answer,我做到了:

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_object_detector.py ../examples/faces")
    exit()
faces_folder = sys.argv[1]


options = dlib.simple_object_detector_training_options()

options.add_left_right_image_flips = True

options.C = 5

options.num_threads = 8
options.be_verbose = True


training_xml_path = os.path.join(faces_folder, "training.xml")
testing_xml_path = os.path.join(faces_folder, "testing.xml")


# Now let's use the detector as you would in a normal application.  First we
# will load it from disk.
detector = dlib.simple_object_detector("detector.svm")

# We can look at the HOG filter we learned.  It should look like a face.  Neat!
win_det = dlib.image_window()
win_det.set_image(detector)

# Now let's run the detector over the images in the faces folder and display the
# results.
print("Showing detections on the images in the faces 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)
    dets = detector(img)
    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()))

    win.clear_overlay()
    win.set_image(img)
    win.add_overlay(dets)
    img1 = dlib.draw_rectangle(img,dets)
    outname = f + "_detected.jpg"
    #img1 = np.where(dets != 0, dets, img)
    io.imsave(outname,img1)

但我收到此错误:

Traceback (most recent call last):
  File "/Users/mas/dlib/python_examples/testing.py", line 74, in <module>
    img1 = dlib.draw_rectangle(img,dets)
AttributeError: 'module' object has no attribute 'draw_rectangle'

【问题讨论】:

  • 你解决过这个问题吗?

标签: python dlib


【解决方案1】:

如果您可以使用 opencv,您可以使用 cv2.rectangle()

您可以使用以下代码,而不是在代码中使用 img1 = dlib.draw_rectangle(img,dets)

for k, d in enumerate(dets):
        print ("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(k, d.left(), d.top(), d.right(), d.bottom()))
        cv2.rectangle(img, (d.left(), d.top()), (d.right(), d.bottom()), (255, 0, 255), 2)

这将在原始图像上绘制矩形。 您可以使用cv2.imwrite() 函数保存图像:

cv2.imwrite(outname, img)

【讨论】:

  • 如果您只想提取面部地标预测(而不是图像),这是否也有效?
【解决方案2】:

我在 DLib 的 Python API 中也找不到 draw_rectangle 方法。但是,您可以简单地使用检测器()方法返回的矩形对象的坐标来自己绘制检测框,如下所示: (使用 skimage 多边形周长)

from skimage import io
from skimage.draw import polygon_perimeter
...
for d in dets:
   rr,cc = polygon_perimeter([d.top(), d.top(), d.bottom(), d.bottom()],
                             [d.right(), d.left(), d.left(), d.right()])
   img[rr, cc] = (255, 0, 0)
...
io.imsave('imgname.jpg', img)

【讨论】:

    猜你喜欢
    • 2021-07-20
    • 2019-06-28
    • 2017-02-21
    • 2015-06-28
    • 2018-11-07
    • 2017-09-12
    • 2016-08-08
    • 2023-02-21
    • 1970-01-01
    相关资源
    最近更新 更多