【问题标题】:i want to display mouse pointer in my recording我想在录音中显示鼠标指针
【发布时间】:2020-06-01 12:26:38
【问题描述】:

我正在制作一个记录桌面屏幕的屏幕录像机。但是当我录制屏幕时,鼠标指针(光标)在录制中不可见。 那么有什么方法可以在我的录音中显示鼠标指针。 这是我的代码..

import cv2
import numpy as np
import pyautogui
import datetime
date=datetime.datetime.now()

SCREEN_SIZE = (1366, 768)
framerate=12

fourcc = cv2.VideoWriter_fourcc(*'XVID')
filename='E:/project/videos/rec_%s%s%s%s%s%s.avi' %(date.year,date.month,date.day,date.hour,date.minute,date.second)

out = cv2.VideoWriter(filename, fourcc,framerate, SCREEN_SIZE)

while True:

    img = pyautogui.screenshot()

    frame = np.array(img)

    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    out.write(frame)

    cv2.imshow('screenshot', frame)

    if cv2.waitKey(1) == ord("q"):
        break

cv2.destroyAllWindows()
out.release()

【问题讨论】:

    标签: python-3.x opencv pyautogui


    【解决方案1】:

    我认为这种方法是不可能的 - 但我总是很高兴得到纠正并学到新的东西。我知道一些解决方法。

    首先是继续使用pyautogui 并调用其mouseposition() 函数并将您自己的合成鼠标指针粘贴/绘制到抓斗上。我使用 OpenCV 的 fillPoly() 函数做到了这一点:

    #!/usr/bin/env python3
    
    import cv2
    import numpy as np
    import pyautogui
    import datetime
    
    # X and Y coordinates of mouse pointer
    Xs = [0,8,6,14,12,4,2,0]
    Ys = [0,2,4,12,14,6,8,0]
    
    while True:
    
        img = pyautogui.screenshot()
        mouseX,mouseY = pyautogui.position()
        mouseX *= 2
        mouseY *= 2
    
        frame = np.array(img)
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    
        # Synthesize mouse pointer
        Xthis = [4*x+mouseX for x in Xs]
        Ythis = [4*y+mouseY for y in Ys]
        points = list(zip(Xthis,Ythis))
        points = np.array(points, 'int32')
        cv2.fillPoly(frame,[points],color=[255,255,255])
    
        # Make it a bit smaller for display
        frame = cv2.resize(frame,(960,540))
    
        cv2.imshow('Title', frame)
        if cv2.waitKey(1) == ord("q"):
            break
    
    cv2.destroyAllWindows()
    out.release()
    


    第二种是使用ffmpeg,它可以捕获鼠标——你可以运行ffmpeg来代替你当前的应用程序,或者通过管道将ffmpeg的输出通过管道传输到你的应用程序中并继续处理它就像你现在一样。可能看起来像这样:

    #!/usr/bin/env python3
    
    # ffmpeg -y -pix_fmt bgr0 -f avfoundation -r 20 -t 10 -i 1 -vf scale=w=3840:h=2160 -f rawvideo /dev/null
    
    import sys
    import cv2
    import time
    import subprocess
    import numpy as np
    
    w,h = 3840, 2160
    
    def ffmpegGrab():
        """Generator to read frames from ffmpeg subprocess"""
        cmd = [
            'ffmpeg',
            '-pix_fmt', 'bgr0',
            '-f', 'avfoundation',
            '-capture_cursor', '1',
            '-capture_mouse_clicks', '1',
            '-r', '20',
            '-i', '1',
            '-vf','scale=w=3840:h=2160',
            '-f', 'rawvideo',
            'pipe:1'
        ]
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
        while True:
            frame = proc.stdout.read(w*h*4)
            yield np.frombuffer(frame, dtype=np.uint8).reshape((h,w,4))
    
    # Get frame generator
    gen = ffmpegGrab()
    
    # Get start time
    start = time.time()
    
    # Read video frames from ffmpeg in loop
    nFrames = 0
    while True:
        # Read next frame from ffmpeg
        frame = next(gen)
        nFrames += 1
        frame = cv2.resize(frame,(960,540))
    
        cv2.imshow('screenshot', frame)
    
        if cv2.waitKey(1) == ord("q"):
            break
    
        fps = nFrames/(time.time()-start)
        print(f'FPS: {fps}')
    
    
    cv2.destroyAllWindows()
    out.release()
    

    请注意,pyautogui 在我的 Mac 上捕获一帧大约需要 600 毫秒,而上面的 ffmpeg 可以达到大约 20fps,即每帧 50 毫秒。

    关键字:Python。图像处理、ffmpeg、pyautogui、屏幕抓取、屏幕捕获、屏幕抓取、屏幕捕获、fps。速度,素数。

    【讨论】:

    • 请注意,-f-i 参数在 Windows 下会有所不同。
    • 我正在使用 Windows 操作系统,而您使用 pyautogui 显示的第一种方法不起作用,它仅在由 opencv 创建的框架中显示光标,而不是在录制时显示最小化框架窗口。
    • 对不起,我不使用 Windows。我只能建议在获得鼠标后在终端上打印鼠标位置,并在 cv2.fillPoly() 调用之前打印 points 数组。
    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 2016-12-14
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 2016-08-11
    • 2023-03-27
    • 2021-08-28
    相关资源
    最近更新 更多