/* * main.cpp * * Created on: 2014-05-05 * Author: mathieu */ #include #include #include #include #include #include "TcpResponse.h" #ifdef WITH_JSONCPP #include #endif void showUsage() { printf("\ntcpRequest [options] --scene image.png --out # --in #\n" " \"out\" is the port to which the image is sent.\n" " \"in\" is the port from which the detection is received.\n" " Options:\n" " --host #.#.#.# Set host address.\n" #ifdef WITH_JSONCPP " --json \"path\" Path to an output JSON file.\n" #endif ); exit(-1); } void writeJSON(const QMultiMap > & objectsDetected, const QString & path) { #ifdef WITH_JSONCPP if(!path.isEmpty()) { Json::Value root; Json::Value detections; if(objectsDetected.size()) { for(QMultiMap >::const_iterator iter = objectsDetected.constBegin(); iter!= objectsDetected.end();) { char index = 'a'; QMultiMap >::const_iterator jter = iter; for(;jter != objectsDetected.constEnd() && jter.key() == iter.key(); ++jter) { QString name = QString("object_%1%2").arg(jter.key()).arg(objectsDetected.count(jter.key())>1?QString(index++):""); detections.append(name.toStdString()); Json::Value homography; homography.append(jter.value().second.m11()); homography.append(jter.value().second.m12()); homography.append(jter.value().second.m13()); homography.append(jter.value().second.m21()); homography.append(jter.value().second.m22()); homography.append(jter.value().second.m23()); homography.append(jter.value().second.m31()); // dx homography.append(jter.value().second.m32()); // dy homography.append(jter.value().second.m33()); root[name.toStdString()]["width"] = jter.value().first.width(); root[name.toStdString()]["height"] = jter.value().first.height(); root[name.toStdString()]["homography"] = homography; } iter = jter; } } root["objects"] = detections; // write in a nice readible way Json::StyledWriter styledWriter; //std::cout << styledWriter.write(root); QFile file(path); file.open(QIODevice::WriteOnly | QIODevice::Text); QTextStream out(&file); out << styledWriter.write(root).c_str(); file.close(); printf("JSON written to \"%s\"\n", path.toStdString().c_str()); } #else printf("Not built with JSON support!\n"); #endif } int main(int argc, char * argv[]) { QString ipAddress; QString scenePath; QString jsonPath; quint16 portOut = 0; quint16 portIn = 0; for(int i=1; i buf; cv::imencode(".png", image, buf); QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_0); out << (quint64)0; out.writeRawData((char*)buf.data(), (int)buf.size()); out.device()->seek(0); out << (quint64)(block.size() - sizeof(quint64)); request.write(block); printf("Image published, waiting for response...\n"); QTime time; time.start(); // wait for response app.exec(); if(response.dataReceived()) { printf("Response received! (%d ms)\n", time.elapsed()); // print detected objects if(response.objectsDetected().size()) { QList ids = response.objectsDetected().uniqueKeys(); for(int i=0; i