如何更新应随迭代而变化的变量?

How can I update a variable that should change over iterations?

提问人:adcast076 提问时间:7/12/2023 最后编辑:Christoph Rackwitzadcast076 更新时间:7/12/2023 访问量:26

问:

我正在使用一个代码,该代码从文件夹中导入照片,创建地标,并在地标中获取距离的值。 问题是,我可以成功地打开照片,制作地标并获得距离,但是每当我尝试打印每张照片的特定距离时,代码都会打印第一个值。 我知道代码确实会从每张照片中获取距离,因为当我更改文件夹中照片的顺序时,我会得到不同的值。 如何使变量通过迭代进行自我更新,以便获取代码以从每张照片中打印特定值?

这是我到目前为止的代码

for file_name in files_names:
    image_path = input_path+'/'+file_name
    image = cv2.imread(image_path)
    image = cv2.resize(image,(0,0), fx=0.25, fy=0.25)
    height, width, c = image.shape
    RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    counting.append(file_name)
    with mp_face_mesh.FaceMesh(
        static_image_mode = True,
        max_num_faces = 1,
        min_detection_confidence = 0.5) as face_mesh:
        results = face_mesh.process(RGB)
        if results.multi_face_landmarks is not None:
            for face_landmarks in results.multi_face_landmarks:
                mp_drawing.draw_landmarks(image, face_landmarks, mp_face_mesh.FACEMESH_CONTOURS,
                                          mp_drawing.DrawingSpec(color =
(255,255,255),thickness=1,circle_radius=1), 
                                          mp_drawing.DrawingSpec(color= (255,255,255),thickness=1,))

                for index in index_list:
                    xx = int(face_landmarks.landmark[index].x * width)
                    yy = int(face_landmarks.landmark[index].y * height)
                    cv2.circle(image, (xx, yy), 2 , (0, 255, 0), 2)
                    for id,puntos in enumerate(face_landmarks.landmark):
                        x,y = int(puntos.x*width), int(puntos.y*height)
                        px.append(x)
                        py.append(y)
                        lista.append([id,x,y])
                        if len(lista) == 468:
                    #distancia referencia
                            x1,y1=lista[4][1:]
                            x2,y2=lista[168][1:]
                            #cx,cy=(x1+x2)//2,(y1+y2)//2
                            longitud1=math.hypot(x2-x1,y2-y1)

                            x3,y3=lista[4][1:]
                            x4,y4=lista[105][1:]
                            #cx2,cy2=(x3+x4)//2,(y3+y4)//2
                            longitud2=math.hypot(x4-x3,y4-y3)
               
                proporcion=longitud1/longitud2            
                measuring.append(proporcion)
                print(proporcion)                              

        cv2.imshow('image', image)
        cv2.waitKey(0)
cv2.destroyAllWindows()

python 数组变量 mediapipe

评论


答: 暂无答案