-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInferenceLocal.java
More file actions
67 lines (56 loc) · 2.52 KB
/
Copy pathInferenceLocal.java
File metadata and controls
67 lines (56 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class InferenceLocal {
// Get Image Path
public String detect(String filePath, String fileName) {
File file = new File(filePath);
// Base 64 Encode
String encodedFile;
try (FileInputStream fileInputStreamReader = new FileInputStream(file)) {
byte[] bytes = new byte[(int) file.length()];
fileInputStreamReader.read(bytes);
encodedFile = new String(Base64.getEncoder().encode(bytes), StandardCharsets.US_ASCII);
String API_KEY = "BNPmtVcTPzgRmg3Xxlq3"; // Your API Key
String MODEL_ENDPOINT = "fruit-qbury/3"; // model endpoint
// Construct the URL
String uploadURL = "https://detect.roboflow.com/" + MODEL_ENDPOINT + "?api_key=" + API_KEY + "&name=" + fileName;
// Http Request
HttpURLConnection connection = null;
try {
// Configure connection to URL
URL url = new URL(uploadURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
// Send request
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.writeBytes(encodedFile);
}
// Get Response
try (InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
response.append(System.lineSeparator());
}
return response.toString().trim(); // Trim to remove any leading or trailing whitespace
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null; // Return null if an error occurs
}
}