-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogoDetection.java
171 lines (142 loc) · 7.19 KB
/
LogoDetection.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package csci576;
import java.util.List;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.io.FileInputStream;
import java.io.PrintStream;
import java.util.*;
import java.util.LinkedList;
import java.nio.file.Files;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;
import java.nio.file.Paths;
import com.google.cloud.vision.v1.*;
import com.google.cloud.vision.v1.Feature.Type;
import com.google.protobuf.ByteString;
public class LogoDetection
{
public static String detectLogos(String path, PrintStream out) throws Exception, IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();
List<String> logos = new ArrayList<>();
ByteString imgBytes = ByteString.readFrom(new FileInputStream(path));
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.LOGO_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();
String foundLogo = null;
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
out.printf("Error: %s\n", res.getError().getMessage());
return null;
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
for (EntityAnnotation annotation : res.getLogoAnnotationsList()) {
foundLogo = annotation.getDescription();
System.out.println("logos adding... " + annotation.getDescription());
}
}
return foundLogo;
}
}
public static LinkedList<String> matchLogoToImage(String filePath, PrintStream out, ByteString imgBytes, List<String> logos) throws Exception, IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();
LinkedList<String> matched = new LinkedList<>();
//ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
//ByteString imgBytes = ByteString.readFrom(img);
try{
//the next few lines create a byte array from the BufferedImage
//you can comment these out and have this function take in a byte array directly
//not sure if this works with a BufferedImage - I ran it on the video and it
//didn't look like it was detecting anything but i didn't get an error back from the API
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
//ImageIO.write( img, "jpg", baos );
//baos.flush();
//byte[] imageInByte = baos.toByteArray();
//.close();
//below is where you would input the byte array (this turns the array into ByteString
//which is needed for this code to work
// ByteString newImage = ByteString.copyFrom(imageInByte);
Image img2 = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.LOGO_DETECTION).build();
AnnotateImageRequest request =
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img2).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()) {
out.printf("Error: %s\n", res.getError().getMessage());
return new LinkedList<>();
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
for (EntityAnnotation annotation : res.getLogoAnnotationsList()) {
String description = annotation.getDescription();
System.out.println("img detected... " + annotation.getDescription());
//matched.add(description);
for(int i = 0; i < logos.size(); i++) {
String logo = logos.get(i);
if(description.contains(logo) || logo.contains(description)) {
matched.add(logo);
}
if(description.contains("hard rock")){
matched.add("hard rock live");
}
}
}
}
return matched;
}
}catch(IOException e){
System.out.println(e.getMessage());
}
return null;
}
public static void getNewAds(File dir, List<String> matched) {
File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
for( String match : matched) {
if(child.getName().contains(match)) {
System.out.println("Found The Ad File To Insert: " + child.getName());
}
}
System.out.println("file: " + child.getName());
}
} else {
System.out.println("No Brand Images Found");
}
}
public static List<String> run(String path)
{
System.out.println("current brand image path: " + path);
try {
List<String> logos = new ArrayList<>();
Files.walk( Paths.get(path))
.filter(s -> s.toString().endsWith(".bmp"))
.map(Path::getFileName)
.collect(Collectors.toList()).stream().forEach(x->{
try {
logos.add(detectLogos(path + x.getFileName().toString(), System.out));
} catch (Exception e) {
e.printStackTrace();
}
});;
return logos;
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}