1

Can you please help me with this? I have created a Python application that records/processes video files with opencv. It worked fine with all my previous cameras.

Recently we purchased 3 models of Logitech Webcams: very good quality, FPS controllable, You can manually alter the brightness, colorfulness, distance, etc, Actually, the best cameras among all the tested ones. However, it takes too long (about 2-2.5 minutes for the Logitech cameras to start in my application).

All the other cameras, including my laptop camera, start immediately or after a few seconds. What can be the reason of such lateness? I used the code suggested in the following link for AV recording: How to capture a video (AND audio) in python, from a camera (or webcam)

John Fadria
  • 1,373
  • 2
  • 19
  • 30
  • When did you try [this simple example](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html) to stream from your camera, does this also open your camera so lately? Sharing camera models also can be helpful – Yunus Temurlenk Mar 05 '21 at 05:09

1 Answers1

0

I used that code and had the same issues. The delay to start recording was inconsistent and occasionally the threads would hang after recording and crash my program. I came up with a much simpler solution that worked well for me. I also ended up with much higher quality videos in the end. Re-encoding the openCV videos with ffmpeg would always produce some weird distortions with my video.

The solution presently only works for Windows because it uses pywinauto and the built-in Windows Camera app. The last bit of the script does some error-checking to confirm the video successfully recorded by checking the timestamp of the name of the video.

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)
mjdargen
  • 51
  • 3
  • You can change the backend on OpenCV without using other libraries – Doch88 Mar 21 '21 at 07:02
  • Thank you, mjdargen, for your answer. Trying to use your code as ffmpeg really causes problems - the app will occasionally crash when stopping recording, possibly due to too much cpu usage. However, get the attribute error. AttributeError: module 'pywinauto' has no attribute 'Desktop'. Can't resolve it. Is there a way around. – Ruzanna Ordyan May 13 '21 at 08:31
  • @RuzannaOrdyan If Desktop in pywinauto does not work for you, you can leave the Camera application open, use Application to set the focus (commented out portion of code), and send keystrokes ('SPACE') to control the camera. – mjdargen May 14 '21 at 11:58