Commit 97cffe2f by Usman Shahid

Upload New File

parent 4e1d64d7
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Querying an Azure Custom Vision endpoint from Python\n",
"\n",
"This is the companion notebook to the talk on [Azure Custom Vison for Beginners](https://www.youtube.com/watch?v=OzMRNVolrKE&lc=UgwYIaTgEIkRtacBGI54AaABAg) hosted by [Data Science Dojo](https://datasciencedojo.com/) and delivered by [Usman Shahid](https://www.linkedin.com/in/shahidusmanm/)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import functions as fn\n",
"import nest_asyncio\n",
"nest_asyncio.apply()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#These have already been imported in functions.py. Re-imported them here to show during the webinar.\n",
"\n",
"# Other libraries used\n",
"import os\n",
"\n",
"# for converting the frames into bytes\n",
"import cv2 \n",
"\n",
"# and for processing arrays \n",
"import numpy as np\n",
"\n",
"# for encoding and decoding Custom Vision predictions \n",
"import json\n",
"\n",
"# for converting the Custom Vision predictions to dataframe \n",
"import pandas as pd\n",
"\n",
"# import async packages\n",
"import asyncio\n",
"import aiohttp\n",
"\n",
"# for file name pattern matching \n",
"import fnmatch \n",
"\n",
"# for displaying images from the processes output video \n",
"import matplotlib.pyplot as plt\n",
"\n",
"# importing other required libraries\n",
"import random\n",
"import textwrap\n",
"import datetime \n",
"from PIL import Image\n",
"import time "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#These have already been declared in functions.py. Declared them here to show during the webinar.\n",
"\n",
"# web service end-point for the Custom Vision model \n",
"# we will process video frames (which are images) \n",
"POST_URL = \"Your custom vision endpoint\"\n",
"\n",
"# providing prediction key\n",
"HEADERS = {'Prediction-Key': \"Your custom vision prediction key\", \"Content-Type\":\"application/json\"}\n",
"\n",
"# number of API calls per pool of request \n",
"MAX_CONNECTIONS = 100 \n",
"\n",
"# initializing the height and width for frames in the video \n",
"WIDTH = 0\n",
"HEIGHT = 0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# creating Output and Stats directories for saving processed videos \n",
"if not os.path.isdir(\"Output\"):\n",
" os.mkdir(\"Output\")\n",
"if not os.path.isdir(\"Stats\"):\n",
" os.mkdir(\"Stats\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# converting raw input video to processed video complete with tags and stats\n",
"threshold=0.3\n",
"fn.ConvertVideo(\"input/input_video.mov\", \"Output/output_video.mp4\", threshold, nframes=70)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# saving processed video as images inside the frames directory\n",
"images = []\n",
"byteImages = []\n",
"vidObj = cv2.VideoCapture(\"Output/output_video.mp4\")\n",
"count = 0\n",
"success = 1\n",
"currentDir = os.getcwd()\n",
"if not os.path.isdir(\"frames\"):\n",
" os.mkdir(\"frames\")\n",
"while success:\n",
" success, image = vidObj.read()\n",
" if success:\n",
" cv2.imwrite(\"frames/frame%d.jpg\" % count, image) \n",
" count += 1\n",
"\n",
"images_num=(len(fnmatch.filter(os.listdir(os.getcwd() + \"/frames\"), '*.jpg')))\n",
"\n",
"# displaying images from frames directory\n",
"i=0\n",
"for i in range(images_num):\n",
" if i%75==0:\n",
" a = plt.imread(\"frames/frame%d.jpg\" % i)\n",
" plt.imshow(a)\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment