Skip to content

Instantly share code, notes, and snippets.

@anilkay
Created December 17, 2019 23:17
Show Gist options
  • Save anilkay/5fa141888ee7713584cf01518768a50c to your computer and use it in GitHub Desktop.
Save anilkay/5fa141888ee7713584cf01518768a50c to your computer and use it in GitHub Desktop.
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.vision.v1.*;
import com.google.protobuf.ByteString;
import com.google.type.LatLng;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
class VisionSingleton {
private static ImageAnnotatorClient client;
public static ImageAnnotatorClient getImageAnnotatorClient() {
if (client == null) {
GoogleCredentials credentials;
File credentialsPath = new File("cred.json");
try {
credentials = GoogleCredentials.fromStream(new FileInputStream(credentialsPath));
} catch (IOException e) {
return null;
}
ImageAnnotatorSettings settings = null;
try {
settings = ImageAnnotatorSettings.newBuilder().setCredentialsProvider(
FixedCredentialsProvider.create(credentials)
).build();
} catch (IOException e) {
return null;
}
try {
client = ImageAnnotatorClient.create(settings);
} catch (IOException e) {
return null;
}
}
return client;
}
}
public class GVision {
ImageAnnotatorClient Client;
GVision() throws IOException {
Client = VisionSingleton.getImageAnnotatorClient();
}
private Image getImagefromPath(String filepath) throws IOException {
Path path = Paths.get(filepath);
byte[] data = Files.readAllBytes(path);
ByteString imgBytes = ByteString.copyFrom(data);
Image img = Image.newBuilder().setContent(imgBytes).build();
return img;
}
public void getWebDetectionThings(String filepath) throws IOException {
Image img = getImagefromPath(filepath);
List<AnnotateImageRequest> requests = new ArrayList<>();
Feature feat = Feature.newBuilder()
.setType(Feature.Type.WEB_DETECTION)
.build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
.addFeatures(feat)
.setImage(img)
.build();
requests.add(request);
BatchAnnotateImagesResponse resp = Client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responseList = resp.getResponsesList();
for (AnnotateImageResponse imageResponse : responseList) {
WebDetection detection = imageResponse.getWebDetection();
List<WebDetection.WebLabel> labels = detection.getBestGuessLabelsList();
for (WebDetection.WebLabel label : labels) {
System.out.println(label.getLabel());
}
}
}
public void getLandmarkDetection(String filepath) throws IOException {
//Bizim Memleketi Landmarktan Saymamak ne demektir yahu.
Image img = getImagefromPath(filepath);
List<AnnotateImageRequest> requests = new ArrayList<>();
Feature feat = Feature.newBuilder()
.setType(Feature.Type.LANDMARK_DETECTION)
.build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
.addFeatures(feat)
.setImage(img)
.build();
requests.add(request);
BatchAnnotateImagesResponse resp = Client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responseList = resp.getResponsesList();
for (AnnotateImageResponse annotateImageResponse : responseList) {
List<EntityAnnotation> entities = annotateImageResponse.getLandmarkAnnotationsList();
for (EntityAnnotation annotation : entities) {
LocationInfo info = annotation.getLocationsList().listIterator().next();
String desc = annotation.getDescription();
LatLng lang = info.getLatLng();
System.out.printf("Nerede: %s %s", desc, lang);
}
}
}
public String textDetection(String filepath) throws IOException {
Image img = getImagefromPath(filepath);
List<AnnotateImageRequest> requests = new ArrayList<>();
Feature feat = Feature.newBuilder()
.setType(Feature.Type.TEXT_DETECTION)
.build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
.addFeatures(feat)
.setImage(img)
.build();
requests.add(request);
BatchAnnotateImagesResponse resp = Client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responseList = resp.getResponsesList();
final String[] texts=new String[responseList.size()];
int i=0;
for (AnnotateImageResponse annotateImageResponse : responseList) {
texts[i++]= annotateImageResponse.getFullTextAnnotation().getText();
}
String result="";
for(String text:texts){
result+=text;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment