std::string file_name = "f:/air-4cam-mp4/00.mp4"; cv::VideoCapture capture(file_name); if (!capture.isOpened()){ std::cout << "fail to open!" << std::endl; }无法打开视频

时间: 2024-03-27 20:41:56 浏览: 17
有几种可能的原因导致无法打开视频: 1. 视频文件不存在或路径错误。请确保视频文件路径和文件名正确,并且视频文件存在。 2. 视频文件格式不受支持。请确保使用的OpenCV库支持该视频文件格式。您可以尝试使用不同的视频文件格式或安装适当的解码器。 3. 视频文件已被损坏。您可以尝试使用其他视频播放器打开该文件以检查其是否可以正常播放。 4. 没有足够的权限读取视频文件。请确保您具有读取视频文件的权限。 您可以尝试检查这些可能的原因,以确定无法打开视频的具体原因。
相关问题

cv::VideoCapture capture(file_name);可以打开本地摄像头,却无法打开本地mp4视频

`cv::VideoCapture`可以用来打开本地mp4视频文件,你需要将`file_name`设置为本地mp4视频的文件路径。如果无法打开本地视频,可能是文件路径设置有误或者文件格式不受支持。你可以尝试重新指定正确的文件路径并检查文件格式是否被支持。如果问题仍然存在,可能需要检查你的代码是否有其他错误。另外,如果你使用的是OpenCV 2.x版本,请尝试升级到OpenCV 3.x或更高版本,因为OpenCV 2.x版本可能无法正常读取mp4格式的视频文件。

将以下程序转换成python程序 #include <opencv2/opencv.hpp> #include <iostream> #include<ctime> int main(int argc,char** argv) { clock_t start,end; int cam_num = 1; // 1,2 the number of cameras used // nano_id dev_id port_id 位置 // 13 0 9202 下巴 // 13 1 9201 前方 // 14 0 9203 左方 // 14 1 9204 右方 // 15 0 9205 腹部(默认) std::string IpLastSegment = "15"; int cam_id = 0; // the id of the camera used if cam_num is 1 if (argc>=2) cam_id = std::atoi(argv[1]); int udpPORT1 = 9201; // port_id of the camera which was used int udpPORT2 = 9202; // port_id of the camera which was used std::string udpstrPrevData = "udpsrc address=192.168.123."+ IpLastSegment + " port="; std::string udpstrBehindData = " ! application/x-rtp,media=video,encoding-name=H264 ! rtph264depay ! h264parse ! omxh264dec ! videoconvert ! appsink"; std::string udpSendIntegratedPipe1 = udpstrPrevData + std::to_string(udpPORT1) + udpstrBehindData; std::string udpSendIntegratedPipe2 = udpstrPrevData + std::to_string(udpPORT2) + udpstrBehindData; std::cout<<"udpSendIntegratedPipe1:"<<udpSendIntegratedPipe1<<std::endl; std::cout<<"udpSendIntegratedPipe2:"<<udpSendIntegratedPipe2<<std::endl; cv::VideoCapture cap1(udpSendIntegratedPipe1); cv::VideoCapture cap2(udpSendIntegratedPipe2); if(!cap1.isOpened()) return 0 ; if(!cap2.isOpened()) return 0 ; cv::Mat frame1, frame2; while(1) { start=clock(); //程序开始计时 cap1 >> frame1; cap2 >> frame2; if(frame1.empty()) break; if(frame2.empty()) break; imshow("video1", frame1); imshow("video2", frame2); end=clock(); double endtime=(double)(end-start)/CLOCKS_PER_SEC; std::cout << "FPS:"<<1/endtime<<"/s"<<std::endl; //ms为单位 char key = cv::waitKey(1); if(key == 27) // press ESC key break; } cap1.release();//释放资源 cap2.release();//释放资源 } return 0; }

