www in docker support
This commit is contained in:
parent
539a848e95
commit
c227fce036
2145 changed files with 399596 additions and 58 deletions
|
|
@ -0,0 +1,243 @@
|
|||
package it.acxent.face.api.vision;
|
||||
|
||||
import it.acxent.cc.Attivita;
|
||||
import it.acxent.common.Parm;
|
||||
import it.acxent.common.StatusMsg;
|
||||
import it.acxent.db.ApplParm;
|
||||
import it.acxent.db.ApplParmFull;
|
||||
import it.acxent.db.DBAdapter;
|
||||
import java.io.File;
|
||||
import java.util.Base64;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class GoogleVisionApi {
|
||||
private Attivita attivita;
|
||||
|
||||
private boolean debug = false;
|
||||
|
||||
private ApplParmFull apFull = null;
|
||||
|
||||
private String googleApiKey;
|
||||
|
||||
private String imageFileName;
|
||||
|
||||
public static final String CUPS_SESSION_ID = "_CUPS_SESSION_ID";
|
||||
|
||||
public static final String API_PRODUCTION = "https://vision.googleapis.com/v1/";
|
||||
|
||||
public static final String P_GOOGLE_API_KEY_VISION = "GOOGLE_API_KEY_VISION";
|
||||
|
||||
private static final String URI_CMD_IMAGE_ANNOTATE = "images:annotate?key={key}";
|
||||
|
||||
public GoogleVisionApi(ApplParmFull apFull) {
|
||||
this.apFull = apFull;
|
||||
this.googleApiKey = apFull.getParm("GOOGLE_API_KEY_VISION").getTesto();
|
||||
}
|
||||
|
||||
public GoogleVisionApi(String l_googleApiKey) {
|
||||
this.googleApiKey = l_googleApiKey;
|
||||
}
|
||||
|
||||
public GoogleVisionApi() {}
|
||||
|
||||
public static void initApplicationParms(ApplParmFull ap) {
|
||||
if (ap != null) {
|
||||
DBAdapter.logDebug(true, "GOOGLE VISION initParms: start");
|
||||
String l_tipoParm = "GOOGLE VISION";
|
||||
StatusMsg.updateMsgByTag(ap, "INIT", l_tipoParm);
|
||||
Parm bean = new Parm(ap);
|
||||
bean.findByCodice("GOOGLE_API_KEY_VISION");
|
||||
bean.setFlgAdmin(1L);
|
||||
bean.setTipoParm(l_tipoParm);
|
||||
bean.setCodice("GOOGLE_API_KEY_VISION");
|
||||
bean.setDescrizione("GOOGLE_API_KEY_VISION");
|
||||
if (bean.getTesto().isEmpty())
|
||||
bean.setTesto("AIzaSyBYEytX_RU1GDazEC4wCVz5SuxmM_XWUvU");
|
||||
bean.setNota("API KEY ACCESSO AL CLOUD VISION DI GOOGLE<br>AIzaSyBYEytX_RU1GDazEC4wCVz5SuxmM_XWUvU su comm<br>AIzaSyBYEytX_RU1GDazEC4wCVz5SuxmM_XWUvU qui");
|
||||
bean.save();
|
||||
DBAdapter.logDebug(true, "GOOGLE VISION initParms: stop");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String hostname = "localhost";
|
||||
String db = "tr";
|
||||
ApplParmFull ap = new ApplParmFull(new ApplParm(3, "//" + hostname + "/" + db, "root", "root", 1, 10, 300));
|
||||
GoogleVisionApi bean = new GoogleVisionApi("AIzaSyBYEytX_RU1GDazEC4wCVz5SuxmM_XWUvU");
|
||||
GoogleVisionResult resF = bean.annotateFaces("/Users/acolzi/Downloads/_testFace/bastogi1.jpg");
|
||||
System.out.println(resF.getMsg() + "\n" + resF.getMsg());
|
||||
}
|
||||
|
||||
protected String getBase64BasicCredential() {
|
||||
String encoding = "aa:bb";
|
||||
return "Basic " + Base64.getEncoder().encodeToString(encoding.getBytes());
|
||||
}
|
||||
|
||||
public ApplParmFull getApFull() {
|
||||
return this.apFull;
|
||||
}
|
||||
|
||||
public void setApFull(ApplParmFull apFull) {
|
||||
this.apFull = apFull;
|
||||
}
|
||||
|
||||
public Attivita getAttivita() {
|
||||
if (this.attivita == null)
|
||||
this.attivita = Attivita.getDefaultInstance(getApFull());
|
||||
return this.attivita;
|
||||
}
|
||||
|
||||
public String getApiEndpoint() {
|
||||
return "https://vision.googleapis.com/v1/";
|
||||
}
|
||||
|
||||
public GoogleVisionResult annotateImage(String l_imageFileName) {
|
||||
GoogleVisionResult resER = new GoogleVisionResult();
|
||||
try {
|
||||
if (this.debug)
|
||||
System.out.println("GoogleVisionApi --> annotateImage");
|
||||
CloseableHttpClient client = HttpClients.createDefault();
|
||||
HttpPost request = new HttpPost(getApiEndpoint() + getApiEndpoint());
|
||||
if (this.debug)
|
||||
System.out.println(getApiEndpoint() + getApiEndpoint());
|
||||
request.setHeader("Accept", "application/json");
|
||||
request.setHeader("Content-Type", "application/json");
|
||||
request.setHeader("Content-Language", "it-IT");
|
||||
JSONObject jBody = new JSONObject();
|
||||
jBody.put("parent", "");
|
||||
JSONArray jRequest = new JSONArray();
|
||||
JSONObject jRequestRow = new JSONObject();
|
||||
JSONObject jImage = new JSONObject();
|
||||
byte[] fileContent = FileUtils.readFileToByteArray(new File(l_imageFileName));
|
||||
String encodedString = Base64.getEncoder().encodeToString(fileContent);
|
||||
jImage.put("content", encodedString);
|
||||
jRequestRow.put("image", jImage);
|
||||
JSONArray jFeatures = new JSONArray();
|
||||
JSONObject jType = new JSONObject();
|
||||
jType.put("type", "DOCUMENT_TEXT_DETECTION");
|
||||
jFeatures.put(jType);
|
||||
jRequestRow.put("features", jFeatures);
|
||||
jRequest.put(jRequestRow);
|
||||
jBody.put("requests", jRequest);
|
||||
if (!this.debug);
|
||||
System.out.println(jBody.toString(4));
|
||||
StringEntity postingString = new StringEntity(jBody.toString());
|
||||
request.setEntity((HttpEntity)postingString);
|
||||
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
||||
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
||||
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
||||
if (this.debug) {
|
||||
System.out.println("Status Code: " + statusCode);
|
||||
System.out.println("\ncontent = " + content);
|
||||
}
|
||||
if (statusCode >= 400) {
|
||||
resER.setOk(false);
|
||||
JSONObject jo = new JSONObject(content);
|
||||
resER.setMsg(jo.toString(2));
|
||||
resER.setResult(jo);
|
||||
} else {
|
||||
JSONObject jo = new JSONObject(content);
|
||||
if (this.debug)
|
||||
System.out.println(jo.toString(4));
|
||||
resER.setMsg("Text Found");
|
||||
resER.setOk(true);
|
||||
resER.setResult(jo.toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
e.printStackTrace();
|
||||
resER.setOk(false);
|
||||
resER.setMsg(e.getMessage());
|
||||
}
|
||||
return resER;
|
||||
}
|
||||
|
||||
public String getGoogleApiKey() {
|
||||
return (this.googleApiKey == null) ? "" : this.googleApiKey.trim();
|
||||
}
|
||||
|
||||
public void setGoogleApiKey(String googleApiKey) {
|
||||
this.googleApiKey = googleApiKey;
|
||||
}
|
||||
|
||||
public String getImageFileName() {
|
||||
return (this.imageFileName == null) ? "" : this.imageFileName.trim();
|
||||
}
|
||||
|
||||
public void setImageFileName(String imageFile) {
|
||||
this.imageFileName = imageFile;
|
||||
}
|
||||
|
||||
public GoogleVisionResult annotateFaces(String l_imageFileName) {
|
||||
GoogleVisionResult resER = new GoogleVisionResult();
|
||||
File l_imageFileNameFile = new File(l_imageFileName);
|
||||
if (!l_imageFileNameFile.exists()) {
|
||||
resER.setOk(false);
|
||||
resER.setMsg("File " + l_imageFileName + " not found!");
|
||||
return resER;
|
||||
}
|
||||
try {
|
||||
if (this.debug)
|
||||
System.out.println("GoogleVisionApi --> annotateImage");
|
||||
CloseableHttpClient client = HttpClients.createDefault();
|
||||
HttpPost request = new HttpPost(getApiEndpoint() + getApiEndpoint());
|
||||
if (this.debug)
|
||||
System.out.println(getApiEndpoint() + getApiEndpoint());
|
||||
request.setHeader("Accept", "application/json");
|
||||
request.setHeader("Content-Type", "application/json");
|
||||
request.setHeader("Content-Language", "it-IT");
|
||||
JSONObject jBody = new JSONObject();
|
||||
jBody.put("parent", "");
|
||||
JSONArray jRequest = new JSONArray();
|
||||
JSONObject jRequestRow = new JSONObject();
|
||||
JSONObject jImage = new JSONObject();
|
||||
byte[] fileContent = FileUtils.readFileToByteArray(l_imageFileNameFile);
|
||||
String encodedString = Base64.getEncoder().encodeToString(fileContent);
|
||||
jImage.put("content", encodedString);
|
||||
jRequestRow.put("image", jImage);
|
||||
JSONArray jFeatures = new JSONArray();
|
||||
JSONObject jType = new JSONObject();
|
||||
jType.put("type", "FACE_DETECTION");
|
||||
jFeatures.put(jType);
|
||||
jRequestRow.put("features", jFeatures);
|
||||
jRequest.put(jRequestRow);
|
||||
jBody.put("requests", jRequest);
|
||||
StringEntity postingString = new StringEntity(jBody.toString());
|
||||
request.setEntity((HttpEntity)postingString);
|
||||
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
||||
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
||||
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
||||
if (this.debug) {
|
||||
System.out.println("Status Code: " + statusCode);
|
||||
System.out.println("\ncontent = " + content);
|
||||
}
|
||||
if (statusCode >= 400) {
|
||||
resER.setOk(false);
|
||||
JSONObject jo = new JSONObject(content);
|
||||
resER.setMsg(jo.toString(2));
|
||||
resER.setResult(jo);
|
||||
} else {
|
||||
JSONObject jo = new JSONObject(content);
|
||||
resER.setMsg("Faces found");
|
||||
resER.setOk(true);
|
||||
resER.setResult(jo);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
e.printStackTrace();
|
||||
resER.setOk(false);
|
||||
resER.setMsg(e.getMessage());
|
||||
}
|
||||
return resER;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package it.acxent.face.api.vision;
|
||||
|
||||
public class GoogleVisionResult {
|
||||
private String msg;
|
||||
|
||||
public GoogleVisionResult(String msg, boolean status, Object result) {
|
||||
this.msg = msg;
|
||||
this.ok = status;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
private boolean ok = true;
|
||||
|
||||
private Object result;
|
||||
|
||||
public GoogleVisionResult() {}
|
||||
|
||||
public String getMsg() {
|
||||
return (this.msg == null) ? "" : this.msg.trim();
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public boolean isOk() {
|
||||
return this.ok;
|
||||
}
|
||||
|
||||
public void setOk(boolean status) {
|
||||
this.ok = status;
|
||||
}
|
||||
|
||||
public Object getResult() {
|
||||
return this.result;
|
||||
}
|
||||
|
||||
public void setResult(Object result) {
|
||||
this.result = result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
package it.acxent.face.api.vision;
|
||||
Loading…
Add table
Add a link
Reference in a new issue