{"id":6983,"date":"2019-06-28T22:47:02","date_gmt":"2019-06-28T13:47:02","guid":{"rendered":"https:\/\/itport.cloud\/?p=6983"},"modified":"2019-06-28T22:47:03","modified_gmt":"2019-06-28T13:47:03","slug":"post-6983","status":"publish","type":"post","link":"https:\/\/itport.cloud\/?p=6983","title":{"rendered":"(\u7b2c4\u56de)Python + OpenCV \u3067\u904a\u3093\u3067\u307f\u308b(\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u9854\u691c\u77e5\u7de8)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"e79baee6aca1-1\">\u76ee\u6b21<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"#opc4-1\">\u306f\u3058\u3081\u306b<\/a><\/li><li><a href=\"#opc4-2\">\u52d5\u753b\u306b\u3064\u3044\u3066<\/a><\/li><li><a href=\"#opc4-3\">\u52d5\u753b\u51e6\u7406<\/a><\/li><li><a href=\"#opc4-4\">\u304a\u308f\u308a\u306b<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"opc4-1\">\u306f\u3058\u3081\u306b<\/h2>\n\n\n\n<p>\u524d\u56de\u306f\u3001\u9759\u6b62\u753b\u306b\u304a\u3044\u3066\u9854\u691c\u51fa\u5668\u3092\u4f7f\u3044\u3001\u9854\u3092\u8a8d\u8b58\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3057\u305f\u3002<br>\u4eca\u5ea6\u306f\u52d5\u753b\u3067\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u306b\u9854\u691c\u51fa\u3092\u3055\u305b\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"opc4-2\">\u52d5\u753b\u306b\u3064\u3044\u3066<\/h2>\n\n\n\n<p>\u307e\u305a\u52d5\u753b\u306f\u3001\u9759\u6b62\u753b\u3092\u30d1\u30e9\u30d1\u30e9\u30a2\u30cb\u30e1\u306e\u3088\u3046\u306b\u9ad8\u901f\u3067\u6620\u3057\u51fa\u3059\u3053\u3068\u3067\u30b9\u30e0\u30fc\u30ba\u306b\u52d5\u3044\u3066\u3044\u308b\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002<br><br> \u305d\u306e1\u30b3\u30de\u306e\u9759\u6b62\u753b\u3092\u30d5\u30ec\u30fc\u30e0\u3068\u3044\u3044\u30011\u79d2\u9593\u3042\u305f\u308a\u306b\u8868\u793a\u3055\u308c\u308b\u30d5\u30ec\u30fc\u30e0\u306e\u6570\u306b\u3088\u3063\u3066\u8868\u793a\u901f\u5ea6\uff08\u52d5\u304d\u306e\u306a\u3081\u3089\u304b\u3055\uff09\u3092\u8868\u3057\u307e\u3059\u3002<br>\u901a\u5e38\u3001\u30c6\u30ec\u30d3\u7b49\u306f30fps(frame per second:1\u79d2\u9593\u306b30\u30b3\u30de)\u3067\u8868\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002 <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"opc4-3\">\u52d5\u753b\u51e6\u7406<\/h2>\n\n\n\n<p>\u52d5\u753b\u51e6\u7406\u306f\u30011\u30d5\u30ec\u30fc\u30e0\u3054\u3068\u306b\u8aad\u8fbc\u307f\u3001\u753b\u50cf\u51e6\u7406\u3092\u3057\u3066\u63cf\u753b\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002 <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport cv2\n\n#\u52d5\u753b\u3092\u8aad\u8fbc\u307f\n#\u30ab\u30e1\u30e9\u7b49\u3067\u30b9\u30c8\u30ea\u30fc\u30e0\u518d\u751f\u306e\u5834\u5408\u306f\u5f15\u6570\u306b0\u7b49\u306e\u30c7\u30d0\u30a4\u30b9ID\u3092\u8a18\u8ff0\u3059\u308b\nvideo = cv2.VideoCapture('inputVideo.avi')\n\nwhile video.isOpened():\n    #\u30d5\u30ec\u30fc\u30e0\u3092\u8aad\u8fbc\u307f\n    ret, frame = video.read()\n\n    #\u30d5\u30ec\u30fc\u30e0\u304c\u8aad\u307f\u8fbc\u3081\u306a\u304b\u3063\u305f\u5834\u5408\u306f\u7d42\u4e86\uff08\u52d5\u753b\u304c\u7d42\u308f\u308b\u3068\u8aad\u307f\u8fbc\u3081\u306a\u304f\u306a\u308b\uff09\n    if not ret: break\n\n    #-----------------\n    # \u753b\u50cf\u51e6\u7406\u3092\u8a18\u8ff0\u3059\u308b\n    #-----------------\n\n    #\u30d5\u30ec\u30fc\u30e0\u306e\u63cf\u753b\n    cv2.imshow('frame', frame)\n\n    #q\u30ad\u30fc\u306e\u62bc\u4e0b\u3067\u51e6\u7406\u3092\u4e2d\u6b62\n    key = cv2.waitKey(1) & 0xFF\n    if key == ord('q'): break\n\n#\u30e1\u30e2\u30ea\u306e\u89e3\u653e\nvideo.release()\ncv2.destroyAllWindows()\n<\/pre><\/div>\n\n\n<p>\u3053\u3053\u306b\u524d\u56de\u3001\u4f7f\u7528\u3057\u305f\u9854\u691c\u77e5\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<br><br>\u52d5\u753b\u306e\u5834\u5408\u306f\u3001\u9ad8\u901f\u306a\u51e6\u7406\u304c\u8981\u6c42\u3055\u308c\u308b\u305f\u3081\u3001\u30ab\u30b9\u30b1\u30fc\u30c9\u578b\u306e\u8b58\u5225\u5668\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002<br><br>\u4eca\u56de\u306f\u9854\u3092\u81ea\u52d5\u7684\u306b\u30e2\u30b6\u30a4\u30af\u51e6\u7406\u3059\u308b\u51e6\u7406\u3082\u8ffd\u52a0\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; highlight: [32]; title: ; notranslate\" title=\"\">\nimport cv2\n\n#\u52d5\u753b\u3092\u8aad\u8fbc\u307f\n#\u30ab\u30e1\u30e9\u7b49\u3067\u30b9\u30c8\u30ea\u30fc\u30e0\u518d\u751f\u306e\u5834\u5408\u306f\u5f15\u6570\u306b0\u7b49\u306e\u30c7\u30d0\u30a4\u30b9ID\u3092\u8a18\u8ff0\u3059\u308b\nvideo = cv2.VideoCapture(0)\n\ncascade_path = \"haarcascade_frontalface_default.xml\"\ncascade = cv2.CascadeClassifier(cascade_path)\n\nwhile video.isOpened():\n    # \u30d5\u30ec\u30fc\u30e0\u3092\u8aad\u8fbc\u307f\n    ret, frame = video.read()\n\n    # \u30d5\u30ec\u30fc\u30e0\u304c\u8aad\u307f\u8fbc\u3081\u306a\u304b\u3063\u305f\u5834\u5408\u306f\u7d42\u4e86\uff08\u52d5\u753b\u304c\u7d42\u308f\u308b\u3068\u8aad\u307f\u8fbc\u3081\u306a\u304f\u306a\u308b\uff09\n    if not ret: break\n    # \u9854\u691c\u51fa\n    facerect = cascade.detectMultiScale(frame, scaleFactor=1.1, minNeighbors=2, minSize=(30, 30))\n    \n    # \u77e9\u5f62\u7dda\u306e\u8272\n    rectangle_color = (0, 255, 0) #\u7dd1\u8272\n\n    # \u9854\u3092\u691c\u51fa\u3057\u305f\u5834\u5408\n    if len(facerect) > 0:\n        for rect in facerect:\n            cv2.rectangle(frame, tuple(rect&#x5B;0:2]),tuple(rect&#x5B;0:2] + rect&#x5B;2:4]), rectangle_color, thickness=2)\n            # \u9854\u90e8\u5206\u306e\u62bd\u51fa\n            cut_frame = frame&#x5B;rect&#x5B;1]:rect&#x5B;1]+rect&#x5B;3],rect&#x5B;0]:rect&#x5B;0]+rect&#x5B;2]]\n            # \u30b5\u30a4\u30ba\u306e\u7e2e\u5c0f\n            cut_frame = cv2.resize(cut_frame,(rect&#x5B;2]\/\/20, rect&#x5B;3]\/\/20))\n            # \u5143\u306e\u30b5\u30a4\u30ba\u306b\u30ea\u30b5\u30a4\u30ba\u3002\n            # cv2.INTER_NEAREST\uff08\u6700\u8fd1\u508d\u88dc\u9593\uff09\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u8352\u304f\u306a\u308b\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f cv2.INTER_LINEAR\uff08\u30d0\u30a4\u30ea\u30cb\u30a2\u88dc\u9593\uff09\u3068\u306a\u308a\u3001\u6ed1\u3089\u304b\u306a\u30e2\u30b6\u30a4\u30af\u3068\u306a\u308b\u3002\n            cut_frame = cv2.resize(cut_frame,(rect&#x5B;2], rect&#x5B;3]),cv2.INTER_NEAREST)\n            # \u7e2e\u5c0f\u2192\u5fa9\u5143\u753b\u50cf\u3067\u5143\u306e\u753b\u50cf\u3068\u7f6e\u63db\n            frame&#x5B;rect&#x5B;1]:rect&#x5B;1]+rect&#x5B;3],rect&#x5B;0]:rect&#x5B;0]+rect&#x5B;2]]=cut_frame\n\n\n    # \u30d5\u30ec\u30fc\u30e0\u306e\u63cf\u753b\n    cv2.imshow('frame', frame)\n\n    # q\u30ad\u30fc\u306e\u62bc\u4e0b\u3067\u51e6\u7406\u3092\u4e2d\u6b62\n    key = cv2.waitKey(1) & 0xFF\n    if key == ord('q'): break\n\n#\u30e1\u30e2\u30ea\u306e\u89e3\u653e\nvideo.release()\ncv2.destroyAllWindows()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"225\" src=\"https:\/\/itport.cloud\/wp-content\/uploads\/2019\/06\/output.gif\" alt=\"\" class=\"wp-image-6984\"\/><figcaption>\u52d5\u304d\u304c\u6b32\u3057\u304f\u3066\u9854\u3060\u3051\u52d5\u304b\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/figcaption><\/figure><\/div>\n\n\n\n<p>\u81ea\u52d5\u7684\u306b\u9854\u306b\u30e2\u30b6\u30a4\u30af\u304c\u639b\u304b\u3063\u3066\u3044\u307e\u3059\u3002<br>\u305f\u3060\u3057\u3001\u9854\u691c\u77e5\u304c\u6b63\u9762\u9854\u306e\u691c\u77e5\u3057\u304b\u3057\u306a\u3044\u305f\u3081\u3001\u6a2a\u9854\u3084\u5f8c\u308d\u3092\u5411\u304f\u3068\u30e2\u30b6\u30a4\u30af\u304c\u306f\u305a\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002<br>\u6a2a\u9854\u691c\u77e5\u306e\u691c\u51fa\u5668\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3068\u6a2a\u9854\u3082\u96a0\u305b\u305d\u3046\u3067\u3059\u306d\u3002 <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"opc4-4\">\u304a\u308f\u308a\u306b<\/h2>\n\n\n\n<p>\u4eca\u56de\u306f\u52d5\u753b\u306e\u8aad\u8fbc\u307f\u51e6\u7406\u3092\u884c\u3044\u3001\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u306b\u9854\u691c\u77e5\u3092\u884c\u3044\u3001\u753b\u50cf\u51e6\u7406\uff08\u30e2\u30b6\u30a4\u30af\u51e6\u7406\uff09\u3092\u884c\u3044\u307e\u3057\u305f\u3002<br><br>\u52d5\u753b\u3092\u8907\u6570\u6bce\u306e\u9759\u6b62\u753b\u306e\u96c6\u307e\u308a\u3068\u8003\u3048\u308c\u3070\u3001\u305d\u308c\u307b\u3069\u8907\u96d1\u3067\u306f\u306a\u304f\u3001\u4ed6\u306e\u753b\u50cf\u51e6\u7406\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u8272\u3093\u306a\u4e8b\u304c\u51fa\u6765\u305d\u3046\u3067\u3059\u3002<br>\u30a2\u30d7\u30ea\u7b49\u3067\u81ea\u52d5\u7684\u306b\u732b\u8033\u304c\u4ed8\u3044\u305f\u308a\u3001\u9854\u3092\u76db\u308c\u308b\u306e\u3082\u9854\u691c\u77e5\u3068\u753b\u50cf\u51e6\u7406\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u884c\u3063\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3057\u305f\u3002<br><br>\u6b21\u56de\u306f\u3001YOLO\u3092\u5229\u7528\u3057\u305f\u7269\u4f53\u691c\u77e5\u3092\u884c\u3063\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 <\/p>\n","protected":false},"excerpt":{"rendered":"\u76ee\u6b21 \u306f\u3058\u3081\u306b\u52d5\u753b\u306b\u3064\u3044\u3066\u52d5\u753b\u51e6\u7406\u304a\u308f\u308a\u306b \u306f\u3058\u3081\u306b \u524d\u56de\u306f\u3001\u9759\u6b62\u753b\u306b\u304a\u3044\u3066\u9854\u691c\u51fa\u5668\u3092\u4f7f\u3044\u3001\u9854\u3092\u8a8d\u8b58\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3057\u305f\u3002\u4eca\u5ea6\u306f\u52d5\u753b\u3067\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u306b\u9854\u691c\u51fa\u3092\u3055\u305b\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u52d5\u753b\u306b\u3064\u3044\u3066 \u307e\u305a\u52d5\u753b\u306f\u3001\u9759\u6b62\u753b\u3092\u30d1\u30e9\u30d1\u30e9\u30a2\u30cb\u30e1\u306e\u3088\u3046\u306b\u9ad8\u901f\u3067\u6620\u3057\u51fa\u3059\u3053\u3068\u3067\u30b9\u30e0\u30fc\u30ba\u306b\u52d5\u3044\u3066\u3044\u308b\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002 \u305d\u306e1\u30b3\u30de\u306e\u9759\u6b62\u753b\u3092\u30d5\u30ec\u30fc\u30e0\u3068\u3044\u3044\u30011\u79d2\u9593\u3042\u305f\u308a\u306b\u8868\u793a\u3055\u308c\u308b\u30d5\u30ec\u30fc\u30e0\u306e\u6570\u306b\u3088\u3063\u3066\u8868\u793a\u901f\u5ea6\uff08\u52d5\u304d\u306e\u306a\u3081\u3089\u304b\u3055\uff09\u3092\u8868\u3057\u307e\u3059\u3002\u901a\u5e38\u3001\u30c6\u30ec\u30d3\u7b49\u306f30fps(frame per second:1\u79d2\u9593\u306b30\u30b3\u30de)\u3067\u8868\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002 \u52d5\u753b\u51e6\u7406 \u52d5\u753b\u51e6\u7406\u306f\u30011\u30d5\u30ec\u30fc\u30e0\u3054\u3068\u306b\u8aad\u8fbc\u307f\u3001\u753b\u50cf\u51e6\u7406\u3092\u3057\u3066\u63cf\u753b\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002 import cv2 #\u52d5\u753b\u3092\u8aad\u8fbc\u307f #\u30ab\u30e1&hellip;","protected":false},"author":6,"featured_media":7081,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[52],"class_list":{"0":"post-6983","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-opencv","8":"tag-season15"},"_links":{"self":[{"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/posts\/6983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/itport.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6983"}],"version-history":[{"count":7,"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/posts\/6983\/revisions"}],"predecessor-version":[{"id":7085,"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/posts\/6983\/revisions\/7085"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/media\/7081"}],"wp:attachment":[{"href":"https:\/\/itport.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itport.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itport.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}