Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
432 changes: 409 additions & 23 deletions .ipynb_checkpoints/Week_1-checkpoint.ipynb

Large diffs are not rendered by default.

345 changes: 345 additions & 0 deletions .ipynb_checkpoints/Week_11-checkpoint.ipynb

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions .ipynb_checkpoints/Week_11_2-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}
200 changes: 200 additions & 0 deletions .ipynb_checkpoints/Week_12-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise\n",
"### Part 1\n",
"1. Take a picture of an apple and put it in the image folder in the notebook environment\n",
"2. Find the contour of the apple\n",
"3. Draw a yellow circle with the center placed on the center of the apple contour\n",
"4. Draw a square that precisely holds the apple\n",
"5. Take a picture of three seperate apples.\n",
"6. Use contours to programmatically identify the number of apples in the photo"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# 1. Take a picture of an apple and put it in the image folder in the notebook environment\n",
"# DONE\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# 2. Find the contour of the apple\n",
"import cv2\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"\n",
"img = cv2.imread('../images/apple.jpg')\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cv2.inRange(img,lower,higher)<class 'numpy.ndarray'> Image shape: (720, 1080, 3) and mask shape: (720, 1080)\n",
" Image contains: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17\n",
" 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35\n",
" 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53\n",
" 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71\n",
" 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89\n",
" 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107\n",
" 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125\n",
" 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143\n",
" 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161\n",
" 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179\n",
" 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197\n",
" 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215\n",
" 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233\n",
" 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251\n",
" 252 253 254 255]\n",
" mask contains: [0] which is good cause object to be found should be white and background should be black.\n"
]
},
{
"data": {
"text/plain": [
"array([[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def create_apple_mask(image):\n",
" \"\"\"From an image create a HSV mask by identifying lower and upper bounds of the three values\"\"\"\n",
" # Color values in HSV\n",
" green_lower = (0, 100, 50) # hsv (0-360, 0-255, 0-255) sometimes written as (0-360, 100%, 100%)\n",
" green_upper = (0, 71, 57)\n",
"\n",
" hsv_img = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)\n",
" \n",
" mask = cv2.inRange(hsv_img, green_lower, green_upper) # inRange: exclude all pixels outside of range\n",
" mask = cv2.dilate(mask, None, iterations=2) # dilate: (like pupils) expand pixels around the center\n",
" mask = cv2.erode(mask, None, iterations=2) # erode: opposite of dilate. remove pixels from outer layer\n",
" print(f'cv2.inRange(img,lower,higher){type(mask)} Image shape: {image.shape} and mask shape: {mask.shape}\\n Image contains: {np.unique(image)}\\n mask contains: {np.unique(mask)} which is good cause object to be found should be white and background should be black.')\n",
" \n",
" return mask\n",
"my_mask = create_apple_mask(img)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def mark_object(image, mask):\n",
" \"\"\"find the largest contour in the mask, then use it to compute the minimum enclosing circle and centroid\"\"\"\n",
" # Finds contours in a binary image. The contours are a useful tool for shape analysis and object detection and recognition.\n",
" contours = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]\n",
"\n",
" c = max(contours, key=cv2.contourArea)\n",
" ((x, y), radius) = cv2.minEnclosingCircle(c)\n",
"\n",
" # draw the circle and centroid on the frame,\n",
" # then update the list of tracked points\n",
" cv2.circle(image, center=(int(x), int(y)), radius=int(radius), color=(255, 0, 0), thickness=3)\n",
" return image\n",
"mark_object(img, )"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cv2.inRange(img,lower,higher)<class 'numpy.ndarray'> Image shape: (720, 1080, 3) and mask shape: (720, 1080)\n",
" Image contains: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17\n",
" 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35\n",
" 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53\n",
" 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71\n",
" 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89\n",
" 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107\n",
" 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125\n",
" 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143\n",
" 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161\n",
" 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179\n",
" 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197\n",
" 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215\n",
" 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233\n",
" 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251\n",
" 252 253 254 255]\n",
" mask contains: [0] which is good cause object to be found should be white and background should be black.\n"
]
},
{
"ename": "ValueError",
"evalue": "max() arg is an empty sequence",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-16-31511dae7576>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmask\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcreate_apple_mask\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mimg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmark_object\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmask\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mimg_converted\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcvtColor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCOLOR_RGB2BGR\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m#cv2.imwrite('./images/week12apple.jpg', img_converted)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m#cv.show()\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-14-a7b900fb2a65>\u001b[0m in \u001b[0;36mmark_object\u001b[0;34m(image, mask)\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mcontours\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfindContours\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmask\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mRETR_EXTERNAL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCHAIN_APPROX_SIMPLE\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcontours\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcontourArea\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mradius\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mminEnclosingCircle\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: max() arg is an empty sequence"
]
}
],
"source": [
"mask = create_apple_mask(img)\n",
"img = mark_object(img, mask)\n",
"img_converted = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n",
"#cv2.imwrite('./images/week12apple.jpg', img_converted)\n",
"#cv.show()"
]
}
],
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
97 changes: 72 additions & 25 deletions .ipynb_checkpoints/Week_2-checkpoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,119 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Week 2 handin"
"# 1. Create a python file with 3 functions:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Assignment\n",
"Here is the text for the assignment linked to"
" 1.`def print_file_content(file)` that can print content of a csv file to the console\n",
" 2. `def write_list_to_file(output_file, lst)` that can take a list of tuple and write each element to a new line in file\n",
" 1. rewrite the function so that it gets an arbitrary number of strings instead of a list\n",
" 3. `def read_csv(input_file)` that take a csv file and read each row into a list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Status\n",
"Here is a status on the handin. How far you got. What is implemented and what is not"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution part 1"
"# ex. 1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 52,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Mælkevejen', ' er en flyvende']\n",
"['glatåljens', 'lars']\n",
"['ko', 'ost']\n"
]
}
],
"source": [
"# 1.1 some code here with docstrings"
"import csv\n",
"\n",
"test_file = \"./ost.csv\"\n",
"\n",
"def print_file_content(file_name):\n",
" with open(file_name, 'r') as file:\n",
" reader = csv.reader(file)\n",
" for row in reader:\n",
" print(row)\n",
" \n",
"print_file_content(test_file)"
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# 1.2 some code here with docstrings"
"# ex. 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 50,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('jens', 'lars')\n",
"('ko', 'ost')\n"
]
}
],
"source": [
"# 1.3"
"import csv\n",
"\n",
"test_file = \"./ost.csv\"\n",
"\n",
"random_list = [(\"jens\", \"lars\"), (\"ko\", \"ost\")]\n",
"\n",
"def write_list_to_file(output_file, lst):\n",
" with open(output_file, 'a') as file:\n",
" write = csv.writer(file)\n",
" for element in lst:\n",
" write.writerow(element)\n",
" print(element)\n",
" \n",
"write_list_to_file(test_file, random_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution part 2"
"## ex. 2 a\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": []
"source": [
"import csv\n",
"\n",
"test_file = \"./ost.csv\"\n",
"\n",
"random_list = [(\"jens\", \"lars\"), (\"ko\", \"ost\")]\n",
"\n",
"def write_arb_to_file(output_file, *strings):\n",
" with open(output_file, 'a') as file:\n",
" write = csv.writer(file)\n",
" write.writerow(*strings)\n",
" \n",
"write_arb_to_file(test_file, \"pat\", \"sovs\", \"osteklokke\")"
]
},
{
"cell_type": "code",
Expand Down
Loading