image - VideoCapture.read() 返回过去的图像
问题描述
我在(操作系统是)python3.6
上运行openCV
Raspberry pi
Raspbian
代码的大致结构如下。
以
image
时间间隔(3~5 分钟)捕获。捕获
image
的在函数中处理并返回度量(精度类型)- 迭代 1.~2。直到
end_check()
返回True
问题是最近拍摄image
的已经过时了。看起来它是在大约 10 分钟前拍摄的。最近拍摄的所有图像都迟到了。但一images
开始的拍摄似乎是定时的。并且所有.jpg
文件中记录的时间都输入正确+看来这个问题是一个多小时后出现的。(20~22 次迭代)
Images
cam0.read()
在cv2
包中捕获。以下是代码的主要部分。上传完整代码很长。有人要求,我会更新。
def run(interval,model_list):
cam0 = cv2.VideoCapture(0) #Only cam0 is used. cam2 is just to record.
camdir = "/home/pi/capstone/cam0/"
cam2 = cv2.VideoCapture(1)
cam2dir = "/home/pi/capstone/cam2/"
runNo = 0
acc_list = list()
error_list = list()
end = False
while(end == False):
print(runNo,"th run")
img_name = "%s.jpg" %runNo
frame, res = cam0.read() #`res` is the image which will be processed
cv2.imwrite(os.path.join(camdir,img_name),res)
_ , cam2pic = cam2.read()
cv2.imwrite(os.path.join(cam2dir,img_name),cam2pic)
try:
temp = Real(res)
mat = temp.match(model_list)
acc_list.append([mat,runNo])
print("Accuracy=", mat)
except ValueError:
acc_list.append(["ValueError",runNo])
error_list.append(["ValueError",runNo])
except AttributeError:
acc_list.append(["AttributeError", runNo])
error_list.append(["AttributeError",runNo])
except SmallObjectError:
acc_list.append(["SmallObjectError", runNo])
error_list.append(["SmallObjectError",runNo])
runNo = runNo+1
endNo = 40
if(runNo/2 > endNo):
end_check(res, end)
elif(runNo > endNo):
end = True
sleep(interval*60)
with open("acc_list.txt", "w") as output: #records for tracking errors
output.write(str(acc_list))
with open("err_list.txt", "w") as output:
output.write(str(error_list))
cam0.release()
cam2.release()
run(3.5,model_list)
(+) 一些新发现的东西和猜测
- 随着代码的运行,
images
时间差距越来越大 - 代码终于显示了
OpenCV Error
- 它看起来有点
Video signal
存储在 RAM 上并R-pi
返回.read()
过时image
RAM
- 存储
Video signal
在RAM
引发资源问题
下面是OpenCV Error
OpenCV Error: Assertion failed (dsize.area() > 0 || (inv_scale_x > 0 && inv_scale_y > 0)) in resize, file /home/pi/opencv/opencv-3.4.0/modules/imgproc/src/resize.cpp, line 4045
Traceback (most recent call last):
File "runpi.py", line 264, in <module>
run(3.5,model_list)
File "runpi.py", line 234, in run
mat = temp.match(model_list)
File "runpi.py", line 184, in match
self.__resize(model.get_m_inform())
File "runpi.py", line 147, in __resize
self.mask = cv2.resize(self.mask, None, fx=reratio, fy=reratio, interpolation = inter_method)
cv2.error: /home/pi/opencv/opencv-3.4.0/modules/imgproc/src/resize.cpp:4045: error: (-215) dsize.area() > 0 || (inv_scale_x > 0 && inv_scale_y > 0) in function resize
(+) 部分代码引发错误
这是__.resize()
方法。当我手动处理image
发生的OpenCV Error
事情时,即使OpenCV Error
指出image
大小问题,它也能很好地工作。所以我认为这无关紧要,image
也无关紧要。无论如何,这是代码。size
md_inf()
def __resize(self, md_inf):
#md_inf = [219, 122, 132, 171, 262] <-sample
reratio = md_inf[0]/self.y
if(reratio>1):
inter_method = cv2.INTER_LINEAR
else:
inter_method = cv2.INTER_AREA
###below is line 147###
self.mask = cv2.resize(self.mask, None, fx=reratio, fy=reratio, interpolation = inter_method)
temp = np.zeros((md_inf[3], md_inf[4]), np.uint8)
m_cx, m_cy = md_inf[1:3]
_, contour, _ = cv2.findContours(self.mask, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
total_contour = contour[0]
for ctr in contour[1:]:
total_contour = np.concatenate((total_contour, ctr), axis =0)
mmt = cv2.moments(total_contour)
if(mmt['m00'] < 7500):
raise SmallObjectError
self.cy = int(mmt['m10']/mmt['m00']) #y is horrizon axis
self.cx = int(mmt['m01']/mmt['m00']) #x is vertical axis
x, y = self.mask.shape
adjust = m_cx - self.cx + x - temp.shape[0]
if(adjust > 0):
m_cx = m_cx - adjust
temp[(m_cx-self.cx):(m_cx-self.cx) +x, (m_cy-self.cy):(m_cy-self.cy) +y] = self.mask
self.mask = temp
解决方案
我同意Mark Serchell的评论。我使用的方式是将变量设置为time + x seconds
并检查。OpenCV 具有用于跳帧的有用功能,例如cam.grab()
. 它只会从缓冲区中读取该帧,但不会对其执行任何操作。这样,您可以避免“遭受缓冲”。简单的代码是:
import cv2
import time
cam = cv2.VideoCapture(url)
ret,frame = cam.read()
timeCheck = time.time()
future = 10*60 # delay
while ret:
if time.time() >= timeCheck:
ret,frame = cam.read()
# Do your staff here
timeCheck = time.time()+future
else:
# Read from buffer, but skip it
ret = cam.grab() # note that grab() function returnt only status code,not the frame
推荐阅读
- php - php unix时间戳与strtotime
- dart - 当我滑回上一页时,使用“webview_flutter”或“flutter_inappbrowser”的颤振应用程序卡住了
- python - Python多处理:在进程之间共享数据
- sqlalchemy - 关于 SQLAlchemy 对象与数据库同步的混淆
- scheme - 我试图弄清楚如何将 3 个变量合并到我的球拍尾递归代码中
- jsf - 单击第一个复选框时如何检查另一个复选框?我的复选框列表从数据文件中检索
- google-cloud-platform - 为 Pubsub 到 Bigquery 流式传输构建此 GCP 数据流示例时出错
- python - 将 \n 替换为 " "
- java - [HttpURLConnection]如何发送带有表单数据的post请求
- python - 修复字典中的排序