Étape 1 du tutoriel sur la reconnaissance optique des caractères (OCR) : Traiter l'image
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Découvrez comment importer des fichiers image dans Cloud Storage, puis extraire et traduire le texte des images à l'aide de l'API Vision et de l'API Translation.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les articles suivants :
packageocrimport("context""fmt""log")// ProcessImage is executed when a file is uploaded to the Cloud Storage bucket you// created for uploading images. It runs detectText, which processes the image for text.funcProcessImage(ctxcontext.Context,eventGCSEvent)error{iferr:=setup(ctx);err!=nil{returnfmt.Errorf("ProcessImage: %v",err)}ifevent.Bucket==""{returnfmt.Errorf("empty file.Bucket")}ifevent.Name==""{returnfmt.Errorf("empty file.Name")}iferr:=detectText(ctx,event.Bucket,event.Name);err!=nil{returnfmt.Errorf("detectText: %v",err)}log.Printf("File %s processed.",event.Name)returnnil}
/** * This function is exported by index.js, and is executed when * a file is uploaded to the Cloud Storage bucket you created * for uploading images. * * @param {object} event A Google Cloud Storage File object. */exports.processImage=asyncevent=>{const{bucket,name}=event;if(!bucket){thrownewError('Bucket not provided. Make sure you have a "bucket" property in your request');}if(!name){thrownewError('Filename not provided. Make sure you have a "name" property in your request');}awaitdetectText(bucket,name);console.log(`File ${name} processed.`);};
defprocess_image(file,context):"""Cloud Function triggered by Cloud Storage when a file is changed. Args: file (dict): Metadata of the changed file, provided by the triggering Cloud Storage event. context (google.cloud.functions.Context): Metadata of triggering event. Returns: None; the output is written to stdout and Stackdriver Logging """bucket=validate_message(file,"bucket")name=validate_message(file,"name")detect_text(bucket,name)print("File {} processed.".format(file["name"]))
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],[],[],[]]