#opencv for python3 => http://www.lfd.uci.edu/~gohlke/pythonlibs/
#video guide : https://www.youtube.com/watch?v=ulJdZn0qBCQ
2021/3/29 更新 : python打印raw date :
b'.............'
一個jpeg開始於\xff\xd8\xff\
2021/03/10 更新 : python3.8 在pycharm程式中 可以在File -> Setting -> Python Interpreter安裝
透過找尋opencv-python
或用 pip install opencv-python
確保python目錄下有cv2.cp38-win_amd64 重開pycharm即可使用
2021/03/11 更新
若安裝完opencv-python還是出現問題 如 :
https://blog.csdn.net/qq_37369201/article/details/108936470
使用系統管理者身分執行cmd : python -m pip -U opencv-contrib-python
Python OpenCV應用總整理
https://jennaweng0621.pixnet.net/blog/post/403630133-%5bpython%5d-opencv%E6%87%89%E7%94%A8%E7%B8%BD%E6%95%B4%E7%90%86
超完整版程式開始
import matplotlib
import numpy as np
import cv2
import imutils //https://stackoverflow.com/questions/29645278/webcam-open-cv-python-black-screen
cv2.namedWindow("preview") #設定視訊框的名稱
#此層放置了可讓 cv2.VideoCapture的視訊源範例
#此層放置了可讓 cv2.VideoCapture的視訊源範例
capture= cv2.VideoCapture(0) #新增一個視訊擷取物件為vc
#capture= cv2.VideoCapture('my_video.avi') #改寫成這樣就是放影片
#capture= cv2.VideoCapture("rtsp://user:password@192.168.2.0/h264") #改寫成這樣就是放串流
#reference :https://www.wongwonggoods.com/python/python-opencv-rtsp/
#此層放置了可讓 cv2.VideoCapture的視訊源範例
#capture.set(3,640) //3 = 寬 https://stackoverflow.com/questions/29645278/webcam-open-cv-python-black-screen
#capture.set(4,480) //4 = 高 https://stackoverflow.com/questions/29645278/webcam-open-cv-python-black-screen
#以上或可寫成:
#capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
#capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
width = capture.get(cv2.CAP_PROP_FRAME_WIDTH) #取得訊框寬
height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT) #取得訊框高
print("Image Size: %d x %d" % (width, height)) #印出訊框寬高
# 若要儲存視訊可用
sz = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
#fourcc = cv2.VideoWriter_fourcc(*'mpeg')
#fps=25
#out = cv2.VideoWriter('./videoCapture/output2.avi', fourcc,fps,sz)
# 若要儲存視訊可用
# 取得 Codec 名稱
fourcc = capture.get(cv2.CAP_PROP_FOURCC)
fourcc = int(fourcc)
codec = "".join([chr((fourcc >> 8 * i) & 0xFF) for i in range(4)])
print("Codec: " + codec)
# 取得 Codec 名稱
if capture.isOpened(): #嘗試打開capture物件並取得偵
rval, frame = capture.read() #讀取偵並返回其布林直確定可以讀取
else:
rval = False
while rval: #假設可以打開 也可使用capture.isOpened()來判斷
# 若要儲存視訊可用
#out.write(frame) # 開始儲存視訊
# 若要儲存視訊可用
cv2.imshow("preview", frame) #將cv2視訊框裡投放 capture物件讀取的frame訊框
#設定成灰階
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#cv2.imshow('frame', gray)
#設定成灰階
rval, frame = vc.read() # 一幀一幀的獲取影象 while 持續讀取
#frame = imutils.resize(frame, width=400) //https://stackoverflow.com/questions/29645278/webcam-open-cv-python-black-screen
key = cv2.waitKey(20)
if key == 113: # 按q拍照, 比照ASCII碼
cv2.imwrite("fangjian2.jpeg", frame) #拍照存檔檔名
break
if key == 27: # 按esc停止, 比照ASCII碼
break
capture.release()
cv2.destroyAllWindows() #強制摧毀所有頁框
cv2.destroyWindow("preview") #摧毀名為preview的視訊框
#While可以存在更多的功能鍵,只要比對ASCII碼即可‧
key = cv2.waitKey(20)
if key == 113: # 按q拍照, 比照ASCII碼
cv2.imwrite("fangjian2.jpeg", frame) #拍照存檔檔名
break
if key == 27: # 按esc停止, 比照ASCII碼
break
capture.release()
cv2.destroyAllWindows() #強制摧毀所有頁框
cv2.destroyWindow("preview") #摧毀名為preview的視訊框
#While可以存在更多的功能鍵,只要比對ASCII碼即可‧
Python - OpenCV 播放影片
https://ricky10116.pixnet.net/blog/post/265683920-python---opencv-%E6%92%AD%E6%94%BE%E5%BD%B1%E7%89%87
import cv2
import time
# 開啟影片檔案
cap = cv2.VideoCapture('C:\\Users\\Ricky\\Desktop\\Jurassic Park Trailer.webm')
# 以迴圈從影片檔案讀取影格,並顯示出來
while(cap.isOpened()):
ret, frame = cap.read()
#time.sleep(0.05)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
opencv-python啟用攝像頭錄影
https://www.itread01.com/content/1547376124.html
import cv2
cap = cv2.VideoCapture(0)
cap.set(3,320)#寬
cap.set(4,240)#高
sz = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# 為儲存視訊做準備
fourcc = cv2.VideoWriter_fourcc(*'mpeg')
fps=25
out = cv2.VideoWriter('./videoCapture/output2.avi', fourcc,fps,sz)
while True:
# 一幀一幀的獲取影象
ret,frame = cap.read()
if ret == True:
frame = cv2.flip(frame, 1)
# 在幀上進行操作
# gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
# 開始儲存視訊
out.write(frame)
# 顯示結果幀
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# 釋放攝像頭資源
cap.release()
out.release()
cv2.destroyAllWindows()
c编程示例代码
回覆刪除c代码测试闰年