代码之家  ›  专栏  ›  技术社区  ›  Kanchana Kariyawasam

在Python中将PNG流程图转换为图形

  •  1
  • Kanchana Kariyawasam  · 技术社区  · 1 年前

    我想使用Graphviz Python包将PNG类型的流程图转换为图形。在这里,我已经通过使用安装了相关的软件包 pip install opencv-python pytesseract graphviz 在我创建了一个python文件之后。

    import cv2
    import pytesseract
    import graphviz
    
    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'  # Replace with your actual path
    
    # Load the flowchart image
    img = cv2.imread('flowchart.png')
    
    # Preprocess the image (adjust as needed)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    
    # Extract text from image elements
    elements = []
    contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    for cntr in contours:
        x, y, w, h = cv2.boundingRect(cntr)
        text = pytesseract.image_to_string(img[y:y+h, x:x+w])
        elements.append({'text': text, 'x': x, 'y': y, 'w': w, 'h': h})
    
    # Create Graphviz graph
    graph = graphviz.Digraph(comment='Flowchart Graph')
    
    # Add nodes and edges based on extracted elements
    for i, element in enumerate(elements):
        node_id = f'node_{i}'
        graph.node(node_id, label=element['text'], shape='box')  # Adjust shape as needed
        if i > 0:  # Add edges based on element positions (adjust logic as needed)
            graph.edge(f'node_{i-1}', node_id)
    
    # Render the graph
    graph.render('flowchart_graph.png', view=True)
    

    当我试图运行这个python文件时,vs代码终端中出现了一个错误,称为

    [ WARN:[email protected]] global loadsave.cpp:248 cv::findDecoder imread_('flowchart.png'): can't open/read file: check file path/integrity
    Traceback (most recent call last):
      File "c:\Users\ASUS\Desktop\FYP Docs\flowchart_graph.py", line 11, in <module>
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.error: OpenCV(4.8.1) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
    

    然而,flowchart.png图像和这个python文件位于同一目录中。 这个错误的原因是什么?我该如何解决?

    1 回复  |  直到 1 年前
        1
  •  0
  •   Gpommerey    1 年前

    以下是一些可以帮助您的提示:

    1. 检查文件路径: 请确保文件路径正确。您可以使用绝对路径来指定图像文件,或者确保图像文件与Python脚本位于同一目录中。

      
      img = cv2.imread('flowchart.png')  # Use an absolute path if needed
      
    2. 验证图像文件: 仔细检查图像文件“flower.png”是否与Python脚本位于同一目录中。此外,请确保文件名拼写正确且大小写匹配。

    3. 检查文件权限: 确保您的Python脚本具有读取图像文件所需的权限。如果您在权限受限的环境中运行脚本,请考虑使用提升的权限运行脚本。

    4. 使用完整路径: 请尝试使用图像文件的完整路径。这样可以确保不存在与工作目录相关的问题。

      
      img = cv2.imread(r'C:\path\to\your\image\flowchart.png')
    

    调试信息: 添加打印语句以输出调试信息,例如当前工作目录和该目录中的文件列表。这可以帮助您识别与脚本执行环境相关的任何问题。

    python
    
    import os
    
    print("Current Working Directory:", os.getcwd())
    print("Files in Current Directory:", os.listdir())
    

    运行脚本并检查工作目录和文件列表是否符合您的期望。

    图像加载: 如果以上步骤不能解决问题,请考虑检查OpenCV是否可以成功加载其他图像。请尝试使用相同的脚本加载不同的映像,看看问题是否仍然存在。

    
        img = cv2.imread('other_image.jpg')  # Replace with the name of another image file
    

    如果这样做有效,则可能表明“flower.png”文件存在特定问题。

    通过这些步骤,您应该能够识别并解决问题的根本原因。

    希望它能帮助你!

    祝你今天过得愉快:)

    纪尧姆