Search in sources :

Example 16 with Feature

use of com.google.cloud.vision.v1p4beta1.Feature in project java-docs-samples by GoogleCloudPlatform.

the class ImageMagick method accept.

// [END functions_imagemagick_setup]
// [START functions_imagemagick_analyze]
@Override
public // Blurs uploaded images that are flagged as Adult or Violence.
void accept(GcsEvent event, Context context) {
    // Validate parameters
    if (event.getBucket() == null || event.getName() == null) {
        logger.severe("Error: Malformed GCS event.");
        return;
    }
    BlobInfo blobInfo = BlobInfo.newBuilder(event.getBucket(), event.getName()).build();
    // Construct URI to GCS bucket and file.
    String gcsPath = String.format("gs://%s/%s", event.getBucket(), event.getName());
    logger.info(String.format("Analyzing %s", event.getName()));
    // Construct request.
    ImageSource imgSource = ImageSource.newBuilder().setImageUri(gcsPath).build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feature = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feature).setImage(img).build();
    List<AnnotateImageRequest> requests = List.of(request);
    // Send request to the Vision API.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                logger.info(String.format("Error: %s", res.getError().getMessage()));
                return;
            }
            // Get Safe Search Annotations
            SafeSearchAnnotation annotation = res.getSafeSearchAnnotation();
            if (annotation.getAdultValue() == 5 || annotation.getViolenceValue() == 5) {
                logger.info(String.format("Detected %s as inappropriate.", event.getName()));
                blur(blobInfo);
            } else {
                logger.info(String.format("Detected %s as OK.", event.getName()));
            }
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Error with Vision API: " + e.getMessage(), e);
    }
}
Also used : SafeSearchAnnotation(com.google.cloud.vision.v1.SafeSearchAnnotation) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) BlobInfo(com.google.cloud.storage.BlobInfo) IOException(java.io.IOException) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ImageSource(com.google.cloud.vision.v1.ImageSource) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 17 with Feature

use of com.google.cloud.vision.v1p4beta1.Feature in project AGREE by loonwerks.

the class AgreeASTBuilder method portToAgreeVar.

private void portToAgreeVar(List<AgreeVar> outputs, List<AgreeVar> inputs, FeatureInstance feature, List<AgreeStatement> assumptions, List<AgreeStatement> guarantees) {
    Feature dataFeature = feature.getFeature();
    NamedElement dataClass;
    if (dataFeature instanceof DataPort) {
        DataPort dataPort = (DataPort) dataFeature;
        dataClass = dataPort.getDataFeatureClassifier();
    } else if (dataFeature instanceof EventDataPort) {
        EventDataPort eventDataPort = (EventDataPort) dataFeature;
        dataClass = eventDataPort.getDataFeatureClassifier();
    } else {
        dataClass = null;
    }
    String name = feature.getName();
    boolean isEvent = feature.getCategory() == FeatureCategory.EVENT_DATA_PORT || feature.getCategory() == FeatureCategory.EVENT_PORT;
    if (isEvent) {
        AgreeVar var = new AgreeVar(name + eventSuffix, NamedType.BOOL, feature.getFeature(), feature.getComponentInstance(), feature);
        switch(feature.getDirection()) {
            case IN:
                inputs.add(var);
                break;
            case OUT:
                outputs.add(var);
                break;
            default:
                throw new AgreeException("Unable to reason about bi-directional event port: " + dataFeature.getQualifiedName());
        }
    }
    if (dataClass == null) {
        // we do not reason about this type
        return;
    }
    AgreeTypeSystem.TypeDef td = AgreeTypeSystem.inferFromNamedElement(dataFeature);
    Type type = symbolTable.updateLustreTypeMap(td);
    if (type == null) {
        // we do not reason about this type
        return;
    }
    AgreeVar agreeVar = new AgreeVar(name, type, feature.getFeature(), feature.getComponentInstance(), feature);
    switch(feature.getDirection()) {
        case IN:
            inputs.add(agreeVar);
            if (dataClass instanceof DataClassifier) {
                List<Expr> constraints = getConstraintsFromTypeDef(name, td);
                if (!constraints.isEmpty()) {
                    assumptions.add(getDataClassifierTypePredicate(feature.getName(), constraints, dataFeature));
                }
            }
            break;
        case OUT:
            outputs.add(agreeVar);
            if (dataClass instanceof DataClassifier) {
                List<Expr> constraints = getConstraintsFromTypeDef(name, td);
                if (!constraints.isEmpty()) {
                    guarantees.add(getDataClassifierTypePredicate(feature.getName(), constraints, dataFeature));
                }
            }
            break;
        default:
            throw new AgreeException("Unable to reason about bi-directional event port: " + dataFeature.getQualifiedName());
    }
}
Also used : DataClassifier(org.osate.aadl2.DataClassifier) Feature(org.osate.aadl2.Feature) DataPort(org.osate.aadl2.DataPort) EventDataPort(org.osate.aadl2.EventDataPort) AgreeTypeSystem(com.rockwellcollins.atc.agree.AgreeTypeSystem) ConnectionType(com.rockwellcollins.atc.agree.analysis.ast.AgreeAADLConnection.ConnectionType) Type(jkind.lustre.Type) NamedType(jkind.lustre.NamedType) FeatureGroupType(org.osate.aadl2.FeatureGroupType) DataSubcomponentType(org.osate.aadl2.DataSubcomponentType) ComponentType(org.osate.aadl2.ComponentType) EnumLitExpr(com.rockwellcollins.atc.agree.agree.EnumLitExpr) IndicesExpr(com.rockwellcollins.atc.agree.agree.IndicesExpr) TimeRiseExpr(com.rockwellcollins.atc.agree.agree.TimeRiseExpr) RecordAccessExpr(jkind.lustre.RecordAccessExpr) FlatmapExpr(com.rockwellcollins.atc.agree.agree.FlatmapExpr) TimeFallExpr(com.rockwellcollins.atc.agree.agree.TimeFallExpr) RealLitExpr(com.rockwellcollins.atc.agree.agree.RealLitExpr) GetPropertyExpr(com.rockwellcollins.atc.agree.agree.GetPropertyExpr) Expr(jkind.lustre.Expr) CastExpr(jkind.lustre.CastExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) TimeOfExpr(com.rockwellcollins.atc.agree.agree.TimeOfExpr) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) RealExpr(jkind.lustre.RealExpr) ArrayExpr(jkind.lustre.ArrayExpr) PrevExpr(com.rockwellcollins.atc.agree.agree.PrevExpr) IdExpr(jkind.lustre.IdExpr) TimeExpr(com.rockwellcollins.atc.agree.agree.TimeExpr) FoldRightExpr(com.rockwellcollins.atc.agree.agree.FoldRightExpr) TagExpr(com.rockwellcollins.atc.agree.agree.TagExpr) EventExpr(com.rockwellcollins.atc.agree.agree.EventExpr) LatchedExpr(com.rockwellcollins.atc.agree.agree.LatchedExpr) NamedElmExpr(com.rockwellcollins.atc.agree.agree.NamedElmExpr) FunctionCallExpr(jkind.lustre.FunctionCallExpr) SelectionExpr(com.rockwellcollins.atc.agree.agree.SelectionExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) TupleExpr(jkind.lustre.TupleExpr) UnaryExpr(jkind.lustre.UnaryExpr) ArraySubExpr(com.rockwellcollins.atc.agree.agree.ArraySubExpr) IntExpr(jkind.lustre.IntExpr) PreExpr(com.rockwellcollins.atc.agree.agree.PreExpr) RecordLitExpr(com.rockwellcollins.atc.agree.agree.RecordLitExpr) ExistsExpr(com.rockwellcollins.atc.agree.agree.ExistsExpr) FoldLeftExpr(com.rockwellcollins.atc.agree.agree.FoldLeftExpr) RecordUpdateExpr(com.rockwellcollins.atc.agree.agree.RecordUpdateExpr) ForallExpr(com.rockwellcollins.atc.agree.agree.ForallExpr) ArrayAccessExpr(jkind.lustre.ArrayAccessExpr) ArrayUpdateExpr(com.rockwellcollins.atc.agree.agree.ArrayUpdateExpr) BoolLitExpr(com.rockwellcollins.atc.agree.agree.BoolLitExpr) NodeBodyExpr(com.rockwellcollins.atc.agree.agree.NodeBodyExpr) IntLitExpr(com.rockwellcollins.atc.agree.agree.IntLitExpr) CallExpr(com.rockwellcollins.atc.agree.agree.CallExpr) ArrayLiteralExpr(com.rockwellcollins.atc.agree.agree.ArrayLiteralExpr) AgreeException(com.rockwellcollins.atc.agree.analysis.AgreeException) EventDataPort(org.osate.aadl2.EventDataPort) NamedElement(org.osate.aadl2.NamedElement)

Example 18 with Feature

use of com.google.cloud.vision.v1p4beta1.Feature in project AGREE by loonwerks.

the class AgreeValidator method checkNamedElement.

@Check(CheckType.FAST)
public void checkNamedElement(NamedElement namedEl) {
    // check for namespace collision in component types of component
    // implementations
    // and for collisions between subcomponent and feature names
    EObject container = namedEl.eContainer();
    if (container == null) {
        return;
    }
    if (container instanceof RecordDef || container instanceof NodeDef) {
        // TODO: perhaps we can ignore all arguments?
        return;
    }
    while (!(container instanceof AadlPackage || container instanceof ComponentImplementation || container instanceof ComponentType)) {
        container = container.eContainer();
    }
    ComponentImplementation compImpl = null;
    ComponentType type = null;
    if (container instanceof ComponentImplementation) {
        compImpl = (ComponentImplementation) container;
        type = compImpl.getType();
        checkDupNames(namedEl, type, compImpl);
    } else if (container instanceof ComponentType) {
        type = (ComponentType) container;
    }
    if (type != null && (namedEl.getName() != null)) {
        for (Feature feat : type.getAllFeatures()) {
            if (namedEl.getName().equals(feat.getName())) {
                error(feat, "Element of the same name ('" + namedEl.getName() + "') in AGREE Annex in '" + (compImpl == null ? type.getName() : compImpl.getName()) + "'");
                error(namedEl, "Feature of the same name ('" + namedEl.getName() + "') in component type");
            }
        }
    }
// check name space collision with enumerated types
}
Also used : ComponentImplementation(org.osate.aadl2.ComponentImplementation) ComponentType(org.osate.aadl2.ComponentType) NodeDef(com.rockwellcollins.atc.agree.agree.NodeDef) AadlPackage(org.osate.aadl2.AadlPackage) EObject(org.eclipse.emf.ecore.EObject) Feature(org.osate.aadl2.Feature) RecordDef(com.rockwellcollins.atc.agree.agree.RecordDef) Check(org.eclipse.xtext.validation.Check)

Example 19 with Feature

use of com.google.cloud.vision.v1p4beta1.Feature in project AGREE by loonwerks.

the class AgreeValidator method checkLiftContract.

@Check(CheckType.FAST)
public void checkLiftContract(LiftContractStatement lcst) {
    Classifier comp = lcst.getContainingClassifier();
    if (comp instanceof ComponentImplementation) {
        ComponentType ct = ((ComponentImplementation) comp).getType();
        List<AnnexSubclause> agreeAnnexes = AnnexUtil.getAllAnnexSubclauses(ct, AgreePackage.eINSTANCE.getAgreeContractSubclause());
        if (agreeAnnexes.size() > 0) {
            error(lcst, "'lift contract;' statement is not allowed in component implementation whose type has an AGREE annex.");
        }
        List<Subcomponent> subcomps = ((ComponentImplementation) comp).getAllSubcomponents();
        if (subcomps.size() == 1) {
            Subcomponent subcomp = subcomps.get(0);
            Classifier subCls = subcomp.getClassifier();
            ComponentType subCt = null;
            if (subCls instanceof ComponentImplementation) {
                subCt = ((ComponentImplementation) subCls).getType();
            } else if (subCls instanceof ComponentType) {
                subCt = (ComponentType) subCls;
            } else {
                throw new RuntimeException();
            }
            {
                Set<String> usedParentInPorts = new HashSet<>();
                Set<String> usedParentOutPorts = new HashSet<>();
                Set<String> usedChildInPorts = new HashSet<>();
                Set<String> usedChildOutPorts = new HashSet<>();
                EList<Classifier> ctPlusAllExtended = ct.getSelfPlusAllExtended();
                EList<Classifier> subCtPlusAllExtended = subCt.getSelfPlusAllExtended();
                for (Connection conn : ((ComponentImplementation) comp).getAllConnections()) {
                    {
                        NamedElement sourceNe = conn.getSource().getConnectionEnd();
                        if (subCtPlusAllExtended.contains(sourceNe.getContainingClassifier())) {
                            if (usedChildOutPorts.contains(sourceNe.getName())) {
                                error(lcst, "'lift contract;' statement is not allowed in component implementation whith more than one connection out of same output " + sourceNe.getQualifiedName() + ".");
                            }
                            usedChildOutPorts.add(sourceNe.getName());
                        }
                        if (ctPlusAllExtended.contains(sourceNe.getContainingClassifier())) {
                            if (usedParentInPorts.contains(sourceNe.getName())) {
                                error(lcst, "'lift contract;' statement is not allowed in component implementation whith more than one connection out of same input " + sourceNe.getQualifiedName() + ".");
                            }
                            usedParentInPorts.add(sourceNe.getName());
                        }
                    }
                    {
                        NamedElement destNe = conn.getDestination().getConnectionEnd();
                        if (subCtPlusAllExtended.contains(destNe.getContainingClassifier())) {
                            if (usedChildInPorts.contains(destNe.getName())) {
                                error(lcst, "'lift contract;' statement is not allowed in component implementation whith more than one connection into same input " + destNe.getQualifiedName() + ".");
                            }
                            usedChildInPorts.add(destNe.getName());
                        }
                        if (ctPlusAllExtended.contains(destNe.getContainingClassifier())) {
                            if (usedParentOutPorts.contains(destNe.getName())) {
                                error(lcst, "'lift contract;' statement is not allowed in component implementation whith more than one connection into same output " + destNe.getQualifiedName() + ".");
                            }
                            usedParentOutPorts.add(destNe.getName());
                        }
                    }
                }
                for (Feature feat : comp.getAllFeatures()) {
                    boolean isIn = false;
                    if (feat instanceof DataPort) {
                        isIn = ((DataPort) feat).isIn();
                    } else if (feat instanceof EventDataPort) {
                        isIn = ((EventDataPort) feat).isIn();
                    } else if (feat instanceof EventPort) {
                        isIn = ((EventPort) feat).isIn();
                    }
                    if (isIn) {
                        if (!usedParentInPorts.contains(feat.getName())) {
                            error(lcst, "'lift contract;' statement is not allowed in component implementation whithout connection from input " + feat.getQualifiedName() + ".");
                        }
                    } else {
                        if (!usedParentOutPorts.contains(feat.getName())) {
                            error(lcst, "'lift contract;' statement is not allowed in component implementation whithout connection to output " + feat.getQualifiedName() + ".");
                        }
                    }
                }
                for (Feature feat : subCt.getAllFeatures()) {
                    boolean isIn = false;
                    if (feat instanceof DataPort) {
                        isIn = ((DataPort) feat).isIn();
                    } else if (feat instanceof EventDataPort) {
                        isIn = ((EventDataPort) feat).isIn();
                    } else if (feat instanceof EventPort) {
                        isIn = ((EventPort) feat).isIn();
                    }
                    if (isIn) {
                        if (!usedChildInPorts.contains(feat.getName())) {
                            error(lcst, "'lift contract;' statement is not allowed in component implementation whithout connection into " + feat.getQualifiedName() + ".");
                        }
                    } else {
                        if (!usedChildOutPorts.contains(feat.getName())) {
                            error(lcst, "'lift contract;' statement is not allowed in component implementation whithout connection out of " + feat.getQualifiedName() + ".");
                        }
                    }
                }
            }
        } else {
            error(lcst, "'lift contract;' statement is not allowed in component implementation whithout exactly one subcomponent.");
        }
    } else {
        error(lcst, "'lift contract;' statement is not allowed in component interface.");
    }
}
Also used : ComponentImplementation(org.osate.aadl2.ComponentImplementation) ComponentType(org.osate.aadl2.ComponentType) Set(java.util.Set) HashSet(java.util.HashSet) Connection(org.osate.aadl2.Connection) ComponentClassifier(org.osate.aadl2.ComponentClassifier) Classifier(org.osate.aadl2.Classifier) Feature(org.osate.aadl2.Feature) DataPort(org.osate.aadl2.DataPort) EventDataPort(org.osate.aadl2.EventDataPort) EList(org.eclipse.emf.common.util.EList) EventPort(org.osate.aadl2.EventPort) Subcomponent(org.osate.aadl2.Subcomponent) DataSubcomponent(org.osate.aadl2.DataSubcomponent) EventDataPort(org.osate.aadl2.EventDataPort) NamedElement(org.osate.aadl2.NamedElement) AnnexSubclause(org.osate.aadl2.AnnexSubclause) Check(org.eclipse.xtext.validation.Check)

Example 20 with Feature

use of com.google.cloud.vision.v1p4beta1.Feature in project java-vision by googleapis.

the class QuickstartSample method main.

public static void main(String... args) throws Exception {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {
        // The path to the image file to annotate
        String fileName = "./resources/wakeupcat.jpg";
        // Reads the image file into memory
        Path path = Paths.get(fileName);
        byte[] data = Files.readAllBytes(path);
        ByteString imgBytes = ByteString.copyFrom(data);
        // 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.format("Error: %s%n", res.getError().getMessage());
                return;
            }
            for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
                annotation.getAllFields().forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString()));
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Aggregations

Feature (org.osate.aadl2.Feature)82 ArrayList (java.util.ArrayList)78 Feature (com.google.cloud.vision.v1.Feature)72 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)69 Image (com.google.cloud.vision.v1.Image)69 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)66 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)66 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)63 ByteString (com.google.protobuf.ByteString)47 ImageSource (com.google.cloud.vision.v1.ImageSource)38 FileInputStream (java.io.FileInputStream)30 Subcomponent (org.osate.aadl2.Subcomponent)29 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)27 Classifier (org.osate.aadl2.Classifier)27 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)26 ComponentClassifier (org.osate.aadl2.ComponentClassifier)22 NamedElement (org.osate.aadl2.NamedElement)22 FeatureGroup (org.osate.aadl2.FeatureGroup)18 FeatureInstance (org.osate.aadl2.instance.FeatureInstance)17 FeatureGroupType (org.osate.aadl2.FeatureGroupType)16