├── classifier ├── labels.txt ├── model.pb ├── requirements.txt ├── .dockerignore ├── Dockerfile ├── app.yaml ├── image_processor.py ├── .gitignore └── predict.py ├── frontend ├── ProgressHub.cs ├── appsettings.Development.json ├── appsettings.json ├── .dockerignore ├── Frontend.csproj ├── Dockerfile ├── Properties │ └── launchSettings.json ├── .vscode │ ├── launch.json │ └── tasks.json ├── Program.cs ├── wwwroot │ └── index.html └── .gitignore └── LICENSE /classifier/labels.txt: -------------------------------------------------------------------------------- 1 | cat 2 | dog -------------------------------------------------------------------------------- /classifier/model.pb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthonychu/container-apps-image-classifier/HEAD/classifier/model.pb -------------------------------------------------------------------------------- /frontend/ProgressHub.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.SignalR; 2 | 3 | public class ProgressHub : Hub 4 | { 5 | } 6 | -------------------------------------------------------------------------------- /classifier/requirements.txt: -------------------------------------------------------------------------------- 1 | azure-storage-queue==12.3.0 2 | azure-identity==1.10.0 3 | tensorflow==1.15.5 4 | Pillow 5 | requests -------------------------------------------------------------------------------- /frontend/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /frontend/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | }, 8 | "AllowedHosts": "*" 9 | } 10 | -------------------------------------------------------------------------------- /frontend/.dockerignore: -------------------------------------------------------------------------------- 1 | **/.classpath 2 | **/.dockerignore 3 | **/.env 4 | **/.git 5 | **/.gitignore 6 | **/.project 7 | **/.settings 8 | **/.toolstarget 9 | **/.vs 10 | **/.vscode 11 | **/*.*proj.user 12 | **/*.dbmdl 13 | **/*.jfm 14 | **/bin 15 | **/charts 16 | **/docker-compose* 17 | **/compose* 18 | **/Dockerfile* 19 | **/node_modules 20 | **/npm-debug.log 21 | **/obj 22 | **/secrets.dev.yaml 23 | **/values.dev.yaml 24 | README.md 25 | -------------------------------------------------------------------------------- /frontend/Frontend.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /classifier/.dockerignore: -------------------------------------------------------------------------------- 1 | **/__pycache__ 2 | **/.venv 3 | **/.classpath 4 | **/.dockerignore 5 | **/.env 6 | **/.git 7 | **/.gitignore 8 | **/.project 9 | **/.settings 10 | **/.toolstarget 11 | **/.vs 12 | **/.vscode 13 | **/*.*proj.user 14 | **/*.dbmdl 15 | **/*.jfm 16 | **/bin 17 | **/charts 18 | **/docker-compose* 19 | **/compose* 20 | **/Dockerfile* 21 | **/node_modules 22 | **/npm-debug.log 23 | **/obj 24 | **/secrets.dev.yaml 25 | **/values.dev.yaml 26 | README.md 27 | app.yaml -------------------------------------------------------------------------------- /frontend/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base 2 | WORKDIR /app 3 | EXPOSE 80 4 | 5 | ENV ASPNETCORE_URLS=http://+:80 6 | 7 | FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build 8 | WORKDIR /src 9 | COPY ["Frontend.csproj", "./"] 10 | RUN dotnet restore "Frontend.csproj" 11 | COPY . . 12 | WORKDIR "/src/." 13 | RUN dotnet build "Frontend.csproj" -c Release -o /app/build 14 | 15 | FROM build AS publish 16 | RUN dotnet publish "Frontend.csproj" -c Release -o /app/publish /p:UseAppHost=false 17 | 18 | FROM base AS final 19 | WORKDIR /app 20 | COPY --from=publish /app/publish . 21 | ENTRYPOINT ["dotnet", "Frontend.dll"] 22 | -------------------------------------------------------------------------------- /frontend/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:45132", 7 | "sslPort": 44335 8 | } 9 | }, 10 | "profiles": { 11 | "Frontend": { 12 | "commandName": "Project", 13 | "dotnetRunMessages": true, 14 | "launchBrowser": true, 15 | "applicationUrl": "https://localhost:7096;http://localhost:5141", 16 | "environmentVariables": { 17 | "ASPNETCORE_ENVIRONMENT": "Development" 18 | } 19 | }, 20 | "IIS Express": { 21 | "commandName": "IISExpress", 22 | "launchBrowser": true, 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /classifier/Dockerfile: -------------------------------------------------------------------------------- 1 | # For more information, please refer to https://aka.ms/vscode-docker-python 2 | FROM python:3.7-slim 3 | 4 | # Keeps Python from generating .pyc files in the container 5 | ENV PYTHONDONTWRITEBYTECODE=1 6 | 7 | # Turns off buffering for easier container logging 8 | ENV PYTHONUNBUFFERED=1 9 | 10 | # Install pip requirements 11 | COPY requirements.txt . 12 | RUN python -m pip install -r requirements.txt 13 | 14 | WORKDIR /app 15 | COPY . /app 16 | 17 | # Creates a non-root user with an explicit UID and adds permission to access the /app folder 18 | # For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers 19 | RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app 20 | USER appuser 21 | 22 | # During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug 23 | CMD ["python", "image_processor.py"] 24 | -------------------------------------------------------------------------------- /classifier/app.yaml: -------------------------------------------------------------------------------- 1 | properties: 2 | template: 3 | containers: 4 | - env: 5 | - name: FRONTEND_RESULT_URL 6 | value: https://.azurecontainerapps.io/result 7 | - name: AZURE_QUEUE_SERVICE_URL 8 | value: https://.queue.core.windows.net 9 | image: albums.azurecr.io/dog-cat-classifier:latest 10 | name: dog-cat-classifier 11 | resources: 12 | cpu: 1 13 | memory: 2Gi 14 | scale: 15 | minReplicas: 0 16 | maxReplicas: 30 17 | rules: 18 | - name: queue-scaler 19 | custom: 20 | type: azure-queue 21 | metadata: 22 | accountName: dogcatstorage 23 | queueName: images 24 | cloud: AzurePublicCloud 25 | queueLength: '1' 26 | auth: 27 | - secretRef: queue-connection-str 28 | triggerParameter: connection 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Anthony Chu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /frontend/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": ".NET Core Launch (web)", 6 | "type": "coreclr", 7 | "request": "launch", 8 | "preLaunchTask": "build", 9 | "program": "${workspaceFolder}/bin/Debug/net6.0/Frontend.dll", 10 | "args": [], 11 | "cwd": "${workspaceFolder}", 12 | "stopAtEntry": false, 13 | "serverReadyAction": { 14 | "action": "openExternally", 15 | "pattern": "\\bNow listening on:\\s+(https?://\\S+)" 16 | }, 17 | "env": { 18 | "ASPNETCORE_ENVIRONMENT": "Development" 19 | }, 20 | "sourceFileMap": { 21 | "/Views": "${workspaceFolder}/Views" 22 | } 23 | }, 24 | { 25 | "name": ".NET Core Attach", 26 | "type": "coreclr", 27 | "request": "attach" 28 | }, 29 | { 30 | "name": "Docker .NET Core Launch", 31 | "type": "docker", 32 | "request": "launch", 33 | "preLaunchTask": "docker-run: debug", 34 | "netCore": { 35 | "appProject": "${workspaceFolder}/Frontend.csproj" 36 | } 37 | } 38 | ] 39 | } -------------------------------------------------------------------------------- /frontend/Program.cs: -------------------------------------------------------------------------------- 1 | using Azure.Identity; 2 | using Azure.Storage.Queues; 3 | using Microsoft.AspNetCore.SignalR; 4 | 5 | var builder = WebApplication.CreateBuilder(args); 6 | builder.Services.AddSignalR(); 7 | 8 | var app = builder.Build(); 9 | 10 | var url = $"{builder.Configuration["AZURE_QUEUE_SERVICE_URL"]}/images"; 11 | System.Console.WriteLine(url); 12 | var credential = new DefaultAzureCredential(); 13 | var queueClient = new QueueClient(new Uri(url), credential, new QueueClientOptions 14 | { 15 | MessageEncoding = QueueMessageEncoding.Base64 16 | }); 17 | 18 | var folders = new string[] { "Cat", "Dog" }; 19 | 20 | app.UseFileServer(); 21 | 22 | app.MapPost("/submitimages", async (int numImages) => 23 | { 24 | numImages = Math.Min(Math.Abs(numImages), 1000); 25 | var tasks = Enumerable.Range(0, numImages).Select(_ => 26 | { 27 | var imageNum = Random.Shared.Next(1, 500); 28 | var folder = folders[Random.Shared.Next(0, 2)]; 29 | var filename = $"{folder}/{imageNum}.jpg"; 30 | System.Console.WriteLine($"{filename}"); 31 | return queueClient.SendMessageAsync($"https://pythonqueueimage.blob.core.windows.net/images/{filename}"); 32 | }); 33 | 34 | await Task.WhenAll(tasks); 35 | 36 | return numImages.ToString(); 37 | }); 38 | 39 | app.MapPost("/result", async (object progress, IHubContext hubContext) => 40 | { 41 | await hubContext.Clients.All.SendAsync("NewProgress", progress); 42 | }); 43 | 44 | app.MapHub("/progress"); 45 | 46 | app.Run(); 47 | -------------------------------------------------------------------------------- /classifier/image_processor.py: -------------------------------------------------------------------------------- 1 | from socket import timeout 2 | from time import sleep 3 | from azure.identity import DefaultAzureCredential 4 | from azure.storage.queue import ( 5 | QueueServiceClient, 6 | BinaryBase64EncodePolicy, 7 | BinaryBase64DecodePolicy 8 | ) 9 | import os 10 | import requests 11 | from predict import predict_image_from_url 12 | import signal 13 | import uuid 14 | 15 | worker_id = str(uuid.uuid4()) 16 | keep_running = True 17 | 18 | def handler_stop_signals(signum, frame): 19 | global keep_running 20 | keep_running = False 21 | 22 | signal.signal(signal.SIGINT, handler_stop_signals) 23 | signal.signal(signal.SIGTERM, handler_stop_signals) 24 | 25 | default_credential = DefaultAzureCredential() 26 | client = QueueServiceClient(os.environ['AZURE_QUEUE_SERVICE_URL'], credential=default_credential) 27 | queue_client = client.get_queue_client("images", 28 | message_encode_policy = BinaryBase64EncodePolicy(), 29 | message_decode_policy = BinaryBase64DecodePolicy()) 30 | 31 | while keep_running: 32 | try: 33 | print("Checking for a message...") 34 | message = queue_client.receive_message(timeout=30) 35 | if message: 36 | image_url = message.content.decode("utf-8") 37 | print(image_url) 38 | results = predict_image_from_url(image_url) 39 | print(results) 40 | process_result = { 41 | 'imageUrl': image_url, 42 | 'prediction': results['predictedTagName'], 43 | 'workerId': worker_id 44 | } 45 | requests.post(os.environ['FRONTEND_RESULT_URL'], json=process_result) 46 | queue_client.delete_message(message) 47 | else: 48 | sleep(5) 49 | except Exception as e: 50 | print(e) 51 | 52 | if (not keep_running): 53 | print("Shutting down...") -------------------------------------------------------------------------------- /frontend/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/Frontend.csproj", 11 | "/property:GenerateFullPaths=true", 12 | "/consoleloggerparameters:NoSummary" 13 | ], 14 | "problemMatcher": "$msCompile" 15 | }, 16 | { 17 | "label": "publish", 18 | "command": "dotnet", 19 | "type": "process", 20 | "args": [ 21 | "publish", 22 | "${workspaceFolder}/Frontend.csproj", 23 | "/property:GenerateFullPaths=true", 24 | "/consoleloggerparameters:NoSummary" 25 | ], 26 | "problemMatcher": "$msCompile" 27 | }, 28 | { 29 | "label": "watch", 30 | "command": "dotnet", 31 | "type": "process", 32 | "args": [ 33 | "watch", 34 | "run", 35 | "--project", 36 | "${workspaceFolder}/Frontend.csproj" 37 | ], 38 | "problemMatcher": "$msCompile" 39 | }, 40 | { 41 | "type": "docker-build", 42 | "label": "docker-build: debug", 43 | "dependsOn": [ 44 | "build" 45 | ], 46 | "dockerBuild": { 47 | "tag": "frontend:dev", 48 | "target": "base", 49 | "dockerfile": "${workspaceFolder}/Dockerfile", 50 | "context": "${workspaceFolder}", 51 | "pull": true 52 | }, 53 | "netCore": { 54 | "appProject": "${workspaceFolder}/Frontend.csproj" 55 | } 56 | }, 57 | { 58 | "type": "docker-build", 59 | "label": "docker-build: release", 60 | "dependsOn": [ 61 | "build" 62 | ], 63 | "dockerBuild": { 64 | "tag": "frontend:latest", 65 | "dockerfile": "${workspaceFolder}/Dockerfile", 66 | "context": "${workspaceFolder}", 67 | "pull": true 68 | }, 69 | "netCore": { 70 | "appProject": "${workspaceFolder}/Frontend.csproj" 71 | } 72 | }, 73 | { 74 | "type": "docker-run", 75 | "label": "docker-run: debug", 76 | "dependsOn": [ 77 | "docker-build: debug" 78 | ], 79 | "dockerRun": {}, 80 | "netCore": { 81 | "appProject": "${workspaceFolder}/Frontend.csproj", 82 | "enableDebugging": true, 83 | "configureSsl": false 84 | } 85 | }, 86 | { 87 | "type": "docker-run", 88 | "label": "docker-run: release", 89 | "dependsOn": [ 90 | "docker-build: release" 91 | ], 92 | "dockerRun": {}, 93 | "netCore": { 94 | "appProject": "${workspaceFolder}/Frontend.csproj" 95 | } 96 | } 97 | ] 98 | } -------------------------------------------------------------------------------- /classifier/.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | -------------------------------------------------------------------------------- /frontend/wwwroot/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Dog or Cat? 8 | 10 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | Dog or Cat? 53 | {{ processingRate }} images/sec (replicas: {{ numWorkers }}) 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | Number of images 62 | 63 | 64 | 65 | 66 | Classify images 67 | 68 | 69 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 🐶 Dog 87 | 😸 Cat 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 168 | 169 | 170 | -------------------------------------------------------------------------------- /classifier/predict.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | import logging 3 | import os 4 | 5 | from urllib.request import urlopen 6 | from PIL import Image 7 | import tensorflow as tf 8 | import numpy as np 9 | 10 | 11 | scriptpath = os.path.abspath(__file__) 12 | scriptdir = os.path.dirname(scriptpath) 13 | filename = os.path.join(scriptdir, 'model.pb') 14 | labels_filename = os.path.join(scriptdir, 'labels.txt') 15 | 16 | 17 | output_layer = 'loss:0' 18 | input_node = 'Placeholder:0' 19 | 20 | graph_def = tf.GraphDef() 21 | labels = [] 22 | network_input_size = 0 23 | 24 | def _initialize(): 25 | global labels, network_input_size 26 | if not labels: 27 | with tf.io.gfile.GFile(filename, 'rb') as f: 28 | graph_def.ParseFromString(f.read()) 29 | tf.import_graph_def(graph_def, name='') 30 | with open(labels_filename, 'rt') as lf: 31 | labels = [l.strip() for l in lf.readlines()] 32 | with tf.compat.v1.Session() as sess: 33 | input_tensor_shape = sess.graph.get_tensor_by_name('Placeholder:0').shape.as_list() 34 | network_input_size = input_tensor_shape[1] 35 | logging.info('network_input_size = ' + str(network_input_size)) 36 | 37 | def _log_msg(msg): 38 | logging.info("{}: {}".format(datetime.now(),msg)) 39 | 40 | def _extract_bilinear_pixel(img, x, y, ratio, xOrigin, yOrigin): 41 | xDelta = (x + 0.5) * ratio - 0.5 42 | x0 = int(xDelta) 43 | xDelta -= x0 44 | x0 += xOrigin 45 | if x0 < 0: 46 | x0 = 0; 47 | x1 = 0; 48 | xDelta = 0.0; 49 | elif x0 >= img.shape[1]-1: 50 | x0 = img.shape[1]-1; 51 | x1 = img.shape[1]-1; 52 | xDelta = 0.0; 53 | else: 54 | x1 = x0 + 1; 55 | 56 | yDelta = (y + 0.5) * ratio - 0.5 57 | y0 = int(yDelta) 58 | yDelta -= y0 59 | y0 += yOrigin 60 | if y0 < 0: 61 | y0 = 0; 62 | y1 = 0; 63 | yDelta = 0.0; 64 | elif y0 >= img.shape[0]-1: 65 | y0 = img.shape[0]-1; 66 | y1 = img.shape[0]-1; 67 | yDelta = 0.0; 68 | else: 69 | y1 = y0 + 1; 70 | 71 | #Get pixels in four corners 72 | bl = img[y0, x0] 73 | br = img[y0, x1] 74 | tl = img[y1, x0] 75 | tr = img[y1, x1] 76 | #Calculate interpolation 77 | b = xDelta * br + (1. - xDelta) * bl 78 | t = xDelta * tr + (1. - xDelta) * tl 79 | pixel = yDelta * t + (1. - yDelta) * b 80 | return pixel.astype(np.uint8) 81 | 82 | def _extract_and_resize(img, targetSize): 83 | determinant = img.shape[1] * targetSize[0] - img.shape[0] * targetSize[1] 84 | if determinant < 0: 85 | ratio = float(img.shape[1]) / float(targetSize[1]) 86 | xOrigin = 0 87 | yOrigin = int(0.5 * (img.shape[0] - ratio * targetSize[0])) 88 | elif determinant > 0: 89 | ratio = float(img.shape[0]) / float(targetSize[0]) 90 | xOrigin = int(0.5 * (img.shape[1] - ratio * targetSize[1])) 91 | yOrigin = 0 92 | else: 93 | ratio = float(img.shape[0]) / float(targetSize[0]) 94 | xOrigin = 0 95 | yOrigin = 0 96 | resize_image = np.empty((targetSize[0], targetSize[1], img.shape[2]), dtype=np.uint8) 97 | for y in range(targetSize[0]): 98 | for x in range(targetSize[1]): 99 | resize_image[y, x] = _extract_bilinear_pixel(img, x, y, ratio, xOrigin, yOrigin) 100 | return resize_image 101 | 102 | def _extract_and_resize_to_256_square(image): 103 | h, w = image.shape[:2] 104 | _log_msg("extract_and_resize_to_256_square: " + str(w) + "x" + str(h) +" and resize to " + str(256) + "x" + str(256)) 105 | return _extract_and_resize(image, (256, 256)) 106 | 107 | def _crop_center(img,cropx,cropy): 108 | h, w = img.shape[:2] 109 | startx = max(0, w//2-(cropx//2) - 1) 110 | starty = max(0, h//2-(cropy//2) - 1) 111 | _log_msg("crop_center: " + str(w) + "x" + str(h) +" to " + str(cropx) + "x" + str(cropy)) 112 | return img[starty:starty+cropy, startx:startx+cropx] 113 | 114 | def _resize_down_to_1600_max_dim(image): 115 | w,h = image.size 116 | if h < 1600 and w < 1600: 117 | return image 118 | 119 | new_size = (1600 * w // h, 1600) if (h > w) else (1600, 1600 * h // w) 120 | _log_msg("resize: " + str(w) + "x" + str(h) + " to " + str(new_size[0]) + "x" + str(new_size[1])) 121 | if max(new_size) / max(image.size) >= 0.5: 122 | method = Image.BILINEAR 123 | else: 124 | method = Image.BICUBIC 125 | return image.resize(new_size, method) 126 | 127 | def _convert_to_nparray(image): 128 | # RGB -> BGR 129 | _log_msg("Convert to numpy array") 130 | image = np.array(image) 131 | return image[:, :, (2,1,0)] 132 | 133 | def _update_orientation(image): 134 | exif_orientation_tag = 0x0112 135 | if hasattr(image, '_getexif'): 136 | exif = image._getexif() 137 | if exif != None and exif_orientation_tag in exif: 138 | orientation = exif.get(exif_orientation_tag, 1) 139 | _log_msg('Image has EXIF Orientation: ' + str(orientation)) 140 | # orientation is 1 based, shift to zero based and flip/transpose based on 0-based values 141 | orientation -= 1 142 | if orientation >= 4: 143 | image = image.transpose(Image.TRANSPOSE) 144 | if orientation == 2 or orientation == 3 or orientation == 6 or orientation == 7: 145 | image = image.transpose(Image.FLIP_TOP_BOTTOM) 146 | if orientation == 1 or orientation == 2 or orientation == 5 or orientation == 6: 147 | image = image.transpose(Image.FLIP_LEFT_RIGHT) 148 | return image 149 | 150 | def _predict_image(image): 151 | try: 152 | if image.mode != "RGB": 153 | _log_msg("Converting to RGB") 154 | image.convert("RGB") 155 | 156 | w,h = image.size 157 | _log_msg("Image size: " + str(w) + "x" + str(h)) 158 | 159 | # Update orientation based on EXIF tags 160 | image = _update_orientation(image) 161 | 162 | # If the image has either w or h greater than 1600 we resize it down respecting 163 | # aspect ratio such that the largest dimention is 1600 164 | image = _resize_down_to_1600_max_dim(image) 165 | 166 | # Convert image to numpy array 167 | image = _convert_to_nparray(image) 168 | 169 | # Crop the center square and resize that square down to 256x256 170 | resized_image = _extract_and_resize_to_256_square(image) 171 | 172 | # Crop the center for the specified network_input_Size 173 | cropped_image = _crop_center(resized_image, network_input_size, network_input_size) 174 | 175 | tf.compat.v1.reset_default_graph() 176 | tf.import_graph_def(graph_def, name='') 177 | 178 | with tf.compat.v1.Session() as sess: 179 | prob_tensor = sess.graph.get_tensor_by_name(output_layer) 180 | predictions, = sess.run(prob_tensor, {input_node: [cropped_image] }) 181 | 182 | result = [] 183 | highest_prediction = None 184 | for p, label in zip(predictions, labels): 185 | truncated_probablity = np.float64(round(p,8)) 186 | if truncated_probablity > 1e-8: 187 | prediction = { 188 | 'tagName': label, 189 | 'probability': truncated_probablity } 190 | result.append(prediction) 191 | if not highest_prediction or prediction['probability'] > highest_prediction['probability']: 192 | highest_prediction = prediction 193 | 194 | response = { 195 | 'created': datetime.utcnow().isoformat(), 196 | 'predictedTagName': highest_prediction['tagName'], 197 | 'prediction': result 198 | } 199 | 200 | _log_msg("Results: " + str(response)) 201 | return response 202 | 203 | except Exception as e: 204 | _log_msg(str(e)) 205 | return 'Error: Could not preprocess image for prediction. ' + str(e) 206 | 207 | def predict_image_from_url(image_url): 208 | logging.info("Predicting from url: " + image_url) 209 | 210 | _initialize() 211 | 212 | with urlopen(image_url) as testImage: 213 | image = Image.open(testImage) 214 | return _predict_image(image) 215 | -------------------------------------------------------------------------------- /frontend/.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.tlog 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 298 | *.vbp 299 | 300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 301 | *.dsw 302 | *.dsp 303 | 304 | # Visual Studio 6 technical files 305 | *.ncb 306 | *.aps 307 | 308 | # Visual Studio LightSwitch build output 309 | **/*.HTMLClient/GeneratedArtifacts 310 | **/*.DesktopClient/GeneratedArtifacts 311 | **/*.DesktopClient/ModelManifest.xml 312 | **/*.Server/GeneratedArtifacts 313 | **/*.Server/ModelManifest.xml 314 | _Pvt_Extensions 315 | 316 | # Paket dependency manager 317 | .paket/paket.exe 318 | paket-files/ 319 | 320 | # FAKE - F# Make 321 | .fake/ 322 | 323 | # CodeRush personal settings 324 | .cr/personal 325 | 326 | # Python Tools for Visual Studio (PTVS) 327 | __pycache__/ 328 | *.pyc 329 | 330 | # Cake - Uncomment if you are using it 331 | # tools/** 332 | # !tools/packages.config 333 | 334 | # Tabs Studio 335 | *.tss 336 | 337 | # Telerik's JustMock configuration file 338 | *.jmconfig 339 | 340 | # BizTalk build output 341 | *.btp.cs 342 | *.btm.cs 343 | *.odx.cs 344 | *.xsd.cs 345 | 346 | # OpenCover UI analysis results 347 | OpenCover/ 348 | 349 | # Azure Stream Analytics local run output 350 | ASALocalRun/ 351 | 352 | # MSBuild Binary and Structured Log 353 | *.binlog 354 | 355 | # NVidia Nsight GPU debugger configuration file 356 | *.nvuser 357 | 358 | # MFractors (Xamarin productivity tool) working folder 359 | .mfractor/ 360 | 361 | # Local History for Visual Studio 362 | .localhistory/ 363 | 364 | # Visual Studio History (VSHistory) files 365 | .vshistory/ 366 | 367 | # BeatPulse healthcheck temp database 368 | healthchecksdb 369 | 370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 371 | MigrationBackup/ 372 | 373 | # Ionide (cross platform F# VS Code tools) working folder 374 | .ionide/ 375 | 376 | # Fody - auto-generated XML schema 377 | FodyWeavers.xsd 378 | 379 | # VS Code files for those working on multiple tools 380 | .vscode/* 381 | !.vscode/settings.json 382 | !.vscode/tasks.json 383 | !.vscode/launch.json 384 | !.vscode/extensions.json 385 | *.code-workspace 386 | 387 | # Local History for Visual Studio Code 388 | .history/ 389 | 390 | # Windows Installer files from build outputs 391 | *.cab 392 | *.msi 393 | *.msix 394 | *.msm 395 | *.msp 396 | 397 | # JetBrains Rider 398 | *.sln.iml 399 | --------------------------------------------------------------------------------