{ "cells": [ { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "# 引入影象處理庫OpenCV\n", "import cv2" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "# 選擇影片檔\n", "cap = cv2.VideoCapture('data/video/smile.mp4')" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "while(True):\n", " # 從VideoCapture擷取一張影像\n", " ret, frame = cap.read() \n", " \n", " # 將影像轉換成灰度\n", " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n", " \n", " # 顯示圖片\n", " cv2.imshow('frame', gray)\n", " \n", " # 若按下 q 鍵則離開迴圈\n", " if cv2.waitKey(24) & 0xFF == ord('q'):\n", " break" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "# 釋放攝影機\n", "cap.release()" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [], "source": [ "# 關閉所有 OpenCV 視窗\n", "cv2.destroyAllWindows()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }