Skip to content

Instantly share code, notes, and snippets.

View nkalra0123's full-sized avatar

Nitin Kalra nkalra0123

View GitHub Profile
@nkalra0123
nkalra0123 / FrontendController.java
Created January 11, 2020 16:24
Upload a file to google cloud storage
// Bucket ID is our Project ID
String bucket = "gs://" + projectIdProvider.getProjectId();
// Generate a random file name
filename = UUID.randomUUID().toString() + ".jpg";
WritableResource resource = (WritableResource)
context.getResource(bucket + "/" + filename);
// Write the file to Cloud Storage using WritableResource
try (OutputStream os = resource.getOutputStream()) {
@nkalra0123
nkalra0123 / FrontendController.java
Created January 11, 2020 16:28
Perform LABEL_DETECTION on the uploaded image
private AnnotateImageResponse analyzeImage(String uri) {
// After the image was written to GCS, analyze it with the GCS URI.
// Note: It's also possible to analyze an image embedded in the
// request as a Base64 encoded payload.
List<AnnotateImageRequest> requests = new ArrayList<>();
ImageSource imgSrc = ImageSource.newBuilder()
.setGcsImageUri(uri).build();
Image img = Image.newBuilder().setSource(imgSrc).build();
Feature feature = Feature.newBuilder()
.setType(Feature.Type.LABEL_DETECTION).build();
@nkalra0123
nkalra0123 / FrontendController.java
Created January 11, 2020 16:30
Collect the relevant label information in a map
// After written to GCS, analyze the image.
AnnotateImageResponse response = analyzeImage(bucket + "/" + filename);
Map<String, Float> imageLabels =
response.getLabelAnnotationsList()
.stream()
.collect(
Collectors.toMap(
EntityAnnotation::getDescription,
EntityAnnotation::getScore,
runtime: java11
instance_class: F1
@nkalra0123
nkalra0123 / FunctionalInterfaces.java
Created January 19, 2020 06:23
UnaryOperator Functional Interface
// UnaryOperator
Stream<Integer> iterate = Stream.iterate(1, new UnaryOperator<Integer>() {
@Override
public Integer apply(Integer integer) {
return integer + 1;
}
});
System.out.println("iterate.findFirst().get() = " + iterate.findFirst().get());
@nkalra0123
nkalra0123 / FunctionalInterfaces.java
Created January 19, 2020 06:24
BinaryOperator Functional Interfaces
// BinaryOperator
System.out.println("binaryOpeartor = " + Stream.of(1,2,3,4).reduce(new BinaryOperator<Integer>() {
@Override
public Integer apply(Integer x, Integer y) {
return x + y;
}
}).get());
System.out.println("binaryOpeartor = " + Stream.of(1,2,3,4).reduce((x, y) -> x + y).get());
@nkalra0123
nkalra0123 / FunctionalInterfaces.java
Created January 19, 2020 06:25
Predicate Functional Interfaces
// Predicate
//Evaluates this predicate on the given argument
System.out.println("Predicate = " + Stream.of(1,2,3,4).filter(new Predicate<Integer>() {
@Override
public boolean test(Integer x) {
return x % 2 == 0;
}
}).findFirst().get());
@nkalra0123
nkalra0123 / FunctionalInterfaces.java
Created January 19, 2020 06:26
Function, Consumer Functional Interfaces
// Function, Consumer
Stream.of(strings).filter(s -> s.matches("\\d+")).map(new Function<String, Integer>() {
/**
* Applies this function to the given argument.
*
* @param s the function argument
* @return the function result
*/
@Override
@nkalra0123
nkalra0123 / FunctionalInterfaces.java
Created January 19, 2020 06:27
Supplier Functional Interfaces
//Supplier
Optional<Integer> value = Stream.of(1,2,3,4).reduce(new BinaryOperator<Integer>() {
@Override
public Integer apply(Integer x, Integer y) {
return x + y;
}
});
value.orElseGet(new Supplier<Integer>() {
@nkalra0123
nkalra0123 / FunctionalInterfaces.java
Created January 19, 2020 06:27
Functional Interfaces, Lamda and Method References
import java.util.Optional;
import java.util.function.*;
import java.util.stream.Stream;
public class FunctionalInterfaces {
@FunctionalInterface
public interface foo
{
abstract public void food();