【问题标题】:how to take a picture with opencv and visualize the webcam at the same time如何用opencv拍照并同时可视化网络摄像头
【发布时间】:2019-02-05 00:13:58
【问题描述】:

在下面的代码中,我在QLabel 中显示了电脑的网络摄像头。 但是,当尝试使用self.boton1 按钮拍照时,他没有拍照。 Self.boton1 连接到def take ()函数,这是我用来拍照的函数。

但它不起作用我希望你能帮助我:

尝试将self.boton1.clicked.connect (self.take (self.capture)) 放在setup_camera () 函数中,以将捕获的数据作为参数传递给take () 函数self. capture,但它不起作用

from PyQt5.QtWidgets import QMainWindow,QApplication
import cv2
from PyQt5 import QtCore
import numpy as np
from PyQt5 import QtGui
from PyQt5 import uic


class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("f.ui",self)

        self.boton1.clicked.connect(self.take)
        self.video_size = QtCore.QSize(320,240)
        self.setup_camera()


        uic.loadUi("f.ui",self)
    def setup_camera(self):
        self.capture = cv2.VideoCapture(0)
        self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
        self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT,self.video_size.height())
        #self.Bfoto.clicked.connect(lambda:self.take(self.capture))

        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.display_video_stream)
        self.timer.start(30)

    def display_video_stream(self):
        _,frame  =self.capture.read()
        frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
        frame = cv2.flip(frame,1)
        image = QtGui.QImage(frame,frame.shape[1],frame.shape[0],frame.strides[0],QtGui.QImage.Format_RGB888)
        self.label.setPixmap(QtGui.QPixmap.fromImage(image))

    def take(self):
        print("value")
        cap = videoCapture(0)
        leido,frame = cap.read()

        if leido ==True:
            cv2.imwrite("photo.png",frame)
            print("ok")
        else:
            print("error")
        cap.release()
app = QApplication([])
m = Main()
m.show()
app.exec_()

f.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>671</width>
    <height>519</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="boton1">
    <property name="geometry">
     <rect>
      <x>530</x>
      <y>400</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>foto</string>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>30</y>
      <width>481</width>
      <height>311</height>
     </rect>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>671</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

希望通过按下self.boton按钮,触发def take ()功能并拍照

【问题讨论】:

    标签: python python-3.x opencv


    【解决方案1】:

    问题

    您的 python 代码主要有两个问题。 1. 你在 init() 块中加载 f.ui 两次

    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("f.ui",self)
    
        self.boton1.clicked.connect(self.take)
        self.video_size = QtCore.QSize(320,240)
        self.setup_camera()
    
    
        uic.loadUi("f.ui",self)
    

    所以在第二个 uic.loadUi() 中,之前的初始化被清除了,这就是你的按钮点击事件不起作用的原因。

    2.

    def take(self):
        print("value")
        cap = videoCapture(0)
        leido,frame = cap.read()
    
        if leido ==True:
            cv2.imwrite("photo.png",frame)
            print("ok")
        else:
            print("error")
        cap.release()
    

    在这个块中,您可以使用之前的 self.capture 对象,这样您就可以只处理一个对象,这使得它变得简单。而且由于这个对象应该只在你退出这个程序时释放,所以你不需要在这里使用 cap.release()

    解决方案

    1. 从您的代码中删除第二个 uic.loadui() 行

      def init(自我): QMainWindow.init(self) uic.loadUi("f.ui",self)

      self.boton1.clicked.connect(self.take)
      self.video_size = QtCore.QSize(320,240)
      self.setup_camera()
      #uic.loadUi("f.ui",self)
      
    2. take() 块应该是这样的

      打印(“价值”) cap = self.capture leido,frame = cap.read()

      if leido ==True:
          cv2.imwrite("photo.png",frame)
          print("ok")
      else:
          print("error")
          cap.release()
      

    【讨论】:

    • 米特拉。新错误:file "C:\Users\Angel\Desktop\s.py", line 30, in display_video_stream frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB) cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
    • @kousikMiltra 新错误:C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
    • 你用的是哪个版本的opencv?你能把“display_video_stream”的代码块发给我吗?
    猜你喜欢
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多