|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "Placeholder for development and debugging" |
| 7 | + "Placeholder for development and debugging\n", |
| 8 | + "\n", |
| 9 | + "User story -> we provide a file_path, if valid file the image is processed and the result of processing are the attributes of the entity" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 1, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import imghdr\n", |
| 19 | + "import requests\n", |
| 20 | + "from PIL import Image\n", |
| 21 | + "import matplotlib.pyplot as plt\n", |
| 22 | + "%matplotlib inline" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 2, |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "## Const\n", |
| 32 | + "HTTP_OK = 200\n", |
| 33 | + "HTTP_BAD_REQUEST = 400\n", |
| 34 | + "HTTP_UNAUTHORIZED = 401\n", |
| 35 | + "\n", |
| 36 | + "## Configurable\n", |
| 37 | + "IP_ADDRESS = 'localhost'\n", |
| 38 | + "PORT = '5000'" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "execution_count": 3, |
| 44 | + "metadata": {}, |
| 45 | + "outputs": [], |
| 46 | + "source": [ |
| 47 | + "def get_matched_faces(predictions : dict):\n", |
| 48 | + " \"\"\"\n", |
| 49 | + " Get the predicted faces and their confidence.\n", |
| 50 | + " \"\"\"\n", |
| 51 | + " try:\n", |
| 52 | + " matched_faces = {face['userid']: round(face['confidence']*100, 1) \n", |
| 53 | + " for face in predictions if not face['userid'] == 'unknown'}\n", |
| 54 | + " return matched_faces\n", |
| 55 | + " except:\n", |
| 56 | + " return {}" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": 4, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "def is_valid_image(file_path : str):\n", |
| 66 | + " \"\"\"\n", |
| 67 | + " Check file_path is valid image.\n", |
| 68 | + " \"\"\"\n", |
| 69 | + " try:\n", |
| 70 | + " image_extension = imghdr.what(file_path)\n", |
| 71 | + " if image_extension in ['jpeg', '.jpg', '.png']:\n", |
| 72 | + " return True\n", |
| 73 | + " return False\n", |
| 74 | + " except Exception as exc:\n", |
| 75 | + " print(exc)\n", |
| 76 | + " return False" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": 5, |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [], |
| 84 | + "source": [ |
| 85 | + "def post_image(url : str, image : bytes):\n", |
| 86 | + " \"\"\"Post an image to the classifier.\"\"\"\n", |
| 87 | + " try:\n", |
| 88 | + " response = requests.post(\n", |
| 89 | + " url,\n", |
| 90 | + " files={\"image\": image},\n", |
| 91 | + " )\n", |
| 92 | + " return response\n", |
| 93 | + " except requests.exceptions.ConnectionError:\n", |
| 94 | + " print(\"ConnectionError: Is %s running?\", CLASSIFIER)\n", |
| 95 | + " return None" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "markdown", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "Create classes" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": 6, |
| 108 | + "metadata": {}, |
| 109 | + "outputs": [], |
| 110 | + "source": [ |
| 111 | + "class DeepstackFace():\n", |
| 112 | + " \"\"\"Work with faces.\"\"\"\n", |
| 113 | + "\n", |
| 114 | + " def __init__(self, ip_address, port):\n", |
| 115 | + "\n", |
| 116 | + " self._url_check = \"http://{}:{}/v1/vision/face/recognize\".format(\n", |
| 117 | + " ip_address, port)\n", |
| 118 | + " self._faces = None\n", |
| 119 | + " self._matched = {}\n", |
| 120 | + "\n", |
| 121 | + " def process_image_bytes(self, image_bytes):\n", |
| 122 | + " \"\"\"Process an image.\"\"\"\n", |
| 123 | + " response = post_image(\n", |
| 124 | + " self._url_check, image_bytes)\n", |
| 125 | + " if response:\n", |
| 126 | + " if response.status_code == HTTP_OK:\n", |
| 127 | + " predictions_json = response.json()[\"predictions\"]\n", |
| 128 | + " self._faces = len(predictions_json)\n", |
| 129 | + " self._matched = get_matched_faces(predictions_json)\n", |
| 130 | + "\n", |
| 131 | + " else:\n", |
| 132 | + " self._faces = None\n", |
| 133 | + " self._matched = {}\n", |
| 134 | + "\n", |
| 135 | + " @property\n", |
| 136 | + " def attributes(self):\n", |
| 137 | + " \"\"\"Return the classifier attributes.\"\"\"\n", |
| 138 | + " return {\n", |
| 139 | + " 'faces': self._faces,\n", |
| 140 | + " 'matched_faces': self._matched,\n", |
| 141 | + " 'total_matched_faces': len(self._matched),\n", |
| 142 | + " }" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "markdown", |
| 147 | + "metadata": {}, |
| 148 | + "source": [ |
| 149 | + "## Usage" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "code", |
| 154 | + "execution_count": 7, |
| 155 | + "metadata": {}, |
| 156 | + "outputs": [], |
| 157 | + "source": [ |
| 158 | + "dsface = DeepstackFace(IP_ADDRESS, PORT)" |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": 8, |
| 164 | + "metadata": {}, |
| 165 | + "outputs": [ |
| 166 | + { |
| 167 | + "data": { |
| 168 | + "text/plain": [ |
| 169 | + "{'faces': None, 'matched_faces': {}, 'total_matched_faces': 0}" |
| 170 | + ] |
| 171 | + }, |
| 172 | + "execution_count": 8, |
| 173 | + "metadata": {}, |
| 174 | + "output_type": "execute_result" |
| 175 | + } |
| 176 | + ], |
| 177 | + "source": [ |
| 178 | + "dsface.attributes" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "code", |
| 183 | + "execution_count": 9, |
| 184 | + "metadata": {}, |
| 185 | + "outputs": [], |
| 186 | + "source": [ |
| 187 | + "image_path = 'test-image2.jpg'\n", |
| 188 | + "\n", |
| 189 | + "with open(image_path, \"rb\") as image_bytes:\n", |
| 190 | + " dsface.process_image_bytes(image_bytes)" |
| 191 | + ] |
| 192 | + }, |
| 193 | + { |
| 194 | + "cell_type": "code", |
| 195 | + "execution_count": 10, |
| 196 | + "metadata": {}, |
| 197 | + "outputs": [ |
| 198 | + { |
| 199 | + "data": { |
| 200 | + "text/plain": [ |
| 201 | + "{'faces': 2, 'matched_faces': {}, 'total_matched_faces': 0}" |
| 202 | + ] |
| 203 | + }, |
| 204 | + "execution_count": 10, |
| 205 | + "metadata": {}, |
| 206 | + "output_type": "execute_result" |
| 207 | + } |
| 208 | + ], |
| 209 | + "source": [ |
| 210 | + "dsface.attributes" |
8 | 211 | ]
|
9 | 212 | },
|
10 | 213 | {
|
|
0 commit comments