-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
119 lines (100 loc) · 4.29 KB
/
main.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
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog,QMessageBox
import mainUI
from openCameraThread import OpenCameraThread
import cv2
from PyQt5 import QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MainCode(QMainWindow, mainUI.MainUi):
def __init__(self):
QMainWindow.__init__(self)
mainUI.MainUi.__init__(self)
# 槽函数
self.left_button_2.clicked.connect(self.openLocalVedioThread)
self.left_button_1.clicked.connect(self.openUSBCameraThread)
self.left_button_3.clicked.connect(self.openGigeCameraThread)
self.left_close.clicked.connect(self.closeApp)
self.button_choose_model_dir.clicked.connect(self.getDir)
def openLocalVedioThread(self):
fname, _ = QFileDialog.getOpenFileName(self, '选择视屏', 'open file', 'Image files(*.mp4 *.mkv *.avi)')
if len(fname) != 0:
if self.is_chinese(fname):
print("有中文")
QMessageBox.warning(self, '警告', '暂不支持含有中文的路径')
return
else:
self.sourceImage_label.setText("加载视屏中......")
with open("./connfig.txt","r") as config:
mode = int(config.readline())
print(mode)
self.openCameraThread = OpenCameraThread(0,mode, self.sourceImage_label,self.resultImage_label,self.showImg,fname)
def openUSBCameraThread(self):
self.sourceImage_label.setText("加载视屏中......")
with open("./connfig.txt","r") as config:
mode = int(config.readline())
print(mode)
self.openCameraThread = OpenCameraThread(1,mode, self.sourceImage_label,self.resultImage_label,self.showImg)
def openGigeCameraThread(self):
self.sourceImage_label.setText("加载视屏中......")
with open("./connfig.txt", "r") as config:
mode = int(config.readline())
print(mode)
self.openCameraThread = OpenCameraThread(2, mode, self.sourceImage_label, self.resultImage_label,
self.showImg)
def showImg(self,img,label):
res = self.img_resize(img, label)
img2 = cv2.cvtColor(res, cv2.COLOR_BGR2RGB) # opencv读取的bgr格式图片转换成rgb格式
_image = QtGui.QImage(img2[:], img2.shape[1], img2.shape[0], img2.shape[1] * 3,
QtGui.QImage.Format_RGB888) # pyqt5转换成自己能放的图片格式
jpg_out = QtGui.QPixmap(_image) # 转换成QPixmap
label.setPixmap(jpg_out) # 设置图片显示
label.setAlignment(Qt.AlignCenter)
def img_resize(self,image,label):
'''
:param image: cv2读取的mat图片
:param label: 显示在那个label
:return: 返回处理后适合显示的图片
'''
if image is None:
return
height, width = image.shape[0], image.shape[1]
# 设置新的图片分辨率框架
width_new = label.width()
height_new = label.height()
# 判断图片的长宽比率
if width / height >= width_new / height_new:
img_new = cv2.resize(image, (width_new, int(height * width_new / width)))
else:
img_new = cv2.resize(image, (int(width * height_new / height), height_new))
return img_new
def closeApp(self):
try:
self.openCameraThread.stop()
self.close()
except:
self.close()
def getDir(self):
_dir = QFileDialog.getExistingDirectory(self, "选取文件夹", "./")
if len(_dir) != 0:
if self.is_chinese(_dir):
print("有中文")
QMessageBox.warning(self, '警告', '暂不支持含有中文的路径')
return
else:
self.dir_lineedit.setText(_dir)
def is_chinese(self,string):
"""
检查整个字符串是否包含中文
:param string: 需要检查的字符串
:return: bool
"""
for ch in string:
if u'\u4e00' <= ch <= u'\u9fff':
return True
return False
if __name__ == '__main__':
app = QApplication(sys.argv)
md = MainCode()
md.show()
sys.exit(app.exec_())