首页 > 解决方案 > 如何在包括列表和元组的同时使文件夹中的所有图片依次显示而不是最后一张图片?

问题描述

所以这里是在文件夹中包含图片的代码,以及尝试在图片上绘制圆圈作为面部标志,因为所有图片都是面部,并调整图片大小。但是,我的代码只显示最后一张带有面部标志的图片作为输出,而不是从第一张图片开始,然后继续显示直到最后一张我想要的图片。

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()

标签: pythonlisttuplesface-recognition

解决方案


感谢另一个人的帮助,我得到了答案,这只是因为我没有将第二个循环缩进成为第一个循环的一部分:)

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()

推荐阅读