Close

re: Problem with uploading an image from my local drive

Share tips and tricks related to Sighthound Video or your full security setup.

Moderator: Staff

no avatar
Dung
 
Posts: 4
Joined: Fri Mar 30, 2018 10:50 am

re: Problem with uploading an image from my local drive

by Dung » Fri Mar 30, 2018 11:15 am

Hello,
I am a newbie and I tried to figure out how could I call Sighthound API to analyze an image that I have on my local drive. I tried it but it gave me an error saying that I had a wrong encoded image as follow:

Code: Select all
[color=#FF4000]{"error":"ERROR_MEDIA_DATA","reason":"Wrong encoded image: /Users/user//Documents/men.jpg","reasonCode":40005}[/color]



Please help if you happen to know what's the problem is. Many Thanks!!

Below is my code

[code]
/code]
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.*;

public class UrlSample {


// https://www.sighthound.com/products/cloud
private static final String api = "https://dev.sighthoundapi.com/v1/detections?type=face,person&faceOption=landmark,gender";

static String YourAPIKey = "xxxxxx";
private static final String accessToken = YourAPIKey;
private static String imageUrl = "/Users/user//Documents/men.jpg";
// private static String imageUrl = "https://vgy.me/ZPkPaX.jpg";
//private static String imageUrl = "https://www.chiaramail.com/confirm_reg_files/20160313_142257.jpg";
public static void main(String[] args) throws IOException {
JsonObject jsonImage = Json.createObjectBuilder().add("image", imageUrl).build();
URL apiURL = new URL(api);
HttpURLConnection connection = (HttpURLConnection) apiURL.openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("X-Access-Token", accessToken);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
byte[] body = jsonImage.toString().getBytes();
connection.setFixedLengthStreamingMode(body.length);
OutputStream os = connection.getOutputStream();
os.write(body);
os.flush();
int httpCode = connection.getResponseCode();
if ( httpCode == 200 ){
JsonReader jReader = Json.createReader(connection.getInputStream());
JsonObject jsonBody = jReader.readObject();
System.out.println(jsonBody);
} else {
JsonReader jReader = Json.createReader(connection.getErrorStream());
JsonObject jsonError = jReader.readObject();
System.out.println(jsonError);
}
}
}

User avatar
Kbond
 
Posts: 194
Joined: Wed Nov 15, 2017 5:51 pm

Re: re: Problem with uploading an image from my local drive

by Kbond » Fri Mar 30, 2018 3:01 pm

Hello,

When you are uploading an image from a file on your machine, you will need to send the binary of that file only and set the request type to "application/octet-stream". You should try changing the following lines and trying again:

connection.setRequestProperty("Content-Type", "application/json");
byte[] body = jsonImage.toString().getBytes();

You will want to change this to the following:

connection.setRequestProperty("Content-Type", "application/octet-stream");
final byte[] body = Files.readAllBytes(Paths.get(imageUrl));


Finally, since you want to get the pose of the image, you will need to fix your request URL to the following:

private static final String api = "https://dev.sighthoundapi.com/v1/detections?type=face,person&faceOption=gender,pose,age";

Adding pose is the significant addition here. Can you please give that a try? Let us know if you are still having issues. We will be standing by for your reply, thank you!

Regards,
-Kevin

no avatar
Dung
 
Posts: 4
Joined: Fri Mar 30, 2018 10:50 am

Re: re: Problem with uploading an image from my local drive

by Dung » Fri Mar 30, 2018 3:27 pm

Thank you so much Kevin. It works out very well. You are a great teacher. Thank you, Sir.

User avatar
Kbond
 
Posts: 194
Joined: Wed Nov 15, 2017 5:51 pm

Re: re: Problem with uploading an image from my local drive

by Kbond » Fri Mar 30, 2018 3:29 pm

You are very welcome! Please don't hesitate to let us know if you have any other questions or concerns.

Regards,
-Kevin

no avatar
Dung
 
Posts: 4
Joined: Fri Mar 30, 2018 10:50 am

Re: re: Problem with uploading an image from my local drive

by Dung » Fri Mar 30, 2018 3:39 pm

Thank you so much, Kevin. You are a great teacher. I learn a lot from you. May I ask one more question regarding detection? If I'd like to detect eye roll of a person, could I call the detections API ? for example if I like to detect my eye roll to the left, right, or up or down, would it give me a coordinate or numeric value?
Many Thanks for all of your wonderful help!

User avatar
Kbond
 
Posts: 194
Joined: Wed Nov 15, 2017 5:51 pm

Re: re: Problem with uploading an image from my local drive

by Kbond » Fri Mar 30, 2018 3:42 pm

Currently at this time, our CloudAPI does not track the eye coordinations only the landmarks around the eye. This is something that we might add in the future. I'll open a feature request for engineering to take a look at this.

Please let us know if you need anything else!

Regards,
-Kevin

no avatar
Dung
 
Posts: 4
Joined: Fri Mar 30, 2018 10:50 am

Re: re: Problem with uploading an image from my local drive

by Dung » Fri Mar 30, 2018 3:52 pm

Thank you so much Kevin.


Return to General Discussion
cron