-
Notifications
You must be signed in to change notification settings - Fork 14
/
camera1.py
143 lines (114 loc) · 5.25 KB
/
camera1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import cv2
import torch
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from track import detect
class Ui_MainWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Ui_MainWindow, self).__init__(parent)
self.CAM_NUM = 1
self.timer_camera = QtCore.QTimer()
self.s_rtsp = "rtsp://iscas:[email protected]:554/Streaming/Channels/101"
self.cap = cv2.VideoCapture()
self.set_ui()
self.slot_init()
self.__flag_work = 0
self.x = 0
self.count = 0
def set_ui(self):
self.__layout_main = QtWidgets.QHBoxLayout()
self.__layout_fun_button = QtWidgets.QVBoxLayout()
self.__layout_data_show = QtWidgets.QVBoxLayout()
self.button_open_camera = QtWidgets.QPushButton(u'打开相机')
self.button_detect = QtWidgets.QPushButton(u'开始检测')
self.button_close = QtWidgets.QPushButton(u'退出')
# Button 的颜色修改
button_color = [self.button_open_camera, self.button_detect, self.button_close]
for i in range(len(button_color)):
button_color[i].setStyleSheet("QPushButton{color:black}"
"QPushButton:hover{color:red}"
"QPushButton{background-color:rgb(78,255,255)}"
"QPushButton{border:2px}"
"QPushButton{border-radius:10px}"
"QPushButton{padding:2px 4px}")
self.button_open_camera.setMinimumHeight(50)
self.button_detect.setMinimumHeight(50)
self.button_close.setMinimumHeight(50)
# move()方法移动窗口在屏幕上的位置到x = 300,y = 300坐标。
self.move(500, 500)
# 信息显示
self.label_show_camera = QtWidgets.QLabel()
self.label_move = QtWidgets.QLabel()
self.label_move.setFixedSize(100, 100)
self.label_show_camera.setFixedSize(700, 600)
self.label_show_camera.setAutoFillBackground(False)
self.__layout_fun_button.addWidget(self.button_open_camera)
self.__layout_fun_button.addWidget(self.button_detect)
self.__layout_fun_button.addWidget(self.button_close)
self.__layout_fun_button.addWidget(self.label_move)
self.__layout_main.addLayout(self.__layout_fun_button)
self.__layout_main.addWidget(self.label_show_camera)
self.setLayout(self.__layout_main)
self.label_move.raise_()
self.setWindowTitle(u'摄像头')
# 设置背景图片
# palette1 = QPalette()
# palette1.setBrush(self.backgroundRole(), QBrush(QPixmap('1.png')))
# self.setPalette(palette1)
def slot_init(self):
self.button_open_camera.clicked.connect(self.button_open_camera_click)
self.timer_camera.timeout.connect(self.show_camera)
self.button_close.clicked.connect(self.close)
# self.button_detect.clicked.connect(self.button_detect_click)
def button_detect_click(self):
pass
def button_open_camera_click(self):
if not self.timer_camera.isActive():
flag = self.cap.open(self.s_rtsp)
# flag = self.cap.isOpened()
if not flag:
msg = QtWidgets.QMessageBox.warning(self, u"Warning", u"请检测相机与电脑是否连接正确",
buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
# if msg==QtGui.QMessageBox.Cancel:
# pass
else:
self.timer_camera.start(30)
self.button_open_camera.setText(u'关闭相机')
else:
self.timer_camera.stop()
self.cap.release()
self.label_show_camera.clear()
self.button_open_camera.setText(u'打开相机')
def show_camera(self):
flag, image = self.cap.read()
show = cv2.resize(image, (640, 480))
show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)
self.label_show_camera.setPixmap(QtGui.QPixmap.fromImage(showImage))
def closeEvent(self, event):
ok = QtWidgets.QPushButton()
cacel = QtWidgets.QPushButton()
msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, u"关闭", u"确定退出?")
msg.addButton(ok, QtWidgets.QMessageBox.ActionRole)
msg.addButton(cacel, QtWidgets.QMessageBox.RejectRole)
ok.setText(u'确定')
cacel.setText(u'取消')
# msg.setDetailedText('sdfsdff')
if msg.exec_() == QtWidgets.QMessageBox.RejectRole:
event.ignore()
else:
# self.socket_client.send_command(self.socket_client.current_user_command)
if self.cap.isOpened():
self.cap.release()
if self.timer_camera.isActive():
self.timer_camera.stop()
event.accept()
if __name__ == "__main__":
App = QApplication(sys.argv)
ex = Ui_MainWindow()
ex.show()
sys.exit(App.exec_())