Pygame Tutorials - Camera Module Introduction — pygame v2.0.0.dev5 documentation
Pygame Tutorials - Camera Module Introduction — pygame v2.0.0.dev5 documentation
dev5 documentation
Importer et initialiser
import pygame
import pygame.camera
from pygame.locals import *
pygame.init()
pygame.camera.init()
Comme le module de caméra est facultatif, il doit être importé et initialisé manuellement comme indiqué ci-
dessus.
https://www.pygame.org/docs/tut/CameraIntro.html 1/6
15/04/2020 Pygame Tutorials - Camera Module Introduction — pygame v2.0.0.dev5 documentation
def get_and_flip(self):
# if you don't want to tie the framerate to the camera, you can check
# if the camera has an image ready. note that while this works
# on most cameras, some will never return true.
if self.cam.query_image():
self.snapshot = self.cam.get_image(self.snapshot)
def main(self):
going = True
while going:
events = pygame.event.get()
for e in events:
if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
# close the camera safely
self.cam.stop()
going = False
self.get_and_flip()
Since get_image() is a blocking call that could take quite a bit of time on a slow camera, this example uses
query_image() to see if the camera is ready. This allows you to separate the framerate of your game from
that of your camera. It is also possible to have the camera capturing images in a separate thread, for
approximately the same performance gain, if you find that your camera does not support the query_image()
function correctly.
Colorspaces
https://www.pygame.org/docs/tut/CameraIntro.html 2/6
15/04/2020 Pygame Tutorials - Camera Module Introduction — pygame v2.0.0.dev5 documentation
When initializing a camera, colorspace is an optional parameter, with 'RGB', 'YUV', and 'HSV' as the
possible choices. YUV and HSV are both generally more useful for computer vision than RGB, and allow
you to more easily threshold by color, something we will look at later in the tutorial.
self.cam = pygame.camera.Camera(self.clist[0], self.size, "RGB")
Thresholding
Using the threshold() function from the transform module, one can do simple green screen like effects, or
isolate specifically colored objects in a scene. In the below example, we threshold out just the green tree
and make the rest of the image black. Check the reference documentation for details on the threshold
function.
https://www.pygame.org/docs/tut/CameraIntro.html 3/6
15/04/2020 Pygame Tutorials - Camera Module Introduction — pygame v2.0.0.dev5 documentation
Of course, this is only useful if you already know the exact color of the object you are looking for. To get
around this and make thresholding usable in the real world, we need to add a calibration stage where we
identify the color of an object and use it to threshold against. We will be using the average_color() function
of the transform module to do this. Below is an example calibration function that you could loop until an
event like a key press, and an image of what it would look like. The color inside the box will be the one that
is used for the threshold. Note that we are using the HSV colorspace in the below images.
def calibrate(self):
# capture the image
self.snapshot = self.cam.get_image(self.snapshot)
# blit it to the display surface
self.display.blit(self.snapshot, (0,0))
# make a rect in the middle of the screen
crect = pygame.draw.rect(self.display, (255,0,0), (145,105,30,30), 4)
# get the average color of the area inside the rect
self.ccolor = pygame.transform.average_color(self.snapshot, crect)
# fill the upper left corner with that color
self.display.fill(self.ccolor, (0,0,50,50))
pygame.display.flip()
pygame.transform.threshold(self.thresholded,self.snapshot,self.ccolor,(30,30,30),(0,0,0),2)
https://www.pygame.org/docs/tut/CameraIntro.html 4/6
15/04/2020 Pygame Tutorials - Camera Module Introduction — pygame v2.0.0.dev5 documentation
You can use the same idea to do a simple green screen/blue screen, by first getting a background image
and then thresholding against it. The below example just has the camera pointed at a blank white wall in
HSV colorspace.
def calibrate(self):
# capture a bunch of background images
bg = []
for i in range(0,5):
bg.append(self.cam.get_image(self.background))
# average them down to one to get rid of some noise
pygame.transform.average_surfaces(bg,self.background)
# blit it to the display surface
self.display.blit(self.background, (0,0))
pygame.display.flip()
pygame.transform.threshold(self.thresholded,self.snapshot,(0,255,0),(30,30,30),(0,0,0),1,self.background)
pour revenir à l'exemple du seuillage d'un objet spécifique, nous pouvons trouver la position de cet objet et
l'utiliser pour contrôler un objet à l'écran.mask module
def get_and_flip(self):
self.snapshot = self.cam.get_image(self.snapshot)
# threshold against the color we got before
mask = pygame.mask.from_threshold(self.snapshot, self.ccolor, (30, 30, 30))
self.display.blit(self.snapshot,(0,0))
# keep only the largest blob of that color
connected = mask.connected_component()
# make sure the blob is big enough that it isn't just noise
if mask.count() > 100:
# find the center of the blob
coord = mask.centroid()
# draw a circle with size variable on the size of the blob
pygame.draw.circle(self.display, (0,255,0), coord, max(min(50,mask.count()/400),5))
pygame.display.flip()
Ceci est juste l'exemple le plus élémentaire. Vous pouvez suivre plusieurs gouttes de couleur différentes,
trouver les contours des objets, avoir une détection de collision entre la vie réelle et les objets de jeu,
obtenir l'angle d'un objet pour permettre un contrôle encore plus fin, et plus encore. S'amuser!
https://www.pygame.org/docs/tut/CameraIntro.html 6/6