【发布时间】:2021-10-22 15:42:19
【问题描述】:
我得到的错误:
C:\Users\KGB\PycharmProjects\Screen_rec\venv\Scripts\python.exe C:/Users/KGB/PycharmProjects/Screen_rec/main.py
[ WARN:0] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Traceback (most recent call last):
File "C:\Users\KGB\PycharmProjects\Screen_rec\main.py", line 24, in <module>
fr_height, fr_width, _ = frame.shape
AttributeError: 'NoneType' object has no attribute 'shape'
Process finished with exit code 1
我的代码在这里给出:
import datetime
from PIL import ImageGrab
import numpy as np
import cv2
from win32api import GetSystemMetrics
import sys
width = GetSystemMetrics(0)
height = GetSystemMetrics(1)
time_stamp = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
file_name = f'{time_stamp}.mp4'
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
captured_video = cv2.VideoWriter(file_name, fourcc, 20.0, (width, height))
webcam = cv2.VideoCapture(1)
while True:
img = ImageGrab.grab(bbox=(0, 0, width, height))
img_np = np.array(img)
img_final = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
_, frame = webcam.read()
fr_height, fr_width, _ = frame.shape
img_final[0:fr_height, 0: fr_width, :] = frame[0: fr_height, 0: fr_width, :]
cv2.imshow('Secret Capture', img_final)
# cv2.imshow('webcam', frame)
captured_video.write(img_final)
if cv2.waitKey(10) == ord('q'):
break
我正在尝试构建一个屏幕记录的 python 程序。 我一直在网上寻找很多解决方案,但我发现没有一个可行,这个程序是我作为学生的第一个高级项目
【问题讨论】:
-
通常意味着您的视频捕获未能获得帧
-
我找到了答案,我只需将
webcam = cv2.VideoCapture(1)更改为webcam = cv2.VideoCapture(0)希望这对遇到相同和/或“类似”问题的每个人都有帮助