{"id":473,"date":"2014-01-23T12:23:12","date_gmt":"2014-01-23T17:23:12","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=473"},"modified":"2014-01-23T12:23:12","modified_gmt":"2014-01-23T17:23:12","slug":"opencv-python-webcam-example","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2014\/01\/23\/opencv-python-webcam-example\/","title":{"rendered":"OpenCV python webcam example"},"content":{"rendered":"<div id=\"file-gistfile1-txt-LC1\">#!\/Users\/brent\/.virtualenvs\/lumber\/bin\/python<\/div>\n<div id=\"file-gistfile1-txt-LC2\"><\/div>\n<div id=\"file-gistfile1-txt-LC3\"><\/div>\n<div id=\"file-gistfile1-txt-LC4\">import cv<\/div>\n<div id=\"file-gistfile1-txt-LC5\"><\/div>\n<div id=\"file-gistfile1-txt-LC6\">cv.NamedWindow(&#8220;w1&#8221;, cv.CV_WINDOW_AUTOSIZE)<\/div>\n<div id=\"file-gistfile1-txt-LC7\">camera_index = 0<\/div>\n<div id=\"file-gistfile1-txt-LC8\">capture = cv.CaptureFromCAM(camera_index)<\/div>\n<div id=\"file-gistfile1-txt-LC9\"><\/div>\n<div id=\"file-gistfile1-txt-LC10\">gx = gy = 1<\/div>\n<div id=\"file-gistfile1-txt-LC11\">grayscale = blur = canny = False<\/div>\n<div id=\"file-gistfile1-txt-LC12\"><\/div>\n<div id=\"file-gistfile1-txt-LC13\">def repeat():<\/div>\n<div id=\"file-gistfile1-txt-LC14\">global capture #declare as globals since we are assigning to them now<\/div>\n<div id=\"file-gistfile1-txt-LC15\">global camera_index<\/div>\n<div id=\"file-gistfile1-txt-LC16\">global gx, gy, grayscale, canny, blur<\/div>\n<div id=\"file-gistfile1-txt-LC17\">frame = cv.QueryFrame(capture)<\/div>\n<div id=\"file-gistfile1-txt-LC18\"># import pdb; pdb.set_trace()<\/div>\n<div id=\"file-gistfile1-txt-LC19\"><\/div>\n<div id=\"file-gistfile1-txt-LC20\">if grayscale:<\/div>\n<div id=\"file-gistfile1-txt-LC21\">gray = cv.CreateImage(cv.GetSize(frame), frame.depth, 1)<\/div>\n<div id=\"file-gistfile1-txt-LC22\">cv.CvtColor(frame, gray, cv.CV_RGB2GRAY)<\/div>\n<div id=\"file-gistfile1-txt-LC23\">frame = gray<\/div>\n<div id=\"file-gistfile1-txt-LC24\"><\/div>\n<div id=\"file-gistfile1-txt-LC25\">if blur:<\/div>\n<div id=\"file-gistfile1-txt-LC26\">g = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, frame.channels)<\/div>\n<div id=\"file-gistfile1-txt-LC27\">cv.Smooth(frame, g, cv.CV_GAUSSIAN, gx, gy)<\/div>\n<div id=\"file-gistfile1-txt-LC28\">frame = g<\/div>\n<div id=\"file-gistfile1-txt-LC29\"><\/div>\n<div id=\"file-gistfile1-txt-LC30\">if grayscale and canny:<\/div>\n<div id=\"file-gistfile1-txt-LC31\">c = cv.CreateImage(cv.GetSize(frame), frame.depth, frame.channels)<\/div>\n<div id=\"file-gistfile1-txt-LC32\">cv.Canny(frame, c, 10, 100, 3)<\/div>\n<div id=\"file-gistfile1-txt-LC33\">frame = c<\/div>\n<div id=\"file-gistfile1-txt-LC34\">cv.ShowImage(&#8220;w1&#8221;, frame)<\/div>\n<div id=\"file-gistfile1-txt-LC35\"><\/div>\n<div id=\"file-gistfile1-txt-LC36\">c = cv.WaitKey(10)<\/div>\n<div id=\"file-gistfile1-txt-LC37\">if c==ord(&#8216;=&#8217;): #in &#8220;n&#8221; key is pressed while the popup window is in focus<\/div>\n<div id=\"file-gistfile1-txt-LC38\">gx += 2<\/div>\n<div id=\"file-gistfile1-txt-LC39\">gy += 2<\/div>\n<div id=\"file-gistfile1-txt-LC40\">elif c == ord(&#8216;-&#8216;):<\/div>\n<div id=\"file-gistfile1-txt-LC41\">gx = max(1, gx-2)<\/div>\n<div id=\"file-gistfile1-txt-LC42\">gy = max(1, gy-2)<\/div>\n<div id=\"file-gistfile1-txt-LC43\">elif c == ord(&#8216;x&#8217;):<\/div>\n<div id=\"file-gistfile1-txt-LC44\">gx += 2<\/div>\n<div id=\"file-gistfile1-txt-LC45\">elif c == ord(&#8216;X&#8217;):<\/div>\n<div id=\"file-gistfile1-txt-LC46\">gx = max(1, gx-2)<\/div>\n<div id=\"file-gistfile1-txt-LC47\">elif c == ord(&#8216;q&#8217;):<\/div>\n<div id=\"file-gistfile1-txt-LC48\">exit(0)<\/div>\n<div id=\"file-gistfile1-txt-LC49\"><\/div>\n<div id=\"file-gistfile1-txt-LC50\">elif c == ord(&#8216;b&#8217;):<\/div>\n<div id=\"file-gistfile1-txt-LC51\">blur = not blur<\/div>\n<div id=\"file-gistfile1-txt-LC52\">elif c == ord(&#8216;g&#8217;):<\/div>\n<div id=\"file-gistfile1-txt-LC53\">grayscale = not grayscale<\/div>\n<div id=\"file-gistfile1-txt-LC54\">elif c == ord(&#8216;c&#8217;):<\/div>\n<div id=\"file-gistfile1-txt-LC55\">canny = not canny<\/div>\n<div id=\"file-gistfile1-txt-LC56\"><\/div>\n<div id=\"file-gistfile1-txt-LC57\"><\/div>\n<div id=\"file-gistfile1-txt-LC58\"><\/div>\n<div id=\"file-gistfile1-txt-LC59\"><\/div>\n<div id=\"file-gistfile1-txt-LC60\">while True:<\/div>\n<div id=\"file-gistfile1-txt-LC61\">repeat()<\/div>\n","protected":false},"excerpt":{"rendered":"<p>#!\/Users\/brent\/.virtualenvs\/lumber\/bin\/python import cv cv.NamedWindow(&#8220;w1&#8221;, cv.CV_WINDOW_AUTOSIZE) camera_index = 0 capture = cv.CaptureFromCAM(camera_index) gx = gy = 1 grayscale = blur = canny = False def repeat():&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-473","post","type-post","status-publish","format-standard","hentry","category-computer-vision"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=473"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/473\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}