当前位置:网站首页>解决OpenCV读取视频结束后报错的问题
解决OpenCV读取视频结束后报错的问题
2022-07-20 04:33:00 【Hong_Youth】
最近由于项目的需要,经常需要使用opencv读视频操作,会经常遇到一个问题,就是视频读完之后会出现报错的情况,虽然不影响代码结果,但是对于吹毛求疵的我实在忍受不了。
会报错的程序:
# -*-coding:utf-8-*-
"""
File Name: read_video.py
Program IDE: PyCharm
Create File By Author: Hong
"""
import cv2
def read_video(video_path: str):
"""
OpenCV读视频小工具,解决视频读完报错的问题
:param video_path: 输入需要读取的视频文件路径
:return: 没有返回值
"""
print('视频路径:', video_path)
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
# get a frame
ret, frame = cap.read()
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print('视频读完!')
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
path = r'D:\multi_insect_videos\multi_object00.mp4'
read_video(path)
可以把视频正常读完,但是结果会输出错误
Traceback (most recent call last):
File "E:/PyCharmDocument/create_ST_image/multi_insect_processing/crop_video_to_images.py", line 76, in <module>
read_video(path)
File "E:/PyCharmDocument/create_ST_image/multi_insect_processing/crop_video_to_images.py", line 65, in read_video
cv2.imshow("capture", frame)
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-uzca7qz1\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
原因是视频结束后,opencv的imshow()函数读不了帧就出错了。解决方法:在imshow()之前加入判断,只有当有帧时才读,没有帧时直接退出循环。
不会报错的代码:
# -*-coding:utf-8-*-
"""
File Name: read_video.py
Program IDE: PyCharm
Create File By Author: Hong
"""
import cv2
def read_video(video_path: str):
"""
OpenCV读视频小工具,解决视频读完报错的问题
:param video_path: 输入需要读取的视频文件路径
:return: 没有返回值
"""
print('视频路径:', video_path)
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
# get a frame
ret, frame = cap.read()
if not ret:
break
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print('视频读完!')
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
path = r'D:\multi_insect_videos\multi_object00.mp4'
read_video(path)
程序输出结果:
视频路径: D:\multi_insect_videos\multi_object00.mp4
视频读完!
Process finished with exit code 0
更多Python小工具请参考GitHub仓库:python-script-tools/read_video.py at master · YouthJourney/python-script-tools · GitHub
边栏推荐
- 得物App数据模拟平台的探索和实践
- I summarize some experience of the whole R & D process
- C语言中getchar()函数的详解
- The beta version of typera is expired and cannot be used normally
- 阿里云国际版账号注册常见问题汇总
- 独立站卖家如何利用Facebook主页进行社交媒体营销?
- 检查日期是否有效
- How does HMS core security detection service help freshmen prevent Telecom fraud?
- 开发者必读:2022年移动应用运营增长洞察白皮书
- Huawei's general card identification function enables multiple card bindings with one key
猜你喜欢
fiddler5+雷电模拟器4.0对app抓包设置
解读Go分布式链路追踪实现原理
NFT fashion shoes ar interactive zero foundation tutorial is coming!
Difi: a go as you pay Wi Fi access system intensive reading notes (II)
百度交易中台之钱包系统架构浅析
织梦DEDE后台系统用户管理显示空白的解决方法
How to set percentage color for Excel data bar? Excel data bar auto fill color by percentage tutorial
Excel打印技巧 十二种Excel打印技巧汇总
Word文档怎么删除页眉页脚和横线
This beta version of Typora is expired,please download and install a newer version.
随机推荐
开发中常见环境配置名词-dev、sit、pro、fac等
Developers must read: 2022 mobile application operation growth insight white paper
Love to learn blog - life is short, only good classes!
Chinese anchors can also bring goods overseas! Simultaneous interpretation helps live broadcast applications expand overseas markets
How to configure access control in Gerrit system
阿里云国际版账号注册常见问题汇总
【MUDUO 】Channel通道
Introduction, installation and basic use of mongodb database
A detailed explanation of the implementation principle of go Distributed Link Tracking
gerrit系统如何配置访问控制
软件界面和简单系统仿真
apple 为什么要改 objc
分布式笔记(05)— 分布式锁之 etcd(分布式锁原理、etcd特点、分布式锁实现方案)
What is the registration process of website domain name?
Software interface and simple system simulation
Alibaba cloud international account registration FAQ summary
检查日期是否有效
Capacity scheduling absolute value configuration queue usage and pit avoidance
记录uni-app 打包成apk后获取定位
From concept to security practice: a basic guide to software supply chain