优化性能 - OpenCV 和线程 Python
Optimise Performance - OpenCV and Threads with Python
我遇到与使用 OpenCV3.2 和 Python 相关的性能问题。我刚刚将另一个子系统集成到我的主程序中,但速度变慢了很多。
这是我的初始代码没有集成新子系统,我使用cv2.getTickCount
来测量时间,正如OpenCv3.2 Official Website所建议的那样。
# Infinite loop
while True:
# getting tick count
e1 = cv2.getTickCount()
# storing frame
_, img = cap.read()
# 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()
基本上,我在无限循环中调用一个函数来查找红色,然后按开始键创建线程并开始游戏。
这是之前我按'S'创建线程所需的时间:
0.019336862
0.016924178
0.022487864
这是需要的时间在我按'S'创建线程:
0.091731532
0.125760734
0.098221829
这里一切正常,时间稍有变化,但没有什么太重要的。添加新子系统时,我开始遇到问题。这里下面的代码集成了新系统,和之前的代码一样,只是一个函数调用改变了:
# 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.045629524
0.023788123
0.10517206
比没有集成的稍微高一点,不过还是可以的。这是我创建线程之后的时间:
1.061517957
0.568310864
0.691701059
这次和上次差距很大,甚至达到了整整一秒。即使从相机输出也很明显,真的很慢。
我的问题是,我是否以错误的方式创建了线程?有更好更有效的方法来使用线程吗?或者实际上有没有一种方法可以在这种情况下优化性能而不必修改这些函数findRedColours(img, board)
、t = Thread(target=playGame)
、gridExtractor.extractGrid(img)
?
我是 OpenCV 和 Python 的新手,但仍然遇到问题。希望有人能以正确的方式称呼我。谢谢。
感谢用户 'deets' 帮助在上面发表评论,优化性能。
在这种情况下,用 Python 中的 multiprocessing 模块中的进程替换线程就足够了。
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
与 Thread 相比,Process 在性能方面的使用效率更高。
我遇到与使用 OpenCV3.2 和 Python 相关的性能问题。我刚刚将另一个子系统集成到我的主程序中,但速度变慢了很多。
这是我的初始代码没有集成新子系统,我使用cv2.getTickCount
来测量时间,正如OpenCv3.2 Official Website所建议的那样。
# Infinite loop
while True:
# getting tick count
e1 = cv2.getTickCount()
# storing frame
_, img = cap.read()
# 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()
基本上,我在无限循环中调用一个函数来查找红色,然后按开始键创建线程并开始游戏。
这是之前我按'S'创建线程所需的时间:
0.019336862
0.016924178
0.022487864
这是需要的时间在我按'S'创建线程:
0.091731532
0.125760734
0.098221829
这里一切正常,时间稍有变化,但没有什么太重要的。添加新子系统时,我开始遇到问题。这里下面的代码集成了新系统,和之前的代码一样,只是一个函数调用改变了:
# 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.045629524
0.023788123
0.10517206
比没有集成的稍微高一点,不过还是可以的。这是我创建线程之后的时间:
1.061517957
0.568310864
0.691701059
这次和上次差距很大,甚至达到了整整一秒。即使从相机输出也很明显,真的很慢。
我的问题是,我是否以错误的方式创建了线程?有更好更有效的方法来使用线程吗?或者实际上有没有一种方法可以在这种情况下优化性能而不必修改这些函数findRedColours(img, board)
、t = Thread(target=playGame)
、gridExtractor.extractGrid(img)
?
我是 OpenCV 和 Python 的新手,但仍然遇到问题。希望有人能以正确的方式称呼我。谢谢。
感谢用户 'deets' 帮助在上面发表评论,优化性能。
在这种情况下,用 Python 中的 multiprocessing 模块中的进程替换线程就足够了。
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
与 Thread 相比,Process 在性能方面的使用效率更高。