-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Vision Product Search #1161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nnegrey
merged 2 commits into
GoogleCloudPlatform:master
from
nirupa-kumar:vision-product-search
Jul 24, 2018
Merged
Vision Product Search #1161
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,248 @@ | ||
<!-- | ||
Copyright 2018 Google Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.example.vision</groupId> | ||
<artifactId>vision-detect-product-search</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<!-- | ||
The parent pom defines common style checks and testing strategies for our samples. | ||
Removing or replacing it should not affect the execution of the samples in anyway. | ||
--> | ||
<parent> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>shared-configuration</artifactId> | ||
<version>1.0.9</version> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- [START dependencies] --> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-vision</artifactId> | ||
<version>1.37.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-storage</artifactId> | ||
<version>1.37.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.sourceforge.argparse4j</groupId> | ||
<artifactId>argparse4j</artifactId> | ||
<version>0.8.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-csv</artifactId> | ||
<version>1.5</version> | ||
</dependency> | ||
<!-- [END dependencies] --> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>0.40</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<profiles> | ||
<profile> | ||
<id>ImportProductSets</id> | ||
<activation> | ||
<property> | ||
<name>ImportProductSets</name> | ||
</property> | ||
</activation> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.6.0</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>java</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<mainClass>com.example.vision.ImportProductSets</mainClass> | ||
<cleanupDaemonThreads>false</cleanupDaemonThreads> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
<profile> | ||
<id>ProductInProductSetManagement</id> | ||
<activation> | ||
<property> | ||
<name>ProductInProductSetManagement</name> | ||
</property> | ||
</activation> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.6.0</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>java</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<mainClass>com.example.vision.ProductInProductSetManagement</mainClass> | ||
<cleanupDaemonThreads>false</cleanupDaemonThreads> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
<profile> | ||
<id>ProductManagement</id> | ||
<activation> | ||
<property> | ||
<name>ProductManagement</name> | ||
</property> | ||
</activation> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.6.0</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>java</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<mainClass>com.example.vision.ProductManagement</mainClass> | ||
<cleanupDaemonThreads>false</cleanupDaemonThreads> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
<profile> | ||
<id>ProductSearch</id> | ||
<activation> | ||
<property> | ||
<name>ProductSearch</name> | ||
</property> | ||
</activation> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.6.0</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>java</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<mainClass>com.example.vision.ProductSearch</mainClass> | ||
<cleanupDaemonThreads>false</cleanupDaemonThreads> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
<profile> | ||
<id>ProductSetManagement</id> | ||
<activation> | ||
<property> | ||
<name>ProductSetManagement</name> | ||
</property> | ||
</activation> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.6.0</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>java</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<mainClass>com.example.vision.ProductSetManagement</mainClass> | ||
<cleanupDaemonThreads>false</cleanupDaemonThreads> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
<profile> | ||
<id>ReferenceImageManagement</id> | ||
<activation> | ||
<property> | ||
<name>ReferenceImageManagement</name> | ||
</property> | ||
</activation> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.6.0</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>java</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<mainClass>com.example.vision.ReferenceImageManagement</mainClass> | ||
<cleanupDaemonThreads>false</cleanupDaemonThreads> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
</profiles> | ||
</project> |
2 changes: 2 additions & 0 deletions
2
vision/product-search/cloud-client/resources/indexed_product_sets.csv
This file contains hidden or 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,2 @@ | ||
"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" | ||
"gs://java-docs-samples-testing/product-search/shoes_2.jpg","indexed_product_set_id_for_testing","indexed_product_id_for_testing_2","apparel",, | ||
2 changes: 2 additions & 0 deletions
2
vision/product-search/cloud-client/resources/product_sets.csv
This file contains hidden or 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,2 @@ | ||
"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" | ||
"gs://java-docs-samples-testing/product-search/shoes_2.jpg","fake_product_set_id_for_testing","fake_product_id_for_testing_2","apparel",, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double comma at the end of this line? |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 119 additions & 0 deletions
119
vision/product-search/cloud-client/src/main/java/com/example/vision/ImportProductSets.java
This file contains hidden or 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,119 @@ | ||
/* | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.vision; | ||
|
||
// [START product_search_import] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.vision.v1p3beta1.BatchOperationMetadata; | ||
import com.google.cloud.vision.v1p3beta1.ImportProductSetsGcsSource; | ||
import com.google.cloud.vision.v1p3beta1.ImportProductSetsGcsSource.Builder; | ||
import com.google.cloud.vision.v1p3beta1.ImportProductSetsInputConfig; | ||
import com.google.cloud.vision.v1p3beta1.ImportProductSetsResponse; | ||
import com.google.cloud.vision.v1p3beta1.LocationName; | ||
import com.google.cloud.vision.v1p3beta1.ProductSearchClient; | ||
import com.google.cloud.vision.v1p3beta1.ReferenceImage; | ||
|
||
import java.io.PrintStream; | ||
import javax.swing.JPanel; | ||
|
||
import net.sourceforge.argparse4j.ArgumentParsers; | ||
import net.sourceforge.argparse4j.inf.ArgumentParser; | ||
import net.sourceforge.argparse4j.inf.ArgumentParserException; | ||
import net.sourceforge.argparse4j.inf.Namespace; | ||
import net.sourceforge.argparse4j.inf.Subparser; | ||
import net.sourceforge.argparse4j.inf.Subparsers; | ||
// [END product_search_import] | ||
|
||
/** | ||
* This application demonstrates how to Import Product Sets in Cloud Vision | ||
* Product Search. | ||
* | ||
* For more information, see the tutorial page at | ||
* https://cloud.google.com/vision/product-search/docs/ | ||
*/ | ||
|
||
public class ImportProductSets extends JPanel { | ||
// [START product_search_import_product_sets] | ||
/** | ||
* Import images of different products in the product set. | ||
* | ||
* @param projectId - Id of the project. | ||
* @param computeRegion - Region name. | ||
* @param gcsUri - Google Cloud Storage URI.Target files must be in Product Search CSV format. | ||
* @throws Exception - on client errors. | ||
*/ | ||
public static void importProductSets(String projectId, String computeRegion, String gcsUri) | ||
throws Exception { | ||
ProductSearchClient client = ProductSearchClient.create(); | ||
|
||
// A resource that represents Google Cloud Platform location. | ||
LocationName projectLocation = LocationName.of(projectId, computeRegion); | ||
Builder gcsSource = ImportProductSetsGcsSource.newBuilder().setCsvFileUri(gcsUri); | ||
|
||
// Set the input configuration along with Google Cloud Storage URI | ||
ImportProductSetsInputConfig inputConfig = | ||
ImportProductSetsInputConfig.newBuilder().setGcsSource(gcsSource).build(); | ||
|
||
// Import the product sets from the input URI. | ||
OperationFuture<ImportProductSetsResponse, BatchOperationMetadata> response = | ||
client.importProductSetsAsync(projectLocation, inputConfig); | ||
|
||
System.out.println(String.format("Processing operation name: %s", response.getName())); | ||
ImportProductSetsResponse results = response.get(); | ||
System.out.println("Processing done."); | ||
System.out.println("Results of the processing:"); | ||
|
||
for (int i = 0; i < results.getStatusesCount(); i++) { | ||
System.out.println( | ||
String.format("Status of processing line %s of the csv: %s", i, results.getStatuses(i))); | ||
// Check the status of reference image. | ||
if (results.getStatuses(i).getCode() == 0) { | ||
ReferenceImage referenceImage = results.getReferenceImages(i); | ||
System.out.println(referenceImage); | ||
} else { | ||
System.out.println("No reference image."); | ||
} | ||
} | ||
} | ||
// [END product_search_import_product_sets] | ||
|
||
public static void main(String[] args) throws Exception { | ||
ImportProductSets importProductSet = new ImportProductSets(); | ||
importProductSet.argsHelper(args, System.out); | ||
} | ||
|
||
public static void argsHelper(String[] args, PrintStream out) throws Exception { | ||
ArgumentParser parser = ArgumentParsers.newFor("Import Product Sets").build(); | ||
Subparsers subparsers = parser.addSubparsers().dest("command"); | ||
|
||
Subparser importProductSetsParser = subparsers.addParser("import_product_sets"); | ||
importProductSetsParser.addArgument("gcsUri"); | ||
|
||
String projectId = System.getenv("PROJECT_ID"); | ||
String computeRegion = System.getenv("REGION_NAME"); | ||
|
||
Namespace ns = null; | ||
try { | ||
ns = parser.parseArgs(args); | ||
if (ns.get("command").equals("import_product_sets")) { | ||
importProductSets(projectId, computeRegion, ns.getString("gcsUri")); | ||
} | ||
} catch (ArgumentParserException e) { | ||
parser.handleError(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double comma at the end of this line?