cv::VideoCapture video(0); int current_frame=static_cast<int>(video.get(CV_CAP_PROP_POS_FRAMES));
时间: 2024-05-31 07:14:51 浏览: 123
这段代码使用了OpenCV库中的VideoCapture类来打开默认的摄像头,并获取当前帧的位置。其中,CV_CAP_PROP_POS_FRAMES是一个常量,用于获取当前帧的位置。但需要注意的是,该常量在较新版本的OpenCV库中已经被弃用,可以使用CAP_PROP_POS_FRAMES常量替代。同时,获取到的当前帧位置是一个双精度浮点型,需要使用static_cast转换为整型。
相关问题
import sys import cv2 from showPic import Ui_MainWindow from PyQt5 import QtGui from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class videoShow(QMainWindow,Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) @pyqtSlot() def on_pushButton_record_clicked(self): 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也是这个尺寸) while True: flag, frame = capture.read() if flag is False: continue frame = cv2.resize(frame, (960, 540)) video_writer.write(frame) cv2.imshow("video", frame) key = cv2.waitKey(25) if key == 27: video_writer.release() break if __name__ == '__main__': app = QApplication(sys.argv) ui = videoShow() ui.show() sys.exit(app.exec_())在这样代码的基础上添加点击按钮播放已经录制好的视频
您可以在`videoShow`类中添加一个点击按钮播放已经录制好的视频的槽函数,然后在UI文件中添加一个按钮,并将其与该槽函数关联。以下是修改后的代码示例:
```python
import sys
import cv2
from showPic import Ui_MainWindow
from PyQt5 import QtGui
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class videoShow(QMainWindow,Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
@pyqtSlot()
def on_pushButton_record_clicked(self):
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也是这个尺寸)
while True:
flag, frame = capture.read()
if flag is False:
continue
frame = cv2.resize(frame, (960, 540))
video_writer.write(frame)
cv2.imshow("video", frame)
key = cv2.waitKey(25)
if key == 27:
video_writer.release()
break
@pyqtSlot()
def on_pushButton_play_clicked(self):
video_path = "image/myself.mp4" # 已经录制好的视频路径
capture = cv2.VideoCapture(video_path) # 初始化播放器
while True:
flag, frame = capture.read()
if flag is False:
break
cv2.imshow("video", frame)
key = cv2.waitKey(25)
if key == 27:
break
capture.release()
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = videoShow()
ui.show()
sys.exit(app.exec_())
```
在修改后的代码中,我们添加了一个名为`on_pushButton_play_clicked`的槽函数,并在该函数中使用`cv2.VideoCapture`打开已经录制好的视频,并通过`cv2.imshow`显示每一帧。您需要在UI文件中添加一个名为`pushButton_play`的按钮,并将其与该槽函数关联。当您点击该按钮时,录制好的视频将会开始播放。
if __name__ == "__main__": unet = Unet() mode = "fps" video_path = "ID01.mp4" video_save_path = "ID01dect3.mp4" video_fps = 50.0 test_interval = 1000 dir_origin_path = "img/" dir_save_path = "img_out/" if mode == "predict": seg_img = np.zeros((np.shape(pr)[0],np.shape(pr)[1],3)) for c in range(self.num_classes): seg_img[:, :, 0] += ((pr == c)*( self.colors[c][0] )).astype('uint8') seg_img[:, :, 1] += ((pr == c)*( self.colors[c][1] )).astype('uint8') seg_img[:, :, 2] += ((pr == c)*( self.colors[c][2] )).astype('uint8') ''' while True: img = input('Input image filename:') try: image = Image.open(img) except: print('Open Error! Try again!') continue else: r_image = unet.detect_image(image) r_image.show() elif mode == "video": capture=cv2.VideoCapture(video_path) if video_save_path!="": fourcc = cv2.VideoWriter_fourcc(*'XVID') size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) out = cv2.VideoWriter(video_save_path, fourcc, video_fps, size) ref, frame = capture.read() if not ref: raise ValueError("未能正确读取摄像头(视频),请注意是否正确安装摄像头(是否正确填写视频路径)。") fps = 0.0 while(True): t1 = time.time() # 读取某一帧 ref, frame = capture.read() if not ref: break # 格式转变,BGRtoRGB frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB) # 转变成Image frame = Image.fromarray(np.uint8(frame)) # 进行检测 frame = np.array(unet.detect_image(frame)) # RGBtoBGR满足opencv显示
这段代码是一个使用Unet模型进行图像分割的程序。其中,`mode`变量可以设置为`predict`或`video`,分别代表单张图片的预测和视频流的分割。
在`predict`模式下,程序会读取一个图片,然后使用Unet模型进行分割,最后将分割结果显示出来。
在`video`模式下,程序会读取一个视频文件,然后对每一帧进行分割,最后将分割后的视频保存下来。
在进行分割时,程序会将分割结果可视化为彩色图像,其中每一类的像素点会被标记为不同的颜色。
阅读全文