Python代码错误(标量数组到标量索引)

时间:2017-04-01 12:42:08

标签: python

我在python脚本中遇到了问题..

我将其视为:

journalctl

它给了我这个错误:

python /path/script.py

这是代码的一部分(从第262行到第284行):

Traceback (most recent call last):
File "/path/script.py", line 332, in <module>
    main()
File "/path/script.py", line 283, in main
    hull1.append(points3[hullIndex[i]])
TypeError: only integer scalar arrays can be converted to a scalar index

和(第330至339行):

    #Following steps detects again the original face using dlib, this time for do face swapping
    #target face is the frame taken from camera and source face is the one morphed before
    dets4 = detector(img4, 1)
    if len(dets4)<1:
        pass
    else:
        for k, d in enumerate(dets4):
            points3 = []
            #Get 68 facial landmarks and save them to an array
            landmark3=np.array([[p.x, p.y] for p in predictor(img4, dets4[0]).parts()])
            for i in range(0,68) :
                x3 = landmark3[i][0]
                y3 = landmark3[i][1]
                points3.append((int(x3), int(y3)))

    # Find convex hull
    hull1 = []
    hull2 = []
    hullIndex = cv2.convexHull(np.array(points2), returnPoints = False)

    for i in xrange(0, len(hullIndex)):
        hull1.append(points3[hullIndex[i]])
        hull2.append(points2[hullIndex[i]])

是否需要修复这些代码的任何部分,或者是与其他内容相关的内容(如python,dlib,任何模块版本)?

1 个答案:

答案 0 :(得分:0)

带有cv2.convexHull()

returnPoints=False会返回一个点索引列表,但每个索引都包含在另一个子列表中,如下所示:[[index1], [index2], ...]

所以你可以简化这些行

for i in xrange(0, len(hullIndex)):
    hull1.append(points3[hullIndex[i]])
    hull2.append(points2[hullIndex[i]])

for point_index, in hullIndex:
    hull1.append(points3[point_index])
    hull2.append(points2[point_index])

有关详细信息,请参阅Contour Features上的教程。