以下是将该程序转换为Python程序的代码: import cv2 def main(cam_id): cam_num = 1 # 1,2 the number of cameras used IpLastSegment = "15" udpPORT1 = 9201 # port_id of the camera which was used udpPORT2 = 9202 # port_id of the camera which was used udpstrPrevData = "udpsrc address=192.168.123." + IpLastSegment + " port=" udpstrBehindData = " ! application/x-rtp,media=video,encoding-name=H264 ! rtph264depay ! h264parse ! omxh264dec ! videoconvert ! appsink" udpSendIntegratedPipe1 = udpstrPrevData + str(udpPORT1) + udpstrBehindData udpSendIntegratedPipe2 = udpstrPrevData + str(udpPORT2) + udpstrBehindData print("udpSendIntegratedPipe1:", udpSendIntegratedPipe1) print("udpSendIntegratedPipe2:", udpSendIntegratedPipe2) cap1 = cv2.VideoCapture(udpSendIntegratedPipe1) cap2 = cv2.VideoCapture(udpSendIntegratedPipe2) if not cap1.isOpened(): return 0 if not cap2.isOpened(): return 0 while True: start = cv2.getTickCount() # 程序开始计时 ret1, frame1 = cap1.read() ret2, frame2 = cap2.read() if not ret1: break if not ret2: break cv2.imshow("video1", frame1) cv2.imshow("video2", frame2) end = cv2.getTickCount() elapsed_time = (end - start) / cv2.getTickFrequency() fps = 1 / elapsed_time print("FPS:", fps, "/s") # ms为单位 key = cv2.waitKey(1) if key == 27: # press ESC key break cap1.release() # 释放资源 cap2.release() # 释放资源 if __name__ == "__main__": cam_id = 0 # the id of the camera used if cam_num is 1 main(cam_id)

相关推荐

将#!/usr/bin/env python2.7 -- coding: UTF-8 -- import time import cv2 from PIL import Image import numpy as np from PIL import Image if name == 'main': rtsp_url = "rtsp://127.0.0.1:8554/live" cap = cv2.VideoCapture(rtsp_url) #判断摄像头是否可用 #若可用,则获取视频返回值ref和每一帧返回值frame if cap.isOpened(): ref, frame = cap.read() else: ref = False #间隔帧数 imageNum = 0 sum=0 timeF = 24 while ref: ref,frame=cap.read() sum+=1 #每隔timeF获取一张图片并保存到指定目录 #"D:/photo/"根据自己的目录修改 if (sum % timeF == 0): # 格式转变,BGRtoRGB frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 转变成Image frame = Image.fromarray(np.uint8(frame)) frame = np.array(frame) # RGBtoBGR满足opencv显示格式 frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) imageNum = imageNum + 1 cv2.imwrite("/root/Pictures/Pictures" + str(imageNum) + '.png', frame) print("success to get frame") #1毫秒刷新一次 k = cv2.waitKey(1) #按q退出 #if k==27:则为按ESC退出 if k == ord('q'): cap.release() break和#!/usr/bin/env python2.7 coding=UTF-8 import os import sys import cv2 from pyzbar import pyzbar def main(image_folder_path, output_file_name): img_files = [f for f in os.listdir(image_folder_path) if f.endswith(('.png'))] qr_codes_found = [] print("Image files:") for img_file in img_files: print(img_file) for img_file in img_files: img_path = os.path.join(image_folder_path,img_file) img = cv2.imread(img_path) barcodes = pyzbar.decode(img) for barcode in barcodes: if barcode.type == 'QRCODE': qr_data = barcode.data.decode("utf-8") qr_codes_found.append((img_file, qr_data)) unique_qr_codes = [] for file_name, qr_content in qr_codes_found: if qr_content not in unique_qr_codes: unique_qr_codes.append(qr_content) with open(output_file_name,'w') as f: for qr_content in unique_qr_codes: f.write("{}\n".format(qr_content)) if name == "main": image_folder_path = '/root/Pictures' output_file_name = 'qr_codes_found.txt' main(image_folder_path,output_file_name)合并成一个代码

import cv2 import face_recognition import numpy as np from PIL import Image, ImageDraw,ImageFont video_capture = cv2.VideoCapture(r'C:/Users/ALIENWARE/123.mp4')#如果输入是(0)为摄像头输入 #现输入为MP4进行识别检测人脸 first_image = face_recognition.load_image_file("1.jpg") first_face_encoding = face_recognition.face_encodings(first_image)[0] Second_image = face_recognition.load_image_file("2.jpg") Second_face_encoding = face_recognition.face_encodings(Second_image)[0] third_image = face_recognition.load_image_file("3.jpg") third_face_encoding = face_recognition.face_encodings(third_image)[0] inside_face_encodings = [first_face_encoding,Second_face_encoding,third_face_encoding] inside_face_names = ['A','B','C'] face_locations = [] face_encodings = [] face_names = [] process_this_frame = True while True: ret, frame = video_capture.read() small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) rgb_small_frame = small_frame[:, :, ::-1] if process_this_frame: face_locations = face_recognition.face_locations(rgb_small_frame) face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations) face_names = [] for face_encoding in face_encodings: matches = face_recognition.compare_faces(inside_face_encodings, face_encoding) name = '未录入人脸' if True in matches: first_match_index = matches.index(True) name = inside_face_names[first_match_index] face_names.append(name) process_this_frame = not process_this_frame for (top, right, bottom, left), name in zip(face_locations, face_names): top *= 4 right *= 4 bottom *= 4 left *= 4 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) img_pil = Image.fromarray(frame) draw = ImageDraw.Draw(img_pil) fontStyle = ImageFont.truetype("C:/Windows/Fonts/simsun.ttc", 32, encoding="utf-8") draw.text((left + 6, bottom - 6), name, (0, 200, 0), font=fontStyle) frame = np.asarray(np.array(img_pil)) cv2.imshow('face_out', frame) if cv2.waitKey(1) & 0xFF == ord('q'): #退出需要按下Q键否则内核会崩溃 break video_capture.release() cv2.destroyAllWindows()

