Skip to content

Commit 6bb1548

Browse files
committed
optimize qt windows
1 parent f9931d3 commit 6bb1548

File tree

4 files changed

+96
-21
lines changed

4 files changed

+96
-21
lines changed

qtproj/SAMQT/CMakeLists.txt.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.11.1, 2023-08-03T18:27:09. -->
3+
<!-- Written by QtCreator 4.11.1, 2023-08-04T18:14:10. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>

qtproj/SAMQT/mainwindow.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "QImage"
55
#include "QFileDialog"
6+
#include "QMimeData"
7+
#include "QMessageBox"
68

79
MainWindow::MainWindow(QWidget *parent)
810
: QMainWindow(parent), ui(new Ui::MainWindow)
@@ -11,7 +13,9 @@ MainWindow::MainWindow(QWidget *parent)
1113
std::string encoder_model_path = "/home/arno/workspace/projects/SegmentAnything-OnnxRunner/onnx_models/mobile_sam_encoder.onnx";
1214
std::string decoder_model_path = "/home/arno/workspace/projects/SegmentAnything-OnnxRunner/onnx_models/mobile_sam_decoder.onnx";
1315
std::string inpaint_model_path = "/home/arno/workspace/pycode/lama/big-lama-regular/big-lama-regular.onnx";
14-
this->ui->label->InitModel(encoder_model_path, decoder_model_path,inpaint_model_path);
16+
this->ui->label->InitModel(encoder_model_path, decoder_model_path, inpaint_model_path);
17+
this->setAcceptDrops(true);
18+
// this->ui->label->setAcceptDrops(true);
1519
}
1620

1721
MainWindow::~MainWindow()
@@ -27,7 +31,8 @@ void MainWindow::on_btn_read_image_clicked()
2731
return;
2832
}
2933
QImage img(filename);
30-
this->ui->label->SetImage(img);
34+
if (img.bits())
35+
this->ui->label->SetImage(img);
3136
}
3237

3338
void MainWindow::on_ckb_boxprompt_stateChanged(int arg1)
@@ -49,3 +54,36 @@ void MainWindow::on_btn_reset_clicked()
4954
{
5055
this->ui->label->Reset();
5156
}
57+
58+
// void MainWindow::dragEnterEvent(QDragEnterEvent *event)
59+
//{
60+
// if (event->mimeData()->hasUrls())
61+
// {
62+
// event->acceptProposedAction();
63+
// }
64+
// else
65+
// {
66+
// event->ignore();
67+
// }
68+
// }
69+
// void MainWindow::dropEvent(QDropEvent *event)
70+
//{
71+
// const QMimeData *mimeData = event->mimeData();
72+
73+
// if (!mimeData->hasUrls())
74+
// {
75+
// return;
76+
// }
77+
78+
// QList<QUrl> urlList = mimeData->urls();
79+
80+
// QString filename = urlList.at(0).toLocalFile();
81+
// if (filename.isEmpty())
82+
// {
83+
// return;
84+
// }
85+
// printf("open from drop:%s\n", filename.toStdString().c_str());
86+
// QImage img(filename);
87+
// if (img.bits())
88+
// this->ui->label->SetImage(img);
89+
//}

qtproj/SAMQT/mainwindow.ui

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
14-
<string>MainWindow</string>
14+
<string>Segment anything and Lama Inpaint</string>
15+
</property>
16+
<property name="documentMode">
17+
<bool>false</bool>
1518
</property>
1619
<widget class="QWidget" name="centralwidget">
1720
<layout class="QGridLayout" name="gridLayout">
@@ -30,13 +33,19 @@
3033
</item>
3134
<item>
3235
<widget class="QCheckBox" name="ckb_realtime_decode">
36+
<property name="toolTip">
37+
<string>real time decode for sam</string>
38+
</property>
3339
<property name="text">
3440
<string>LiveDecode</string>
3541
</property>
3642
</widget>
3743
</item>
3844
<item>
3945
<widget class="QCheckBox" name="ckb_boxprompt">
46+
<property name="toolTip">
47+
<string>draw box by mouse,and decode sam for the box</string>
48+
</property>
4049
<property name="text">
4150
<string>BoxPrompt</string>
4251
</property>
@@ -46,6 +55,9 @@
4655
</item>
4756
<item>
4857
<widget class="QPushButton" name="btn_remove_obj">
58+
<property name="toolTip">
59+
<string>remove mask object and do sam encode in inpainted image</string>
60+
</property>
4961
<property name="text">
5062
<string>RemoveObject</string>
5163
</property>
@@ -84,17 +96,6 @@
8496
</item>
8597
</layout>
8698
</widget>
87-
<widget class="QMenuBar" name="menubar">
88-
<property name="geometry">
89-
<rect>
90-
<x>0</x>
91-
<y>0</y>
92-
<width>870</width>
93-
<height>20</height>
94-
</rect>
95-
</property>
96-
</widget>
97-
<widget class="QStatusBar" name="statusbar"/>
9899
</widget>
99100
<customwidgets>
100101
<customwidget>

qtproj/SAMQT/myqlabel.h

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <qimage.h>
66
#include <QMouseEvent>
77
#include "QDialog"
8+
#include "QMimeData"
89
#include "libsam/src/Runner/SAM.hpp"
910
#include "libsam/src/Runner/LamaInpaintOnnx.hpp"
1011

@@ -30,6 +31,35 @@ class myQLabel : public QLabel
3031
SAM mSam;
3132
LamaInpaintOnnx mInpaint;
3233

34+
void dragEnterEvent(QDragEnterEvent *event) override
35+
{
36+
if (event->mimeData()->hasUrls())
37+
event->acceptProposedAction();
38+
else
39+
event->ignore();
40+
}
41+
void dropEvent(QDropEvent *event) override
42+
{
43+
const QMimeData *mimeData = event->mimeData();
44+
45+
if (!mimeData->hasUrls())
46+
{
47+
return;
48+
}
49+
50+
QList<QUrl> urlList = mimeData->urls();
51+
52+
QString filename = urlList.at(0).toLocalFile();
53+
if (filename.isEmpty())
54+
{
55+
return;
56+
}
57+
printf("open from drop:%s\n", filename.toStdString().c_str());
58+
QImage img(filename);
59+
if (img.bits())
60+
SetImage(img);
61+
}
62+
3363
void mousePressEvent(QMouseEvent *e) override
3464
{
3565
pt_img_first = pt_img_secend = getSourcePoint(this->size(), cur_image.size(), e->pos());
@@ -138,23 +168,29 @@ class myQLabel : public QLabel
138168
isRealtimeDecode = RealtimeDecode;
139169
}
140170

141-
void InitModel(std::string encoder_model, std::string decoder_model,std::string inpaint_model)
171+
void InitModel(std::string encoder_model, std::string decoder_model, std::string inpaint_model)
142172
{
143173
mSam.Load(encoder_model, decoder_model);
144174
mInpaint.Load(inpaint_model);
145175
}
146176

147177
void ShowRemoveObject()
148178
{
149-
if(!cur_image.bits()||!grab_mask.data)
179+
if (!cur_image.bits() || !grab_mask.data)
150180
{
151181
return;
152182
}
153-
cv::Mat src(cur_image.height(), cur_image.width(), CV_8UC(cur_image.format()==QImage::Format_BGR888?3:4), cur_image.bits());
183+
int channel = cur_image.format() == QImage::Format_BGR888 ? 3 : 4;
184+
int stride = cur_image.bytesPerLine();
185+
cv::Mat src(cur_image.height(), cur_image.width(), CV_8UC(channel), cur_image.bits(), stride);
154186
cv::Mat rgb;
155-
cv::cvtColor(src, rgb, cv::COLOR_RGBA2RGB);
156-
cv::Mat inpainted = mInpaint.Inpaint(rgb,grab_mask);
157-
QImage qinpainted(inpainted.data,inpainted.cols,inpainted.rows,QImage::Format_BGR888);
187+
if(channel==3)
188+
src.copyTo(rgb);
189+
else if(channel==4)
190+
cv::cvtColor(src, rgb, cv::COLOR_RGBA2RGB);
191+
cv::Mat inpainted = mInpaint.Inpaint(rgb, grab_mask);
192+
mSam.Encode(inpainted);
193+
QImage qinpainted(inpainted.data, inpainted.cols, inpainted.rows, inpainted.step1(),QImage::Format_BGR888);
158194
cur_image = qinpainted.copy();
159195
cur_mask = QImage();
160196
pt_img_first = QPoint(-10000, -10000);

0 commit comments

Comments
 (0)