-------------- EDIT --------------
我简化了原始查询,因为大多数人都不理解。
-------------- EDIT --------------
嗨,我想使用OpenCV全屏显示视频但不确定如何。这是我的代码到目前为止,是的我是一个新手,但你必须从某个地方开始。我正在运行Python 3.6.3
问题:如何使用OpenCV全屏显示视频?
import cv2
import numpy as np
import os
os.environ['SDL_VIDEO_CENTERED'] = '1'
cap = cv2.VideoCapture("video.mp4")
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("video.mp4",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
cv2.imshow('Frame',frame)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
我也收到此错误
追踪(最近一次通话): 文件“C:/Users/A/Desktop/vid.py”,第7行,in cv2.setWindowProperty( “video.mp4”,cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN) cv2.error:C:\ projects \ opencv-python \ opencv \ modules \ highgui \ src \ window_w32.cpp:443:错误:( - 27)函数cvSetModeWindow_W32中的NULL窗口
答案 0 :(得分:5)
感谢Dan Masek,我的orignial问题的答案是:
import cv2
import numpy as np
file_name = "video location here"
window_name = "window"
interframe_wait_ms = 30
cap = cv2.VideoCapture(file_name)
if not cap.isOpened():
print("Error: Could not open video.")
exit()
cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
while (True):
ret, frame = cap.read()
if not ret:
print("Reached end of video, exiting.")
break
cv2.imshow(window_name, frame)
if cv2.waitKey(interframe_wait_ms) & 0x7F == ord('q'):
print("Exit requested.")
break
cap.release()
cv2.destroyAllWindows()
答案 1 :(得分:1)
我不明白你的问题但是如果你想打开摄像机你可以尝试这个代码:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret:
gray = cv2.cvtColor(src=frame, code=cv2.COLOR_BGR2GRAY)
out.write(gray)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
答案 2 :(得分:0)
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)
cv2.imshow('window',frame)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
else:
break
cap.release() cv2.destroyAllWindows()