# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'fruitcamera.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(989, 685)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.btnopen = QtWidgets.QPushButton(self.centralwidget)
self.btnopen.setGeometry(QtCore.QRect(850, 110, 121, 31))
self.btnopen.setStyleSheet("background-color: rgb(0, 170, 255);\n"
"font: 18pt \"楷体\";")
self.btnopen.setObjectName("btnopen")
self.btncapture = QtWidgets.QPushButton(self.centralwidget)
self.btncapture.setGeometry(QtCore.QRect(850, 170, 121, 31))
self.btncapture.setStyleSheet("background-color: rgb(0, 255, 0);\n"
"font: 18pt \"楷体\";")
self.btncapture.setObjectName("btncapture")
self.btnread = QtWidgets.QPushButton(self.centralwidget)
self.btnread.setGeometry(QtCore.QRect(850, 230, 121, 31))
self.btnread.setStyleSheet("background-color: rgb(255, 0, 255);\n"
"font: 18pt \"楷体\";")
self.btnread.setObjectName("btnread")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(20, 50, 791, 371))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.label = QtWidgets.QLabel(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 0, 1, 1, 1)
self.leftlabel = QtWidgets.QLabel(self.layoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.leftlabel.sizePolicy().hasHeightForWidth())
self.leftlabel.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(19)
font.setBold(True)
font.setWeight(75)
self.leftlabel.setFont(font)
self.leftlabel.setAlignment(QtCore.Qt.AlignCenter)
self.leftlabel.setObjectName("leftlabel")
self.gridLayout.addWidget(self.leftlabel, 1, 0, 1, 1)
self.rightlabel = QtWidgets.QLabel(self.layoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.rightlabel.sizePolicy().hasHeightForWidth())
self.rightlabel.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.rightlabel.setFont(font)
self.rightlabel.setAlignment(QtCore.Qt.AlignCenter)
self.rightlabel.setObjectName("rightlabel")
self.gridLayout.addWidget(self.rightlabel, 1, 1, 1, 1)
self.btncut = QtWidgets.QPushButton(self.centralwidget)
self.btncut.setGeometry(QtCore.QRect(850, 290, 121, 31))
self.btncut.setStyleSheet("background-color: rgb(255, 255, 0);\n"
"font: 18pt \"楷体\";")
self.btncut.setObjectName("btncut")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 989, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.btnopen.clicked.connect(MainWindow.btnopenclick)
self.btncapture.clicked.connect(MainWindow.btncaptureclick)
self.btnread.clicked.connect(MainWindow.btnreadclick)
self.btncut.clicked.connect(MainWindow.btncutclick)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.btnopen.setText(_translate("MainWindow", "采集图像"))
self.btncapture.setText(_translate("MainWindow", "捕捉图像"))
self.btnread.setText(_translate("MainWindow", "读取图像"))
self.label.setText(_translate("MainWindow", "左相机"))
self.label_2.setText(_translate("MainWindow", "右相机"))
self.leftlabel.setText(_translate("MainWindow", "左相机图像"))
self.rightlabel.setText(_translate("MainWindow", "右相机图像"))
self.btncut.setText(_translate("MainWindow", "图像处理"))
要是忘记了海康威视相机接口密码,通过下面软件截图更改
电脑IP记得更改
注意下面的程序我只写了基于海康威视相机录制视频,捕获照片,处理照片的信号与槽,其他的大家可以基于本程序进一步丰富修改,在单个槽函数加自己的
import sys, os
if hasattr(sys, 'frozen'):
os.environ['PATH'] = sys._MEIPASS + ";" + os.environ['PATH']
import PyQt5.sip
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QFileDialog, QMainWindow
from PyQt5.QtCore import QTimer
from PyQt5.QtCore import QDate, QTime, QDateTime
from PyQt5.QtWidgets import QAction
from fruitcamera import Ui_MainWindow
import matplotlib.pyplot as plt
import math
import numpy
import numpy as np
from functools import reduce
import cv2
from PIL import Image
from numpy import *
from scipy import optimize
import functools
class mywindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.setWindowTitle('农机自主识别双目视觉系统V1')
self.setWindowIcon(QIcon('nongji.png')) # 设置窗体标题图标
# 相机参数设置
url = "rtsp://admin:[email protected]/Streaming/Channels/1"
ur2 = "rtsp://admin:[email protected]/Streaming/Channels/1"
self.camera1 = cv2.VideoCapture(url)
self.camera2 = cv2.VideoCapture(ur2)
# 设置摄像头分辨率
self.camera1.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
self.camera1.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
self.camera2.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
self.camera2.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
self.camera1.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
self.camera2.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
self.is_camera1_opened = False # 摄像头有没有打开标记
self.is_camera2_opened = False
# 定时器:30ms捕获一帧
self._timer = QtCore.QTimer(self)
self._timer.timeout.connect(self._queryFrame)
self._timer.setInterval(5)
#初始参数设置
self.m=0
self.n= 0
self.filename= ''
self.result1 = ''
self.result2 = ''
def btnopenclick(self):
''' 打开和关闭摄像头'''
self.is_camera1_opened = ~self.is_camera1_opened
self.is_camera2_opened = ~self.is_camera2_opened
if self.is_camera1_opened and self.is_camera2_opened:
self.btnopen.setText("关闭摄像头")
self._timer.start()
else:
self.btnopen.setText("打开摄像头")
self._timer.stop()
def btnreadclick(self):
# 从本地读取图片 文件路径不能有中文,打开文件选取对话框
filename, _ = QFileDialog.getOpenFileName(self, '打开图片')
if filename:
self.frame1= cv2.imread(str(filename))
self.frame2 = cv2.imread(str(filename))
# OpenCV图像以BGR通道存储,显示时需要从BGR转到RGB
self.frame1= cv2.cvtColor(self.frame1, cv2.COLOR_BGR2RGB)
self.frame2 = cv2.cvtColor(self.frame2, cv2.COLOR_BGR2RGB)
rows, cols, channels = self.frame1.shape
bytesPerLine = channels * cols
QImg1 = QImage(self.frame1.data, cols, rows, bytesPerLine, QImage.Format_RGB888)
self.leftlabel.setPixmap(QPixmap.fromImage(QImg1).scaled(
self.leftlabel.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
QImg2 = QImage(self.frame2.data, cols, rows, bytesPerLine, QImage.Format_RGB888)
self.rightlabel.setPixmap(QPixmap.fromImage(QImg2).scaled(
self.rightlabel.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
def btncaptureclick(self):
# 捕获图片,摄像头未打开,不执行任何操作
if not self.is_camera1_opened and not self.is_camera2_opened :
return
else:
cv2.imwrite('C:/Users/Administrator/Desktop/camera/left/' + str(self.m) + '.bmp', self.frame1)
cv2.imwrite('C:/Users/Administrator/Desktop/camera/right/' + str(self.m) + '.bmp', self.frame2)
self.m = self.m + 1
# 后面这几行代码几乎都一样,可以尝试封装成一个函数
rows, cols, channels = self.frame1.shape
bytesPerLine = channels * cols
QImg1 = QImage(self.frame1.data, cols, rows, bytesPerLine, QImage.Format_RGB888)
self.leftlabel.setPixmap(QPixmap.fromImage(QImg1).scaled(
self.leftlabel.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
QImg2 = QImage(self.frame2.data, cols, rows, bytesPerLine, QImage.Format_RGB888)
self.rightlabel.setPixmap(QPixmap.fromImage(QImg2).scaled(
self.rightlabel.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
def btncutclick(self):
##处理图片
if not self.is_camera1_opened and not self.is_camera2_opened :
self.result1 = cv2.cvtColor(self.frame1, cv2.COLOR_RGB2GRAY)
self.result2 = cv2.cvtColor(self.frame2, cv2.COLOR_RGB2GRAY)
else:
self.result1 = cv2.cvtColor(self.frame1, cv2.COLOR_RGB2GRAY)
self.result2 = cv2.cvtColor(self.frame2, cv2.COLOR_RGB2GRAY)
cv2.imwrite('C:/Users/Administrator/Desktop/camera/gray1/' + str(self.n) + '.bmp', self.result1)
cv2.imwrite('C:/Users/Administrator/Desktop/camera/gray2/' + str(self.n) + '.bmp', self.result2)
self.n = self.n + 1
rows, columns = self.result1.shape
bytesPerLine = columns
# 灰度图是单通道,所以需要用Format_Indexed8
QImg1 = QImage(self.result1.data, columns, rows, bytesPerLine, QImage.Format_Indexed8)
self.leftlabel.setPixmap(QPixmap.fromImage(QImg1).scaled(
self.leftlabel.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
QImg2 = QImage(self.result2.data, columns, rows, bytesPerLine, QImage.Format_Indexed8)
self.rightlabel.setPixmap(QPixmap.fromImage(QImg2).scaled(
self.rightlabel.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
def _queryFrame(self):
# 循环捕获图片
ret, self.frame1 = self.camera1.read()
ret, self.frame2 = self.camera2.read()
print(type(self.frame1))
img_rows, img_cols, channels = self.frame1.shape
bytesPerLine = channels * img_cols
QImg1 = QImage(self.frame1.data, img_cols, img_rows, bytesPerLine, QImage.Format_RGB888)
self.leftlabel.setPixmap(QPixmap.fromImage(QImg1).scaled(
self.leftlabel.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
self.leftlabel.setScaledContents(True)
QImg2 = QImage(self.frame2.data, img_cols, img_rows, bytesPerLine, QImage.Format_RGB888)
self.rightlabel.setPixmap(QPixmap.fromImage(QImg2).scaled(
self.rightlabel.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
self.rightlabel.setScaledContents(True)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = mywindow()
window.show()
sys.exit(app.exec_())