{"id":1028792,"date":"2024-12-31T11:02:38","date_gmt":"2024-12-31T03:02:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1028792.html"},"modified":"2024-12-31T11:02:41","modified_gmt":"2024-12-31T03:02:41","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python3%e4%b8%ad%e6%b7%bb%e5%8a%a0opencv","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1028792.html","title":{"rendered":"\u5982\u4f55\u5728python3\u4e2d\u6dfb\u52a0opencv"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/d474269f-ae77-44b5-a145-51d544b75c3b.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5728python3\u4e2d\u6dfb\u52a0opencv\" \/><\/p>\n<p><p> <strong>\u5728Python3\u4e2d\u6dfb\u52a0OpenCV\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u6b65\u5b9e\u73b0\uff1a\u5b89\u88c5Python\u3001\u5b89\u88c5pip\u3001\u4f7f\u7528pip\u5b89\u88c5OpenCV\u3001\u9a8c\u8bc1\u5b89\u88c5\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86Python3\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u8bbf\u95ee\u3002\u5176\u6b21\uff0c\u5b89\u88c5pip\u6765\u7ba1\u7406Python\u5305\u3002\u7136\u540e\uff0c\u4f7f\u7528pip\u5b89\u88c5OpenCV\u3002\u6700\u540e\uff0c\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5Python3<\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86Python3\u3002\u5982\u679c\u6ca1\u6709\uff0c\u8bf7\u8bbf\u95ee<a href=\"https:\/\/www.python.org\/downloads\/\">Python\u7684\u5b98\u65b9\u7f51\u7ad9<\/a>\u4e0b\u8f7d\u5e76\u5b89\u88c5Python3\u3002\u5b89\u88c5\u8fc7\u7a0b\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u3002\u5b89\u88c5\u65f6\uff0c\u8bf7\u786e\u4fdd\u52fe\u9009\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u8fd9\u6837\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u8bbf\u95eePython\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5b89\u88c5pip<\/p>\n<\/p>\n<p><p>pip\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u901a\u5e38\u5728\u5b89\u88c5Python\u65f6\u4f1a\u81ea\u52a8\u5b89\u88c5\u3002\u5982\u679c\u6ca1\u6709\uff0c\u8bf7\u624b\u52a8\u5b89\u88c5pip\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u68c0\u67e5pip\u662f\u5426\u5df2\u7ecf\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u547d\u4ee4\u8fd4\u56depip\u7684\u7248\u672c\u4fe1\u606f\uff0c\u5219\u8bf4\u660epip\u5df2\u7ecf\u5b89\u88c5\u3002\u5982\u679c\u6ca1\u6709\uff0c\u8bf7\u53c2\u8003<a href=\"https:\/\/pip.pypa.io\/en\/stable\/installation\/\">\u5b98\u65b9\u6587\u6863<\/a>\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528pip\u5b89\u88c5OpenCV<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528pip\u6765\u5b89\u88c5OpenCV\u3002OpenCV\u7684Python\u5e93\u540d\u4e3a<code>opencv-python<\/code>\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528OpenCV\u7684\u6269\u5c55\u6a21\u5757\uff08\u5982\u56fe\u50cf\u5904\u7406\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u9ad8\u7ea7\u529f\u80fd\uff09\uff0c\u5efa\u8bae\u5b89\u88c5<code>opencv-contrib-python<\/code>\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-contrib-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u9a8c\u8bc1\u5b89\u88c5<\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u9a8c\u8bc1OpenCV\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>print(cv2.__version__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4ee3\u7801\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\uff0c\u5e76\u8f93\u51faOpenCV\u7684\u7248\u672c\u53f7\uff0c\u5219\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Python3<\/h3>\n<\/p>\n<p><h4>1.1 \u68c0\u67e5\u662f\u5426\u5df2\u7ecf\u5b89\u88c5Python3<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u68c0\u67e5Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8f93\u51fa\u663e\u793aPython 3.x.x\u7248\u672c\uff0c\u5219\u8bf4\u660ePython3\u5df2\u7ecf\u5b89\u88c5\u3002\u5982\u679c\u6ca1\u6709\uff0c\u8bf7\u4ece<a href=\"https:\/\/www.python.org\/downloads\/\">Python\u5b98\u65b9\u7f51\u7ad9<\/a>\u4e0b\u8f7d\u5e76\u5b89\u88c5\u9002\u5408\u4f60\u64cd\u4f5c\u7cfb\u7edf\u7684Python3\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u5b89\u88c5Python3<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u9009\u62e9\u4e0b\u8f7d\u9002\u5408\u7684\u5b89\u88c5\u5305\u5e76\u8fdb\u884c\u5b89\u88c5\u3002\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0c\u786e\u4fdd\u52fe\u9009\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u8bbf\u95eePython\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b89\u88c5pip<\/h3>\n<\/p>\n<p><h4>2.1 \u68c0\u67e5\u662f\u5426\u5df2\u7ecf\u5b89\u88c5pip<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u68c0\u67e5pip\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8f93\u51fa\u663e\u793apip\u7684\u7248\u672c\u4fe1\u606f\uff0c\u5219\u8bf4\u660epip\u5df2\u7ecf\u5b89\u88c5\u3002\u5982\u679c\u6ca1\u6709\uff0c\u8bf7\u53c2\u8003<a href=\"https:\/\/pip.pypa.io\/en\/stable\/installation\/\">\u5b98\u65b9\u6587\u6863<\/a>\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u624b\u52a8\u5b89\u88c5pip<\/h4>\n<\/p>\n<p><p>\u5982\u679cpip\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u624b\u52a8\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u4e0b\u8f7d<code>get-pip.py<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">curl https:\/\/bootstrap.pypa.io\/get-pip.py -o get-pip.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u8fd0\u884c<code>get-pip.py<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 get-pip.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u518d\u6b21\u68c0\u67e5pip\u7248\u672c\uff0c\u786e\u4fdd\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pip\u5b89\u88c5OpenCV<\/h3>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5\u57fa\u7840OpenCV\u5305<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5OpenCV\u7684\u57fa\u7840\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u5b89\u88c5\u6269\u5c55\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u4f7f\u7528OpenCV\u7684\u6269\u5c55\u6a21\u5757\uff0c\u53ef\u4ee5\u5b89\u88c5<code>opencv-contrib-python<\/code>\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-contrib-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3 \u5b89\u88c5\u5176\u4ed6\u4f9d\u8d56<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u9879\u76ee\u9700\u6c42\uff0c\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u5176\u4ed6\u76f8\u5173\u4f9d\u8d56\u5305\uff0c\u6bd4\u5982NumPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u9a8c\u8bc1\u5b89\u88c5<\/h3>\n<\/p>\n<p><h4>4.1 \u7f16\u5199\u6d4b\u8bd5\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c\uff0c\u9a8c\u8bc1OpenCV\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>print(cv2.__version__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u8fd0\u884c\u6d4b\u8bd5\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4fdd\u5b58\u811a\u672c\u4e3a<code>test_opencv.py<\/code>\uff0c\u7136\u540e\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 test_opencv.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8f93\u51fa\u663e\u793aOpenCV\u7684\u7248\u672c\u53f7\uff0c\u5219\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001OpenCV\u7684\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><h4>5.1 \u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf\u662f\u975e\u5e38\u7b80\u5355\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Image&#39;, image)<\/p>\n<h2><strong>\u7b49\u5f85\u6309\u952e<\/strong><\/h2>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<h2><strong>\u5173\u95ed\u7a97\u53e3<\/strong><\/h2>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u56fe\u50cf\u5904\u7406\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u6bd4\u5982\u7070\u5ea6\u8f6c\u6362\u3001\u8fb9\u7f18\u68c0\u6d4b\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7070\u5ea6\u8f6c\u6362\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u663e\u793a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Gray Image&#39;, gray_image)<\/p>\n<h2><strong>\u7b49\u5f85\u6309\u952e<\/strong><\/h2>\n<p>cv2.waitKey(0)<\/p>\n<h2><strong>\u5173\u95ed\u7a97\u53e3<\/strong><\/h2>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001OpenCV\u7684\u9ad8\u7ea7\u529f\u80fd<\/h3>\n<\/p>\n<p><h4>6.1 \u89c6\u9891\u5904\u7406<\/h4>\n<\/p>\n<p><p>OpenCV\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u9759\u6001\u56fe\u50cf\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u89c6\u9891\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u89c6\u9891\u6355\u83b7\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u6253\u5f00\u6444\u50cf\u5934<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(0)<\/p>\n<p>while True:<\/p>\n<p>    # \u8bfb\u53d6\u6bcf\u4e00\u5e27<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    # \u663e\u793a\u5e27<\/p>\n<p>    cv2.imshow(&#39;Video&#39;, frame)<\/p>\n<p>    # \u6309\u4e0b&#39;q&#39;\u952e\u9000\u51fa<\/p>\n<p>    if cv2.waitKey(1) &amp; 0xFF == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<h2><strong>\u91ca\u653e\u6444\u50cf\u5934\u548c\u5173\u95ed\u7a97\u53e3<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2 \u673a\u5668\u5b66\u4e60<\/h4>\n<\/p>\n<p><p>OpenCV\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u5206\u7c7b\u3001\u5bf9\u8c61\u68c0\u6d4b\u7b49\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5bf9\u8c61\u68c0\u6d4b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u52a0\u8f7d\u9884\u8bad\u7ec3\u7684\u5bf9\u8c61\u68c0\u6d4b\u6a21\u578b<\/strong><\/h2>\n<p>net = cv2.dnn.readNetFromCaffe(&#39;path_to_prototxt&#39;, &#39;path_to_caffemodel&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u5c3a\u5bf8<\/strong><\/h2>\n<p>(h, w) = image.shape[:2]<\/p>\n<h2><strong>\u9884\u5904\u7406\u56fe\u50cf<\/strong><\/h2>\n<p>blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007843, (300, 300), 127.5)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8f93\u5165<\/strong><\/h2>\n<p>net.setInput(blob)<\/p>\n<h2><strong>\u524d\u5411\u4f20\u64ad\uff0c\u83b7\u53d6\u68c0\u6d4b\u7ed3\u679c<\/strong><\/h2>\n<p>detections = net.forward()<\/p>\n<h2><strong>\u7ed8\u5236\u68c0\u6d4b\u7ed3\u679c<\/strong><\/h2>\n<p>for i in range(detections.shape[2]):<\/p>\n<p>    confidence = detections[0, 0, i, 2]<\/p>\n<p>    if confidence &gt; 0.2:<\/p>\n<p>        idx = int(detections[0, 0, i, 1])<\/p>\n<p>        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])<\/p>\n<p>        (startX, startY, endX, endY) = box.astype(&#39;int&#39;)<\/p>\n<p>        label = f&#39;{idx}: {confidence:.2f}&#39;<\/p>\n<p>        cv2.rectangle(image, (startX, startY), (endX, endY), (0, 255, 0), 2)<\/p>\n<p>        cv2.putText(image, label, (startX, startY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Detection&#39;, image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<p><h4>7.1 \u5b89\u88c5\u5931\u8d25<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5728\u5b89\u88c5OpenCV\u65f6\u9047\u5230\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u786e\u4fddpip\u7248\u672c\u662f\u6700\u65b0\u7684\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install --upgrade pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u56fd\u5185\u955c\u50cf\u6e90\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>7.2 ImportError: No module named &#39;cv2&#39;<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5728\u5bfc\u5165cv2\u6a21\u5757\u65f6\u9047\u5230ImportError\uff0c\u53ef\u80fd\u662f\u56e0\u4e3aOpenCV\u5b89\u88c5\u8def\u5f84\u672a\u88ab\u6b63\u786e\u6dfb\u52a0\u5230Python\u8def\u5f84\u4e2d\u3002\u53ef\u4ee5\u5c1d\u8bd5\u91cd\u65b0\u5b89\u88c5OpenCV\uff0c\u6216\u8005\u624b\u52a8\u6dfb\u52a0\u5b89\u88c5\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python3\u4e2d\u5b89\u88c5\u548c\u4f7f\u7528OpenCV\uff0c\u5305\u62ec\u57fa\u7840\u5b89\u88c5\u3001\u9a8c\u8bc1\u3001\u57fa\u672c\u4f7f\u7528\u548c\u9ad8\u7ea7\u529f\u80fd\u7b49\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u987a\u5229\u5728Python3\u4e2d\u6dfb\u52a0\u5e76\u4f7f\u7528OpenCV\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u76f8\u5173\u7684\u5f00\u53d1\u3002\u5982\u679c\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\u9047\u5230\u95ee\u9898\uff0c\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u6587\u6863\u6216\u793e\u533a\u8d44\u6e90\u5bfb\u6c42\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python3\u4e2d\u5b89\u88c5OpenCV\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python3\u4e2d\u5b89\u88c5OpenCV\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u3002\u6253\u5f00\u547d\u4ee4\u884c\u7a97\u53e3\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a  <\/p>\n<pre><code>pip install opencv-python\n<\/code><\/pre>\n<p>\u5982\u679c\u9700\u8981\u5b89\u88c5\u5305\u542b\u989d\u5916\u529f\u80fd\u7684\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a  <\/p>\n<pre><code>pip install opencv-contrib-python\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u5b89\u88c5\u4e4b\u524d\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u4e86Python\u548cpip\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u8bbf\u95ee\u3002<\/p>\n<p><strong>\u5728\u5b89\u88c5OpenCV\u540e\uff0c\u5982\u4f55\u9a8c\u8bc1\u5176\u662f\u5426\u6210\u529f\u5b89\u88c5\uff1f<\/strong><br \/>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6253\u5f00Python\u89e3\u91ca\u5668\u5e76\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\u6765\u9a8c\u8bc1OpenCV\u662f\u5426\u6210\u529f\u5b89\u88c5\uff1a  <\/p>\n<pre><code class=\"language-python\">import cv2\nprint(cv2.__version__)\n<\/code><\/pre>\n<p>\u5982\u679c\u6ca1\u6709\u9519\u8bef\u4fe1\u606f\u5e76\u4e14\u8f93\u51fa\u4e86OpenCV\u7684\u7248\u672c\u53f7\uff0c\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<p><strong>OpenCV\u5728Python3\u4e2d\u53ef\u4ee5\u5b9e\u73b0\u54ea\u4e9b\u529f\u80fd\uff1f<\/strong><br \/>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u529f\u80fd\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a\u56fe\u50cf\u5904\u7406\u3001\u89c6\u9891\u5206\u6790\u3001\u7269\u4f53\u68c0\u6d4b\u3001\u9762\u90e8\u8bc6\u522b\u3001\u7279\u5f81\u5339\u914d\u3001\u56fe\u50cf\u53d8\u6362\u548c\u673a\u5668\u5b66\u4e60\u7b49\u3002\u901a\u8fc7\u7ed3\u5408NumPy\u7b49\u5176\u4ed6\u5e93\uff0cOpenCV\u80fd\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u65b9\u9762\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python3\u4e2d\u6dfb\u52a0OpenCV\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u6b65\u5b9e\u73b0\uff1a\u5b89\u88c5Python\u3001\u5b89\u88c5pip\u3001\u4f7f\u7528pip\u5b89\u88c5Open [&hellip;]","protected":false},"author":3,"featured_media":1028805,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028792"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1028792"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028792\/revisions"}],"predecessor-version":[{"id":1028807,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028792\/revisions\/1028807"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1028805"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1028792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1028792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1028792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}