[ OpenCV ] MacOS 無法使用 cv2.imshow()播放 Webcam的替代方案
OpenCV在 MacOS系統下有些功能無法使用, Python官方 也清楚地告知了。 IMPORTANT NOTE MacOS and Linux wheels have currently some limitations: video related functionality is not supported (not compiled with FFmpeg) for example cv2.imshow() will not work (not compiled with GTK+ 2.x or Carbon support) 嘗試了各種 OpenCV的安裝方法,OpenCV在 MacOS上總會碰上不少挫折。據說有人跟著這篇 安裝指南 後,能正常運作。但我安裝到一半仍然卡關了。 但為了使用 Webcam,不得不做一些妥協。若不願意在別的系統上開發,就只好先用一些替代方案。 import cv2 import numpy as np import matplotlib.pyplot as plt face_cascade = cv2.CascadeClassifier('./cascades/haarcascade_frontalface_default.xml') cam = cv2.VideoCapture(0) plt.ion() while(True): ret, img = cam.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.2, 3) for (x,y,w,h) in faces: img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) img = cv2.flip(img,1) # 1 水平翻轉, 0 垂直翻轉, -1 水平垂直翻轉 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.imshow(img) plt.pause(.01) plt.cla() ...