在使用python時,經常會暫停(不是停止)運行程序查看結果,網上的方法比較多,但都會用到專用的庫,因此比較麻煩且繁瑣。考慮到使用python經常會用到opencv庫,所以可以使用opencv庫中的相關函數進行實現。
使用到的函數或庫
1.cv2.imshow()
2.cv2.waitKey()
3.time.sleep()
4.cv2.putText()
5.np.zeros()
程序如下
程序說明:
運行後,無操作則等待一段時間後繼續運行程序;若按空格鍵暫停運行程序,再按空格鍵繼續運行程序。
import time
import cv2
import numpy as np
def pause_key(keypress, seconds):
key = 0
print('準備開始,按空格鍵暫停及繼續。。。。。。\n')
img = np.zeros((100, 200, 3), np.uint8)
img.fill(255)
cv2.putText(img, '!!!!!!', (10, 50),cv2.FONT_HERSHEY_COMPLEX, 2.0, (100, 200, 200), 5)
cv2.imshow('attention!', img)
for second in range(seconds):
input_kb = cv2.waitKey(1) & 0xFF
if input_kb == ord(' '):
print('paused')
cv2.waitKey(0)
print('continued')
time.sleep(1)
print(second)
second += 1
cv2.destroyAllWindows()
if __name__ == '__main__':
pause_key(keypress=' ', seconds=10)
運行結果
寫於關雎
新浪微博:https://weibo.com/tianzhejia
CSDN博客:https://blog.csdn.net/qq_35605018
博客網站:http://www.zhijiadeboke.xyz
GitHub:https://github.com/ZhijiaTian
QQ郵箱:[email protected]
126郵箱:[email protected]
Outlook郵箱:[email protected]
以上均可與本人取得聯繫,歡迎探討。^ v ^