Python:循环内仅打印一次
作者:互联网
我有一个要从相机捕获视频的代码.我想使用Python的Logging库在外壳上获取消息或将其导出到文本文件.
这是我的代码的一部分,在while循环中,我要打印成功打开的Camera
import numpy as np
import cv2
import logging as log
cap = cv2.VideoCapture('5.mpg')
while True:
ret, image = cap.read()
if ret == True:
log.warning('Camera Opened Successfully')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
clahe = cv2.createCLAHE(clipLimit = 15.0, tileGridSize=(8,8))
gray1 = clahe.apply(gray)
但是我得到的是:
直到我终止正在运行的脚本.关于如何使其仅打印一次的任何想法.
解决方法:
import numpy as np
import cv2
import logging as log
cap = cv2.VideoCapture('5.mpg')
hasOpened = False
while True:
ret, image = cap.read()
if ret and not hasOpened:
log.warning('Camera Opened Successfully')
hasOpened = True
如果要在打印后跳出循环,请遵循Matt的回答.此选项将继续循环,仅打印一次.
标签:opencv3-0,loops,python 来源: https://codeday.me/bug/20191118/2028577.html