python - 如何在包括列表和元组的同时使文件夹中的所有图片依次显示而不是最后一张图片?
问题描述
所以这里是在文件夹中包含图片的代码,以及尝试在图片上绘制圆圈作为面部标志,因为所有图片都是面部,并调整图片大小。但是,我的代码只显示最后一张带有面部标志的图片作为输出,而不是从第一张图片开始,然后继续显示直到最后一张我想要的图片。
import cv2
import glob
import face_recognition
import numpy as np
path = glob.glob("C:/Users/Celine Ng/Desktop/codings/Faces/*jpg")
for file in path:
img = cv2.imread(file)
# Load the jpg file into a numpy array
image = face_recognition.load_image_file(file)
# Find all facial features in the faces of the image
face_landmarks_list = face_recognition.face_landmarks(image)
#(0,0) is upper left corner
#extraction of coordinates
lists=[]
for face_landmarks in face_landmarks_list:
for facial_feature in face_landmarks.keys():
print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))
lists.append(face_landmarks[facial_feature])
chin=[]
left_eyebrow=[]
right_eyebrow=[]
nose_bridge=[]
nose_tip=[]
left_eye=[]
right_eye=[]
top_lip=[]
bottom_lip=[]
chin.append(lists[0])
left_eyebrow.append(lists[1])
right_eyebrow.append(lists[2])
nose_bridge.append(lists[3])
nose_tip.append(lists[4])
left_eye.append(lists[5])
right_eye.append(lists[6])
top_lip.append(lists[7])
bottom_lip.append(lists[8])
#chin
(x18,y18)=chin[0][8]
cv2.circle(img, (x18, y18), 3, (0,0,255), -1)
#left_eyebrow
(x22,y22)=left_eyebrow[0][2]
cv2.circle(img, (x22, y22), 4, (0,0,255), -1)
#right_eyebrow
(x32,y32)=right_eyebrow[0][2]
cv2.circle(img, (x32, y32), 4, (0,0,255), -1)
#nose
(x52,y52)=nose_tip[0][2]
cv2.circle(img, (x52, y52), 4, (0,0,255), -1)
#left_eye
(x60,y60)=left_eye[0][0]
cv2.circle(img, (x60, y60), 4, (0,0,255), -1)
(x63,y63)=left_eye[0][3]
cv2.circle(img, (x63, y63), 4, (0,0,255), -1)
#right_eye
(x70,y70)=right_eye[0][0]
cv2.circle(img, (x70, y70), 4, (0,0,255), -1)
(x73,y73)=right_eye[0][3]
cv2.circle(img, (x73, y73), 4, (0,0,255), -1)
#lip
(x82,y82)=top_lip[0][2]
cv2.circle(img, (x82, y82), 4, (0,0,255), -1)
(x83,y83)=top_lip[0][3]
cv2.circle(img, (x83, y83), 4, (0,0,255), -1)
(x84,y84)=top_lip[0][4]
cv2.circle(img, (x84, y84), 4, (0,0,255), -1)
(x86,y86)=top_lip[0][6]
cv2.circle(img, (x86, y86), 4, (0,0,255), -1)
(x80,y80)=top_lip[0][0]
cv2.circle(img, (x80, y80), 4, (0,0,255), -1)
#bottom_lip
(x93,y93)=bottom_lip[0][3]
cv2.circle(img, (x93, y93), 4, (0,0,255), -1)
r = 400.0 /img.shape[1]
dim = (400, int(img.shape[0] * r))
resizedimg = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
cv2.imshow("Image", resizedimg)
cv2.waitKey(2000)
cv2.destroyAllWindows()
解决方案
感谢另一个人的帮助,我得到了答案,这只是因为我没有将第二个循环缩进成为第一个循环的一部分:)
import cv2
import glob
import face_recognition
import numpy as np
path = glob.glob("C:/Users/Celine Ng/Desktop/codings/Faces/*jpg")
for file in path:
img = cv2.imread(file)
# Load the jpg file into a numpy array
image = face_recognition.load_image_file(file)
# Find all facial features in the faces of the image
face_landmarks_list = face_recognition.face_landmarks(image)
#(0,0) is upper left corner
#extraction of coordinates
lists=[]
for face_landmarks in face_landmarks_list:
for facial_feature in face_landmarks.keys():
print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))
lists.append(face_landmarks[facial_feature])
chin=[]
left_eyebrow=[]
right_eyebrow=[]
nose_bridge=[]
nose_tip=[]
left_eye=[]
right_eye=[]
top_lip=[]
bottom_lip=[]
chin.append(lists[0])
left_eyebrow.append(lists[1])
right_eyebrow.append(lists[2])
nose_bridge.append(lists[3])
nose_tip.append(lists[4])
left_eye.append(lists[5])
right_eye.append(lists[6])
top_lip.append(lists[7])
bottom_lip.append(lists[8])
#chin
(x18,y18)=chin[0][8]
cv2.circle(img, (x18, y18), 4, (255,0,0), -1)
#left_eyebrow
(x22,y22)=left_eyebrow[0][2]
cv2.circle(img, (x22, y22), 4, (255,0,0), -1)
#right_eyebrow
(x32,y32)=right_eyebrow[0][2]
cv2.circle(img, (x32, y32), 4, (255,0,0), -1)
#nose
(x52,y52)=nose_tip[0][2]
cv2.circle(img, (x52, y52), 4, (255,0,0), -1)
#left_eye
(x60,y60)=left_eye[0][0]
cv2.circle(img, (x60, y60), 4, (255,0,0), -1)
(x63,y63)=left_eye[0][3]
cv2.circle(img, (x63, y63), 4, (255,0,0), -1)
#right_eye
(x70,y70)=right_eye[0][0]
cv2.circle(img, (x70, y70), 4, (255,0,0), -1)
(x73,y73)=right_eye[0][3]
cv2.circle(img, (x73, y73), 4, (255,0,0), -1)
#lip
(x82,y82)=top_lip[0][2]
cv2.circle(img, (x82, y82), 4, (255,0,0), -1)
(x83,y83)=top_lip[0][3]
cv2.circle(img, (x83, y83), 4, (255,0,0), -1)
(x84,y84)=top_lip[0][4]
cv2.circle(img, (x84, y84), 4, (255,0,0), -1)
(x86,y86)=top_lip[0][6]
cv2.circle(img, (x86, y86), 4, (255,0,0), -1)
(x80,y80)=top_lip[0][0]
cv2.circle(img, (x80, y80), 4, (255,0,0), -1)
#bottom_lip
(x93,y93)=bottom_lip[0][3]
cv2.circle(img, (x93, y93), 4, (255,0,0), -1)
r = 500.0 /img.shape[1]
dim = (500, int(img.shape[0] * r))
resizedimg = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
cv2.imshow("Image", resizedimg)
cv2.waitKey(2000)
cv2.destroyAllWindows()
推荐阅读
- javascript - 是否可以通过运行所有代码 try...catch 块来捕获所有 JS 异常?
- java - 使用 AWS S3 和 Lambda 处理大文件
- r - ggplot2 闪避条和图例未排序
- javascript - 发送邮件进度条
- r - 通过删除 R 中的重复项来计算多列
- android - Android ViewPager 具有相同类的所有片段而无需事务
- node.js - Sequelize 不生成连接表或获取/添加方法
- user-interface - Flutter:如何重现这个对手指运动有反应的底部容器
- c++ - 为什么说表达式必须具有指向对象的类型?我不想使用指针。只有二维数组,一切都不那么复杂
- jquery - 从同一域中的不同 html 文件加载 div