Created
November 22, 2022 03:51
-
-
Save EvanBurnette/11f8516db17d376896451923566575d6 to your computer and use it in GitHub Desktop.
Find green box on Wyze image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "1b86a996", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from PIL import Image, ImageFilter, ImageDraw" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "ad271ccb", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#is there a green box in our image?\n", | |
"im = Image.open('greenBox.webp')\n", | |
"im" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "5c99b1df", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# detect a certain color by setting a distance threshold i.e. pythagorean thereom\n", | |
"def distance(rgb1, rgb2):\n", | |
" channels = len(rgb1)\n", | |
" distSquared = 0\n", | |
" if channels != len(rgb2):\n", | |
" raise Exception(\"color channel mismatch\", rgb1, rgb2)\n", | |
" for ch in range(channels):\n", | |
" distSquared += (rgb1[ch]-rgb2[ch])**2\n", | |
" distance = distSquared ** 0.5\n", | |
" return distance" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "d2f51a6b", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#double check distance function with 3,4,5 triangle\n", | |
"distance((3,4),(0,0))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "e2ed2b24", | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"im2 = Image.new('RGB', size=im.size)\n", | |
"# I used gimp to identify the color in 100s so we need to convert to 0-255 (8 bit))\n", | |
"targetColor = (int(32*2.55), int(93*2.55), int(74*2.55))\n", | |
"\n", | |
"for w in range(im.size[0]):\n", | |
" for h in range(im.size[1]):\n", | |
" if distance(im.getpixel((w, h)), targetColor) < 50:\n", | |
" im2.putpixel((w,h), (0,255,0))\n", | |
"im2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "762c7dee", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"line_size = 2\n", | |
"def findTopLeftCorner(image, ):\n", | |
" h1 = -100\n", | |
" w1 = -100\n", | |
" h2 = -1\n", | |
" w2 = -1\n", | |
" \n", | |
" found_h1 = False\n", | |
" for h in range(image.size[1]):\n", | |
" if found_h1:\n", | |
" break\n", | |
" for w in range(image.size[0]):\n", | |
" if image.getpixel((w,h)) != (0,0,0):\n", | |
" h1 = h\n", | |
" found_h1 = True\n", | |
" break\n", | |
" x = 0\n", | |
" y = 1\n", | |
" found_w1 = False\n", | |
" for w in range(image.size[0]):\n", | |
" if found_w1:\n", | |
" break\n", | |
" for h in range(image.size[1]):\n", | |
" if image.getpixel((w,h)) != (0,0,0):\n", | |
" w1 = w\n", | |
" found_w1 = True\n", | |
" break\n", | |
" \n", | |
" if w1 < 0 or h1 < 0:\n", | |
" return (w1,h1)\n", | |
" \n", | |
" w2 = 3 + w1\n", | |
" h2 = 3 + h1\n", | |
" \n", | |
" while (image.getpixel((w2,h2)) != (0,0,0)):\n", | |
" h2 += 1\n", | |
" w2 += 1\n", | |
" \n", | |
" line_size = abs(int((w1-w2)/2))\n", | |
" return (\n", | |
" int((w1+w2)/2),\n", | |
" int((h1+h2)/2)\n", | |
" )\n", | |
"\n", | |
"topLeftCorner = findTopLeftCorner(im2)\n", | |
"topLeftCorner" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "0564a924", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"corners = [topLeftCorner, (-100, -100),(-100, -100),(-100, -100)]\n", | |
"penPoint = topLeftCorner\n", | |
"\n", | |
"# Trace the outline of the box starting in the top left corner\n", | |
"while penPoint[0] < im.size[0] and im2.getpixel(penPoint) != (0,0,0):\n", | |
" penPoint = (penPoint[0] + 1, penPoint[1])\n", | |
"\n", | |
"penPoint = (penPoint[0] - line_size, penPoint[1])\n", | |
"corners[1] = penPoint\n", | |
"\n", | |
"while penPoint[1] < im.size[1] and im2.getpixel(penPoint) != (0,0,0):\n", | |
" penPoint = (penPoint[0], penPoint[1] + 1)\n", | |
"\n", | |
"penPoint = (penPoint[0], penPoint[1] - line_size)\n", | |
"corners[2] = penPoint\n", | |
"\n", | |
"while penPoint[0] > 0 and im2.getpixel(penPoint) != (0,0,0):\n", | |
" penPoint = (penPoint[0] - 1, penPoint[1])\n", | |
"\n", | |
"penPoint = (penPoint[0] + line_size, penPoint[1])\n", | |
"corners[3] = penPoint\n", | |
"\n", | |
"corners" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "c4650d56", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"draw = ImageDraw.Draw(im)\n", | |
"draw.rectangle((corners[0], corners[2]))\n", | |
"im" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "34c08c7a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"corners[0][1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "ff1597f7", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def isRectangle(corners):\n", | |
" for point in corners:\n", | |
" if point[0] < 0 or point[1] < 0:\n", | |
" return False\n", | |
" deviation = abs(corners[0][1] - corners[1][1])\n", | |
" deviation += abs(corners[0][0] - corners[3][0])\n", | |
" deviation += abs(corners[1][0] - corners[2][0])\n", | |
" deviation += abs(corners[2][1] - corners[3][1])\n", | |
" return deviation < line_size * 2\n", | |
"\n", | |
"print(isRectangle(corners))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "842837c2", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"im2.filter(ImageFilter.FIND_EDGES)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "76271b54", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment