如何在 python 中以 utf-8 格式写入文件名?

how to write filename in utf-8 in python?

我正在尝试将帧保存到 "veriseti" 文件中,但帧名称无法正常显示。

cam = cv2.VideoCapture(0)
    harcascadePath = "haarcascade_frontalface_default.xml"
    detector = cv2.CascadeClassifier(harcascadePath)
    sampleNum = 0
    while (True):
        ret, img = cam.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = detector.detectMultiScale(gray, 1.3, 5)
        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            # incrementing sample number
            sampleNum = sampleNum + 1
            # saving the captured face in the dataset folder TrainingImage

            cv2.imwrite("veriseti\ " + username1 + "." + id1 + '.' + str(sampleNum) + ".jpg", gray[y:y + h, x:x + w])*

您是否包含了所有代码?你的最后一行末尾有一个星号,所以我想知道。我不知道 username1 或 id1 是什么,但我假设是字符串,您可以将它们包装在 str() 中,不会造成伤害。