import cv2 import sys import os import time from PyQt5 import QtGui #重新导入 from PyQt5 import QtCore #重新导入 from showPic import Ui_MainWindow from PyQt5.QtCore import * from PyQt5.QtGui import * #导入的外面 from PyQt5.QtWidgets import * camera_path = 0 # 0:自带摄像头 1:外接摄像头 "xxx.mp4" "rtsp://admin:pwd@192.168.2.10/cam/..." capture = cv2.VideoCapture(camera_path) # 初始化播放器 流媒体 fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V') # XVID/DIVX MPEG MJPG X264 video_writer = cv2.VideoWriter("image/myself.mp4", fourcc, 25, (960, 540)) # 存放路径、、帧率fps、尺寸(且保证下面的frame也是这个尺寸) class videoShow(QMainWindow, Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) self.dir_path = r"E:\pycharm\new_subject\image/" self.pushButton_play.clicked.connect(self.play_video) self.pushButton_pause.clicked.connect(self.pause_video) def play_video(self): self.playing = True def pause_video(self): self.playing = False def timer_pic(self): image_name = self.dir_path + self.file_list[self.n] url = image_name pic_image = cv2.imread(url) pic_image = cv2.cvtColor(pic_image, cv2.COLOR_BGR2RGB) # 将BGR格式图像转换成RGB height, width = pic_image.shape[:2] pixMap = QImage(pic_image.data, width, height, width*3, QImage.Format_RGB888) # 将RGB格式图像转换为八位图 pixMap = QPixmap.fromImage(pixMap) ratio = max(width/self.label.width(), height/self.label.height()) pixMap.setDevicePixelRatio(ratio) # 根据图片比例显示 self.label.setAlignment(Qt.AlignCenter) # 设置居中 self.label.setPixmap(pixMap) if self.playing: flag, frame = capture.read() if flag is False: return frame = cv2.resize(frame, (960, 540)) video_writer.write(frame) cv2.namedWindow("video", 0) cv2.imshow("video", frame) key = cv2.waitKey(25) if key == 27: video_writer.release() cv2.destroyAllWindows() sys.exit(0) if __name__ == '__main__': app = QApplication(sys.argv) ui = videoShow() ui.show() sys.exit(app.exec_()) 优化这段代码,实现录制视频以及点击按钮实现录制、播放、暂停

最新推荐

recommend-type

####这是一篇对python的详细解析

python
recommend-type

菜日常菜日常菜日常菜日常

菜日常菜日常菜日常菜日常
recommend-type

VB学生档案管理系统设计(源代码+论文).rar

计算机专业毕业设计VB精品论文资源
recommend-type

电商到底怎么做?淘系电商三维经营心法(59节课)-课程网盘链接提取码下载 .txt

课程内容: 10-经营常见4大循环-被资本绑架思维.mp4 11-落地中的47个坑-产品坑.mp4 12-落地中的47个坑-一把手坑.mp4 13-落地中的47个坑-迷信坑.mp4 14-落地中的47个坑-缺乏坑.mp4 15-落地中的47个坑-团队坑.mp4 16-电商经营常见导致的10种挂法.mp4 18-淘系电商干法介绍.mp4 19-淘系电商的特点.mp4 20-淘系买家购物场景.mp4 21-淘系干法-标品.mp4 22-淘系电商干法-半标品.mp4 23-答疑.mp4 25-3类7种常见“干法”模型.mp4 26-6类产品日常运营报表.mp4 27-6类产品日常运营报表-高客单价店铺.mp4 28-6类产品运营报表-低客单价.mp4 29-6类产品运营报表-爆款数据模型季节性商品.mp4 2-前言.mp4 30-6类产品日常运营-标品.mp4 31-6类产品日常运营报表-非标品.mp4 32-6类产品日常运营报表-全店客服.mp4 33-执行就是一条:运营公式.mp4 35-搜索算法逻辑.mp4 36-2024年词层分层“激
recommend-type

grpcio-1.63.0-cp312-cp312-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。