Skip to content

Cannot capture images from multiple cameras on linux #2010

Open
@t-walker-wei

Description

@t-walker-wei

Now, I connect 2 Usb camers which are the same to Raspberry 4 B+, use Raspberry Pi OS base on Debian Buster.
pygame version:1.9.4.post1

When I try to capture from cameras one by one, it will blocked in the second camera when cal get_image() function.
Here is my code from :https://gist.github.com/ereli/c9998a6911c2f426cd41dd85c99cda7a

import time
from pathlib import Path
import pygame
import pygame.camera
from pygame.locals import *
from datetime import datetime

pygame.init()
pygame.camera.init()

try:
    pygame.camera.list_cameras()  # Camera detected or not
except:
    raise("couldn't read camera")
cam_list = ['cam0', 'cam1']
actual_cam_list = ['/dev/video0','/dev/video2']
active_cameras = []
for x, y in zip(actual_cam_list, cam_list):
    globals()[y] = pygame.camera.Camera(
        x, (640, 480))  # generate_function(x,'data/')
    globals()[y].start()
    active_cameras.append(globals()[y])


def init(folder):
    folder_path = Path(folder).resolve()
    try:
        folder_path.mkdir(exist_ok=False)
    except FileExistsError:
        pass


def capture(camera, folder="data/"):
    t = time.localtime()
    folder_path = Path(folder).resolve()
    init(folder)
    # print(camera)
    img = camera.get_image()
    now = datetime.now()
    timestamp = now.strftime("%H:%M:%S.%f")
    out_file = str(folder_path.joinpath(
        "img_{0}.jpg".format(timestamp)).absolute())
    print("Saving filename: {0}".format(out_file))
    pygame.image.save(img, out_file)


if __name__ == '__main__':
    for i in active_cameras:
        capture(i)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions