{"id":174,"date":"2026-02-07T01:00:00","date_gmt":"2026-02-06T16:00:00","guid":{"rendered":"https:\/\/hmhr.ath.cx\/?p=174"},"modified":"2026-02-04T14:41:05","modified_gmt":"2026-02-04T05:41:05","slug":"rasberry-pi%e3%81%a7%e5%8b%95%e7%94%bb%e9%80%81%e4%bf%a1%e3%81%97%e3%81%a6%e3%81%bf%e3%82%8b","status":"publish","type":"post","link":"https:\/\/hmhr.ath.cx\/?p=174","title":{"rendered":"Rasberry Pi\u3067\u52d5\u753b\u9001\u4fe1\u3057\u3066\u307f\u308b"},"content":{"rendered":"\n<p>Raspberry Pi\u3067\u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u305f\u753b\u50cf\u3092\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u3042\u3063\u305f\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306bJpeg\u753b\u50cf\u3067\u9001\u308bTCP\u30b5\u30fc\u30d0\u3068\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3092Python\u3067\u4f5c\u3063\u3066\u307f\u308b\u3002<br>\u9023\u7d9a\u3067Jpeg\u753b\u50cf\u3092\u8868\u793a\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u52d5\u753b\u3068\u3057\u3066\u518d\u751f\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">\u30b5\u30fc\u30d0\u5074<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport cv2\nimport numpy\n\nclass Getter:\n    cap: cv2.VideoCapture\n    avg: numpy.ndarray = None\n    encode_param = &#x5B;int(cv2.IMWRITE_JPEG_QUALITY), 100]\n\n    def __init__(self):\n        self.cap = cv2.VideoCapture(0)\n\n    def __del__(self):\n        self.cap.release()\n\n    def getImage(self):\n        ret, frame = self.cap.read()\n        rreturn cv2.imencode('.jpeg', frame, self.encode_param)&#x5B;1]\n<\/pre><\/div>\n\n\n<p>Jpeg\u30a4\u30e1\u30fc\u30b8\u3092\u53d6\u5f97\u3059\u308b\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3059\u308b\u3002(getter.py)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport getter\n\nimport cv2\nimport socketserver\nimport sys\n\nHOST = &quot;192.168.0.2&quot; # \u30b5\u30fc\u30d0\u306e\u30a2\u30c9\u30ec\u30b9\nPORT = 1234\n\nclass TCPHandler(socketserver.BaseRequestHandler):\n    gt = getter.Getter()\n\n    def handle(self):\n        while True:\n            recv = self.request.recv(1024).strip()\n            recvStr = recv.decode('utf-8')\n\n            if recvStr == &quot;bye&quot;:\n                break\n\n            jpeg_bytes = self.gt.getImage()\n\n            if jpeg_bytes is None or not jpeg_bytes.any():\n                self.request.send((0).to_bytes(4, 'little'))\n                continue\n\n            self.request.send(len(jpeg_bytes).to_bytes(4, 'little'))\n            self.request.send(jpeg_bytes)\n\nsocketserver.TCPServer.allow_reuse_address = True\nserver = socketserver.TCPServer((HOST, PORT), TCPHandler)\n\ntry:\n    server.serve_forever()\nexcept KeyboardInterrupt:\n    server.shutdown()\n    sys.exit()\n<\/pre><\/div>\n\n\n<p>\u30b5\u30fc\u30d0\u30d7\u30ed\u30b0\u30e9\u30e0\u3001TCP\u63a5\u7d9a\u3092\u53d7\u3051\u5165\u308c\u305f\u3089\u306a\u306b\u304b\u30c7\u30fc\u30bf\u304c\u9001\u3089\u308c\u3066\u304f\u308b\u306e\u3092\u5f85\u3064\u3002<br>\u9001\u3089\u308c\u3066\u304d\u305f\u30c7\u30fc\u30bf\u304c\u6587\u5b57\u5217\u3067bye\u3060\u3063\u305f\u5834\u5408\u306f\u901a\u4fe1\u3092\u7d42\u4e86\u3059\u308b\u3002<br>bye\u3067\u306a\u3051\u308c\u3070\u753b\u50cf\u3092\u53d6\u5f97\u3057\u30014byte\u3067\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u3092\u9001\u4fe1\u3057\u305f\u5f8c\u306b\u3001\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u9001\u4fe1\u3059\u308b\u3002<br>\u4e00\u679a\u9001\u4fe1\u3057\u7d42\u308f\u3063\u305f\u3089\u307e\u305f\u53d7\u4fe1\u5f85\u6a5f\u3057\u3066\u5f85\u3064\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport cv2\nimport numpy\nimport socket\n\nHOST = &quot;192.168.0.2&quot;\nPORT = 1234\n\nsock = None\n\nclass Getter:\n    def __init__(self):\n        self.sock = sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n        self.sock.connect((HOST, PORT))\n\n    def __del__(self):\n        self.sock.send('bye'.encode('utf-8'))\n        self.sock.close()\n\n    def getImage(self):\n        buf = &#x5B;]\n        recvlen = 180\n        image_size = 0\n\n        self.sock.send('img'.encode('utf-8'))\n\n        while True:\n            received = self.sock.recv(1024 * 8)\n            recvlen = len(received)\n\n            if recvlen &lt;= 0:\n                if len(buf) &lt;= 0:\n                    print(&quot;No Data&quot;)\n                break\n\n            if image_size == 0:\n                image_size = int.from_bytes(received&#x5B;:4], 'little')\n\n                if image_size == 0:\n                    print(&quot;No image&quot;)\n                    return None\n\n                received = received&#x5B;4:]\n\n            buf += received\n\n            if len(buf) &gt;= image_size:\n                break\n\n        if image_size == 0:\n            return None\n\n        recdata = numpy.array(buf, dtype='uint8')\n\n        return cv2.imdecode(recdata, 1)\n\ndef main():\n    getter = Getter()\n\n    while True:\n        img = getter.getImage()\n\n        if img is None:\n            continue\n\n        cv2.imshow('Capture', img)\n        if cv2.waitKey(5) &amp; 0xFF == ord('q'):\n            break\n\n\nif __name__ == '__main__':\n    main()\n<\/pre><\/div>\n\n\n<p>\u30b5\u30fc\u30d0\u5074\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u5f8c\u306b\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3059\u308b\u3068\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u306e\u30de\u30b7\u30f3\u3067\u3082\u52d5\u753b\u3092\u898b\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u3002<br>\u3053\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5834\u5408\u3001Jpeg\u753b\u50cf\u3092\u305d\u306e\u307e\u307e\u9001\u3063\u3066\u3044\u308b\u306e\u3067\u901a\u4fe1\u901f\u5ea6\u304c\u305d\u308c\u306a\u308a\u306b\u5fc5\u8981\u3068\u306a\u308b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Raspberry Pi\u3067\u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u305f\u753b\u50cf\u3092\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u3042\u3063\u305f\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306bJpeg\u753b\u50cf\u3067\u9001\u308bTCP\u30b5\u30fc\u30d0\u3068\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3092Python\u3067\u4f5c\u3063\u3066\u307f\u308b\u3002\u9023\u7d9a\u3067Jpeg\u753b\u50cf\u3092\u8868\u793a\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u52d5\u753b\u3068\u3057\u3066\u518d\u751f\u3067\u304d\u308b\u3088\u3046\u306b &hellip; <a href=\"https:\/\/hmhr.ath.cx\/?p=174\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">Rasberry Pi\u3067\u52d5\u753b\u9001\u4fe1\u3057\u3066\u307f\u308b<\/span><\/a><\/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-174","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\/174","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=174"}],"version-history":[{"count":1,"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=\/wp\/v2\/posts\/174\/revisions"}],"predecessor-version":[{"id":175,"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=\/wp\/v2\/posts\/174\/revisions\/175"}],"wp:attachment":[{"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hmhr.ath.cx\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}