当我从 VidGear 运行 ScreenGear API 的示例代码时,它抱怨缺少
pyscreenshot
,尽管我按照 ImportError 消息中的建议使用 pip 安装了它。
screen.py
我从VidGear 的使用示例,最低限度使用中运行了此示例代码。它使用 ScreenGear 从屏幕读取帧:
from vidgear.gears import ScreenGear
import cv2
# open video stream with default parameters
stream = ScreenGear().start()
prev_frame_time = 0
new_frame_time = 0
# loop over
while True:
# read frames from stream
frame = stream.read()
# check for frame if Nonetype
if frame is None:
break
# {do something with the frame here}
gray = frame
font = cv2.FONT_HERSHEY_SIMPLEX
new_frame_time = time.time()
try:
fps = round(1 / (new_frame_time-prev_frame_time))
except:
fps = 0
prev_frame_time = new_frame_time
fps = str(fps)
cv2.putText(gray, fps, (1, 75), font, 3, (10, 100, 200), 3, cv2.LINE_AA)
# Show output window
cv2.imshow("Output Frame", gray)
# check for 'q' key if pressed
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
# close output window
cv2.destroyAllWindows()
# safely close video stream
stream.stop()
当我从
screen.py
文件运行代码时,出现此错误。
Traceback (most recent call last):
File "c:\Users\Root\Downloads\Testing\screen.py", line 5, in <module>
stream = ScreenGear().start()
File "C:\Users\Root\AppData\Local\Programs\Python\Python310\lib\site-packages\vidgear\gears\screengear.py", line 70, in __init__
import_dependency_safe("pyscreenshot" if pysct is None else "")
File "C:\Users\Root\AppData\Local\Programs\Python\Python310\lib\site-packages\vidgear\gears\helper.py", line 193, in import_dependency_safe
raise ImportError(msg) from None
ImportError: Failed to find required dependency 'pyscreenshot'. Install it with `pip install pyscreenshot` command.
然后我尝试按照
pip install pyscreenshot
推荐的方式安装它:
PS C:\Users\Root\Downloads\Testing> pip install pyscreenshot
Requirement already satisfied: pyscreenshot in c:\users\root\appdata\local\programs\python\python310\lib\site-packages (3.0)
Requirement already satisfied: mss in c:\users\root\appdata\local\programs\python\python310\lib\site-packages (from pyscreenshot) (6.1.0)
Requirement already satisfied: entrypoint2 in c:\users\root\appdata\local\programs\python\python310\lib\site-packages (from pyscreenshot) (0.2.4)
Requirement already satisfied: EasyProcess in c:\users\root\appdata\local\programs\python\python310\lib\site-packages (from pyscreenshot) (0.3)
当我再次运行
screen.py
时,它仍然显示相同的错误。
如何修复 VidGear 的 ScreenGear 的导入错误(缺少 pyscreenshot)?
我正在编写代码来显示屏幕截图并遇到了同样的问题。我使用“pip install Pillow”,我的问题解决了。
我的代码:
from vidgear.gears import ScreenGear
import cv2
import numpy as np
def show_img(image):
"""
This function shows the image
"""
img_to_show = np.array(image)
cv2.imshow("img", img_to_show)
cv2.waitKey(0)
cv2.destroyAllWindows()
stream = ScreenGear().start()
while True:
frame = stream.read()
if frame is None:
break
show_img(frame)