-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
145 lines (114 loc) · 4.35 KB
/
gui.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
144
145
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from utils import *
from image import letter_image
from yolo_voc.darknet import Darknet
def detect(cfgfile, weightfile, imgfile, namefile):
m = Darknet(cfgfile)
m.load_weights(weightfile)
print('Loading weights from {}... Done!'.format(weightfile))
use_cuda = False
if use_cuda:
m.cuda()
img = Image.open(imgfile).convert('RGB')
sized = letter_image(img, m.width, m.height)
start = time.time()
boxes = do_detect(m, sized, 0.5, 0.4)
finish = time.time()
print('{}: Predicted in {:f} seconds.'.format(imgfile, (finish-start)))
class_names = read_class_names(namefile)
plot_boxes(img, boxes, 'predictions.jpg', class_names)
class MainUI(QWidget):
def __init__(self):
super().__init__()
self.setUI()
def setUI(self):
self.resize(1000, 618)
self.move(100, 100)
self.setWindowIcon(QIcon('pics/rocket.png'))
self.setWindowTitle("检测演示")
self.img_path = None
inp_btn = QPushButton("选择图片", self) # self类似于C++ this指针
inp_btn.setToolTip("点击选择想要检测的图片")
# btn.clicked.connect(QCoreApplication.instance().quit)
inp_btn.move(250, 0)
inp_btn.resize(60, 34)
inp_btn.clicked.connect(self.openimg)
pred_btn = QPushButton("进行预测", self) # self类似于C++ this指针
pred_btn.setToolTip("对已选择图片检测")
pred_btn.move(750, 0)
pred_btn.resize(60, 34)
pred_btn.clicked.connect(self.show_predict_result)
self.input_label = QLabel(self)
self.input_label.resize(0, 0)
self.input_label.move(160, 160)
self.input_label.setStyleSheet("QLabel{background:white;}""QLabel{color:rgb(300,300,300,120);"
"font-size:10px;font-weight:bold;font-family:宋体;}")
self.output_label = QLabel(self)
self.output_label.resize(0, 0)
self.output_label.move(540, 160)
self.output_label.setStyleSheet("QLabel{background:white;}""QLabel{color:rgb(300,300,300,120);"
"font-size:10px;font-weight:bold;font-family:宋体;}")
self.center()
self.show()
"""
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Message',
"确定退出?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
"""
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def openimg(self):
imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png;;*.rng;;All Files(*)")
input_img = QPixmap(imgName)
self.img_path = imgName
h = input_img.height()
w = input_img.width()
fix_w = 300
fix_h = 300
if w*h == 0:
return
if fix_w/w < fix_h/h:
h = (h * fix_w) / w
w = fix_w
else:
w = (w*fix_h)/h
h = fix_h
self.input_label.resize(w, h)
input_img = input_img.scaled(w, h)
self.input_label.setPixmap(input_img)
self.output_label.resize(w, h)
def show_predict_result(self):
cfgfile = 'data/yolo_v3.cfg'
weightfile = 'data/model.weights'
imgfile = self.img_path
namefile = 'data/voc.names'
detect(cfgfile, weightfile, imgfile, namefile)
output_img = QPixmap('predictions.jpg')
h = output_img.height()
w = output_img.width()
fix_w = 300
fix_h = 300
if w * h == 0:
return
if fix_w / w < fix_h / h:
h = (h * fix_w) / w
w = fix_w
else:
w = (w * fix_h) / h
h = fix_h
output_img = output_img.scaled(w, h)
self.output_label.setPixmap(output_img)
if __name__ == '__main__':
app = QApplication(sys.argv)
gui = MainUI()
sys.exit(app.exec_())