|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "Import libraries." |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": null, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "import os\n", |
| 17 | + "from azure.ai.vision.imageanalysis import ImageAnalysisClient\n", |
| 18 | + "from azure.ai.vision.imageanalysis.models import VisualFeatures\n", |
| 19 | + "from azure.core.credentials import AzureKeyCredential\n", |
| 20 | + "from pdf2image import convert_from_path" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "markdown", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "Helper function for turning pdfs to jpgs." |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": null, |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "def pdf_to_jpg(pdf_path, output_path=\"converted_image.jpg\"):\n", |
| 37 | + " \"\"\"\n", |
| 38 | + " Convert the first page of a PDF to a JPG image.\n", |
| 39 | + "\n", |
| 40 | + " Args:\n", |
| 41 | + " pdf_path (str): Path to the input PDF file.\n", |
| 42 | + " output_path (str): Path to save the converted JPG image.\n", |
| 43 | + "\n", |
| 44 | + " Returns:\n", |
| 45 | + " str: Path to the saved JPG image.\n", |
| 46 | + " \"\"\"\n", |
| 47 | + " # Convert the first page of the PDF to an image\n", |
| 48 | + " images = convert_from_path(pdf_path, first_page=1, last_page=1)\n", |
| 49 | + " if images:\n", |
| 50 | + " images[0].save(output_path, format=\"JPEG\")\n", |
| 51 | + " return output_path\n", |
| 52 | + " else:\n", |
| 53 | + " raise ValueError(\"No pages found in the PDF file.\")" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "markdown", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "Set up the values for the endpoint and api key." |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": null, |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "try:\n", |
| 70 | + " endpoint = \"\"\n", |
| 71 | + " key = \"\"\n", |
| 72 | + "except KeyError:\n", |
| 73 | + " print(\"Missing environment variable 'VISION_ENDPOINT' or 'VISION_KEY'\")\n", |
| 74 | + " print(\"Set them before running this sample.\")" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "markdown", |
| 79 | + "metadata": {}, |
| 80 | + "source": [ |
| 81 | + "Set up an Image Analysis client." |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "code", |
| 86 | + "execution_count": null, |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [], |
| 89 | + "source": [ |
| 90 | + "client = ImageAnalysisClient(\n", |
| 91 | + " endpoint=endpoint,\n", |
| 92 | + " credential=AzureKeyCredential(key)\n", |
| 93 | + ")" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "markdown", |
| 98 | + "metadata": {}, |
| 99 | + "source": [ |
| 100 | + "Initiate the image." |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": null, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "#image_path = \"200.jpg\"\n", |
| 110 | + "\n", |
| 111 | + "image_path = pdf_to_jpg(\"200_tegnforklaring.pdf\")\n", |
| 112 | + "\n", |
| 113 | + "with open(image_path, \"rb\") as f:\n", |
| 114 | + " image_data = f.read()" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "markdown", |
| 119 | + "metadata": {}, |
| 120 | + "source": [ |
| 121 | + "Define the visual features you want to analyze." |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "code", |
| 126 | + "execution_count": null, |
| 127 | + "metadata": {}, |
| 128 | + "outputs": [], |
| 129 | + "source": [ |
| 130 | + "visual_features =[\n", |
| 131 | + " VisualFeatures.TAGS,\n", |
| 132 | + " VisualFeatures.OBJECTS,\n", |
| 133 | + " VisualFeatures.CAPTION,\n", |
| 134 | + " VisualFeatures.DENSE_CAPTIONS,\n", |
| 135 | + " VisualFeatures.READ,\n", |
| 136 | + " VisualFeatures.SMART_CROPS,\n", |
| 137 | + " VisualFeatures.PEOPLE,\n", |
| 138 | + "]" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "markdown", |
| 143 | + "metadata": {}, |
| 144 | + "source": [ |
| 145 | + "Analyze the image using the analyze method." |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": null, |
| 151 | + "metadata": {}, |
| 152 | + "outputs": [], |
| 153 | + "source": [ |
| 154 | + "result = client.analyze(\n", |
| 155 | + " image_data=image_data,\n", |
| 156 | + " visual_features=visual_features,\n", |
| 157 | + " gender_neutral_caption=True, \n", |
| 158 | + " language=\"en\"\n", |
| 159 | + ")" |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "markdown", |
| 164 | + "metadata": {}, |
| 165 | + "source": [ |
| 166 | + "Print all the analysis results." |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": null, |
| 172 | + "metadata": {}, |
| 173 | + "outputs": [], |
| 174 | + "source": [ |
| 175 | + "print(\"Image analysis results:\")\n", |
| 176 | + "\n", |
| 177 | + "if result.caption is not None:\n", |
| 178 | + " print(\" Caption:\")\n", |
| 179 | + " print(f\" '{result.caption.text}', Confidence {result.caption.confidence:.4f}\")\n", |
| 180 | + "\n", |
| 181 | + "if result.dense_captions is not None:\n", |
| 182 | + " print(\" Dense Captions:\")\n", |
| 183 | + " for caption in result.dense_captions.list:\n", |
| 184 | + " print(f\" '{caption.text}', {caption.bounding_box}, Confidence: {caption.confidence:.4f}\")\n", |
| 185 | + "\n", |
| 186 | + "if result.read is not None:\n", |
| 187 | + " print(\" Read:\")\n", |
| 188 | + " for line in result.read.blocks[0].lines:\n", |
| 189 | + " print(f\" Line: '{line.text}', Bounding box {line.bounding_polygon}\")\n", |
| 190 | + " for word in line.words:\n", |
| 191 | + " print(f\" Word: '{word.text}', Bounding polygon {word.bounding_polygon}, Confidence {word.confidence:.4f}\")\n", |
| 192 | + "\n", |
| 193 | + "if result.tags is not None:\n", |
| 194 | + " print(\" Tags:\")\n", |
| 195 | + " for tag in result.tags.list:\n", |
| 196 | + " print(f\" '{tag.name}', Confidence {tag.confidence:.4f}\")\n", |
| 197 | + "\n", |
| 198 | + "if result.objects is not None:\n", |
| 199 | + " print(\" Objects:\")\n", |
| 200 | + " for object in result.objects.list:\n", |
| 201 | + " print(f\" '{object.tags[0].name}', {object.bounding_box}, Confidence: {object.tags[0].confidence:.4f}\")\n", |
| 202 | + "\n", |
| 203 | + "if result.people is not None:\n", |
| 204 | + " print(\" People:\")\n", |
| 205 | + " for person in result.people.list:\n", |
| 206 | + " print(f\" {person.bounding_box}, Confidence {person.confidence:.4f}\")\n", |
| 207 | + "\n", |
| 208 | + "if result.smart_crops is not None:\n", |
| 209 | + " print(\" Smart Cropping:\")\n", |
| 210 | + " for smart_crop in result.smart_crops.list:\n", |
| 211 | + " print(f\" Aspect ratio {smart_crop.aspect_ratio}: Smart crop {smart_crop.bounding_box}\")\n", |
| 212 | + "\n", |
| 213 | + "print(f\" Image height: {result.metadata.height}\")\n", |
| 214 | + "print(f\" Image width: {result.metadata.width}\")\n", |
| 215 | + "print(f\" Model version: {result.model_version}\")" |
| 216 | + ] |
| 217 | + } |
| 218 | + ], |
| 219 | + "metadata": { |
| 220 | + "kernelspec": { |
| 221 | + "display_name": "Python 3", |
| 222 | + "language": "python", |
| 223 | + "name": "python3" |
| 224 | + }, |
| 225 | + "language_info": { |
| 226 | + "name": "python", |
| 227 | + "version": "3.13.0" |
| 228 | + } |
| 229 | + }, |
| 230 | + "nbformat": 4, |
| 231 | + "nbformat_minor": 2 |
| 232 | +} |
0 commit comments