Skip to content

Commit

Permalink
optimize qt windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ZHEQIUSHUI committed Aug 4, 2023
1 parent f9931d3 commit 6bb1548
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 21 deletions.
2 changes: 1 addition & 1 deletion qtproj/SAMQT/CMakeLists.txt.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2023-08-03T18:27:09. -->
<!-- Written by QtCreator 4.11.1, 2023-08-04T18:14:10. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
42 changes: 40 additions & 2 deletions qtproj/SAMQT/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "QImage"
#include "QFileDialog"
#include "QMimeData"
#include "QMessageBox"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
Expand All @@ -11,7 +13,9 @@ MainWindow::MainWindow(QWidget *parent)
std::string encoder_model_path = "/home/arno/workspace/projects/SegmentAnything-OnnxRunner/onnx_models/mobile_sam_encoder.onnx";
std::string decoder_model_path = "/home/arno/workspace/projects/SegmentAnything-OnnxRunner/onnx_models/mobile_sam_decoder.onnx";
std::string inpaint_model_path = "/home/arno/workspace/pycode/lama/big-lama-regular/big-lama-regular.onnx";
this->ui->label->InitModel(encoder_model_path, decoder_model_path,inpaint_model_path);
this->ui->label->InitModel(encoder_model_path, decoder_model_path, inpaint_model_path);
this->setAcceptDrops(true);
// this->ui->label->setAcceptDrops(true);
}

MainWindow::~MainWindow()
Expand All @@ -27,7 +31,8 @@ void MainWindow::on_btn_read_image_clicked()
return;
}
QImage img(filename);
this->ui->label->SetImage(img);
if (img.bits())
this->ui->label->SetImage(img);
}

void MainWindow::on_ckb_boxprompt_stateChanged(int arg1)
Expand All @@ -49,3 +54,36 @@ void MainWindow::on_btn_reset_clicked()
{
this->ui->label->Reset();
}

// void MainWindow::dragEnterEvent(QDragEnterEvent *event)
//{
// if (event->mimeData()->hasUrls())
// {
// event->acceptProposedAction();
// }
// else
// {
// event->ignore();
// }
// }
// void MainWindow::dropEvent(QDropEvent *event)
//{
// const QMimeData *mimeData = event->mimeData();

// if (!mimeData->hasUrls())
// {
// return;
// }

// QList<QUrl> urlList = mimeData->urls();

// QString filename = urlList.at(0).toLocalFile();
// if (filename.isEmpty())
// {
// return;
// }
// printf("open from drop:%s\n", filename.toStdString().c_str());
// QImage img(filename);
// if (img.bits())
// this->ui->label->SetImage(img);
//}
25 changes: 13 additions & 12 deletions qtproj/SAMQT/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
<string>Segment anything and Lama Inpaint</string>
</property>
<property name="documentMode">
<bool>false</bool>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
Expand All @@ -30,13 +33,19 @@
</item>
<item>
<widget class="QCheckBox" name="ckb_realtime_decode">
<property name="toolTip">
<string>real time decode for sam</string>
</property>
<property name="text">
<string>LiveDecode</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckb_boxprompt">
<property name="toolTip">
<string>draw box by mouse,and decode sam for the box</string>
</property>
<property name="text">
<string>BoxPrompt</string>
</property>
Expand All @@ -46,6 +55,9 @@
</item>
<item>
<widget class="QPushButton" name="btn_remove_obj">
<property name="toolTip">
<string>remove mask object and do sam encode in inpainted image</string>
</property>
<property name="text">
<string>RemoveObject</string>
</property>
Expand Down Expand Up @@ -84,17 +96,6 @@
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>870</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
Expand Down
48 changes: 42 additions & 6 deletions qtproj/SAMQT/myqlabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <qimage.h>
#include <QMouseEvent>
#include "QDialog"
#include "QMimeData"
#include "libsam/src/Runner/SAM.hpp"
#include "libsam/src/Runner/LamaInpaintOnnx.hpp"

Expand All @@ -30,6 +31,35 @@ class myQLabel : public QLabel
SAM mSam;
LamaInpaintOnnx mInpaint;

void dragEnterEvent(QDragEnterEvent *event) override
{
if (event->mimeData()->hasUrls())
event->acceptProposedAction();
else
event->ignore();
}
void dropEvent(QDropEvent *event) override
{
const QMimeData *mimeData = event->mimeData();

if (!mimeData->hasUrls())
{
return;
}

QList<QUrl> urlList = mimeData->urls();

QString filename = urlList.at(0).toLocalFile();
if (filename.isEmpty())
{
return;
}
printf("open from drop:%s\n", filename.toStdString().c_str());
QImage img(filename);
if (img.bits())
SetImage(img);
}

void mousePressEvent(QMouseEvent *e) override
{
pt_img_first = pt_img_secend = getSourcePoint(this->size(), cur_image.size(), e->pos());
Expand Down Expand Up @@ -138,23 +168,29 @@ class myQLabel : public QLabel
isRealtimeDecode = RealtimeDecode;
}

void InitModel(std::string encoder_model, std::string decoder_model,std::string inpaint_model)
void InitModel(std::string encoder_model, std::string decoder_model, std::string inpaint_model)
{
mSam.Load(encoder_model, decoder_model);
mInpaint.Load(inpaint_model);
}

void ShowRemoveObject()
{
if(!cur_image.bits()||!grab_mask.data)
if (!cur_image.bits() || !grab_mask.data)
{
return;
}
cv::Mat src(cur_image.height(), cur_image.width(), CV_8UC(cur_image.format()==QImage::Format_BGR888?3:4), cur_image.bits());
int channel = cur_image.format() == QImage::Format_BGR888 ? 3 : 4;
int stride = cur_image.bytesPerLine();
cv::Mat src(cur_image.height(), cur_image.width(), CV_8UC(channel), cur_image.bits(), stride);
cv::Mat rgb;
cv::cvtColor(src, rgb, cv::COLOR_RGBA2RGB);
cv::Mat inpainted = mInpaint.Inpaint(rgb,grab_mask);
QImage qinpainted(inpainted.data,inpainted.cols,inpainted.rows,QImage::Format_BGR888);
if(channel==3)
src.copyTo(rgb);
else if(channel==4)
cv::cvtColor(src, rgb, cv::COLOR_RGBA2RGB);
cv::Mat inpainted = mInpaint.Inpaint(rgb, grab_mask);
mSam.Encode(inpainted);
QImage qinpainted(inpainted.data, inpainted.cols, inpainted.rows, inpainted.step1(),QImage::Format_BGR888);
cur_image = qinpainted.copy();
cur_mask = QImage();
pt_img_first = QPoint(-10000, -10000);
Expand Down

0 comments on commit 6bb1548

Please sign in to comment.