UTF 8 文本文件 Raspi 错误,问题是什么?

问题描述 投票:0回答:0

问题:有时我们的项目没有检测到对象。用这个函数生成的文本文件变成UTF-8编码错误。也许我不是 python 专家,但我似乎无法追踪问题出在哪里/UTF-8 错误的来源

函数作用:以检测到的对象列表及其百分比为参数,登录result.txt文件并说出检测到的项目。

这是值得关注的代码:

def CountFreq(li, probs):
    freq = {}
    if len(li) > 0:
        for items in li:
            freq[items] = li.count(items)
            # print(freq)
    if not freq:
        a = str("No object detected")
        probs.append("No probabilities")
    else:
        a = str(freq)
    words = ["Object/s detected: " + a]
    if cv2.waitKey(1) & 0xFF == ord('r'):
        speak(a)
        # This is log txt
        with open("result.txt", "a") as f:
            for word in words:
                f.writelines(word + " probability in order " + str(probs) + "\n")

整个代码片段:

import cv2
import numpy as np
from tflite_support.task import processor
import subprocess

_MARGIN = 10  # pixels
_ROW_SIZE = 10  # pixels
_FONT_SIZE = 1
_FONT_THICKNESS = 1
_TEXT_COLOR = (0, 0, 255)  # red


def speak(a):
    subprocess.call(['espeak', '-v', 'en-gb', '-s', '120', '-p', '70', '-a', '200', a])


def CountFreq(li, probs):
    freq = {}
    if len(li) > 0:
        for items in li:
            freq[items] = li.count(items)
            # print(freq)
    if not freq:
        a = str("No object detected")
        probs.append("No probabilities")
    else:
        a = str(freq)
    words = ["Object/s detected: " + a]
    if cv2.waitKey(1) & 0xFF == ord('r'):
        speak(a)
        # This is log txt
        with open("result.txt", "a") as f:
            for word in words:
                f.writelines(word + " probability in order " + str(probs) + "\n")


def visualize(
        image: np.ndarray,
        detection_result: processor.DetectionResult,
) -> np.ndarray:
    """Draws bounding boxes on the input image and return it.

    Args:
      image: The input RGB image.
      detection_result: The list of all "Detection" entities to be visualize.

    Returns:
      Image with bounding boxes.
    """

    list = []
    probs = []
    for detection in detection_result.detections:
        # Draw bounding_box
        bbox = detection.bounding_box
        start_point = bbox.origin_x, bbox.origin_y
        end_point = bbox.origin_x + bbox.width, bbox.origin_y + bbox.height
        cv2.rectangle(image, start_point, end_point, _TEXT_COLOR, 3)

        category = detection.categories[0]
        category_name = category.category_name
        list.append(category_name)
        probability = round(category.score, 2)
        probs.append(probability)
        # result_text = category_name + ' (' + str(probability) + ')'
        # trying this code below
        result_text = f"{category_name} ({probability})"
        text_location = (_MARGIN + bbox.origin_x,
                         _MARGIN + _ROW_SIZE + bbox.origin_y)
        cv2.putText(image, result_text, text_location, cv2.FONT_HERSHEY_PLAIN,
                    _FONT_SIZE, _TEXT_COLOR, _FONT_THICKNESS)

    CountFreq(list, probs)
    return image

编辑错误消息:

除了当我打开 result.txt 文件时,raspi 中没有错误消息,它变成了 UTF-8 错误。当 opencv 没有在其相机中检测到任何物体时,这尤其会发生。我尝试使用 if len(li)==0 修复它,但在进一步测试时错误仍然存在。

python raspbian file-handling raspberry-pi4
© www.soinside.com 2019 - 2024. All rights reserved.