-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from Mazawrath/GoogleVision
Google vision
- Loading branch information
Showing
7 changed files
with
309 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
src/main/java/com/mazawrath/beanbot/commands/googlevision/AnalyzeCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package com.mazawrath.beanbot.commands.googlevision; | ||
|
||
import com.google.cloud.vision.v1.AnnotateImageResponse; | ||
import com.google.cloud.vision.v1.EntityAnnotation; | ||
import com.google.cloud.vision.v1.SafeSearchAnnotation; | ||
import com.google.cloud.vision.v1.WebDetection; | ||
import com.mazawrath.beanbot.utilities.GoogleCloudVision; | ||
import com.mazawrath.beanbot.utilities.Points; | ||
import de.btobastian.sdcf4j.Command; | ||
import de.btobastian.sdcf4j.CommandExecutor; | ||
import org.apache.commons.lang3.text.WordUtils; | ||
import org.javacord.api.DiscordApi; | ||
import org.javacord.api.entity.channel.ServerTextChannel; | ||
import org.javacord.api.entity.message.Message; | ||
import org.javacord.api.entity.message.embed.EmbedBuilder; | ||
import org.javacord.api.entity.server.Server; | ||
import org.javacord.api.entity.user.User; | ||
|
||
import javax.activation.MimetypesFileTypeMap; | ||
import java.awt.*; | ||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.List; | ||
|
||
public class AnalyzeCommand implements CommandExecutor { | ||
private Points points; | ||
private GoogleCloudVision cloudVision; | ||
|
||
public AnalyzeCommand(Points points, GoogleCloudVision cloudVision) { | ||
this.points = points; | ||
this.cloudVision = cloudVision; | ||
} | ||
|
||
@Command( | ||
aliases = {"analyze"}, | ||
usage = "cloudVision", | ||
privateMessages = false, | ||
async = true | ||
) | ||
|
||
public void onCommand(String[] args, Message message, DiscordApi api, ServerTextChannel serverTextChannel, User author, Server server) { | ||
URL url; | ||
if (message.getAttachments().size() != 0) | ||
url = message.getAttachments().get(0).getUrl(); | ||
else if (args.length > 0) { | ||
try { | ||
url = new URL(args[0]); | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
serverTextChannel.sendMessage("URL is not valid."); | ||
return; | ||
} | ||
} else { | ||
serverTextChannel.sendMessage("You must either have a URL in your message or an attachment."); | ||
return; | ||
} | ||
|
||
if (!points.removePoints(author.getIdAsString(), api.getYourself().getIdAsString(), server.getIdAsString(), Points.GOOGLE_VISION_COST)) { | ||
serverTextChannel.sendMessage("You do not have enough beanCoin for this command"); | ||
return; | ||
} | ||
|
||
serverTextChannel.sendMessage("Analyzing image..."); | ||
|
||
List<EntityAnnotation> labelAnnotation; | ||
AnnotateImageResponse faceDetection; | ||
SafeSearchAnnotation safeSearchAnnotation; | ||
WebDetection webDetection; | ||
|
||
if (urlContainsImage(url)) { | ||
try { | ||
labelAnnotation = cloudVision.getLabelDetection(url); | ||
faceDetection = cloudVision.getFaceDetection(url); | ||
safeSearchAnnotation = cloudVision.detectSafeSearch(url); | ||
webDetection = cloudVision.getWebDetection(url); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
serverTextChannel.sendMessage("Something went wrong."); | ||
return; | ||
} | ||
} else { | ||
serverTextChannel.sendMessage("URL must be an image."); | ||
return; | ||
} | ||
|
||
EmbedBuilder embed = new EmbedBuilder() | ||
.setTitle("Image Analysis") | ||
.setColor(Color.BLUE); | ||
|
||
StringBuilder labels = new StringBuilder(); | ||
for (int i = 0; i < labelAnnotation.size(); i++) { | ||
if (i != labelAnnotation.size() - 1) { | ||
labels.append(labelAnnotation.get(i).getDescription()).append(" (").append(Math.round(labelAnnotation.get(0).getScore() * 100)).append("%), "); | ||
} else | ||
labels.append(labelAnnotation.get(i).getDescription()).append(" (").append(Math.round(labelAnnotation.get(0).getScore() * 100)).append("%)"); | ||
} | ||
|
||
embed.addField("Things I See", labels.toString()); | ||
|
||
embed.addInlineField("Faces I See", String.valueOf(faceDetection.getFaceAnnotationsCount())); | ||
|
||
for (int i = 0; i < faceDetection.getFaceAnnotationsCount(); i++) { | ||
StringBuilder emotionsSeen = new StringBuilder(); | ||
if (faceDetection.getFaceAnnotations(i).getJoyLikelihoodValue() > 1) | ||
emotionsSeen.append("joy, "); | ||
if (faceDetection.getFaceAnnotations(i).getSorrowLikelihoodValue() > 1) | ||
emotionsSeen.append("sorrow, "); | ||
if (faceDetection.getFaceAnnotations(i).getAngerLikelihoodValue() > 1) | ||
emotionsSeen.append("anger, "); | ||
if (faceDetection.getFaceAnnotations(i).getSurpriseLikelihoodValue() > 1) | ||
emotionsSeen.append("surprise, "); | ||
|
||
if (emotionsSeen.length() != 0) | ||
embed.addInlineField("Face " + (i + 1) + "'s Possible Emotions", WordUtils.capitalizeFully(emotionsSeen.substring(0, emotionsSeen.length() - 2))); | ||
else | ||
embed.addInlineField("Face " + (i + 1) + "'s Possible Emotions", "none"); | ||
} | ||
embed.addInlineField("Best Guess", webDetection.getBestGuessLabels(0).getLabel()); | ||
|
||
StringBuilder webLabels = new StringBuilder(); | ||
for (int i = 0; i < webDetection.getWebEntitiesCount(); i++) { | ||
if (i != webDetection.getWebEntitiesCount() - 1) | ||
webLabels.append(webDetection.getWebEntities(i).getDescription()).append(" (").append(Math.round(webDetection.getWebEntities(i).getScore() * 100)).append("%), "); | ||
else | ||
webLabels.append(webDetection.getWebEntities(i).getDescription()).append(" (").append(Math.round(webDetection.getWebEntities(i).getScore() * 100)).append("%)"); | ||
} | ||
embed.addField("Things I Think This Is", webLabels.toString()); | ||
|
||
if (safeSearchAnnotation.getAdultValue() > 2) | ||
embed.addInlineField("Adult Content", WordUtils.capitalizeFully(safeSearchAnnotation.getAdult().name().replaceAll("_", " "))); | ||
if (safeSearchAnnotation.getSpoofValue() > 2) | ||
embed.addInlineField("Spoof / Edited Photo", WordUtils.capitalizeFully(safeSearchAnnotation.getSpoof().name().replaceAll("_", " "))); | ||
if (safeSearchAnnotation.getMedicalValue() > 2) | ||
embed.addInlineField("Medical / Surgery", WordUtils.capitalizeFully(safeSearchAnnotation.getMedical().name().replaceAll("_", " "))); | ||
if (safeSearchAnnotation.getViolenceValue() > 2) | ||
embed.addInlineField("Violence / Blood / Gore", WordUtils.capitalizeFully(safeSearchAnnotation.getViolence().name().replaceAll("_", " "))); | ||
if (safeSearchAnnotation.getRacyValue() > 2) | ||
embed.addInlineField("Skimpy / Nudity", WordUtils.capitalizeFully(safeSearchAnnotation.getRacy().name().replaceAll("_", " "))); | ||
|
||
serverTextChannel.sendMessage(embed); | ||
} | ||
|
||
private boolean urlContainsImage(URL url) { | ||
File f = new File(url.toString()); | ||
String mimetype = new MimetypesFileTypeMap().getContentType(f); | ||
String type = mimetype.split("/")[0]; | ||
return type.equals("image"); | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
src/main/java/com/mazawrath/beanbot/utilities/GoogleCloudVision.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package com.mazawrath.beanbot.utilities; | ||
|
||
import com.google.cloud.vision.v1.*; | ||
import com.google.cloud.vision.v1.Feature.Type; | ||
import com.google.protobuf.ByteString; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class GoogleCloudVision { | ||
|
||
public GoogleCloudVision() { | ||
System.setProperty("http.agent", "Chrome"); | ||
} | ||
|
||
public List<EntityAnnotation> getLabelDetection(URL image) { | ||
try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) { | ||
|
||
ByteString imgBytes = ByteString.copyFrom(Objects.requireNonNull(downloadFile(image))); | ||
|
||
// Builds the image annotation request | ||
List<AnnotateImageRequest> requests = new ArrayList<>(); | ||
Image img = Image.newBuilder().setContent(imgBytes).build(); | ||
Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build(); | ||
AnnotateImageRequest request = AnnotateImageRequest.newBuilder() | ||
.addFeatures(feat) | ||
.setImage(img) | ||
.build(); | ||
requests.add(request); | ||
|
||
// Performs label detection on the image file | ||
BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests); | ||
List<AnnotateImageResponse> responses = response.getResponsesList(); | ||
|
||
for (AnnotateImageResponse res : responses) { | ||
if (res.hasError()) { | ||
System.out.printf("Error: %s\n", res.getError().getMessage()); | ||
return null; | ||
} | ||
|
||
return res.getLabelAnnotationsList(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
public SafeSearchAnnotation detectSafeSearch(URL image) throws IOException { | ||
List<AnnotateImageRequest> requests = new ArrayList<>(); | ||
|
||
ByteString imgBytes = ByteString.copyFrom(Objects.requireNonNull(downloadFile(image))); | ||
|
||
Image img = Image.newBuilder().setContent(imgBytes).build(); | ||
Feature feat = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build(); | ||
AnnotateImageRequest request = | ||
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); | ||
requests.add(request); | ||
|
||
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { | ||
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); | ||
List<AnnotateImageResponse> responses = response.getResponsesList(); | ||
|
||
for (AnnotateImageResponse res : responses) { | ||
if (res.hasError()) { | ||
return null; | ||
} | ||
|
||
// For full list of available annotations, see http://g.co/cloud/vision/docs | ||
return res.getSafeSearchAnnotation(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public AnnotateImageResponse getFaceDetection(URL image) throws Exception, IOException { | ||
List<AnnotateImageRequest> requests = new ArrayList<>(); | ||
|
||
ByteString imgBytes = ByteString.copyFrom(Objects.requireNonNull(downloadFile(image))); | ||
|
||
Image img = Image.newBuilder().setContent(imgBytes).build(); | ||
Feature feat = Feature.newBuilder().setType(Type.FACE_DETECTION).build(); | ||
AnnotateImageRequest request = | ||
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); | ||
requests.add(request); | ||
|
||
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { | ||
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); | ||
return response.getResponsesList().get(0); | ||
} | ||
} | ||
|
||
public WebDetection getWebDetection(URL image) throws Exception, | ||
IOException { | ||
List<AnnotateImageRequest> requests = new ArrayList<>(); | ||
|
||
ByteString imgBytes = ByteString.copyFrom(Objects.requireNonNull(downloadFile(image))); | ||
|
||
Image img = Image.newBuilder().setContent(imgBytes).build(); | ||
Feature feat = Feature.newBuilder().setType(Type.WEB_DETECTION).build(); | ||
AnnotateImageRequest request = | ||
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); | ||
requests.add(request); | ||
|
||
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { | ||
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); | ||
|
||
return response.getResponsesList().get(0).getWebDetection(); | ||
} | ||
} | ||
|
||
|
||
private static byte[] downloadFile(URL url) { | ||
try { | ||
URLConnection conn = url.openConnection(); | ||
conn.setConnectTimeout(5000); | ||
conn.setReadTimeout(5000); | ||
conn.connect(); | ||
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
IOUtils.copy(conn.getInputStream(), baos); | ||
|
||
return baos.toByteArray(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters