Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace GoogleCloudSamplesVision;
- use GoogleCloudVisionV1ImageAnnotatorClient;
- // $path = 'gs://path/to/your/image.jpg'
- function detect_document_text_gcs($path)
- {
- $imageAnnotator = new ImageAnnotatorClient();
- # annotate the image
- $response = $imageAnnotator->documentTextDetection($path);
- $annotation = $response->getFullTextAnnotation();
- # print out detailed and structured information about document text
- if ($annotation) {
- foreach ($annotation->getPages() as $page) {
- foreach ($page->getBlocks() as $block) {
- $block_text = '';
- foreach ($block->getParagraphs() as $paragraph) {
- foreach ($paragraph->getWords() as $word) {
- foreach ($word->getSymbols() as $symbol) {
- $block_text .= $symbol->getText();
- }
- $block_text .= ' ';
- }
- $block_text .= "n";
- }
- printf('Block content: %s', $block_text);
- printf('Block confidence: %f' . PHP_EOL,
- $block->getConfidence());
- # get bounds
- $vertices = $block->getBoundingBox()->getVertices();
- $bounds = [];
- foreach ($vertices as $vertex) {
- $bounds[] = sprintf('(%d,%d)', $vertex->getX(),
- $vertex->getY());
- }
- print('Bounds: ' . join(', ',$bounds) . PHP_EOL);
- print(PHP_EOL);
- }
- }
- } else {
- print('No text found' . PHP_EOL);
- }
- $imageAnnotator->close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement