实时更新matplotlib饼图

3
这是我用于检测和展示性别和年龄的循环。我尝试在matplotlib饼图上绘制它,但每次运行循环时,它都会为每个预测打开一个不同的图表。如何实时更新同一张图表。
这里我正在更新完整的代码
使用Python和OpenCV检测人物年龄和性别的完整代码。
```
import os
import cv2
import dlib
import numpy as np
import argparse
import inception_resnet_v1
import tensorflow as tf
from imutils.face_utils import FaceAligner
from imutils.face_utils import rect_to_bb
import matplotlib.pyplot as plt

def get_args():
    parser = argparse.ArgumentParser(description="This script detects faces from web cam input, "
                                                 "and estimates age and gender for the detected faces.",
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--weight_file", type=str, default=None,
                        help="path to weight file (e.g. weights.18-4.06.hdf5)")
    parser.add_argument("--depth", type=int, default=16,
                        help="depth of network")
    parser.add_argument("--width", type=int, default=8,
                        help="width of network")
    args = parser.parse_args()
    return args


def draw_label(image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
               font_scale=1, thickness=2):
    size = cv2.getTextSize(label, font, font_scale, thickness)[0]
    x, y = point
    cv2.rectangle(image, (x, y - size[1]), (x + size[0], y), (255, 0, 0), cv2.FILLED)
    cv2.putText(image, label, point, font, font_scale, (255, 255, 255), thickness)


def main(sess,age,gender,train_mode,images_pl):
    args = get_args()
    depth = args.depth
    k = args.width

    # for face detection
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    fa = FaceAligner(predictor, desiredFaceWidth=160)

    # load model and weights
    img_size = 160

    # cap video
    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

    while True:
        # get video frame
        ret, img = cap.read()

        if not ret:
            print("error: failed to capture image")
            return -1

        input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        img_h, img_w, _ = np.shape(input_img)


        detected = detector(input_img, 1)
        faces = np.empty((len(detected), img_size, img_size, 3))

        for i, d in enumerate(detected):
            x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
            xw1 = max(int(x1 - 0.4 * w), 0)
            yw1 = max(int(y1 - 0.4 * h), 0)
            xw2 = min(int(x2 + 0.4 * w), img_w - 1)
            yw2 = min(int(y2 + 0.4 * h), img_h - 1)
            cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
            # cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
            faces[i, :, :, :] = fa.align(input_img, gray, detected[i])
            # faces[i,:,:,:] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))

        if len(detected) > 0:
            # predict ages and genders of the detected faces
            ages,genders = sess.run([age, gender], feed_dict={images_pl: faces, train_mode: False})

        # draw results
        for i, d in enumerate(detected):
            label = "{}, {}".format(int(ages[i]), "F" if genders[i] == 0 else "M")
            draw_label(img, (d.left(), d.top()), label)
            #for i in range(20):

                #label = 'M', 'F'
            sizes = [15, 30]
            explode = (0, 0.1)
            fig1, ax1 = plt.subplots()
            ax1.pie(sizes, explode=explode, labels=label, autopct='%1.1f%%',
                    shadow=True, startangle=90)
            ax1.axis('equal')  # Equal aspect ratio 
        plt.show()
            #break


        cv2.imshow("result", img)
        key = cv2.waitKey(1)

        if key == 27:
            break

def load_network(model_path):
    sess = tf.Session()
    images_pl = tf.placeholder(tf.float32, shape=[None, 160, 160, 3], name='input_image')
    images_norm = tf.map_fn(lambda frame: tf.image.per_image_standardization(frame), images_pl)
    train_mode = tf.placeholder(tf.bool)
    age_logits, gender_logits, _ = inception_resnet_v1.inference(images_norm, keep_probability=0.8,
                                                                 phase_train=train_mode,
                                                                 weight_decay=1e-5)
    gender = tf.argmax(tf.nn.softmax(gender_logits), 1)
    age_ = tf.cast(tf.constant([i for i in range(0, 101)]), tf.float32)
    age = tf.reduce_sum(tf.multiply(tf.nn.softmax(age_logits), age_), axis=1)
    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())
    sess.run(init_op)
    saver = tf.train.Saver()
    ckpt = tf.train.get_checkpoint_state(model_path)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("restore model!")
    else:
        pass
    return sess,age,gender,train_mode,images_pl
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--model_path", "--M", default="./models", type=str, help="Model Path")
    args = parser.parse_args()
    sess, age, gender, train_mode,images_pl = load_network(args.model_path)
    main(sess,age,gender,train_mode,images_pl)

```


请提供一个最小、完整、可验证的示例。您现在提供的代码无法运行,因为下面的答案并不能解决您的问题,您的脚本中可能发生了更多的事情,我们看不到。 - Thomas Kühn
已更新问题并附上完整代码。 - Ashwin Phadke
我并不是要求你发布完整的脚本,我怀疑它对其他人来说可能无法运行,因为我们没有输入。相反,请花些时间将您的代码简化为一个Minimal, Complete, and Verifiable example,以分离出你的问题,这样我们才能提供特定问题的解决方案。 - Thomas Kühn
1个回答

0

fig1, ax1 = plt.subplots() 放在 for 循环开始之前


问题仍然存在,但是我已经移除了for xx(1000)循环,因为它超出了20个图形的范围,所以将其放在第一个for循环之上,但结果并不如预期。 - Ashwin Phadke
那么你输入的数据可能在每次运行脚本时都不相同。在第一次运行时尝试使用 pickle 进行序列化。在第二次运行时,检查使用 pickle 得到的数据是否相同。 - BushLee
由于这些是关于年龄和性别的预测,数据经常发生变化。每张图片都能正确绘制,但在实时情况下,图表就无法显示更新后的结果。 - Ashwin Phadke
所以你想实时绘制饼图?实时指的是它会在每个循环中自动修改。 - BushLee
是的,每次数据更改时都要修改图表,我还在我的问题中更新了完整版本的代码。 - Ashwin Phadke
1
哦,那么你正在绘制一个普通图表。你需要使用matplotlib.animation来制作实时图表。这里有一个关于实时图表的教程:https://www.youtube.com/watch?v=ZmYPzESC5YY - BushLee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接