pygame-ce icon indicating copy to clipboard operation
pygame-ce copied to clipboard

Segfault and other problems when initializing a camera without opencv

Open bilhox opened this issue 1 year ago • 3 comments
trafficstars

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)

bilhox avatar Jul 27 '24 15:07 bilhox

As discussed on discord, this an incorrect use of the API.

camera.list_cameras() gives you a list of things that can be passed into Camera() as the device name, you're not honoring that here, you're passing in something random that list_cameras did not give you. That being said it shouldn't segfault either.

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.

It's doing exactly what the docs says it does. Except IDK what you mean about getting None out of this.

When I don't force opencv backend, I get this error which is not logical :

What's not logical about it? You have to give a device, that's what the docs say.

Starbuck5 avatar Jul 28 '24 07:07 Starbuck5

What's not logical about it? You have to give a device, that's what the docs say.

Then stubs for the camera constructor are incorrect ? I see a default value for devices and other arguments.

bilhox avatar Jul 28 '24 08:07 bilhox

Yeah looks like it.

Starbuck5 avatar Jul 28 '24 08:07 Starbuck5