python 啟動攝像頭錄制視頻


錄制視頻
# coding:utf-8
import cv2
import sys
import time
reload(sys)
sys.setdefaultencoding('utf8')

time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
cap.set(1, 10.0)


#此處fourcc的在MAC上有效,如果視頻保存為空,那么可以改一下這個參數試試, 也可以是-1
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
# 第三個參數則是鏡頭快慢的,10為正常,小於10為慢鏡頭
out = cv2.VideoWriter('./video/output2'+time+'.avi', fourcc, 10, (640, 480))
while True:
    ret,frame = cap.read()
    if ret == True:
        frame = cv2.flip(frame, 1)
        a = out.write(frame)
        # cv2.imshow("frame", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
cap.release()
out.release()
cv2.destroyAllWindows()


拍照保存

import cv2
import time
time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))

cap = cv2.VideoCapture(0)
while(1):
    # get a frame
    ret, frame = cap.read()
    # show a frame
    cv2.imshow("capture", frame)
    if cv2.waitKey(1):
        cv2.imwrite('./face_photo/photo'+time+'.jpeg', frame)
        break
cap.release()
cv2.destroyAllWindows()

 





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM