尝试在分配局部变量之前打印其值

时间:2018-12-12 07:40:48

标签: python

我一直关注this tutorial。目的是将屏幕转换为灰色,以便神经网络可以更轻松地操作数据。

但是,当我运行我的代码时,它说分配前引用了局部变量processed_img。因此,我将global放在其前面,然后说它未定义。

我该如何解决?

import time
import cv2
import mss
import numpy

def process_img(image):
    processed_img
    processed_img = cv2.Canny(processed_img, threshold1=200, threshold2=300)
    return processed_img

with mss.mss() as sct:
    monitor = {"top": 40, "left": 0, "width": 960, "height": 540}     # Part of the screen to capture
    new_screen = process_img(monitor)

while (True):
    last_time = time.time()
    img = numpy.array(sct.grab(monitor))                          # Get raw pixels from the screen, save it to a Numpy array
    cv2.imshow('Window',new_screen)                               # Display the picture
    if cv2.waitKey(1) & 0xFF == ord('q'):                         #This keeps the screen displayed over time, 1 = screen's refresh rate
        cv2.destroyAllWindows()
        break

1 个答案:

答案 0 :(得分:0)

我想您需要进行以下更改:

def process_img(image):
    processed_img = cv2.Canny(image, threshold1=200, threshold2=300)
    return processed_img