{"id":444,"date":"2015-10-06T19:05:51","date_gmt":"2015-10-06T19:05:51","guid":{"rendered":"http:\/\/www.alexander-miles.com\/?p=444"},"modified":"2016-05-08T16:26:53","modified_gmt":"2016-05-08T16:26:53","slug":"imaging-programatically-recognizing-art","status":"publish","type":"post","link":"http:\/\/www.alexander-miles.com\/?p=444","title":{"rendered":"Recognizing Cards - First Attempts"},"content":{"rendered":"<p>Sorting images has been a problem on my mind for years, but I never had a really good reason to sink time into it. Just recently, I finally found a reason. It occurred to me that it would be useful to have my webcam recognize magic cards by their art, and add them to a database for collection tracking. I've since learned that this wasn't as new an idea as I'd thought, and several complete software packages for this specific application have become available in the last 6 months. Nevertheless, it was an interesting excursion into image processing, admittedly not my home turf. It worked much better than planned too.<\/p>\n<p>To my mind, the problem had three main tasks that I'd not undertaken before, in order of drastically increasing difficulty:<\/p>\n<ol>\n<li>Grabbing camera input from Python<\/li>\n<li>Isolating the card from the background<\/li>\n<li>Usefully comparing images<\/li>\n<\/ol>\n<p>The first part was mostly just an exercise in googling it and adapting the code to my webcam. Short version, use OpenCV2's VideoCapture method, and be sure to chuck out a few frames while the camera is auto-focusing and tuning the white balance. The second bit proved to be slightly more interesting. Given a photo of a card on a plain white background, canny edge detection can reliably pull out the edge (though I haven't tried this with white-boarded cards yet). The card-background contour is easily picked it; it has the largest area. Once we've cropped the image down to that, we can isolate the art by knowing the ratios used to layout the cards. This process is shown below.<\/p>\n<figure id=\"attachment_448\" aria-describedby=\"caption-attachment-448\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/input.png\"><img data-attachment-id=\"448\" data-permalink=\"http:\/\/www.alexander-miles.com\/?attachment_id=448\" data-orig-file=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/input.png?fit=1280%2C960\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Original image\" data-image-description=\"&lt;p&gt;Original image&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Original image&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/input.png?fit=300%2C225\" data-large-file=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/input.png?fit=660%2C495\" decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-448\" src=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/input.png?resize=300%2C225\" alt=\"Original image\" width=\"300\" height=\"225\" srcset=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/input.png?resize=300%2C225 300w, https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/input.png?resize=1024%2C768 1024w, https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/input.png?w=1280 1280w\" sizes=\"(max-width: 300px) 100vw, 300px\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-448\" class=\"wp-caption-text\">Original image<\/figcaption><\/figure>\n<figure id=\"attachment_447\" aria-describedby=\"caption-attachment-447\" style=\"width: 220px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/contours.png\"><img data-attachment-id=\"447\" data-permalink=\"http:\/\/www.alexander-miles.com\/?attachment_id=447\" data-orig-file=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/contours.png?fit=330%2C449\" data-orig-size=\"330,449\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Contours\" data-image-description=\"&lt;p&gt;Contours&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Contours&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/contours.png?fit=220%2C300\" data-large-file=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/contours.png?fit=330%2C449\" decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-447\" src=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/contours.png?resize=220%2C300\" alt=\"Contours\" width=\"220\" height=\"300\" srcset=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/contours.png?resize=220%2C300 220w, https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/contours.png?w=330 330w\" sizes=\"(max-width: 220px) 100vw, 220px\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-447\" class=\"wp-caption-text\">Contours<\/figcaption><\/figure>\n<figure id=\"attachment_446\" aria-describedby=\"caption-attachment-446\" style=\"width: 221px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/max_area.png\"><img data-attachment-id=\"446\" data-permalink=\"http:\/\/www.alexander-miles.com\/?attachment_id=446\" data-orig-file=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/max_area.png?fit=363%2C492\" data-orig-size=\"363,492\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Isolated card\" data-image-description=\"&lt;p&gt;Isolated card&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Isolated card&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/max_area.png?fit=221%2C300\" data-large-file=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/max_area.png?fit=363%2C492\" decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-446\" src=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/max_area.png?resize=221%2C300\" alt=\"Isolated card\" width=\"221\" height=\"300\" srcset=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/max_area.png?resize=221%2C300 221w, https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/max_area.png?w=363 363w\" sizes=\"(max-width: 221px) 100vw, 221px\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-446\" class=\"wp-caption-text\">Isolated card<\/figcaption><\/figure>\n<figure id=\"attachment_449\" aria-describedby=\"caption-attachment-449\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/just_art.png\"><img data-attachment-id=\"449\" data-permalink=\"http:\/\/www.alexander-miles.com\/?attachment_id=449\" data-orig-file=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/just_art.png?fit=352%2C254\" data-orig-size=\"352,254\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Isolated art\" data-image-description=\"&lt;p&gt;Isolated art&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Isolated art&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/just_art.png?fit=300%2C216\" data-large-file=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/just_art.png?fit=352%2C254\" decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-449\" src=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/just_art.png?resize=300%2C216\" alt=\"Isolated art\" width=\"300\" height=\"216\" srcset=\"https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/just_art.png?resize=300%2C216 300w, https:\/\/i0.wp.com\/www.alexander-miles.com\/wp-content\/uploads\/just_art.png?w=352 352w\" sizes=\"(max-width: 300px) 100vw, 300px\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-449\" class=\"wp-caption-text\">Isolated art<\/figcaption><\/figure>\n<p>The third and most difficult step, effectively comparing the images, will have to wait until I've got more time to write. Soon!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sorting images has been a problem on my mind for years, but I never had a really good reason to sink time into it. Just recently, I finally found a reason. It occurred to me that it would be useful to have my webcam recognize magic cards by their art, and add them to a &hellip; <a href=\"http:\/\/www.alexander-miles.com\/?p=444\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Recognizing Cards - First Attempts<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","footnotes":""},"categories":[1],"tags":[67,11,22],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3y4AW-7a","_links":{"self":[{"href":"http:\/\/www.alexander-miles.com\/index.php?rest_route=\/wp\/v2\/posts\/444"}],"collection":[{"href":"http:\/\/www.alexander-miles.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.alexander-miles.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.alexander-miles.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.alexander-miles.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=444"}],"version-history":[{"count":3,"href":"http:\/\/www.alexander-miles.com\/index.php?rest_route=\/wp\/v2\/posts\/444\/revisions"}],"predecessor-version":[{"id":529,"href":"http:\/\/www.alexander-miles.com\/index.php?rest_route=\/wp\/v2\/posts\/444\/revisions\/529"}],"wp:attachment":[{"href":"http:\/\/www.alexander-miles.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.alexander-miles.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=444"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.alexander-miles.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}