8000 Vision Product Search (#1161) · ssahaya/java-docs-samples@7d03cf8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d03cf8

Browse files
nirupa-kumarnnegrey
authored andcommitted
Vision Product Search (GoogleCloudPlatform#1161)
* Vision Product Search * Update ProductInProductSetManagement.java
1 parent 8cc8e35 commit 7d03cf8

File tree

11 files changed

+1589
-0
lines changed

11 files changed

+1589
-0
lines changed
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
<!--
2+
Copyright 2018 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<groupId>com.example.vision</groupId>
19+
<artifactId>vision-detect-product-search</artifactId>
20+
<packaging>jar</packaging>
21+
22+
<!--
23+
The parent pom defines common style checks and testing strategies for our samples.
24+
Removing or replacing it should not affect the execution of the samples in anyway.
25+
-->
26+
<parent>
27+
<groupId>com.google.cloud.samples</groupId>
28+
<artifactId>shared-configuration</artifactId>
29+
<version>1.0.9</version>
30+
</parent>
31+
32+
<properties>
33+
<maven.compiler.target>1.8</maven.compiler.target>
34+
<maven.compiler.source>1.8</maven.compiler.source>
35+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36+
</properties>
37+
38+
<dependencies>
39+
<!-- [START dependencies] -->
40+
<dependency>
41+
<groupId>com.google.cloud</groupId>
42+
<artifactId>google-cloud-vision</artifactId>
43+
<version>1.37.1</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.google.cloud</groupId>
47+
<artifactId>google-cloud-storage</artifactId>
48+
<version>1.37.1</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>net.sourceforge.argparse4j</groupId>
52+
<artifactId>argparse4j</artifactId>
53+
<version>0.8.1</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.apache.commons</groupId>
57+
<artifactId>commons-csv</artifactId>
58+
<version>1.5</version>
59+
</dependency>
60+
<!-- [END dependencies] -->
61+
62+
<!-- Test dependencies -->
63+
<dependency>
64+
<groupId>junit</groupId>
65+
<artifactId>junit</artifactId>
66+
<version>4.12</version>
67+
<scope>test</scope>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>com.google.truth</groupId>
72+
<artifactId>truth</artifactId>
73+
<version>0.40</version>
74+
<scope>test</scope>
75+
</dependency>
76+
</dependencies>
77+
78+
<profiles>
79+
<profile>
80+
<id>ImportProductSets</id>
81+
<activation>
82+
<property>
83+
<name>ImportProductSets</name>
84+
</property>
85+
</activation>
86+
<build>
87+
<plugins>
88+
<plugin>
89+
<groupId>org.codehaus.mojo</groupId>
90+
<artifactId>exec-maven-plugin</artifactId>
91+
<version>1.6.0</version>
92+
<executions>
93+
<execution>
94+
<goals>
95+
<goal>java</goal>
96+
</goals>
97+
</execution>
98+
</executions>
99+
<configuration>
100+
<mainClass>com.example.vision.ImportProductSets</mainClass>
101+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
102+
</configuration>
103+
</plugin>
104+
</plugins>
105+
</build>
106+
</profile>
107+
<profile>
108+
<id>ProductInProductSetManagement</id>
109+
<activation>
110+
<property>
111+
<name>ProductInProductSetManagement</name>
112+
</property>
113+
</activation>
114+
<build>
115+
<plugins>
116+
<plugin>
117+
<groupId>org.codehaus.mojo</groupId>
118+
<artifactId>exec-maven-plugin</artifactId>
119+
<version>1.6.0</version>
120+
<executions>
121+
<execution>
122+
<goals>
123+
<goal>java</goal>
124+
</goals>
125+
</execution>
126+
</executions>
127+
<configuration>
128+
<mainClass>com.example.vision.ProductInProductSetManagement</mainClass>
129+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
130+
</configuration>
131+
</plugin>
132+
</plugins>
133+
</build>
134+
</profile>
135+
<profile>
136+
<id>ProductManagement</id>
137+
<activation>
138+
<property>
139+
<name>ProductManagement</name>
140+
</property>
141+
</activation>
142+
<build>
143+
<plugins>
144+
<plugin>
145+
<groupId>org.codehaus.mojo</groupId>
146+
<artifactId>exec-maven-plugin</artifactId>
147+
<version>1.6.0</version>
148+
<executions>
149+
<execution>
150+
<goals>
151+
<goal>java</goal>
152+
</goals>
153+
</execution>
154+
</executions>
155+
<configuration>
156+
<mainClass>com.example.vision.ProductManagement</mainClass>
157+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
158+
</configuration>
159+
</plugin>
160+
</plugins>
161+
</build>
162+
</profile>
163+
<profile>
164+
<id>ProductSearch</id>
165+
<activation>
166+
<property>
167+
<name>ProductSearch</name>
168+
</property>
169+
</activation>
170+
<build>
171+
<plugins>
172+
<plugin>
173+
<groupId>org.codehaus.mojo</groupId>
174+
<artifactId>exec-maven-plugin</artifactId>
175+
<version>1.6.0</version>
176+
<executions>
177+
<execution>
178+
<goals>
179+
<goal>java</goal>
180+
</goals>
181+
</execution>
182+
</executions>
183+
<configuration>
184+
<mainClass>com.example.vision.ProductSearch</mainClass>
185+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
186+
</configuration>
187+
</plugin>
188+
</plugins>
189+
</build>
190+
</profile>
191+
<profile>
192+
<id>ProductSetManagement</id>
193+
<activation>
194+
<property>
195+
<name>ProductSetManagement</name>
196+
</property>
197+
</activation>
198+
<build>
199+
<plugins>
200+
<plugin>
201+
<groupId>org.codehaus.mojo</groupId>
202+
<artifactId>exec-maven-plugin</artifactId>
203+
<version>1.6.0</version>
204+
<executions>
205+
<execution>
206+
<goals>
207+
<goal>java</goal>
208+
</goals>
209+
</execution>
210+
</executions>
211+
<configuration>
212+
<mainClass>com.example.vision.ProductSetManagement</mainClass>
213+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
214+
</configuration>
215+
</plugin>
216+
</plugins>
217+
</build>
218+
</profile>
219+
<profile>
220+
<id>ReferenceImageManagement</id>
221+
<activation>
222+
<property>
223+
<name>ReferenceImageManagement</name>
224+
</property>
225+
</activation>
226+
<build>
227+
<plugins>
228+
<plugin>
229+
<groupId>org.codehaus.mojo</groupId>
230+
<artifactId>exec-maven-plugin</artifactId>
231+
<version>1.6.0</version>
232+
<executions>
233+
<execution>
234+
<goals>
235+
<goal>java</goal>
236+
</goals>
237+
</execution>
238+
</executions>
239+
<configuration>
240+
<mainClass>com.example.vision.ReferenceImageManagement</mainClass>
241+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
242+
</configuration>
243+
</plugin>
244+
</plugins>
245+
</build>
246+
</profile>
247+
</profiles>
248+
</project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"gs://java-docs-samples-testing/product-search/shoes_1.jpg","indexed_product_set_id_for_testing","indexed_product_id_for_testing_1","apparel","style=womens","0.1,0.1,0.9,0.1,0.9,0.9,0.1,0.9"
2+
"gs://java-docs-samples-testing/product-search/shoes_2.jpg","indexed_product_set_id_for_testing","indexed_product_id_for_testing_2","apparel",,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"gs://java-docs-samples-testing/product-search/shoes_1.jpg","fake_product_set_id_for_testing","fake_product_id_for_testing_1","apparel","style=womens","0.1,0.1,0.9,0.1,0.9,0.9,0.1,0.9"
2+
"gs://java-docs-samples-testing/product-search/shoes_2.jpg","fake_product_set_id_for_testing","fake_product_id_for_testing_2","apparel",,
Loading
Loading
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.vision;
18+
19+
// [START product_search_import]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.vision.v1p3beta1.BatchOperationMetadata;
22+
import com.google.cloud.vision.v1p3beta1.ImportProductSetsGcsSource;
23+
import com.google.cloud.vision.v1p3beta1.ImportProductSetsGcsSource.Builder;
24+
import com.google.cloud.vision.v1p3beta1.ImportProductSetsInputConfig;
25+
import com.google.cloud.vision.v1p3beta1.ImportProductSetsResponse;
26+
import com.google.cloud.vision.v1p3beta1.LocationName;
27+
import com.google.cloud.vision.v1p3beta1.ProductSearchClient;
28+
import com.google.cloud.vision.v1p3beta1.ReferenceImage;
29+
30+
import java.io.PrintStream;
31+
import javax.swing.JPanel;
32+
33+
import net.sourceforge.argparse4j.ArgumentParsers;
34+
import net.sourceforge.argparse4j.inf.ArgumentParser;
35+
import net.sourceforge.argparse4j.inf.ArgumentParserException;
36+
import net.sourceforge.argparse4j.inf.Namespace;
37+
import net.sourceforge.argparse4j.inf.Subparser;
38+
import net.sourceforge.argparse4j.inf.Subparsers;
39+
// [END product_search_import]
40+
41+
/**
42+
* This application demonstrates how to Import Product Sets in Cloud Vision
43+
* Product Search.
44+
*
45+
* For more information, see the tutorial page at
46+
* https://cloud.google.com/vision/product-search/docs/
47+
*/
48+
49+
public class ImportProductSets extends JPanel {
50+
// [START product_search_import_product_sets]
51+
/**
52+
* Import images of different products in the product set.
53+
*
54+
* @param projectId - Id of the project.
55+
* @param computeRegion - Region name.
56+
* @param gcsUri - Google Cloud Storage URI.Target files must be in Product Search CSV format.
57+
* @throws Exception - on client errors.
58+
*/
59+
public static void importProductSets(String projectId, String computeRegion, String gcsUri)
60+
throws Exception {
61+
ProductSearchClient client = ProductSearchClient.create();
62+
63+
// A resource that represents Google Cloud Platform location.
64+
LocationName projectLocation = LocationName.of(projectId, computeRegion);
65+
Builder gcsSource = ImportProductSetsGcsSource.newBuilder().setCsvFileUri(gcsUri);
66+
67+
// Set the input configuration along with Google Cloud Storage URI
68+
ImportProductSetsInputConfig inputConfig =
69+
ImportProductSetsInputConfig.newBuilder().setGcsSource(gcsSource).build();
70+
71+
// Import the product sets from the input URI.
72+
OperationFuture<ImportProductSetsResponse, BatchOperationMetadata> response =
73+
client.importProductSetsAsync(projectLocation, inputConfig);
74+
75+
System.out.println(String.format("Processing operation name: %s", response.getName()));
76+
ImportProductSetsResponse results = response.get();
77+
System.out.println("Processing done.");
78+
System.out.println("Results of the processing:");
79+
80+
for (int i = 0; i < results.getStatusesCount(); i++) {
81+
System.out.println(
82+
String.format("Status of processing line %s of the csv: %s", i, results.getStatuses(i)));
83+
// Check the status of reference image.
84+
if (results.getStatuses(i).getCode() == 0) {
85+
ReferenceImage referenceImage = results.getReferenceImages(i);
86+
System.out.println(referenceImage);
87+
} else {
88+
System.out.println("No reference image.");
89+
}
90+
}
91+
}
92+
// [END product_search_import_product_sets]
93+
94+
public static void main(String[] args) throws Exception {
95+
ImportProductSets importProductSet = new ImportProductSets();
96+
importProductSet.argsHelper(args, System.out);
97+
}
98+
99+
public static void argsHelper(String[] args, PrintStream out) throws Exception {
100+
ArgumentParser parser = ArgumentParsers.newFor("Import Product Sets").build();
101+
Subparsers subparsers = parser.addSubparsers().dest("command");
102+
103+
Subparser importProductSetsParser = subparsers.addParser("import_product_sets");
104+
importProductSetsParser.addArgument("gcsUri");
105+
106+
String projectId = System.getenv("PROJECT_ID");
107+
String computeRegion = System.getenv("REGION_NAME");
108+
109+
Namespace ns = null;
110+
try {
111+
ns = parser.parseArgs(args);
112+
if (ns.get("command").equals("import_product_sets")) {
113+
importProductSets(projectId, computeRegion, ns.getString("gcsUri"));
114+
}
115+
} catch (ArgumentParserException e) {
116+
parser.handleError(e);
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)
0