Skip to content

Segfault and other problems when initializing a camera without opencv #3022

Open
@bilhox

Description

@bilhox

Environment:

pygame-ce 2.5.1.dev1 (SDL 2.30.5, Python 3.12.0)
Platform:               Windows-11-10.0.22631-SP0
System:                 Windows
System Version:         10.0.22631
Processor:              AMD64 Family 23 Model 104 Stepping 1, AuthenticAMD      SSE2: Yes       AVX2: Yes       NEON: No
Architecture:           Bits: 64bit     Linkage: WindowsPE

Python:                 CPython 3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)]
pygame version:         2.5.1.dev1
SDL versions:           Linked: 2.30.5  Compiled: 2.30.5
SDL Mixer versions:     Linked: 2.8.0   Compiled: 2.8.0
SDL Font versions:      Linked: 2.22.0  Compiled: 2.22.0
SDL Image versions:     Linked: 2.8.2   Compiled: 2.8.2
Freetype versions:      Linked: 2.11.1  Compiled: 2.11.1

Display Driver:         windows
Mixer Driver:           Mixer Not Initialized

Current behavior:

Imagining you don't have opencv-python installed, and you don't force OpenCV backend with environment variables , you get a segfault when initialising a camera object.

Expected behavior:

No segfault, and a descriptive message of what is actually hapenning.

Test code

import pygame
import os
import pygame.camera
# os.environ["PYGAME_CAMERA"] = "opencv"

screen = pygame.display.set_mode([512, 512])

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

print(pygame.camera.list_cameras())

camera = pygame.camera.Camera(0)
camera.start()

version = True

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    screen.blit(camera.get_image())
    pygame.display.flip()

Other things to mention :

  • It would be interesting to tell in the wiki for compilation from source for Windows, how to get access to pygame.camera, by explaining how to compile with MSVC.
  • pygame.camera.list_cameras() is broken, sometimes it returns you a list of string (when I don't force opencv backend), sometimes a list of one integer (when I force opencv backend) and ultimately a None value.
  • When I don't force opencv backend, I get this error which is not logical :
Traceback (most recent call last):
  File "\\wsl.localhost\Ubuntu-24.04\home\rakoon\pygame-ce\a.py", line 13, in <module>
    camera = pygame.camera.Camera()
             ^^^^^^^^^^^^^^^^^^^^^^
TypeError: function takes at least 1 argument (0 given)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugNot working as intendedcamerapygame.camerasegfaultFor PRs that fix segfaults or issues containing segfaults which have an higher priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions