Python应用程序中照相机启动太晚:原因是什么?

2024-06-16 08:38:35 发布

您现在位置:Python中文网/ 问答频道 /正文

你能帮我做这个吗?我创建了一个Python应用程序,它使用opencv记录/处理视频文件。我以前所有的相机都拍得很好

最近我们购买了3款Logitech网络摄像头:质量非常好,FPS可控,可以手动改变亮度、色彩、距离等,实际上是所有测试摄像头中最好的。但是,它需要花费太长的时间(在我的应用程序中启动罗技摄像头大约需要2-2.5分钟)

所有其他摄像头,包括我的笔记本电脑摄像头,都会立即或几秒钟后启动。这么晚的原因是什么?我使用下面链接中建议的代码进行AV录制:How to capture a video (AND audio) in python, from a camera (or webcam)


Tags: 网络应用程序距离记录时间质量手动opencv
1条回答
网友
1楼 · 发布于 2024-06-16 08:38:35

我使用了那个代码,也有同样的问题。开始录制的延迟不一致,有时录制后线程会挂起并使程序崩溃。我想出了一个简单得多的解决方案,对我来说效果很好。最后,我还制作了质量更高的视频。用ffmpeg重新编码openCV视频总是会对我的视频产生一些奇怪的失真

该解决方案目前仅适用于Windows,因为它使用pywinauto和内置的Windows摄像头应用程序。脚本的最后一位执行一些错误检查,通过检查视频名称的时间戳来确认成功录制的视频

https://gist.github.com/mjdargen/956cc968864f38bfc4e20c9798c7d670

import pywinauto
import time
import subprocess
import os
import datetime

def win_record(duration):
    subprocess.run('start microsoft.windows.camera:', shell=True)  # open camera app

    # focus window by getting handle using title and class name
    # subprocess call opens camera and gets focus, but this provides alternate way
    # t, c = 'Camera', 'ApplicationFrameWindow'
    # handle = pywinauto.findwindows.find_windows(title=t, class_name=c)[0]
    # # get app and window
    # app = pywinauto.application.Application().connect(handle=handle)
    # window = app.window(handle=handle)
    # window.set_focus()  # set focus
    time.sleep(2)  # have to sleep

    # take control of camera window to take video
    desktop = pywinauto.Desktop(backend="uia")
    cam = desktop['Camera']
    # cam.print_control_identifiers()
    # make sure in video mode
    if cam.child_window(title="Switch to Video mode", auto_id="CaptureButton_1", control_type="Button").exists():
        cam.child_window(title="Switch to Video mode", auto_id="CaptureButton_1", control_type="Button").click()
    time.sleep(1)
    # start then stop video
    cam.child_window(title="Take Video", auto_id="CaptureButton_1", control_type="Button").click()
    time.sleep(duration+2)
    cam.child_window(title="Stop taking Video", auto_id="CaptureButton_1", control_type="Button").click()

    # retrieve vids from camera roll and sort
    dir = 'C:/Users/michael.dargenio/Pictures/Camera Roll'
    all_contents = list(os.listdir(dir))
    vids = [f for f in all_contents if "_Pro.mp4" in f]
    vids.sort()
    vid = vids[-1]
    # compute time difference
    vid_time = vid.replace('WIN_', '').replace('_Pro.mp4', '')
    vid_time = datetime.datetime.strptime(vid_time, '%Y%m%d_%H_%M_%S')
    now = datetime.datetime.now()
    diff = now - vid_time
    # time different greater than 2 minutes, assume something wrong & quit
    if diff.seconds > 120:
        quit()
    
    subprocess.run('Taskkill /IM WindowsCamera.exe /F', shell=True)  # close camera app
    print('Recorded successfully!')


win_record(2)

相关问题 更多 >