Skip to content

Instantly share code, notes, and snippets.

@jamessdixon
Created December 16, 2024 18:07
Show Gist options
  • Select an option

  • Save jamessdixon/4b80df8573dd9531f3e0015f84f758d9 to your computer and use it in GitHub Desktop.

Select an option

Save jamessdixon/4b80df8573dd9531f3e0015f84f758d9 to your computer and use it in GitHub Desktop.
pipe-isolation
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
".venv/lib/python3.9/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020\n",
" warnings.warn(\n"
]
}
],
"source": [
"import os\n",
"import openai\n",
"import random\n",
"import requests\n",
"from io import BytesIO\n",
"from PIL import Image\n",
"\n",
"import cv2\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Generated image URLs:\n",
"1: https://oaidalleapiprodscus.blob.core.windows.net/private/org-jXMZj5kjVPIA0aF9jkikgCi6/user-hn35HYMc07e601A7nEXk4LTH/img-cMzrqIL1h3EEBaLBhgSU2ZOV.png?st=2024-12-16T16%3A46%3A43Z&se=2024-12-16T18%3A46%3A43Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-12-16T09%3A09%3A53Z&ske=2024-12-17T09%3A09%3A53Z&sks=b&skv=2024-08-04&sig=ri29Pex9705Nj2Ye1VTV41GYV3NiPFUUvroMJygCoqM%3D\n",
"2: https://oaidalleapiprodscus.blob.core.windows.net/private/org-jXMZj5kjVPIA0aF9jkikgCi6/user-hn35HYMc07e601A7nEXk4LTH/img-ocgCFhulCfK9VZPOTJFWwFcx.png?st=2024-12-16T16%3A46%3A54Z&se=2024-12-16T18%3A46%3A54Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-12-16T10%3A20%3A11Z&ske=2024-12-17T10%3A20%3A11Z&sks=b&skv=2024-08-04&sig=22SVMRo4zbRTJJfq1OMiWsilzUn3y7nVyAZQ3C4BwG8%3D\n",
"3: https://oaidalleapiprodscus.blob.core.windows.net/private/org-jXMZj5kjVPIA0aF9jkikgCi6/user-hn35HYMc07e601A7nEXk4LTH/img-rUCZgjuYyrQFBFYu9t3B50Sv.png?st=2024-12-16T16%3A47%3A06Z&se=2024-12-16T18%3A47%3A06Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-12-15T18%3A59%3A29Z&ske=2024-12-16T18%3A59%3A29Z&sks=b&skv=2024-08-04&sig=3Hsc/dEnthqamYgVjQZOgeRGtrpgubv0DqsSKDXZ20A%3D\n",
"4: https://oaidalleapiprodscus.blob.core.windows.net/private/org-jXMZj5kjVPIA0aF9jkikgCi6/user-hn35HYMc07e601A7nEXk4LTH/img-viX10MwSajTeNGUIdZD7rvJT.png?st=2024-12-16T16%3A47%3A18Z&se=2024-12-16T18%3A47%3A18Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-12-16T09%3A30%3A32Z&ske=2024-12-17T09%3A30%3A32Z&sks=b&skv=2024-08-04&sig=QE5xk%2ByiLXdPA%2BN14maQbiMO24qTyx/6KXT9dCvFecc%3D\n",
"5: https://oaidalleapiprodscus.blob.core.windows.net/private/org-jXMZj5kjVPIA0aF9jkikgCi6/user-hn35HYMc07e601A7nEXk4LTH/img-rgUOrZf1zeWXNb6BYLzLGbhW.png?st=2024-12-16T16%3A47%3A28Z&se=2024-12-16T18%3A47%3A28Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-12-16T11%3A56%3A01Z&ske=2024-12-17T11%3A56%3A01Z&sks=b&skv=2024-08-04&sig=1DsjbYAj2NVvYuCr/1quQR7VbI9GmasVvzUM6zAeCLg%3D\n",
"6: https://oaidalleapiprodscus.blob.core.windows.net/private/org-jXMZj5kjVPIA0aF9jkikgCi6/user-hn35HYMc07e601A7nEXk4LTH/img-TT2RbCJzSeF6u47NFp3LPHpq.png?st=2024-12-16T16%3A47%3A40Z&se=2024-12-16T18%3A47%3A40Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-12-16T03%3A02%3A45Z&ske=2024-12-17T03%3A02%3A45Z&sks=b&skv=2024-08-04&sig=iROMhO2DFmjnvRdSryJbiXNYAfwazYRqWxYACm7f%2Br0%3D\n",
"7: https://oaidalleapiprodscus.blob.core.windows.net/private/org-jXMZj5kjVPIA0aF9jkikgCi6/user-hn35HYMc07e601A7nEXk4LTH/img-kVLcPzJjl5LFTuqYzQE9jiM0.png?st=2024-12-16T16%3A47%3A51Z&se=2024-12-16T18%3A47%3A51Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-12-16T00%3A55%3A40Z&ske=2024-12-17T00%3A55%3A40Z&sks=b&skv=2024-08-04&sig=CNq01vE2oj0HwwI72DVarKuEj4vUJ3XnvK1SRY5gink%3D\n",
"8: https://oaidalleapiprodscus.blob.core.windows.net/private/org-jXMZj5kjVPIA0aF9jkikgCi6/user-hn35HYMc07e601A7nEXk4LTH/img-j4qAUx8olhDObgMG1pYYszzW.png?st=2024-12-16T16%3A48%3A03Z&se=2024-12-16T18%3A48%3A03Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-12-16T04%3A07%3A59Z&ske=2024-12-17T04%3A07%3A59Z&sks=b&skv=2024-08-04&sig=AXWgndLh/IyW1wKthvP1yz75cBTjAr4vm%2BMp%2BEsJ%2Brc%3D\n",
"9: https://oaidalleapiprodscus.blob.core.windows.net/private/org-jXMZj5kjVPIA0aF9jkikgCi6/user-hn35HYMc07e601A7nEXk4LTH/img-BNFt669Uq81MAarIDjvDJauY.png?st=2024-12-16T16%3A48%3A15Z&se=2024-12-16T18%3A48%3A15Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-12-16T00%3A48%3A16Z&ske=2024-12-17T00%3A48%3A16Z&sks=b&skv=2024-08-04&sig=6nPEvaM1Q4N1pg2gfU8kEtbbZi5n12HHnlN0X2gGEuY%3D\n",
"10: https://oaidalleapiprodscus.blob.core.windows.net/private/org-jXMZj5kjVPIA0aF9jkikgCi6/user-hn35HYMc07e601A7nEXk4LTH/img-KTSGlfPKZNIfum6ppsj8NpMH.png?st=2024-12-16T16%3A48%3A26Z&se=2024-12-16T18%3A48%3A26Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-12-15T23%3A05%3A05Z&ske=2024-12-16T23%3A05%3A05Z&sks=b&skv=2024-08-04&sig=JY0n4VFm410WSxEgN5K86pH8TO62tZ8AgdTtCYYWzbQ%3D\n"
]
}
],
"source": [
"#create test images\n",
"open_ai_key = \"XXX\"\n",
"os.environ[\"OPENAI_API_KEY\"] = open_ai_key\n",
"\n",
"prompt = (\n",
" \"A diverse set of household plumbing pipes with cracks, rust , and other imperfections \"\n",
" \"suitable for testing a machine learning model. The images should feature a variety of backgrounds, textures, \"\n",
" \"and compositions, with some objects appearing plastic, others metal, others cast-ion, and some PVC.\"\n",
")\n",
"\n",
"n_images = 10\n",
"\n",
"example_image_urls = []\n",
"for _ in range(n_images):\n",
" response = openai.images.generate(prompt=prompt, n=1, size=\"1024x1024\")\n",
" image_url = response.data[0].url \n",
" example_image_urls.append(image_url)\n",
"\n",
"print(\"Generated image URLs:\")\n",
"for idx, url in enumerate(example_image_urls, 1):\n",
" print(f\"{idx}: {url}\")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"def fetch_image(url):\n",
" response = requests.get(url)\n",
" if response.status_code == 200:\n",
" img = Image.open(BytesIO(response.content))\n",
" return img\n",
" else:\n",
" print(f\"Failed to fetch image from {url}\")\n",
" return None\n",
"\n",
"example_images = [fetch_image(url) for url in example_image_urls]\n",
"for example_image in example_images:\n",
" example_image.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/truth_set/updated_2024-12-11T07_30_28_069791.jpg\n"
]
}
],
"source": [
"#Get Test Image - Random From Truth Set Directory\n",
"\n",
"def get_random_query_image(truth_set_directory):\n",
" files = [\n",
" os.path.join(truth_set_directory, f)\n",
" for f in os.listdir(truth_set_directory)\n",
" if os.path.isfile(os.path.join(truth_set_directory, f))\n",
" ]\n",
" \n",
" if not files:\n",
" raise ValueError(f\"No files found in the directory: {truth_set_directory}\")\n",
" \n",
" query_image = random.choice(files)\n",
" return query_image\n",
"\n",
"truth_set_directory = \"truth_set\"\n",
"query_image_path = get_random_query_image(truth_set_directory)\n",
"print(query_image_path)\n",
"query_image = Image.open(query_image_path)\n",
"query_image.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"query_image_adj = np.array(query_image) \n",
"query_image_gray = cv2.cvtColor(query_image_adj, cv2.COLOR_BGR2GRAY)\n",
"orb = cv2.ORB_create()"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-12-16 10:55:43.939 Python[7835:165865] +[IMKClient subclass]: chose IMKClient_Modern\n",
"2024-12-16 10:55:43.939 Python[7835:165865] +[IMKInputSession subclass]: chose IMKInputSession_Modern\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[32], line 30\u001b[0m\n\u001b[1;32m 28\u001b[0m \u001b[38;5;66;03m# Display the result\u001b[39;00m\n\u001b[1;32m 29\u001b[0m cv2\u001b[38;5;241m.\u001b[39mimshow(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBest Match\u001b[39m\u001b[38;5;124m\"\u001b[39m, img_matches)\n\u001b[0;32m---> 30\u001b[0m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwaitKey\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 31\u001b[0m cv2\u001b[38;5;241m.\u001b[39mdestroyAllWindows()\n\u001b[1;32m 32\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"best_match_count = 0\n",
"best_example_image = None\n",
"best_kp1 = None\n",
"best_kp2 = None\n",
"best_good_matches = []\n",
"\n",
"for example_image in example_images:\n",
" example_image_adj = np.array(example_image) \n",
" example_image_gray = cv2.cvtColor(example_image_adj, cv2.COLOR_BGR2GRAY)\n",
" kp1, des1 = orb.detectAndCompute(query_image_gray, None)\n",
" kp2, des2 = orb.detectAndCompute(example_image_gray, None)\n",
" index_params = dict(algorithm=6, table_number=6, key_size=12, multi_probe_level=1)\n",
" search_params = dict(checks=50)\n",
" flann = cv2.FlannBasedMatcher(index_params, search_params)\n",
" matches = flann.knnMatch(des1, des2, k=2)\n",
" # Apply Lowe's ratio test to filter out weak matches\n",
" good_matches = []\n",
" for match_pair in matches:\n",
" # Ensure there are two matches to unpack\n",
" if len(match_pair) == 2:\n",
" m, n = match_pair\n",
" if m.distance < 0.7 * n.distance:\n",
" good_matches.append(m)\n",
"\n",
" # If we found a good match, draw it on the image\n",
" if len(good_matches) > 0:\n",
" img_matches = cv2.drawMatches(query_image_adj, kp1, example_image_adj, kp2, good_matches, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)\n",
" # Display the result\n",
" cv2.imshow(\"Best Match\", img_matches)\n",
" cv2.waitKey(0)\n",
" cv2.destroyAllWindows()\n",
" else:\n",
" print(\"No good match found.\")\n",
" \n",
"\n",
" "
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment