微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python脚本在执行过程中停止,没有错误

如何解决python脚本在执行过程中停止,没有错误

我有一个简单的脚本来显示使用 python 的 dfs,它没有完全执行就停止了

脚本只是输入一张图片,然后将其转换为只有 2 种颜色的图片(类似于 dfs 的岛屿和水问题)。

它从一张空白图像(背景)开始,然后显示一个动画,其中岛屿(前景色)一个一个变得可见。

它可以工作到一定程度,然后完全停止。我已经检查了 while 循环停止时不满足的条件。

甚至不打印 atexit 消息。

这是脚本

import imageFilters as imf
import imutils
import cv2
import numpy as np
import random
import sys
import atexit

sys.setrecursionlimit(1000000000)

atexit.register(print,"exited ")


background = [255,255,255] #white
foreground = [0,0] #black

img_path = input("ENTER IMAGE PATH:")

img = cv2.imread(img_path)

 #imf.createLineDrawing: resizes with height = 600 and creates an image with only two colors;
img = imf.createLineDrawing(img,foreground,background)
(height,width,channel) = img.shape

blank_image = np.ndarray([height,channel],img.dtype)
blank_image.fill(255)

done = np.ndarray([height,width],img.dtype)
countDone = 0

def searchNeighbour(x,y):

    global done,countDone

    done[y,x] = 1
    countDone += 1
    if list(img[y,x]) == foreground:
        blank_image[y,x] = foreground
    else:
        return

    cv2.imshow("o",blank_image)
    cv2.waitKey(1)

    if x - 1 >= 0 and not(done[y,x - 1]):
        searchNeighbour(x - 1,y)
    if x + 1 <= width - 1 and not(done[y,x + 1]):
        searchNeighbour(x + 1,y)
    if y - 1 >= 0 and not(done[y - 1,x]):
        searchNeighbour(x,y - 1)
    if y + 1 <= height - 1 and not(done[y + 1,y + 1)


while countDone < height*width:
    x = random.randrange(0,width - 1)
    y = random.randrange(0,height - 1)
    if not(done[y,y)

解决方法

把它贴在某个地方:

import pdb; pdb.set_trace()

然后在它停止以获取说明时键入 h。您可以逐行逐行并找出每一步的值


此外,在它完成后,也许尝试输入 echo $? 以查看它是否因错误退出。 (如果您使用的是unixy) (0 表示已完成的事情,任何其他数字表示一般情况下有问题)


或者可能只是在项目的底部放一行

print("DONE PROGRAM %s" % countDone)

100% 不只是完成循环

,

我得到了解决方案。这是python的一个主要缺点。 Python 不是函数式语言,因此无法容纳大型递归。

实际上默认的递归限制(10000)不是任意的,而是因为堆栈大小小而设置的。所以即使增加递归限制,也会造成栈溢出。

但是我使用多线程得到了解决方案;

我只是将它包装在一个函数中,并在堆栈限制增加的不同线程中运行它;

在 Python 中运行大型递归的步骤

  1. 在代码开头添加以下几行
import sys

sys.setrecursionlimit(10**9)
threading.stack_size(10**8)
  1. 将递归函数包装在父函数中

  2. 在代码末尾添加以下行 -

threading.Thread(target=<your_parent_function>).start()

其中 <your_parent_function> 是您的父函数的名称

我的最终代码-

import imageFilters as imf
import cv2
import numpy as np
from random import randrange
import threading
import sys

sys.setrecursionlimit(10**9)
threading.stack_size(10**8)


background = [255,255,255]
foreground = [56,50,36]

img_path = input("ENTER IMAGE PATH:")

img = cv2.imread(img_path)
img = imf.createLineDrawing(img,foreground,background)
(height,width,channel) = img.shape

blank_image = np.ndarray([height,channel],img.dtype)
blank_image.fill(255)

done = np.ndarray([height,width],img.dtype)
countDone = 0


def searchNeighbour(x,y):

    global done,countDone
    print(countDone)
    countDone += 1
    done[y,x] = 1

    if list(img[y,x]) == foreground:
        blank_image[y,x] = foreground
    else:
        print("returning")
        return 0

    cv2.imshow("O",blank_image)
    cv2.waitKey(1)

    if x - 1 >= 0 and not(done[y,x - 1]):
        searchNeighbour(x - 1,y)
    if x + 1 <= width - 1 and not(done[y,x + 1]):
        searchNeighbour(x + 1,y)
    if y - 1 >= 0 and not(done[y - 1,x]):
        searchNeighbour(x,y - 1)
    if y + 1 <= height - 1 and not(done[y + 1,y + 1)


def showAllForegrounds():
    while countDone < height*width:
        x = randrange(0,width - 1)
        y = randrange(0,height - 1)
        if not(done[y,x]):
            searchNeighbour(x,y)


threading.Thread(target=showAllForegrounds).start()

print("DONE PROGRAM %s" % countDone)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。