Results 1 to 3 of 3

Thread: QNetworkReply is empty

  1. #1
    Join Date
    Mar 2020
    Posts
    4
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default QNetworkReply is empty

    Hello, I am new to using QT and I am working on it in my final year project. I am consuming an api made in python for facial recognition, to which I pass the path of an image and it returns the identification of the people present in it. The problem that I present is that when I consume the service, when I print the answer it comes out empty. From the same python if I managed to consume the api and get the response, converting it to json. Here is the api code in python and the one in Qt to consume the microservice.

    Code in Qt

    QString a="E:/fotos movil/carlos marx/IMG_2925.jpg";
    QVariantMap feed;
    feed.insert("ruta", QVariant(a).toString());
    QByteArray payload=QJsonDocument::fromVariant(feed).toJson();

    QUrl myurl;
    myurl.setScheme("http");
    myurl.setHost("localhost");
    myurl.setPort(5001);
    myurl.setPath("/reconocer");

    QNetworkRequest request;
    request.setUrl(myurl);
    request.setHeader(QNetworkRequest::ContentTypeHead er, "application/json");

    QNetworkAccessManager *restclient;
    restclient = new QNetworkAccessManager(this);
    QNetworkReply *reply = restclient->post(request,payload);

    qDebug() << reply->readAll(); // it is empty


    api code in python
    import face_recognition
    import cv2
    import numpy as np
    from flask import Flask, jsonify
    import requests
    import json
    from flask import request

    app = Flask(__name__)


    @app.route("/reconocer",methods=['POST'])
    def reconocer():

    # Load a sample picture and learn how to recognize it.
    s1_image = face_recognition.load_image_file("lena.png")
    s1_face_encoding = face_recognition.face_encodings(s1_image)[0]

    # # Load a second sample picture and learn how to recognize it.
    s2_image = face_recognition.load_image_file("vidal.jpg")
    s2_face_encoding = face_recognition.face_encodings(s2_image)[0]

    s3_image = face_recognition.load_image_file("viltres.jpg")
    s3_face_encoding = face_recognition.face_encodings(s3_image)[0]

    # Create arrays of known face encodings and their names
    known_face_encodings = [
    s1_face_encoding,
    s2_face_encoding,
    s2_face_encoding
    ]
    known_face_names = [
    "Lena",
    "Vidal",
    "viltres"
    ]

    # Initialize some variables
    face_locations = []
    face_encodings = []
    face_names = []

    frame = cv2.imread(request.json["ruta"])

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_small_frame = small_frame[:, :, ::-1]

    # Only process every other frame of video to save time

    # Find all the faces and face encodings in the current frame of video
    face_locations = face_recognition.face_locations(rgb_small_frame)
    face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

    face_names = []


    for face_encoding in face_encodings:
    # See if the face is a match for the known face(s)
    matches = face_recognition.compare_faces(known_face_encoding s, face_encoding)
    name = "Desconocido"

    # # If a match was found in known_face_encodings, just use the first one.
    # if True in matches:
    # first_match_index = matches.index(True)
    # name = known_face_names[first_match_index]

    # Or instead, use the known face with the smallest distance to the new face
    face_distances = face_recognition.face_distance(known_face_encoding s, face_encoding)
    best_match_index = np.argmin(face_distances)
    if matches[best_match_index]:
    name = known_face_names[best_match_index]

    face_names.append(name)



    p= {'personas': face_names}
    print (p)
    return jsonify(p)

    if __name__ == "__main__":
    app.run(host='localhost', port='5001', threaded=True)

    I would appreciate if anyone can guide me on what I can do to access the answer that returns python through the dictionary. Taking into account that it was verified that the api is receiving the image path and is identifying correctly.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,386
    Thanks
    247
    Thanked 687 Times in 677 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QNetworkReply is empty

    In your QNetworkAccessManager::post() command, you do not wait for the QNetworkAccessManager::finished() signal before trying to read the data. The QNetworkReply instance is returned in this signal, and at that point the data will be ready to read.

    So you need to define a slot to connect to that signal, and in that slot you can then call QNetworkReply::readAll() using the pointer to the instance that is passed by the signal.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    lagarcia (18th March 2020)

  4. #3
    Join Date
    Mar 2020
    Posts
    4
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QNetworkReply is empty

    Thank you very much, really, I have been working on this for days, it has solved a problem that although it seems simple, I did not find the solution, it is the first time I am in the forum, and I did not expect such a quick and effective response, thanks again.

  5. The following user says thank you to lagarcia for this useful post:

    d_stranz (18th March 2020)

Similar Threads

  1. How to use setErrorString() of QNetworkReply?
    By npatil15 in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2019, 08:47
  2. Replies: 9
    Last Post: 25th May 2015, 15:25
  3. QNetworkReply: how to know when data is sent?
    By mentalmushroom in forum Qt Programming
    Replies: 17
    Last Post: 24th June 2011, 08:22
  4. Cannot get QNetworkCookie from QNetworkReply
    By MorrisLiang in forum Newbie
    Replies: 1
    Last Post: 25th May 2010, 16:59
  5. remove directory empty or not empty
    By raphaelf in forum Newbie
    Replies: 12
    Last Post: 27th October 2006, 07:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.