Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. namespace GoogleCloudSamplesVision;
  2.  
  3. use GoogleCloudVisionV1ImageAnnotatorClient;
  4.  
  5. // $path = 'gs://path/to/your/image.jpg'
  6.  
  7. function detect_document_text_gcs($path)
  8. {
  9. $imageAnnotator = new ImageAnnotatorClient();
  10.  
  11. # annotate the image
  12. $response = $imageAnnotator->documentTextDetection($path);
  13. $annotation = $response->getFullTextAnnotation();
  14.  
  15. # print out detailed and structured information about document text
  16. if ($annotation) {
  17. foreach ($annotation->getPages() as $page) {
  18. foreach ($page->getBlocks() as $block) {
  19. $block_text = '';
  20. foreach ($block->getParagraphs() as $paragraph) {
  21. foreach ($paragraph->getWords() as $word) {
  22. foreach ($word->getSymbols() as $symbol) {
  23. $block_text .= $symbol->getText();
  24. }
  25. $block_text .= ' ';
  26. }
  27. $block_text .= "n";
  28. }
  29. printf('Block content: %s', $block_text);
  30. printf('Block confidence: %f' . PHP_EOL,
  31. $block->getConfidence());
  32.  
  33. # get bounds
  34. $vertices = $block->getBoundingBox()->getVertices();
  35. $bounds = [];
  36. foreach ($vertices as $vertex) {
  37. $bounds[] = sprintf('(%d,%d)', $vertex->getX(),
  38. $vertex->getY());
  39. }
  40. print('Bounds: ' . join(', ',$bounds) . PHP_EOL);
  41.  
  42. print(PHP_EOL);
  43. }
  44. }
  45. } else {
  46. print('No text found' . PHP_EOL);
  47. }
  48.  
  49. $imageAnnotator->close();
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement