這篇文章將為大家詳細(xì)講解有關(guān)opencv如何實(shí)現(xiàn)調(diào)用攝像頭并顯示,讀取視頻文件并顯示,保存視頻等操作,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
“專業(yè)、務(wù)實(shí)、高效、創(chuàng)新、把客戶的事當(dāng)成自己的事”是我們每一個(gè)人一直以來堅(jiān)持追求的企業(yè)文化。 創(chuàng)新互聯(lián)公司是您可以信賴的網(wǎng)站建設(shè)服務(wù)商、專業(yè)的互聯(lián)網(wǎng)服務(wù)提供商! 專注于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、軟件開發(fā)、設(shè)計(jì)服務(wù)業(yè)務(wù)。我們始終堅(jiān)持以客戶需求為導(dǎo)向,結(jié)合用戶體驗(yàn)與視覺傳達(dá),提供有針對(duì)性的項(xiàng)目解決方案,提供專業(yè)性的建議,創(chuàng)新互聯(lián)建站將不斷地超越自我,追逐市場,引領(lǐng)市場!
調(diào)用攝像頭,并顯示視頻:
import numpy as npimport cv2 as cv# cap = cv.VideoCapture(-1)# cap = cv.VideoCapture(1)cap = cv.VideoCapture(0) # 傳入一個(gè)設(shè)備索引,代表不同的攝像頭,或者傳入一個(gè)視頻文件名# cap = cv.VideoCapture('testVideo.mp4')if not cap.isOpened():print("Cannot open camera")exit()width = cap.get(cv.CAP_PROP_FRAME_WIDTH)height = cap.get(cv.CAP_PROP_FRAME_HEIGHT)print('寬度和高度分別是:', width, height) # 寬度和高度分別是: 640.0 480.0ret = cap.set(cv.CAP_PROP_FRAME_WIDTH,320) # 設(shè)定寬度ret = cap.set(cv.CAP_PROP_FRAME_HEIGHT,240) # 設(shè)定高度while True:# Capture frame-by-frameret, frame = cap.read() # 返回True或者False,如果讀取正確是True,可以用False來判斷是否到達(dá)視頻的末尾 # if frame is read correctly ret is Trueif not ret:print("Can't receive frame (stream end?). Exiting ...")break# Our operations on the frame come heregray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)# Display the resulting framecv.imshow('LinMaZi-frame-LinZuQuan', gray) # 設(shè)定顯示窗口標(biāo)題文字if cv.waitKey(1) == ord('q'): # 判斷用戶是否輸入q鍵break# When everything done, release the capturecap.release() # 釋放資源cv.destroyAllWindows()
讀取視頻文件并顯示:
import numpy as npimport cv2 as cv cap = cv.VideoCapture('畢業(yè)晚會(huì).MP4')# cap = cv.VideoCapture(0)while cap.isOpened():ret, frame = cap.read()# if frame is read correctly ret is Trueif not ret:print("Can't receive frame (stream end?). Exiting ...")breakgray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)cv.imshow('frame', gray)if cv.waitKey(25) == ord('q'): # 選擇合適的刷新時(shí)間間隔,25毫秒還不錯(cuò)breakcap.release()cv.destroyAllWindows()
調(diào)用攝像頭并顯示,保存為視頻文件:
import numpy as npimport cv2 as cvimport time cap = cv.VideoCapture(0)# Define the codec and create VideoWriter objectfourcc = cv.VideoWriter_fourcc(*'XVID') # 兩者等效# fourcc = cv.VideoWriter_fourcc('X','V','I','D') # 兩者等效saveTime = time.strftime("%Yy-%mm-%dd@%Hh%Mm%Ss", time.localtime())out = cv.VideoWriter('output_' + saveTime + '.mp4', fourcc, 20.0, (640, 480)) # 參數(shù)說明 output file name, FourCC code, frames per second (fps), frame sizewhile cap.isOpened():ret, frame = cap.read()if not ret:print("Can't receive frame (stream end?). Exiting ...")break# frame = cv.flip(frame, 1) # 左右橫向翻轉(zhuǎn)frame = cv.flip(frame, 0) # 上下縱向翻轉(zhuǎn)# write the flipped frameout.write(frame)cv.imshow('LinMaZi-frame-LinZuQuan', frame)if cv.waitKey(1) == ord('q'):break# Release everything if job is finishedcap.release()out.release()cv.destroyAllWindows()
關(guān)于“opencv如何實(shí)現(xiàn)調(diào)用攝像頭并顯示,讀取視頻文件并顯示,保存視頻等操作”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。