{"id":811,"date":"2022-04-11T10:21:59","date_gmt":"2022-04-11T02:21:59","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=811"},"modified":"2025-10-09T13:55:35","modified_gmt":"2025-10-09T05:55:35","slug":"pycv-multi-threading-multi-cam","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2022\/04\/11\/pycv-multi-threading-multi-cam\/","title":{"rendered":"Python-OpenCV \u591a\u7ebf\u7a0b-\u591a\u955c\u5934"},"content":{"rendered":"<h2>\u4e00\u3001\u95ee\u9898<\/h2>\n<p>&emsp;&emsp;\u73b0\u5728\u6211\u4eec\u8bd5\u56fe\u8bfb\u53d6\u4e00\u4e2a\u53cc\u76ee\u6444\u50cf\u5934\u8bbe\u5907\uff0c\u4f46\u5176\u4e2d\u4e00\u4e2a\u6444\u50cf\u5934\u6709\u7740\u660e\u663e\u7684\u5ef6\u8fdf\u3002\u6240\u4ee5\u6211\u4eec\u8bd5\u56fe\u4f7f\u7528\u591a\u7ebf\u7a0b\uff08\u5e76\u884c\uff09\u6765\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002\u4f46\u662f\u5728\u6b64\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u770b\u4e00\u4e0b\u4e32\u884c\u7684\u5ef6\u8fdf\u6240\u5728\uff0c\u4e0b\u9762\u662f\u4e00\u6bb5\u4e32\u884c\u4ee3\u7801\u7684\u5c55\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">import cv2\ncap = cv2.VideoCapture(&quot;rtsp:\/\/192.168.1.127\/mpeg4\/ch1&quot;)\ncap1 = cv2.VideoCapture(&quot;rtsp:\/\/192.168.1.127\/mpeg4&quot;)\n\n# \u66f4\u6539\u5185\u90e8\u7f13\u51b2\u533a\u7684\u5e27\u6570\ncap.set(cv2.CAP_PROP_BUFFERSIZE, 3)\ncap1.set(cv2.CAP_PROP_BUFFERSIZE, 3)\n\nwhile ret:\n    ret, frame = cap.read()\n    ret1, frame1 = cap1.read()\n\n    cv2.imshow(&quot;frame&quot;, frame)\n    cv2.imshow(&quot;frame1&quot;, frame1)\n\n    if cv2.waitKey(1) &amp; 0xFF == ord(&#039;q&#039;):\n        break\n    # End if\n# End while\ncv2.destroyAllWindows()\ncap.release()<\/code><\/pre>\n<p>&emsp;&emsp;\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7f51\u7edc\u63a5\u53e3\u6765\u83b7\u53d6\u56fe\u50cf\uff0c\u8fd9\u91cc\u7a0b\u5e8f\u9700\u8981\u7b49\u5f85read()\u6765\u5b8c\u6210\u4e24\u6b21\u6293\u53d6\u52a8\u4f5c\uff0c\u7ea2\u5916\u90e8\u5206\u56e0\u4e3a\u8bfb\u53d6\u901f\u5ea6\u6162\uff0c\u56e0\u800c\u9020\u6210\u4e86\u9ad8\u5ef6\u8fdf\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u51b3\u5b9a\u4f7f\u7528\u4e24\u4e2a\u7ebf\u7a0b\u6765\u6293\u53d6\u56fe\u50cf\uff0c\u5e76\u4e14\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u663e\u793a\u3002<\/p>\n<h2>\u4e8c\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b<\/h2>\n<pre><code class=\"language-python\">import cv2, time\nfrom threading import Thread\n\n# cap1 = cv2.VideoCapture(&quot;rtsp:\/\/192.168.1.127\/mpeg4\/ch1&quot;)\n# cap2 = cv2.VideoCapture(&quot;rtsp:\/\/192.168.1.127\/mpeg4&quot;)\n\nclass CamWidget:\n\n    # Func 00 : init\n    # path\uff1astring, path of capture\n    def __init__(self, path):\n        self.cap = cv2.VideoCapture(path)\n        self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 3)\n\n        self.thread = Thread(target=self.update, args=())\n        self.thread.daemon = True\n        self.thread.start()\n    # End def\n\n    # Func 01 : update the frame\n    def update(self):\n        while True:\n            if self.cap.isOpened():\n                (self.status, self.frame) = self.cap.read()\n            # End if\n            time.sleep(.01)\n        # End while\n    # End def\n\n    # Func 02 : show img in main program\n    # widgetName: string\n    def show(self, widgetName):\n        cv2.imshow(widgetName, self.frame)\n        key = cv2.waitKey(1)\n        if key == ord(&#039;q&#039;):\n            self.capture.release()\n            cv2.destroyAllWindows()\n            exit(1)\n        # End if\n    # End def\n# End class\n\nif __name__ == &#039;__main__&#039;:\n    cap1 = CamWidget(&quot;rtsp:\/\/192.168.1.127\/mpeg4\/ch1&quot;)\n    cap2 = CamWidget(&quot;rtsp:\/\/192.168.1.127\/mpeg4&quot;)\n    while True:\n        try:\n            cap1.show(&#039;cap1&#039;)\n            cap2.show(&#039;cap2&#039;)\n        except AttributeError:\n            pass\n        # End try\n    # End while\n# End if<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u95ee\u9898 &emsp;&emsp;\u73b0\u5728\u6211\u4eec\u8bd5\u56fe\u8bfb\u53d6\u4e00\u4e2a\u53cc\u76ee\u6444\u50cf\u5934\u8bbe\u5907\uff0c\u4f46\u5176\u4e2d\u4e00\u4e2a\u6444\u50cf\u5934\u6709\u7740\u660e\u663e\u7684\u5ef6\u8fdf\u3002\u6240\u4ee5\u6211\u4eec\u8bd5\u56fe\u4f7f\u7528\u591a\u7ebf\u7a0b\uff08\u5e76\u884c &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57],"tags":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/811"}],"collection":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/comments?post=811"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/811\/revisions"}],"predecessor-version":[{"id":812,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/811\/revisions\/812"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}