优化性能-使用Python优化OpenCV和线程

优化性能-使用Python优化OpenCV和线程,python,multithreading,opencv,python-multithreading,Python,Multithreading,Opencv,Python Multithreading,我在使用OpenCV3.2和Python时遇到了与性能相关的问题。我刚刚将另一个子系统集成到我的主程序中,速度慢了很多 这是我的初始代码,没有集成新的子系统,我使用cv2.getTickCount来测量时间,如所建议的 基本上,我在无限循环中调用一个函数来查找红色,然后按start创建线程,游戏开始 这是在按下“S”键创建线程之前所需的时间: 0.019336862 0.016924178 0.022487864 0.091731532 0.125760734 0.098221829 0.0

我在使用OpenCV3.2Python时遇到了与性能相关的问题。我刚刚将另一个子系统集成到我的主程序中,速度慢了很多

这是我的初始代码,没有集成新的子系统,我使用
cv2.getTickCount
来测量时间,如所建议的

基本上,我在无限循环中调用一个函数来查找红色,然后按start创建线程,游戏开始

这是在按下“S”键创建线程之前所需的时间:

0.019336862
0.016924178
0.022487864
0.091731532
0.125760734
0.098221829
0.045629524
0.023788123
0.10517206
这是在按下“S”键创建线程后所需的时间:

0.019336862
0.016924178
0.022487864
0.091731532
0.125760734
0.098221829
0.045629524
0.023788123
0.10517206
这里一切都很好,时间上有轻微的变化,但没什么太重要的。当我添加新的子系统时,我开始出现问题。下面的代码与新系统集成后,与前一个代码相同,只是一个函数调用发生了变化:

# Infinite loop
    while True:
        # getting tick count
        e1 = cv2.getTickCount()
        # storing frame
        _, img = cap.read()
        # extract grid
        gridExtractor.extractGrid(img)
        # define red colours in the screen
        findRedColours(img, board)
        # getting tick count after the functions
        e2 = cv2.getTickCount()
        # calculating time
        t = (e2 - e1) / cv2.getTickFrequency()
        # print time
        print(t)

        # check if img is none
        if img is not None:
            # omitted code

            k = cv2.waitKey(20) & 0xFF
            # start the game, hide info
            if (k == ord('s') or k == ord('S')) and start is False:
                # create new thread to play game
                t = Thread(target=playGame)
                t.start()
这是我创建线程之前的时间:

0.019336862
0.016924178
0.022487864
0.091731532
0.125760734
0.098221829
0.045629524
0.023788123
0.10517206
它比没有集成的略高,但仍然可以。以下是我创建线程后的时间:

1.061517957
0.568310864
0.691701059
这一次和前一次有很大的不同,甚至达到了整整一秒。即使是从相机输出中也可以看到,速度非常慢

我的问题是,我是否以错误的方式创建线程?有更好更有效的方法来使用线程吗?或者,在这种情况下,是否有一种方法可以优化性能,而不必修改这些函数
findredclours(img,board)
t=Thread(target=playGame)
gridExtractor.extractGrid(img)


我刚开始使用OpenCV和Python,但仍然有一些问题。希望有人能用正确的方式称呼我。谢谢

多亏了用户“deets”在上面的评论,才有可能优化性能

在这种情况下,在Python中用模块中的进程替换线程就足够了

from multiprocessing import Process
#omitted code

while True:
    # getting tick count
    e1 = cv2.getTickCount()
    # storing frame
    _, img = cap.read()
    # extract grid - first subsystem
    gridExtractor.extractGrid(img)
    # define red colours in the screen - second subsystem
    findRedColours(img, board)
    # getting tick count after the functions
    e2 = cv2.getTickCount()
    # calculating time
    t = (e2 - e1) / cv2.getTickFrequency()
    # print time
    print(t)

    # check if img is none
    if img is not None:
        # omitted code

        k = cv2.waitKey(20) & 0xFF
        # start the game, hide info
        if (k == ord('s') or k == ord('S')) and start is False:
            # create new thread to play game
            p = Process(target=playGame)
            p.start()
所需的相对时间为:

0.022570883
0.11354852
0.119643379

与线程相比,进程的使用在性能方面更加有效。

多亏了用户“deets”的帮助,上面的评论使得优化性能成为可能

在这种情况下,在Python中用模块中的进程替换线程就足够了

from multiprocessing import Process
#omitted code

while True:
    # getting tick count
    e1 = cv2.getTickCount()
    # storing frame
    _, img = cap.read()
    # extract grid - first subsystem
    gridExtractor.extractGrid(img)
    # define red colours in the screen - second subsystem
    findRedColours(img, board)
    # getting tick count after the functions
    e2 = cv2.getTickCount()
    # calculating time
    t = (e2 - e1) / cv2.getTickFrequency()
    # print time
    print(t)

    # check if img is none
    if img is not None:
        # omitted code

        k = cv2.waitKey(20) & 0xFF
        # start the game, hide info
        if (k == ord('s') or k == ord('S')) and start is False:
            # create new thread to play game
            p = Process(target=playGame)
            p.start()
所需的相对时间为:

0.022570883
0.11354852
0.119643379

与线程相比,进程的使用在性能方面要高效得多。

我说得对吗,如果按了“s”,你每20毫秒启动一个线程?不,我只在按了“s”键后创建一次线程。我发现你的描述很难理解,但有一件事突然出现:如果你尝试使用线程来提高性能,这将在Python中失败。它使用GIL(全局解释器锁),有效地只允许一个线程运行。可以使用线程,它们在等待阻塞IO时很有用-但是如果您想使用多个内核,请使用多进程和进程间通信(请参阅多进程模块)。我是否正确,如果按下“s”,每20毫秒启动一个线程?否,我只在按下“S”键后创建了一次线程。我发现您的描述很难理解,但有一件事突然出现:如果您尝试使用线程来提高性能,这在Python中会失败。它使用GIL(全局解释器锁),有效地只允许一个线程运行。可以使用线程,它们在等待阻塞IO时很有用,但如果您想使用多个内核,请使用多进程和进程间通信(请参阅多进程模块)