使用Python+OpenCV實現圖像數據采集
完整代碼:import cv2import matplotlib.pyplot as pltcap = cv2.VideoCapture(10)if not (cap.isOpened()): print("Video device unconnected.")arb = input('Press enter to take picture.')ret, frame = cap.read()cap.release()cv2.destroyAllWindows()cv2_im = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)plt.imshow(cv2_im)plt.show()格式化為模型標準格式卷積神經網絡只接受固定大小的圖像,例如(100,100,3)。有幾種方法可以做到這一點。為了保持圖像的比例長度,可以嘗試裁剪圖像。一般語法是:plt.imshow(cv2_im[y_upper_bound:y_lower_bound,x_lower_bound:x_higher_bound])其中“upper”和“l(fā)ower”由圖像上的位置確定(y的“upper”表示圖像的上方,x的“upper”表示圖像的右側)。例如,plt.imshow(cv2_im[100:400,100:400])
這里把照片裁剪成正方形。但是,尺寸仍然是300×300。為了解決這個問題,我們將再次使用Pillow:pil_image = Image.fromarray(cv2_im[100:400,100:400])width = 100height = 100pil_image = pil_image.resize((width,height), Image.ANTIALIAS)NumPy自動將Pillow圖像轉換為數組。import numpy as npcv2_im_new = np.array(pil_image)查看新圖像:plt.imshow(cv2_im_new)
好多了!圖像的新形狀是(100,100,3), 非常適合我們的模型。在模型中運行現在我們有了NumPy數組,只需將其傳遞到模型中即可。model.predict(cv2_im_new)基于此,通過一些手動編碼來標記圖像的真實標簽,可以在title中標記它們:plt.imshow(cv2_im_new)plt.title('Hand Gesture: '+classification)
在本教程中,你將學習如何實現一個簡單的拍照界面,以查看你的機器學習模型的實際應用性能。

請輸入評論內容...
請輸入評論/評論長度6~500個字