通过“网状”在R中重现Python脚本

时间:2017-05-31 00:13:25

标签: python r

我正在尝试使用RStudio的reticulate包为python重新创建一个有效的python脚本。工作脚本是dlib face detection script的改编版。我认为,它几乎可以工作,但我不知道如何沿着一个对象进行枚举。在原始脚本中,它显示为:

from imutils import face_utils
import numpy as np
import imutils
import dlib
import cv2

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("path/to/shape_predictor_68_face_landmarks.dat")

image = cv2.imread("path/to/defiant2.jpg")
print(image.shape)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

rects = detector(gray, 1)

for (i, rect) in enumerate(rects):
    shape = predictor(gray, rect)
    shape = face_utils.shape_to_np(shape)

    bb = face_utils.rect_to_bb(rect)


rect = shape[1]
for rect in shape:
    print("{} {}".format(*rect))
print("{} {} {} {}".format(*bb))

当我尝试在R中尽我所能重新创建它时,当它应该沿着dlib探测器对象进行枚举时,我遇到了障碍。具体来说,我收到以下错误:

library(reticulate)

cv2 <- import('cv2', convert = FALSE)
dlib <- import('dlib', convert = FALSE)
face_utils <- import('imutils.face_utils')
imutils <- import('imutils')
np <- import('numpy')
main <- import_main()
py <- import_builtins()

shape_predictor = 'shape_predictor_68_face_landmarks.dat'
img = 'defiant2.jpg'

detector = dlib$get_frontal_face_detector()
predictor = dlib$shape_predictor(shape_predictor)

image = cv2$imread(img)
gray = cv2$cvtColor(image, cv2$COLOR_BGR2GRAY)

rects = detector$run(gray)

py$enumerate(rects)
Error: attempt to apply non-function

非常感谢将代码转移到R的任何帮助。如果有帮助,我已将完整的脚本文件以及测试图像和形状预测器地标文件上传到Github

0 个答案:

没有答案