为了账号安全,请及时绑定邮箱和手机立即绑定

在 Python 中使用这个二维数组的更好方法

在 Python 中使用这个二维数组的更好方法

慕标琳琳 2023-12-05 14:56:16
我正在开展一个个人项目来帮助我学习 Python。我正在使用face_recognition 库和PIL 库在图像上绘图。我目前正在通过执行以下操作在一个人的脸上画“眼线”:# x and y coordinates of top of left_eyeshape_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_eyeshape_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) 我还想要一种能够仅调整 y 点而不更改 x 点的方法,这就是为什么我单独表示每个点。d对象如下:d = PIL.ImageDraw.Draw(pil_image, 'RGBA')和pil_image = PIL.Image.fromarray(image)任何有关我如何清理此问题的建议将不胜感激。
查看完整描述

1 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

现在我会说实话,我不知道这是否更有效,而且它肯定更复杂,但我试了一下:


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)

这有帮助吗?更重要的是它是否有效,因为没有您的设置我无法测试它。


查看完整回答
反对 回复 2023-12-05
  • 1 回答
  • 0 关注
  • 62 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信