forked from zapdoss/EatHealthy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient1.java
79 lines (68 loc) · 2.68 KB
/
client1.java
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
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.*;
import java.net.*;
import java.io.*;
import java.lang.Runtime;
class client1{
private static boolean netIsAvailable(){
try {
final URL url = new URL("http://www.google.com");
final URLConnection conn = url.openConnection();
conn.connect();
return true;
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
return false;
}
}
public static void main(String args[]){
if(!netIsAvailable()){
System.out.println("Maa Chuda. Net to chala le bhikaari!");
return;
}
String address = "/home/skirmish/pizza.jpg";
String c2a = "Authorization: Bearer 9xXZbLHwxeVxsm3CKDQ1vdId9BqkY2";
String cmd = "https://api.clarifai.com/v1/tag/";
String model = "model=food-items-v1.0";
String enc_address = "encoded_data=@"+address;
String[] commands = {"curl", cmd, "-F",model,"-F",enc_address,"-H",c2a};
StringBuilder test = new StringBuilder();
try {
Process p = Runtime.getRuntime().exec(commands);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line = null;
while ((line = reader.readLine())!= null)
{
// System.out.println(line);
test.append(line);
}
while ((line = error.readLine()) != null) {
// System.out.println(line);
}
}
catch (Exception ex){
ex.printStackTrace();
}
String test1 = test.toString();
String new1[] = test1.split("classes");
String new2[] = new1[1].split("concept_ids");
String new3[] = new2[1].split("probs");
String new4[] = new3[1].split("docid_str");
String x = new4[0];
x=x.replace("[","").replace("\"","").replace(",","").replace("]","").replace(": ","").replace("}","");
String[] resultProb = x.split(" ");
String y = new2[0];
y=y.replace("[","").replace(",","").replace("]","").replace(": ","");
String[] result = y.split("\" \"");
result[0]=result[0].replace("\"","");
List<String> list = new ArrayList();
int i;
for(i=0;i<resultProb.length;i++){
double pc = Double.parseDouble(resultProb[i]);
if (pc>0.9) list.add(result[i]);
}
for(i=0;i<list.size();i++) System.out.println(list.get(i));
}
}