Skip to content

Instantly share code, notes, and snippets.

@kiransair
Created September 29, 2023 10:22
Show Gist options
  • Save kiransair/44079614b662006bb8d227e02b40ca73 to your computer and use it in GitHub Desktop.
Save kiransair/44079614b662006bb8d227e02b40ca73 to your computer and use it in GitHub Desktop.
TF_Forum_19782.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "T4",
"authorship_tag": "ABX9TyN6/lZGn74wStwlK6j9mqOO",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/kiransair/44079614b662006bb8d227e02b40ca73/tf_forum_19782.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"import tensorflow as tf\n",
"\n",
"import tensorflow_hub as hub\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import tempfile\n",
"from six.moves.urllib.request import urlopen\n",
"from six import BytesIO\n",
"\n",
"import numpy as np\n",
"from PIL import Image\n",
"from PIL import ImageColor\n",
"from PIL import ImageDraw\n",
"from PIL import ImageFont\n",
"from PIL import ImageOps\n",
"\n",
"\n",
"print(tf.__version__)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "iJbt5R_wZokX",
"outputId": "1f125045-06dd-4702-a351-073c81f7d861"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"2.13.0\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"module_handle = \"https://tfhub.dev/google/faster_rcnn/openimages_v4/inception_resnet_v2/1\"\n",
"\n",
"detector = hub.load(module_handle).signatures['default']"
],
"metadata": {
"id": "8LwusJF_ZrqT"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"image_url=\"https://upload.wikimedia.org/wikipedia/commons/6/60/Naxos_Taverna.jpg\""
],
"metadata": {
"id": "mmK3vUDnaCrf"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def display_image(image):\n",
" fig = plt.figure(figsize=(20, 15))\n",
" plt.grid(False)\n",
" plt.imshow(image)\n",
"\n",
"\n",
"def download_and_resize_image(url, new_width=256, new_height=256,\n",
" display=False):\n",
" _, filename = tempfile.mkstemp(suffix=\".jpg\")\n",
" response = urlopen(url)\n",
" image_data = response.read()\n",
" image_data = BytesIO(image_data)\n",
" pil_image = Image.open(image_data)\n",
" pil_image = ImageOps.fit(pil_image, (new_width, new_height), Image.LANCZOS)\n",
" pil_image_rgb = pil_image.convert(\"RGB\")\n",
" pil_image_rgb.save(filename, format=\"JPEG\", quality=90)\n",
" print(\"Image downloaded to %s.\" % filename)\n",
" if display:\n",
" display_image(pil_image)\n",
" return filename\n",
"\n",
"\n",
"def draw_bounding_box_on_image(image,\n",
" ymin,\n",
" xmin,\n",
" ymax,\n",
" xmax,\n",
" color,\n",
" font,\n",
" thickness=4,\n",
" display_str_list=()):\n",
" \"\"\"Adds a bounding box to an image.\"\"\"\n",
" draw = ImageDraw.Draw(image)\n",
" im_width, im_height = image.size\n",
" (left, right, top, bottom) = (xmin * im_width, xmax * im_width,\n",
" ymin * im_height, ymax * im_height)\n",
" draw.line([(left, top), (left, bottom), (right, bottom), (right, top),\n",
" (left, top)],\n",
" width=thickness,\n",
" fill=color)\n",
"\n",
" # If the total height of the display strings added to the top of the bounding\n",
" # box exceeds the top of the image, stack the strings below the bounding box\n",
" # instead of above.\n",
" display_str_heights = [font.getbbox(ds)[3] for ds in display_str_list]\n",
" # Each display_str has a top and bottom margin of 0.05x.\n",
" total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights)\n",
"\n",
" if top > total_display_str_height:\n",
" text_bottom = top\n",
" else:\n",
" text_bottom = top + total_display_str_height\n",
" # Reverse list and print from bottom to top.\n",
" for display_str in display_str_list[::-1]:\n",
" bbox = font.getbbox(display_str)\n",
" text_width, text_height = bbox[2], bbox[3]\n",
" margin = np.ceil(0.05 * text_height)\n",
" draw.rectangle([(left, text_bottom - text_height - 2 * margin),\n",
" (left + text_width, text_bottom)],\n",
" fill=color)\n",
" draw.text((left + margin, text_bottom - text_height - margin),\n",
" display_str,\n",
" fill=\"black\",\n",
" font=font)\n",
" text_bottom -= text_height - 2 * margin\n",
"\n",
"\n",
"def draw_boxes(image, boxes, class_names, scores, max_boxes=10, min_score=0.1):\n",
" \"\"\"Overlay labeled boxes on an image with formatted scores and label names.\"\"\"\n",
" colors = list(ImageColor.colormap.values())\n",
"\n",
" try:\n",
" font = ImageFont.truetype(\"/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Regular.ttf\",\n",
" 25)\n",
" except IOError:\n",
" print(\"Font not found, using default font.\")\n",
" font = ImageFont.load_default()\n",
"\n",
" for i in range(min(boxes.shape[0], max_boxes)):\n",
" if scores[i] >= min_score:\n",
" ymin, xmin, ymax, xmax = tuple(boxes[i])\n",
" display_str = \"{}: {}%\".format(class_names[i].decode(\"ascii\"),\n",
" int(100 * scores[i]))\n",
" color = colors[hash(class_names[i]) % len(colors)]\n",
" image_pil = Image.fromarray(np.uint8(image)).convert(\"RGB\")\n",
" draw_bounding_box_on_image(\n",
" image_pil,\n",
" ymin,\n",
" xmin,\n",
" ymax,\n",
" xmax,\n",
" color,\n",
" font,\n",
" display_str_list=[display_str])\n",
" np.copyto(image, np.array(image_pil))\n",
" return image"
],
"metadata": {
"id": "nKvMEbq5aYkj"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [
"image_path = download_and_resize_image(image_url, 640, 480)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "EGLdoGiLaQSU",
"outputId": "d40dde5c-d012-4cc1-a345-42f2138a0c2f"
},
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Image downloaded to /tmp/tmp8p88jbky.jpg.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"def load_img(path):\n",
" img = tf.io.read_file(path)\n",
" img = tf.image.decode_jpeg(img, channels=3)\n",
" return img"
],
"metadata": {
"id": "rDMHh0WMahPh"
},
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"source": [
"img = load_img(image_path)"
],
"metadata": {
"id": "kw7kOMV8aZQY"
},
"execution_count": 8,
"outputs": []
},
{
"cell_type": "code",
"source": [
"converted_img = tf.image.convert_image_dtype(img, tf.float32)[tf.newaxis, ...]\n",
"result = detector(converted_img)"
],
"metadata": {
"id": "I8msbvsbaiDa"
},
"execution_count": 10,
"outputs": []
},
{
"cell_type": "code",
"source": [
"result['detection_boxes']"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "o1sk9cVqalBF",
"outputId": "9f0ef862-256e-46f9-f43c-d02d0069dd87"
},
"execution_count": 16,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<tf.Tensor: shape=(100, 4), dtype=float32, numpy=\n",
"array([[6.29714787e-01, 2.58667856e-01, 9.24072087e-01, 3.87427926e-01],\n",
" [7.54902840e-01, 0.00000000e+00, 9.82881367e-01, 2.41151229e-01],\n",
" [6.10230029e-01, 5.22934854e-01, 8.00976098e-01, 6.18315816e-01],\n",
" [6.30680382e-01, 8.11941445e-01, 7.10187614e-01, 9.91220772e-01],\n",
" [0.00000000e+00, 1.84439085e-02, 6.83354616e-01, 9.65996385e-01],\n",
" [6.75925434e-01, 6.63116455e-01, 9.83437121e-01, 9.79164839e-01],\n",
" [6.61310136e-01, 3.72873634e-01, 9.40228820e-01, 5.04609168e-01],\n",
" [6.51130438e-01, 6.13535881e-01, 7.29262054e-01, 6.70051694e-01],\n",
" [6.14358246e-01, 3.79992515e-01, 7.75979817e-01, 5.88142812e-01],\n",
" [6.16996782e-03, 8.96988809e-03, 5.69360077e-01, 3.04916441e-01],\n",
" [6.75604880e-01, 6.67575538e-01, 9.88016903e-01, 8.21752787e-01],\n",
" [6.29913568e-01, 2.36798644e-01, 8.85104477e-01, 3.59651744e-01],\n",
" [6.27157629e-01, 2.43593901e-01, 9.03830588e-01, 5.35699785e-01],\n",
" [1.48308650e-01, 3.16028595e-02, 6.41681433e-01, 5.54697394e-01],\n",
" [7.79754147e-02, 2.99184024e-02, 1.00000000e+00, 9.96253490e-01],\n",
" [6.38614058e-01, 3.94707114e-01, 8.83755624e-01, 5.11403978e-01],\n",
" [6.70072019e-01, 6.44761324e-01, 9.74955142e-01, 9.97416794e-01],\n",
" [6.18822515e-01, 2.20020592e-01, 9.32154238e-01, 5.67533016e-01],\n",
" [5.61007321e-01, 6.04286611e-01, 7.26622403e-01, 6.97563291e-01],\n",
" [5.88979721e-01, 1.91167846e-01, 7.52461135e-01, 2.65358448e-01],\n",
" [2.20607489e-01, 7.11494327e-01, 6.16825998e-01, 8.30817342e-01],\n",
" [8.03393960e-01, 1.01090856e-01, 1.00000000e+00, 2.64388204e-01],\n",
" [5.72116375e-02, 1.78852618e-01, 6.95218205e-01, 9.41267729e-01],\n",
" [6.44664347e-01, 3.27818453e-01, 9.15155351e-01, 5.04475594e-01],\n",
" [1.37128577e-01, 3.18424217e-03, 6.95364594e-01, 5.99480510e-01],\n",
" [6.15216255e-01, 2.36601025e-01, 7.97937512e-01, 3.49647164e-01],\n",
" [4.99310225e-01, 1.52454704e-01, 6.29056513e-01, 2.35714719e-01],\n",
" [6.13371313e-01, 4.57683861e-01, 7.87951469e-01, 5.85088670e-01],\n",
" [4.99628365e-01, 4.00385894e-02, 6.28541231e-01, 1.27153933e-01],\n",
" [6.75886869e-01, 7.71725774e-01, 9.74003315e-01, 9.46021378e-01],\n",
" [6.15983367e-01, 5.02711833e-01, 7.83075511e-01, 5.65581977e-01],\n",
" [7.70291388e-01, 1.31698037e-02, 9.91557419e-01, 2.62946516e-01],\n",
" [5.58560610e-01, 8.59219491e-01, 6.34648025e-01, 8.95711482e-01],\n",
" [6.10257626e-01, 2.09116772e-01, 7.56156206e-01, 3.61418575e-01],\n",
" [6.47484004e-01, 6.57450333e-02, 6.77364886e-01, 9.06353518e-02],\n",
" [6.20201409e-01, 4.18475688e-01, 8.11285198e-01, 5.29682398e-01],\n",
" [5.32911062e-01, 6.98723257e-01, 6.86554909e-01, 8.22128296e-01],\n",
" [3.46994027e-02, 1.96697995e-01, 7.19007730e-01, 9.43760455e-01],\n",
" [5.41288257e-01, 7.00862586e-01, 7.04424441e-01, 8.24200392e-01],\n",
" [5.96008062e-01, 2.28032038e-01, 6.60379112e-01, 3.53208691e-01],\n",
" [6.86671197e-01, 7.86475599e-01, 9.68437731e-01, 9.86384332e-01],\n",
" [6.99612975e-01, 6.59627140e-01, 9.86045659e-01, 8.42538357e-01],\n",
" [6.13034844e-01, 4.37129855e-01, 7.71155477e-01, 5.15501916e-01],\n",
" [9.83451866e-03, 9.13387258e-03, 4.63899434e-01, 4.66680199e-01],\n",
" [2.83347573e-02, 7.91438878e-01, 6.08148098e-01, 9.82485771e-01],\n",
" [6.54912472e-01, 3.37937474e-01, 9.24294770e-01, 4.63403076e-01],\n",
" [6.09765768e-01, 4.71535563e-01, 7.84157813e-01, 5.36660373e-01],\n",
" [6.17203832e-01, 2.81548828e-01, 8.68777156e-01, 4.85892087e-01],\n",
" [5.60183823e-01, 6.03982568e-01, 6.83209240e-01, 6.99804366e-01],\n",
" [0.00000000e+00, 5.56546189e-02, 6.10025167e-01, 5.31458437e-01],\n",
" [6.26316667e-01, 8.09906006e-01, 7.06378341e-01, 9.78910029e-01],\n",
" [6.77173853e-01, 6.47916198e-01, 9.71765876e-01, 9.85887825e-01],\n",
" [5.79933822e-01, 7.81865537e-01, 6.92503035e-01, 8.34695876e-01],\n",
" [5.04131496e-01, 3.86336669e-02, 6.40080094e-01, 1.25018552e-01],\n",
" [9.95018054e-03, 4.29741085e-01, 7.09871948e-01, 9.89586174e-01],\n",
" [6.49301291e-01, 8.05389464e-01, 8.79004002e-01, 9.96624649e-01],\n",
" [5.75048387e-01, 8.61946166e-01, 6.29341900e-01, 8.92458081e-01],\n",
" [7.24409744e-02, 6.13107800e-01, 6.80724680e-01, 9.88265514e-01],\n",
" [4.12544519e-01, 1.95863470e-03, 6.03012979e-01, 5.31112477e-02],\n",
" [6.06396616e-01, 2.35215887e-01, 7.59872437e-01, 3.18319142e-01],\n",
" [6.39366746e-01, 7.00491846e-01, 9.77408469e-01, 9.08113778e-01],\n",
" [6.35795653e-01, 3.02026480e-01, 9.17284548e-01, 5.16079187e-01],\n",
" [6.01408124e-01, 5.45901954e-01, 7.61106789e-01, 6.24258935e-01],\n",
" [9.35597718e-03, 7.52428472e-01, 6.83746457e-01, 9.89543974e-01],\n",
" [6.17937744e-01, 4.12930280e-01, 7.57408082e-01, 5.04332662e-01],\n",
" [6.36702955e-01, 2.14507394e-02, 7.74459302e-01, 2.08525762e-01],\n",
" [6.07432365e-01, 3.83894950e-01, 7.90953517e-01, 6.11809671e-01],\n",
" [6.06049955e-01, 3.62700045e-01, 8.56632113e-01, 5.31732798e-01],\n",
" [5.70421696e-01, 9.21983719e-02, 9.41535354e-01, 8.95882070e-01],\n",
" [6.18271232e-01, 8.14893544e-01, 7.04694569e-01, 8.95614088e-01],\n",
" [5.81046164e-01, 7.91048825e-01, 6.04750872e-01, 8.05426538e-01],\n",
" [7.90564060e-01, 6.75203204e-02, 1.00000000e+00, 2.50069618e-01],\n",
" [6.54336751e-01, 2.21644272e-03, 7.91302383e-01, 3.04254759e-02],\n",
" [5.03341675e-01, 1.43592983e-01, 7.12702632e-01, 2.51648784e-01],\n",
" [5.90023816e-01, 3.13502848e-01, 6.51989043e-01, 3.49997550e-01],\n",
" [3.73666018e-01, 3.02880281e-03, 6.14752293e-01, 1.24579914e-01],\n",
" [5.83859801e-01, 9.52891529e-01, 6.30386949e-01, 9.85884666e-01],\n",
" [6.27861023e-01, 2.33287156e-01, 8.73763978e-01, 4.03011650e-01],\n",
" [6.53785825e-01, 3.48141678e-02, 9.31715429e-01, 4.28984433e-01],\n",
" [8.09100151e-01, 9.76607203e-02, 9.90838826e-01, 2.63654888e-01],\n",
" [6.14334404e-01, 4.16892618e-01, 7.02013731e-01, 4.97752130e-01],\n",
" [6.17057145e-01, 4.98952955e-01, 7.65341163e-01, 5.92154622e-01],\n",
" [1.08313374e-01, 9.02400434e-01, 6.05159104e-01, 9.98061121e-01],\n",
" [5.95593929e-01, 2.66863108e-01, 7.45359898e-01, 3.42691123e-01],\n",
" [9.56639349e-02, 6.98434591e-01, 6.30638540e-01, 9.63428020e-01],\n",
" [6.09125853e-01, 3.97170931e-01, 6.79588795e-01, 5.19437790e-01],\n",
" [6.39739752e-01, 1.38193602e-02, 7.71312296e-01, 1.12617016e-01],\n",
" [6.07205391e-01, 5.20045519e-01, 7.90195048e-01, 6.15292907e-01],\n",
" [4.21690285e-01, 8.02538358e-04, 5.98956108e-01, 5.32318205e-02],\n",
" [7.55449772e-01, 1.08405109e-02, 9.86996591e-01, 2.36557648e-01],\n",
" [9.12010908e-01, 9.77933049e-01, 1.00000000e+00, 9.99486089e-01],\n",
" [5.79267561e-01, 4.64523256e-01, 6.13602996e-01, 5.13987303e-01],\n",
" [5.05770564e-01, 9.61984515e-01, 5.78107595e-01, 9.96620595e-01],\n",
" [5.03527164e-01, 1.51584432e-01, 6.44254029e-01, 2.36856490e-01],\n",
" [4.91892010e-01, 9.62366939e-01, 5.79898596e-01, 9.97372091e-01],\n",
" [6.80102885e-01, 1.66317692e-03, 7.86272526e-01, 2.83277463e-02],\n",
" [7.17962682e-02, 3.26736048e-02, 1.00000000e+00, 9.76304591e-01],\n",
" [6.10111535e-01, 4.84969199e-01, 7.11247265e-01, 5.80639064e-01],\n",
" [6.12174451e-01, 4.43246692e-01, 7.94013619e-01, 5.59269071e-01],\n",
" [6.10381424e-01, 5.18863678e-01, 7.31902957e-01, 5.79622269e-01]],\n",
" dtype=float32)>"
]
},
"metadata": {},
"execution_count": 16
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "N2H5OjEQausX"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment