{"id":180,"date":"2026-02-06T01:00:00","date_gmt":"2026-02-05T16:00:00","guid":{"rendered":"https:\/\/hmhr.ath.cx\/?p=180"},"modified":"2026-02-04T14:39:38","modified_gmt":"2026-02-04T05:39:38","slug":"raspberry-pi%e3%81%a7%e3%82%ab%e3%83%a1%e3%83%a9%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%81%bf%e3%82%8b","status":"publish","type":"post","link":"https:\/\/hmhr.ath.cx\/?p=180","title":{"rendered":"Raspberry Pi\u3067\u30ab\u30e1\u30e9\u3092\u4f7f\u3063\u3066\u307f\u308b"},"content":{"rendered":"\n<p>\u4eca\u56de\u306f\u30e9\u30ba\u30d1\u30a4\u3067\u753b\u50cf\u30ad\u30e3\u30d7\u30c1\u30e3\u3084\u3001\u52d5\u4f53\u691c\u77e5\u3092Python\u3092\u4f7f\u3063\u3066\u3084\u3063\u3066\u307f\u308b\u3002<br>\u6700\u5f8c\u306bPicamera2\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3082\u3057\u3066\u307f\u308b\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\">\u50cf\u3092\u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u3066\u307f\u308b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; quick-code: false; notranslate\" title=\"\">\nimport cv2\nimport sys\n\ncamera = cv2.VideoCapture(0)\nret, frame = camera.read()\n\nif not ret:\n    print(&quot;read error&quot;)\n    sys.exit()\n\ncv2.imwrite('capture.jpg', frame)\ncamera.release()\n<\/pre><\/div>\n\n\n<p>\u4e0a\u306e\u30b3\u30fc\u30c9\u3092\u9069\u5f53\u306a\u540d\u524d\u3092\u3064\u3051\u3066\u3001\u4fdd\u5b58\u3057\u5b9f\u884c\u3059\u308b\u3002\uff08\u3053\u3053\u3067\u306fct.py\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u540d\u3067\u4fdd\u5b58\u3057\u305f\uff09<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npython ct.py\n<\/pre><\/div>\n\n\n<p>\u4e0a\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u3001\u3046\u307e\u304f\u3044\u304f\u3068\u30ab\u30ec\u30f3\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306bcapture.jpg\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u540d\u3067\u3001\u30ab\u30e1\u30e9\u3067\u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u305f\u753b\u50cf\u304c\u4fdd\u5b58\u3055\u308c\u308b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u52d5\u4f53\u691c\u77e5\u3057\u3066\u307f\u308b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport cv2\n\ncap = cv2.VideoCapture(-1)\n\navg = None\ni = 0\n\nwhile(True):\n    i += 1\n    ret, frame = cap.read()\n\n    if not ret:\n        print(&quot;capture failed...&quot;)\n        break\n\n    #\u30b0\u30ec\u30fc\u30b9\u30b1\u30fc\u30eb\u5316\n    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n\n    #cv2.imwrite('g' + str(i) + '.jpg', gray)\n\n    if avg is None:\n        # \u6700\u521d\u306e\u30d5\u30ec\u30fc\u30e0\u3092\u4fdd\u5b58\n        avg = gray.copy().astype(&quot;float&quot;)\n        continue\n\n    # \u6700\u65b0\u753b\u50cf\u3001\u5e73\u5747\u753b\u50cf(in\/out)\u3001\u6700\u65b0\u753b\u50cf\u306e\u91cd\u307f(\u6570\u5024\u304c\u3067\u304b\u3044\u307b\u3046\u304c\u904e\u53bb\u3092\u5fd8\u308c\u3084\u3059\u3044\u306f\u305a\uff09\n    cv2.accumulateWeighted(gray, avg, 0.5)\n    frame_delta = cv2.absdiff(gray, cv2.convertScaleAbs(avg))\n\n    #cv2.imwrite('d' + str(i) + '.jpg', frame_delta)\n\n    #\u753b\u50cf\u3092\uff12\u5024\u5316\n    # \u5bfe\u8c61\u753b\u50cf\u3001\u95be\u5024\u3001\u6700\u5927\u5024\u3001\u30bf\u30a4\u30d7\u3002\u623b\u308a\u5024\u306f\u95be\u5024\u3001\u5909\u63db\u5f8c\u753b\u50cf\n    bin_img = cv2.threshold(frame_delta, 3, 255, cv2.THRESH_BINARY)&#x5B;1]\n\n    # \u8f2a\u90ed\u62bd\u51fa\n    contours = cv2.findContours(bin_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)&#x5B;0]\n\n    diff_num = 0\n\n    # \u5dee\u5206\u7b87\u6240\u63cf\u753b\n    for target in contours:\n        x, y, w, h = cv2.boundingRect(target)\n\n        # \u5dee\u5206\u7bc4\u56f2\u304c\u5c0f\u3055\u3044\u5834\u5408\u306f\u7121\u8996\u3059\u308b\n        if w * h &lt; 10:\n            continue\n\n        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)\n        diff_num += 1\n\n    if diff_num &gt; 0:\n        cv2.imwrite('b' + str(i) + '.jpg', bin_img)\n        cv2.imwrite('r' + str(i) + '.jpg', frame)\n\ncap.release()\n<\/pre><\/div>\n\n\n<p>\u3053\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u52d5\u4f53\u691c\u77e5\u3057\u305f\u5834\u5408\uff12\u5024\u5316\u3057\u305f\u753b\u50cf\uff08bxx.jpg)\u3068\u3001\u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u305f\u753b\u50cf\u306b\u52d5\u304d\u3092\u691c\u77e5\u3057\u305f\u90e8\u5206\u3092\u63cf\u753b\u3057\u305f\u753b\u50cf\u304c\u4fdd\u5b58\u3055\u308c\u308b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Picamera2\u3092\u4f7f\u3063\u3066\u307f\u308b<\/h4>\n\n\n\n<p>Picamera2\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u524d\u306e\u6e96\u5099<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo apt install -y python3-libcamera python3-kms++\nsudo apt install -y python3-prctl libatlas-base-dev ffmpeg python3-pip\nsudo apt install -y python3-pyqt5 python3-opengl # only if you want GUI features\npip3 install numpy --upgrade\n<\/pre><\/div>\n\n\n<p>\u516c\u5f0f\u3067\u306f\u4e0a\u306e\u30b3\u30de\u30f3\u30c9\u304c\u63b2\u8f09\u3055\u308c\u3066\u3044\u305f\u304c\u3001ffmpeg\u3044\u304c\u3044\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u6e08\u307f\u3060\u3063\u305f\u3002<br>\u63a1\u7528\u74b0\u5883\u304cCUI\u306a\u306e\u3067\u3001pyqt5\u3001opengl\u306f\u8a66\u3057\u3066\u3044\u306a\u3044\u3002<\/p>\n\n\n\n<p>Picamera2\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo pip3 install picamera2\n<\/pre><\/div>\n\n\n<p>\u3068\u3053\u3053\u307e\u3067\u3084\u3063\u3066\u307f\u305f\u304c\u3001Picamera2\u306f\u3059\u3067\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u305f<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfrom picamera2 import Picamera2, Preview\nimport time\n\npicam2 = Picamera2()\ncamera_config = picam2.create_preview_configuration()\npicam2.configure(camera_config)\npicam2.start_preview(Preview.NULL)\npicam2.start()\ntime.sleep(2)\npicam2.capture_file(&quot;test.jpg&quot;)\n<\/pre><\/div>\n\n\n<p>\u3053\u308c\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u30a8\u30e9\u30fc\u304c\u51fa\u3066\u3057\u307e\u3063\u305f\u3002<br>\u5185\u5bb9\u306f\u3001Legacy Camera\u3092\u6709\u52b9\u306b\u3057\u3066\u3044\u308b\u306e\u304c\u3060\u3081\u3060\u3063\u305f\u3002raspi-config\u3067\u7121\u52b9\u306b\u3059\u308b\u3068\u304d\u305f\u3044\u3069\u304a\u308a\u306e\u52d5\u4f5c\u3068\u306a\u3063\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u56de\u306f\u30e9\u30ba\u30d1\u30a4\u3067\u753b\u50cf\u30ad\u30e3\u30d7\u30c1\u30e3\u3084\u3001\u52d5\u4f53\u691c\u77e5\u3092Python\u3092\u4f7f\u3063\u3066\u3084\u3063\u3066\u307f\u308b\u3002\u6700\u5f8c\u306bPicamera2\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3082\u3057\u3066\u307f\u308b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,14],"tags":[15],"class_list":["post-180","post","type-post","status-publish","format-standard","hentry","category-development","category-python","tag-rasberry-pi"],"_links":{"self":[{"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=180"}],"version-history":[{"count":1,"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":182,"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions\/182"}],"wp:attachment":[{"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}