Skip to content

Instantly share code, notes, and snippets.

@OlegJakushkin
Created April 23, 2022 08:25
Show Gist options
  • Save OlegJakushkin/4c6a7eaaacc073820d0540ead3b80eae to your computer and use it in GitHub Desktop.
Save OlegJakushkin/4c6a7eaaacc073820d0540ead3b80eae to your computer and use it in GitHub Desktop.
Qml image provider
import QtQuick 2.0
Item {
property color borderColor : "#ff00f7"
Rectangle {
id: rectangle
color: "#00ffffff"
border.color: borderColor
border.width: 10
anchors.fill: parent
}
}
/*##^##
Designer {
D{i:0;autoSize:true;height:480;width:640}D{i:1}
}
##^##*/
#include "Handler.h"
#include <iostream>
#include <QLibrary>
#include <QImage>
#include <api.h>
typedef ssdResultC (*runSsdCimproted)(const myVectorFloat &,
const myVectorLong &,
const myVectorChar &);
Handler::Handler(QObject *parent) : QObject(parent)
{
timesCalled = 0;
}
void Handler::buttonClicked()
{
QImage img;
//ToDO: 1) shape?
std::vector<long> shape = {1,3,img.height(),img.width()};
auto imgSize = shape[1]* shape[2] * shape[3];
auto input = std::vector<float>(imgSize, 0);
for(int i = 0; i < imgSize; i++){
input[i] = float(img.bits()[i])/256;
}
qInfo("callsed from C++ handler!");
timesCalled++;
{
QLibrary myLib("/home/user/CLionProjects/torchpp/cmake-build-debug/libtorc_ssd_hpp.so");
auto cImportedFunction = (runSsdCimproted) myLib.resolve("runSsdC");
if (cImportedFunction) {
std::cout << "BEFORE!!!" << std::endl;
std::string path = "/home/user/CLionProjects/torchpp/ssd300.pt";
auto inputC = toC<myVectorFloat>(input);
auto shapeC = toC<myVectorLong>(shape);
auto pathC = toC<myVectorChar>(path);
auto resultC = cImportedFunction(inputC, shapeC, pathC); // External Fun
auto result = toCpp(resultC );
std::cout << "AFTER!!!" << std::endl;
for(auto i = 0; i < 10; i++ ) {
std::cout << result.outClasses[i] << ", ";
}
std::cout << "ssd pytorch is Ok"<< std::endl;
} else {
std::cout << "AAAAAAAAAAA" << std::endl;
}
}
valueChanged(timesCalled);
}
#ifndef HANDLER_H
#define HANDLER_H
#include <QObject>
class Handler : public QObject
{
Q_OBJECT
int timesCalled;
public:
explicit Handler(QObject *parent = nullptr);
public slots:
void buttonClicked();
signals:
void valueChanged(int newValue);
};
#endif // HANDLER_H
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Dialogs
import './' as MyComponents
Item {
FileDialog {
id: fileDialog
nameFilters: [ "Image files (*.jpg *.png)", "All files (*)" ]
title: "file for ssd processing"
onAccepted: {
console.log("You chose: " + fileDialog.selectedFile)
//image.source = fileDialog.selectedFile
ourqip.updateImage(fileDialog.selectedFile.toString());
image.source = "image://imageprovider/" + fileDialog.selectedFile.toString()
// SSD
var ourBorder = Qt.createComponent("qrc:/Border.qml", image);
var sprite = ourBorder.createObject(image, {
x: 100,
y: 100,
width:110,
height:110,
borderColor: "#AA00FF"
});
}
onRejected: {
console.log("Canceled")
}
Component.onCompleted: visible = false
}
Button {
id: button
x: 19
y: 19
text: qsTr("Open file dialog")
onClicked: fileDialog.visible = true
}
Button {
id: buttonRunAI
x: 19
y: 19
text: qsTr("Run NN")
onClicked: handler.buttonClicked(image.data)
}
Image {
id: image
x: 24
y: 65
width: 357
height: 357
source: "image://imageprovider/Downloads/cat.jpg"
fillMode: Image.PreserveAspectFit
}
MyComponents.Border {
anchors.fill: image
}
}
/*##^##
Designer {
D{i:0;autoSize:true;height:480;width:640}D{i:1}D{i:2}D{i:3}D{i:4}
}
##^##*/
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <iostream>
#include <QtQuickControls2/QtQuickControls2>
#include <QQmlContext>
#include "Handler.h"
#include "qmlimagecontroller.h"
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
auto our_qip = new QmlImageController(&app);
engine.addImageProvider(QLatin1String("imageprovider"),
our_qip->img_provider);
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
auto ctx = engine.rootContext();
auto handler = Handler();
ctx->setContextProperty("handler", &handler);
ctx->setContextProperty("ourqip", our_qip );
engine.load(url);
return app.exec();
}
import QtQuick 2.15
import QtQuick.Window 2.15
import './' as MyComponents
Window {
width: 640
height: 480
visible: true
title: qsTr("SSD test")
MyComponents.ImageLoader{}
}
QT += quick
QT += core
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
INCLUDEPATH += /home/user/CLionProjects/torchpp/include
#LIBS += -L"/home/user/CLionProjects/torchpp/cmake-build-debug" -ltorc_ssd_hpp
SOURCES += \
Handler.cpp \
main.cpp \
qmlimagecontroller.cpp \
qmlimageprovider.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
Handler.h \
qmlimagecontroller.h \
qmlimageprovider.h
#include "qmlimagecontroller.h"
QmlImageController::QmlImageController(QObject *parent): QObject(parent)
{
img_provider = new QmlImageProvider();
}
QmlImageController::~QmlImageController()
{
}
void QmlImageController::updateImage( QString image_path)
{
QImage newImg;
auto str = image_path.replace("file://", "");
newImg.load(str);
this->img_provider->image = newImg;
}
#ifndef QMLIMAGECONTROLLER_H
#define QMLIMAGECONTROLLER_H
#include <QObject>
#include <QString>
#include "qmlimageprovider.h"
class QmlImageController : public QObject
{
Q_OBJECT
public:
QmlImageController(QObject *parent = nullptr);
virtual ~QmlImageController();
QmlImageProvider *img_provider;
public slots:
void updateImage( QString image_path);
};
#endif // QMLIMAGECONTROLLER_H
#include "qmlimageprovider.h"
QmlImageProvider::QmlImageProvider()
: QQuickImageProvider(QQuickImageProvider::Image)
{
image.load("/home/user/torch_ssd_demo/assets/cat.jpg");
}
QmlImageProvider::~QmlImageProvider() {}
QImage QmlImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
return image;
}
#ifndef QMLIMAGEPROVIDER_H
#define QMLIMAGEPROVIDER_H
#include <QObject>
#include <QQuickImageProvider>
class QmlImageProvider : public QQuickImageProvider
{
public:
explicit QmlImageProvider();
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override;
QImage image;
virtual ~QmlImageProvider();
};
#endif // QMLIMAGEPROVIDER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment