当前位置:   article > 正文

pyqt5+opencv对图片和视频的简单处理_pyqt+opencv录视频、播放视频

pyqt+opencv录视频、播放视频

功能

读取图片

读取视频

录制视频

正文

  1. import os
  2. import sys
  3. from PyQt5.QtWidgets import *
  4. from PyQt5.QtGui import *
  5. from PyQt5.QtCore import *
  6. import cv2 as cv
  7. import numpy as np
  8. from init import picturepath
  9. from ffpyplayer.player import MediaPlayer
  10. # 路径有中文,cv,imread,读不出
  11. def imread(path):
  12. img = cv.imdecode(np.fromfile(path, dtype=np.uint8), -1)
  13. return img
  14. class a(QWidget):
  15. def __init__(self):
  16. super().__init__()
  17. self.initui()
  18. def initui(self):
  19. self.resize(1085, 729)
  20. self.setWindowTitle("视频与图片")
  21. self.play_video = QPushButton(self)
  22. self.play_video.setText("播放视频")
  23. self.play_video.clicked.connect(self.ok)
  24. self.play_video.setGeometry(30, 40, 93, 28)
  25. self.open_pic = QPushButton(self)
  26. self.open_pic.clicked.connect(self.pic)
  27. self.open_pic.setGeometry(130, 40, 93, 28)
  28. self.open_capture = QPushButton(self)
  29. self.open_capture.setGeometry(240, 40, 93, 28)
  30. self.open_capture.setText("录制视频")
  31. self.open_capture.clicked.connect(self.get_video)
  32. self.save = QLineEdit(self)
  33. self.save.setPlaceholderText('写视频的名字(1.mp4)')
  34. self.save.setGeometry(350, 40, 160, 28)
  35. self.label = QLabel(self)
  36. self.open_pic.setText("打开图片")
  37. # 录制视频
  38. def get_video(self):
  39. if self.save.text():
  40. video = cv.VideoCapture(0)
  41. fourcc = cv.VideoWriter_fourcc(*'mp4v')
  42. save = cv.VideoWriter(f'{self.save.text()}.mp4', fourcc, 24, (640, 480))
  43. while True:
  44. ok, frame = video.read()
  45. if not ok:
  46. break
  47. self.label.setGeometry(30,100,640,480)
  48. data = cv.cvtColor(frame, cv.COLOR_RGB2BGR)
  49. img = QImage(frame,640,400, QImage.Format_RGB888)
  50. self.label.setPixmap(QPixmap(img).scaled(640,480))
  51. save.write(frame)
  52. if cv.waitKey(1) == 27:
  53. break
  54. video.release()
  55. save.release()
  56. cv.destroyAllWindows()
  57. else:
  58. QMessageBox.information(self, '消息', '视频的名字为空', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
  59. # 打开视频,有声音
  60. def ok(self):
  61. filename, ok = QFileDialog.getOpenFileName(self, '打开视频', os.getcwd(), '(*.mp4 *.avi)')
  62. if ok:
  63. video = cv.VideoCapture(filename)
  64. z = video.get(5)
  65. player=MediaPlayer(filename)
  66. while True:
  67. yes,data = video.read()
  68. frame, val=player.get_frame()
  69. if val=='eof':
  70. break
  71. if not yes:
  72. break
  73. data = cv.cvtColor(data, cv.COLOR_BGR2RGB)
  74. h,w,_=data.shape
  75. self.label.setGeometry(500,0, h, w)
  76. img = QImage(data.data,w,h, QImage.Format_RGB888)
  77. self.label.setPixmap(QPixmap(img).scaled(w,h))
  78. key = cv.waitKey(1000 // int(z))
  79. video.release()
  80. cv.destroyAllWindows()
  81. img = imread(picturepath+'/31.jpg')
  82. w, h, _= img.shape
  83. self.label.setGeometry(30, 100,int(h/2),int(w/2))
  84. self.label.setPixmap(QPixmap(picturepath+'/31.jpg').scaled(self.label.width(),self.label.width(),self.label.height()))
  85. else:
  86. pass
  87. # 打开图片
  88. def pic(self):
  89. filename, ok = QFileDialog.getOpenFileName(self, '打开图片', os.getcwd(), '(*.jpg *.png)')
  90. if ok:
  91. img = imread(filename)
  92. w, h, _= img.shape
  93. self.label.setGeometry(30, 100,int(h/2),int(w/2))
  94. self.label.setPixmap(QPixmap(filename).scaled(self.label.width(),self.label.height()))
  95. else:
  96. pass
  97. if __name__ == '__main__':
  98. app = QApplication(sys.argv)
  99. app.setStyle(QStyleFactory.create('Fusion'))
  100. a = a()
  101. a.show()
  102. sys.exit(app.exec_())

说明

1,用了文件对话框,选择视频或者图片文件。

2,opencv读的是图片,没有声音,用了第三方库ffpyplayer,我运行的时候有bug

[ffpyplayer_abuffersink @ 000002446343da40] Removing channel layout 0x3, redundant with 2 channels

不知道什么情况,没有解决

操作

主页面

打开图片

选择不同的图片,label会根据图片动态变化大小。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/103623
推荐阅读
相关标签
  

闽ICP备14008679号