2

ウェブカメラでpygameを介してビデオを表示させようとしています。コードは次のとおりです。

# import the relevant libraries
import time
import pygame
import pygame.camera
from pygame.locals import *
# this is where one sets how long the script
# sleeps for, between frames.sleeptime__in_seconds = 0.05
# initialise the display window
pygame.init()
pygame.camera.init()

screen = pygame.display.set_mode((640, 480), 0, 32)

# set up a camera object
cam = pygame.camera.Camera(0)
# start the camera
cam.start()

while 1:

    # sleep between every frame
    time.sleep( 10 )
    # fetch the camera image
    image = cam.get_image()
    # blank out the screen
    screen.fill((0,0,2))
    # copy the camera image to the screen
    screen.blit( image, ( 0, 0 ) )
    # update the screen to show the latest screen image
    pygame.display.update()

これを試してみると、screen.blit(image、(0、0))partからエラーが発生します

Traceback (most recent call last):
  File "C:\Python32\src\webcam.py", line 28, in <module>
    screen.blit( image, ( 0, 0 ) )
TypeError: argument 1 must be pygame.Surface, not None

画像をpygameで動作するものに変換しなかったためだと思いますが、わかりません。

どんな助けでもいただければ幸いです。ありがとう。

-アレックス

OK、これが新しいコードです。これは、画像を現在のフォルダに保存するために機能します。最後のものが機能する理由を理解しました。画面はまだ黒いですが=\

# import the relevant libraries
import time
import pygame
import pygame.camera
from pygame.locals import *
# this is where one sets how long the script
# sleeps for, between frames.sleeptime__in_seconds = 0.05
# initialise the display window
pygame.init()
pygame.camera.init()
# set up a camera object
size = (640,480)
screen = pygame.display.set_mode(size,0)


surface = pygame.surface.Surface(size,0,screen)

cam = pygame.camera.Camera(0,size)
# start the camera
cam.start()

while 1:

    # sleep between every frame
    time.sleep( 10 )
    # fetch the camera image
    pic = cam.get_image(surface)
    # blank out the screen
    #screen.fill((0,0,0))
    # copy the camera image to the screen
    screen.blit(pic,(0,0))
    # update the screen to show the latest screen image
    p=("outimage.jpg")

    pygame.image.save(surface,p)
    pygame.display.update()
4

1 に答える 1

1

このようなカメラを作成してみてください。

cam = pygame.camera.Camera(camlist[0],(640,480))

それが、pygame docs のこのページで行われている方法です。


のAPI ページを見ると、pygame.camera役立つ可能性のある 2 つのことがわかりました。初め、

Pygame は現在、Linux と v4l2 カメラのみをサポートしています。

実験的!: この API は、後の pygame リリースで変更または消滅する可能性があります。これを使用すると、次の pygame リリースでコードが壊れる可能性が非常に高くなります。

なぜこれが驚くほど失敗するのか疑問に思うときは、そのことを覚えておいてください。

より明るいメモでは...呼び出しcamera.get_raw()て結果を印刷してみてください。生の画像データを含む文字列である必要があります。空の文字列、 、または意味のないテキストを取得した場合None: ここでそれを共有してください。カメラから何かを取得しているかどうかがわかります。

カメラのネイティブ ピクセル形式の文字列として、カメラから画像を取得します。他のライブラリとの統合に役立ちます。

于 2011-12-11T02:41:56.567 に答える