python - 如何编码以修复检测到的 fatal error : Failed to execute script BoxDetection after use auto-py-to-exe compile python code to exe file?

标签 python python-3.x opencv pyinstaller detection

如何编码以修复检测到的 fatal error :使用 auto-py-to-exe 将 python 代码编译为 exe 文件后无法执行脚本 BoxDetection?
我学习使用 vdo 链接 https://www.youtube.com/watch?v=ZtBTrARHJps&list=WL&index=3&t=156s 将 python 代码编译为 exe 文件.
在使用 auto-py-to-exe 将 python 代码编译为 exe 文件并在程序完成工作后出现弹出错误后,我遇到了问题。

Fatal error detected
Failed to execute script BoxDetection
图片链接弹出错误 - https://i.imgur.com/ycQrFJ4.jpg询问如何编码以将弹出错误修复为 执行脚本 BoxDetection 失败 问题。
示例代码。
  • BoxDetection.py
  • import cv2
    import numpy as np
    
    from tkinter import Tk
    from tkinter.filedialog import askopenfilename
    
    Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
    filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
    cap=cv2.VideoCapture(filename) # Compatible with box2 mp4 video file
    
    while(cap.read()) :
         ref,frame = cap.read()
         roi=frame[:1080,0:1920]
    
         gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
         gray_blur=cv2.GaussianBlur(gray,(25,25),0)
         thresh=cv2.adaptiveThreshold(gray_blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,21,4)
         kernel=np.ones((3,3),np.uint8)
         closing=cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel,iterations=4)
    
         result_img=closing.copy()
         contours,hierachy=cv2.findContours(result_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
         counter=0
         for cnt in contours:
             area = cv2.contourArea(cnt)
             if area<622 :
                 continue
             # ellipse = cv2.fitEllipse(cnt)
             # cv2.ellipse(roi,ellipse,(0,255,0),2)
             counter+=1
    
         cv2.putText(roi,str(counter),(10,100),cv2.FONT_HERSHEY_SIMPLEX,4,(0,0,255),4,cv2.LINE_AA)
         cv2.imshow("Show",roi)
    
         if cv2.waitKey(1) & 0xFF==ord('q'):
             break
    
    cap.release()
    cv2.destroyAllWindows()
    
  • boxdetectionerrorlog.txt带下载链接。

  • https://doanga2007.github.io/boxdetectionerrorlog.txt
  • sample VDO box2.mp4带链接 - https://doanga2007.github.io/box2.mp4
  • BoxDetection.exe 64 位使用 auto-py-to-exe 将 python 代码编译为带有下载链接的 exe 文件。

  • https://drive.google.com/file/d/1tnnnDWRrg1NbPZ3hr9mhY-t_4zry21rB/view?usp=sharing
    如果打开 BoxDetection.exe 并选择示例 VDO box2.mp4 ,程序完成处理图片链接后会弹出错误。
    https://i.imgur.com/p06GzjP.jpg
  • boxdetectionerrorlog+debug.txt带下载链接。

  • https://doanga2007.github.io/boxdetectionerrorlog+debug.txt
  • Python Shell 用于描述 Run Module 之后的错误日志。
  • Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license()" for more information.
    >>> 
    == RESTART: C:\Users\tum\Desktop\โปรแกรม BoxDetection แบบ EXE\BoxDetection.py ==
    Traceback (most recent call last):
      File "C:\Users\tum\Desktop\โปรแกรม BoxDetection แบบ EXE\BoxDetection.py", line 13, in <module>
        roi=frame[:1080,0:1920]
    TypeError: 'NoneType' object is not subscriptable
    >>>
    

    最佳答案

    好消息 :我有修复检测到 fatal error 的答案:使用 auto-py-to-exe 使用我的完整源代码将 python 代码编译为 exe 文件后,无法执行脚本 BoxDetection。
    我有 link1 的答案, link2 , link3link4 .

  • 使用命令安装 pyinstaller/auto-py-to-exe/win32com:
  • pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip
    pip install auto-py-to-exe
    pip install pywin32
    
  • 用样本编码。

  • 2.1 BoxDetection.py
    import cv2
    import numpy as np
    
    from tkinter import Tk
    from tkinter.filedialog import askopenfilename
    
    Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
    filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
    cap=cv2.VideoCapture(filename) # Compatible with box2 mp4 video file
    
    while(cap.read()) :
         ref,frame = cap.read()
         roi=frame[:1080,0:1920]
    
         gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
         gray_blur=cv2.GaussianBlur(gray,(25,25),0)
         thresh=cv2.adaptiveThreshold(gray_blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,21,4)
         kernel=np.ones((3,3),np.uint8)
         closing=cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel,iterations=4)
    
         result_img=closing.copy()
         contours,hierachy=cv2.findContours(result_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
         counter=0
         for cnt in contours:
             area = cv2.contourArea(cnt)
             if area<800 :
                 continue
             # ellipse = cv2.fitEllipse(cnt)
             # cv2.ellipse(roi,ellipse,(0,255,0),2)
             counter+=1
    
         cv2.putText(roi,str(counter),(10,100),cv2.FONT_HERSHEY_SIMPLEX,4,(0,0,255),4,cv2.LINE_AA)
         cv2.imshow("Show",roi)
    
         if cv2.waitKey(300000) & 0xFF==ord('q'):
             break
    
    cap.release()
    cv2.destroyAllWindows()
    
    和 VDO box2.mp4带有链接的示例文件 - https://doanga2007.github.io/box2.mp4
  • 打开cmd并使用auto-py-to-exe命令打开auto-py-to-exe程序,打开python文件并按编译按钮立即将python文件编译为exe文件。
  • BoxDetection.exe带链接 - https://drive.google.com/file/d/1tnnnDWRrg1NbPZ3hr9mhY-t_4zry21rB/view?usp=sharing
  • 关于python - 如何编码以修复检测到的 fatal error : Failed to execute script BoxDetection after use auto-py-to-exe compile python code to exe file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63423530/

    相关文章:

    c++ - OpenCV:SVM train_auto 断言 sv_count != 0 失败

    python - 如何使用 Tornado 和 MongoDB 发送用户注册确认邮件?

    python - 如何在2 2d列表中将第二列添加在一起?

    python - pipenv 始终无法安装 psycopg2

    linux - 在虚拟机上运行时如何查看 Dask 仪表盘?

    c++ - 使用冲浪描述符+弗兰匹配器的静态手势识别

    opencv - 安装特定库 OpenCV iOS

    python - 使用cql从python插入到cassandra

    Python:用于显示包含特定字符串的列表的语句?

    python - 用字典理解过滤字典