在 Python 中使用此二维数组的更好方法
Better way to use this 2D array in Python
我正在做一个个人项目来帮助我学习 Python。我正在使用 face_recognition 库和 PIL 库在图像上绘图。我目前正在通过执行以下操作在一个人的脸上画“眼线”:
# x and y coordinates of top of left_eye
shape_left_eye = face_landmarks['left_eye']
l_i_x0 = shape_left_eye[0][0]
l_i_y0 = shape_left_eye[0][1]
l_i_x1 = shape_left_eye[1][0]
l_i_y1 = shape_left_eye[1][1]
l_i_x2 = shape_left_eye[2][0]
l_i_y2 = shape_left_eye[2][1]
l_i_x3 = shape_left_eye[3][0]
l_i_y3 = shape_left_eye[3][1]
# x and y coordinates of top of right_eye
shape_right_eye = face_landmarks['right_eye']
r_i_x0 = shape_right_eye[0][0]
r_i_y0 = shape_right_eye[0][1]
r_i_x1 = shape_right_eye[1][0]
r_i_y1 = shape_right_eye[1][1]
r_i_x2 = shape_right_eye[2][0]
r_i_y2 = shape_right_eye[2][1]
r_i_x3 = shape_right_eye[3][0]
r_i_y3 = shape_right_eye[3][1]
d.line([(l_i_x0,l_i_y0-5),(l_i_x1,l_i_y1-5),(l_i_x2,l_i_y2-5),(l_i_x3,l_i_y3-5)], fill=(0, 0, 0, 175), width=6)
d.line([(r_i_x0,r_i_y0-5),(r_i_x1,r_i_y1-5),(r_i_x2,r_i_y2-5),(r_i_x3,r_i_y3-5)], fill=(0, 0, 0, 175), width=6)
上述方法有效,但似乎效率很低,我相信他们是更好的方法。这种方式更有效,但在整个眼睛周围画了一个“圆圈”,我只是想在眼睛上方画一条线。
d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=4)
我还想要一种方法,能够在不改变 x 点的情况下只调整 y 点,这就是为什么我每个点都是单独的。
d对象如下:
d = PIL.ImageDraw.Draw(pil_image, 'RGBA')
和
pil_image = PIL.Image.fromarray(image)
任何关于如何清理它的建议都将不胜感激。
老实说,我不知道这是否更有效,而且肯定更复杂,但我试了一下:
shape_left_eye = face_landmarks['left_eye']
l = [
shape_left_eye[i][j] if j != 1 else
shape_left_eye[i][j] - 5 for i in
range(4) for j in range(2)
]
shape_right_eye = face_landmarks['right_eye']
r = [
shape_right_eye[i][j] if j != 1 else
shape_right_eye[i][j] - 5 for i in
range(4) for j in range(2)
]
d.line([(l[0],l[1]),(l[2],l[3]),(l[4],l[5]),(l[6],l[7])], fill=(0, 0, 0, 175), width=6)
d.line([(r[0],r[1]),(r[2],r[3]),(r[4],r[5]),(r[6],r[7])], fill=(0, 0, 0, 175), width=6)
这有帮助吗?更重要的是它是否有效,因为没有你的设置我无法测试它。
我正在做一个个人项目来帮助我学习 Python。我正在使用 face_recognition 库和 PIL 库在图像上绘图。我目前正在通过执行以下操作在一个人的脸上画“眼线”:
# x and y coordinates of top of left_eye
shape_left_eye = face_landmarks['left_eye']
l_i_x0 = shape_left_eye[0][0]
l_i_y0 = shape_left_eye[0][1]
l_i_x1 = shape_left_eye[1][0]
l_i_y1 = shape_left_eye[1][1]
l_i_x2 = shape_left_eye[2][0]
l_i_y2 = shape_left_eye[2][1]
l_i_x3 = shape_left_eye[3][0]
l_i_y3 = shape_left_eye[3][1]
# x and y coordinates of top of right_eye
shape_right_eye = face_landmarks['right_eye']
r_i_x0 = shape_right_eye[0][0]
r_i_y0 = shape_right_eye[0][1]
r_i_x1 = shape_right_eye[1][0]
r_i_y1 = shape_right_eye[1][1]
r_i_x2 = shape_right_eye[2][0]
r_i_y2 = shape_right_eye[2][1]
r_i_x3 = shape_right_eye[3][0]
r_i_y3 = shape_right_eye[3][1]
d.line([(l_i_x0,l_i_y0-5),(l_i_x1,l_i_y1-5),(l_i_x2,l_i_y2-5),(l_i_x3,l_i_y3-5)], fill=(0, 0, 0, 175), width=6)
d.line([(r_i_x0,r_i_y0-5),(r_i_x1,r_i_y1-5),(r_i_x2,r_i_y2-5),(r_i_x3,r_i_y3-5)], fill=(0, 0, 0, 175), width=6)
上述方法有效,但似乎效率很低,我相信他们是更好的方法。这种方式更有效,但在整个眼睛周围画了一个“圆圈”,我只是想在眼睛上方画一条线。
d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=4)
我还想要一种方法,能够在不改变 x 点的情况下只调整 y 点,这就是为什么我每个点都是单独的。
d对象如下:
d = PIL.ImageDraw.Draw(pil_image, 'RGBA')
和
pil_image = PIL.Image.fromarray(image)
任何关于如何清理它的建议都将不胜感激。
老实说,我不知道这是否更有效,而且肯定更复杂,但我试了一下:
shape_left_eye = face_landmarks['left_eye']
l = [
shape_left_eye[i][j] if j != 1 else
shape_left_eye[i][j] - 5 for i in
range(4) for j in range(2)
]
shape_right_eye = face_landmarks['right_eye']
r = [
shape_right_eye[i][j] if j != 1 else
shape_right_eye[i][j] - 5 for i in
range(4) for j in range(2)
]
d.line([(l[0],l[1]),(l[2],l[3]),(l[4],l[5]),(l[6],l[7])], fill=(0, 0, 0, 175), width=6)
d.line([(r[0],r[1]),(r[2],r[3]),(r[4],r[5]),(r[6],r[7])], fill=(0, 0, 0, 175), width=6)
这有帮助吗?更重要的是它是否有效,因为没有你的设置我无法测试它。