Skip to content

Commit

Permalink
Updated vision sdk (#9)
Browse files Browse the repository at this point in the history
* updated Vision versions

* fix filename in background

* remove finished content

* remove content

* remove unnecessary content

* format fixes

* update OCR
  • Loading branch information
ivorb authored Feb 13, 2024
1 parent d0dd9f7 commit ba3f851
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 538 deletions.
348 changes: 141 additions & 207 deletions Instructions/Exercises/01-analyze-images.md

Large diffs are not rendered by default.

534 changes: 244 additions & 290 deletions Instructions/Exercises/05-ocr.md

Large diffs are not rendered by default.

34 changes: 20 additions & 14 deletions Labfiles/01-analyze-images/C-Sharp/image-analysis/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Drawing;
using Microsoft.Extensions.Configuration;
using Azure;
using System.IO;

// Import namespaces


namespace image_analysis
{
class Program
{

static void Main(string[] args)
static async Task Main(string[] args)
{
try
{
Expand All @@ -33,10 +37,10 @@ static void Main(string[] args)


// Analyze image
AnalyzeImage(imageFile, cvClient);
AnalyzeImage(imageFile, client);

// Remove the background or generate a foreground matte from the image
BackgroundForeground(imageFile, cvClient);
await BackgroundForeground(imageFile, aiSvcEndpoint, aiSvcKey);

}
catch (Exception ex)
Expand All @@ -45,23 +49,25 @@ static void Main(string[] args)
}
}

static void AnalyzeImage(string imageFile, VisionServiceOptions serviceOptions)
static void AnalyzeImage(string imageFile, ImageAnalysisClient client)
{
Console.WriteLine($"\nAnalyzing {imageFile} \n");

var analysisOptions = new ImageAnalysisOptions()
{
// Specify features to be retrieved

};
// Use a file stream to pass the image data to the analyze call
using FileStream stream = new FileStream(imageFile,
FileMode.Open);

// Get image analysis
// Get result with specified features to be retrieved


// Display analysis results


}
static void BackgroundForeground(string imageFile, VisionServiceOptions serviceOptions)
static async Task BackgroundForeground(string imageFile, string endpoint, string key)
{
// Remove the background from the image or generate a foreground matte

}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"AIServicesEndpoint": "YOUR_AZURE_AI_SERVICES_SERVICES_ENDPOINT",
"AIServicesEndpoint": "YOUR_AZURE_AI_SERVICES_SERVICES_ENDPOINT",
"AIServicesKey": "YOUR_AZURE_AI_SERVICES_KEY"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp7.0</TargetFramework>
<RootNamespace>image_analysis</RootNamespace>
<NoWarn>1998</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.Vision.ImageAnalysis" Version="1.0.0-beta.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
<PackageReference Include="System.Drawing.Common" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
42 changes: 25 additions & 17 deletions Labfiles/01-analyze-images/Python/image-analysis/image-analysis.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from dotenv import load_dotenv
import os
from array import array
from PIL import Image, ImageDraw
import sys
import time
from matplotlib import pyplot as plt
import numpy as np
from azure.core.exceptions import HttpResponseError
import requests

# Import namespaces



def main():
global cv_client

Expand All @@ -25,34 +23,44 @@ def main():
if len(sys.argv) > 1:
image_file = sys.argv[1]

with open(image_file, "rb") as f:
image_data = f.read()

# Authenticate Azure AI Vision client


# Analyze image
AnalyzeImage(image_file, cv_client)

# Generate thumbnail
BackgroundForeground(image_file, cv_client)
AnalyzeImage(image_file, image_data, cv_client)
# Background removal
BackgroundForeground(ai_endpoint, ai_key, image_file)

except Exception as ex:
print(ex)


def AnalyzeImage(image_file, cv_client):
print('\nAnalyzing', image_file)

# Specify features to be retrieved

def AnalyzeImage(image_filename, image_data, cv_client):
print('\nAnalyzing image...')

# Get image analysis
try:
# Get result with specified features to be retrieved


except HttpResponseError as e:
print(f"Status code: {e.status_code}")
print(f"Reason: {e.reason}")
print(f"Message: {e.error.message}")

# Display analysis results


def BackgroundForeground(image_file, cv_client):
print('\n')
def BackgroundForeground(endpoint, key, image_file):
# Define the API version and mode
api_version = "2023-02-01-preview"
mode="backgroundRemoval" # Can be "foregroundMatting" or "backgroundRemoval"

# Remove the background from the image or generate a foreground matte



if __name__ == "__main__":
Expand Down
16 changes: 11 additions & 5 deletions Labfiles/05-ocr/C-Sharp/read-text/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ static void Main(string[] args)
string aiSvcKey = configuration["AIServicesKey"];

// Authenticate Azure AI Vision client



// Menu for text reading functions
Expand All @@ -40,11 +39,11 @@ static void Main(string[] args)
{
case "1":
imageFile = "images/Lincoln.jpg";
GetTextRead(imageFile, cvClient);
GetTextRead(imageFile, client);
break;
case "2":
imageFile = "images/Note.jpg";
GetTextRead(imageFile, cvClient);
GetTextRead(imageFile, client);
break;
default:
break;
Expand All @@ -57,11 +56,18 @@ static void Main(string[] args)
}
}

static void GetTextRead(string imageFile, VisionServiceOptions serviceOptions)
static void GetTextRead(string imageFile, ImageAnalysisClient client)
{
// Use Analyze image function to read text in image
Console.WriteLine($"\nReading text from {imageFile} \n");

// Use a file stream to pass the image data to the analyze call
using FileStream stream = new FileStream(imageFile,
FileMode.Open);

// Use Analyze image function to read text in image


}
}
}

9 changes: 6 additions & 3 deletions Labfiles/05-ocr/Python/read-text/read-text.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ def main():

# Authenticate Azure AI Vision client




# Menu for text reading functions
print('\n1: Use Read API for image (Lincoln.jpg)\n2: Read handwriting (Note.jpg)\nAny other key to quit\n')
Expand All @@ -39,8 +37,13 @@ def main():
def GetTextRead(image_file):
print('\n')

# Use Analyze image function to read text in image
# Open image file
with open(image_file, "rb") as f:
image_data = f.read()

# Use Analyze image function to read text in image





Expand Down

0 comments on commit ba3f851

Please sign in to comment.