FongEye
Wednesday, July 24, 2013
Tuesday, July 02, 2013
MCP23S17 old blog posts to refresh memory
So it was around May 12 when I started playing with MCP23S17.
MCP23S17 board assembly notes Sunday, May 12, 2013
http://tlfong01.blogspot.hk/2013/05/mcp23s17-board-assembly-notes.html
Now I am thinking of playing with SPI MCP23S17. Months ago I started with MCP23008 and MCP23017 I2C and find them interesting. But later I played with Guzunty Pi which uses SPI, and therefore a bit awkard to integrate with I2C MCP23017.
So I am now reading the datasheet again.
...
.END
MCP23S17 board assembly notes Sunday, May 12, 2013
http://tlfong01.blogspot.hk/2013/05/mcp23s17-board-assembly-notes.html
Now I am thinking of playing with SPI MCP23S17. Months ago I started with MCP23008 and MCP23017 I2C and find them interesting. But later I played with Guzunty Pi which uses SPI, and therefore a bit awkard to integrate with I2C MCP23017.
So I am now reading the datasheet again.
...
.END
Wednesday, June 19, 2013
pygame.transform learning notes
pygame.transform - pygame module to transform surfaces
http://www.pygame.org/docs/ref/transform.html
pygame.transform.flip — flip vertically and horizontally
pygame.transform.scale — resize to new resolution
pygame.transform.rotate — rotate an image
pygame.transform.rotozoom — filtered scale and rotation
pygame.transform.scale2x — specialized image doubler
pygame.transform.smoothscale — scale a surface to an arbitrary size smoothly
pygame.transform.get_smoothscale_backend — return smoothscale filter version in use: ‘GENERIC’, ‘MMX’, or ‘SSE’
pygame.transform.set_smoothscale_backend — set smoothscale filter version to one of: ‘GENERIC’, ‘MMX’, or ‘SSE’
pygame.transform.chop — gets a copy of an image with an interior area removed
pygame.transform.laplacian — find edges in a surface
pygame.transform.average_surfaces — find the average surface from many surfaces.
pygame.transform.average_color — finds the average color of a surface
pygame.transform.threshold — finds which, and how many pixels in a surface are within a threshold of a color.
A Surface transform is an operation that moves or resizes the pixels. All these functions take a Surface to operate on and return a new Surface with the results.
Some of the transforms are considered destructive. These means every time they are performed they lose pixel data. Common examples of this are resizing and rotating. For this reason, it is better to retransform the original surface than to keep transforming an image multiple times. (For example, suppose you are animating a bouncing spring which expands and contracts. If you applied the size changes incrementally to the previous images, you would lose detail. Instead, always begin with the original image and scale to the desired size.)
pygame.transform.flip()
flip vertically and horizontally
flip(Surface, xbool, ybool) -> Surface
This can flip a Surface either vertically, horizontally, or both. Flipping a Surface is nondestructive and returns a new Surface with the same dimensions.
pygame.transform.scale()
resize to new resolution
scale(Surface, (width, height), DestSurface = None) -> Surface
Resizes the Surface to a new resolution. This is a fast scale operation that does not sample the results.
An optional destination surface can be used, rather than have it create a new one. This is quicker if you want to repeatedly scale something. However the destination must be the same size as the (width, height) passed in. Also the destination surface must be the same format.
pygame.transform.rotate()
rotate an image
rotate(Surface, angle) -> Surface
Unfiltered counterclockwise rotation. The angle argument represents degrees and can be any floating point value. Negative angle amounts will rotate clockwise.
Unless rotating by 90 degree increments, the image will be padded larger to hold the new size. If the image has pixel alphas, the padded area will be transparent. Otherwise pygame will pick a color that matches the Surface colorkey or the topleft pixel value.
pygame.transform.rotozoom()
filtered scale and rotation
rotozoom(Surface, angle, scale) -> Surface
This is a combined scale and rotation transform. The resulting Surface will be a filtered 32-bit Surface. The scale argument is a floating point value that will be multiplied by the current resolution. The angle argument is a floating point value that represents the counterclockwise degrees to rotate. A negative rotation angle will rotate clockwise.
pygame.transform.scale2x()
specialized image doubler
scale2x(Surface, DestSurface = None) -> Surface
This will return a new image that is double the size of the original. It uses the AdvanceMAME Scale2X algorithm which does a ‘jaggie-less’ scale of bitmap graphics.
This really only has an effect on simple images with solid colors. On photographic and antialiased images it will look like a regular unfiltered scale.
An optional destination surface can be used, rather than have it create a new one. This is quicker if you want to repeatedly scale something. However the destination must be twice the size of the source surface passed in. Also the destination surface must be the same format.
pygame.transform.smoothscale()
scale a surface to an arbitrary size smoothly
smoothscale(Surface, (width, height), DestSurface = None) -> Surface
Uses one of two different algorithms for scaling each dimension of the input surface as required. For shrinkage, the output pixels are area averages of the colors they cover. For expansion, a bilinear filter is used. For the amd64 and i686 architectures, optimized MMX routines are included and will run much faster than other machine types. The size is a 2 number sequence for (width, height). This function only works for 24-bit or 32-bit surfaces. An exception will be thrown if the input surface bit depth is less than 24.
pygame.transform.get_smoothscale_backend()
return smoothscale filter version in use: ‘GENERIC’, ‘MMX’, or ‘SSE’
get_smoothscale_backend() -> String
Shows whether or not smoothscale is using MMX or SSE acceleration. If no acceleration is available then “GENERIC” is returned. For a x86 processor the level of acceleration to use is determined at runtime.
This function is provided for Pygame testing and debugging.
pygame.transform.set_smoothscale_backend()
set smoothscale filter version to one of: ‘GENERIC’, ‘MMX’, or ‘SSE’
set_smoothscale_backend(type) -> None
Sets smoothscale acceleration. Takes a string argument. A value of ‘GENERIC’ turns off acceleration. ‘MMX’ uses MMX instructions only. ‘SSE’ allows SSE extensions as well. A value error is raised if type is not recognized or not supported by the current processor.
This function is provided for Pygame testing and debugging. If smoothscale causes an invalid instruction error then it is a Pygame/SDL bug that should be reported. Use this function as a temporary fix only.
pygame.transform.chop()
gets a copy of an image with an interior area removed
chop(Surface, rect) -> Surface
Extracts a portion of an image. All vertical and horizontal pixels surrounding the given rectangle area are removed. The corner areas (diagonal to the rect) are then brought together. (The original image is not altered by this operation.)
NOTE: If you want a “crop” that returns the part of an image within a rect, you can blit with a rect to a new surface or copy a subsurface.
pygame.transform.laplacian()
find edges in a surface
laplacian(Surface, DestSurface = None) -> Surface
Finds the edges in a surface using the laplacian algorithm.
pygame.transform.average_surfaces()
find the average surface from many surfaces.
average_surfaces(Surfaces, DestSurface = None, palette_colors = 1) -> Surface
Takes a sequence of surfaces and returns a surface with average colors from each of the surfaces.
palette_colors - if true we average the colors in palette, otherwise we average the pixel values. This is useful if the surface is actually greyscale colors, and not palette colors.
Note, this function currently does not handle palette using surfaces correctly.
pygame.transform.average_color()
finds the average color of a surface
average_color(Surface, Rect = None) -> Color
Finds the average color of a Surface or a region of a surface specified by a Rect, and returns it as a Color.
pygame.transform.threshold()
finds which, and how many pixels in a surface are within a threshold of a color.
threshold(DestSurface, Surface, color, threshold = (0,0,0,0), diff_color = (0,0,0,0), change_return = 1, Surface = None, inverse = False) -> num_threshold_pixels
Finds which, and how many pixels in a surface are within a threshold of a color.
It can set the destination surface where all of the pixels not within the threshold are changed to diff_color. If inverse is optionally set to True, the pixels that are within the threshold are instead changed to diff_color.
If the optional second surface is given, it is used to threshold against rather than the specified color. That is, it will find each pixel in the first Surface that is within the threshold of the pixel at the same coordinates of the second Surface.
If change_return is set to 0, it can be used to just count the number of pixels within the threshold if you set
If change_return is set to 1, the pixels set in DestSurface will be those from the color.
If change_return is set to 2, the pixels set in DestSurface will be those from the first Surface.
You can use a threshold of (r,g,b,a) where the r,g,b can have different thresholds. So you could use an r threshold of 40 and a blue threshold of 2 if you like.
.END
http://www.pygame.org/docs/ref/transform.html
pygame.transform.flip — flip vertically and horizontally
pygame.transform.scale — resize to new resolution
pygame.transform.rotate — rotate an image
pygame.transform.rotozoom — filtered scale and rotation
pygame.transform.scale2x — specialized image doubler
pygame.transform.smoothscale — scale a surface to an arbitrary size smoothly
pygame.transform.get_smoothscale_backend — return smoothscale filter version in use: ‘GENERIC’, ‘MMX’, or ‘SSE’
pygame.transform.set_smoothscale_backend — set smoothscale filter version to one of: ‘GENERIC’, ‘MMX’, or ‘SSE’
pygame.transform.chop — gets a copy of an image with an interior area removed
pygame.transform.laplacian — find edges in a surface
pygame.transform.average_surfaces — find the average surface from many surfaces.
pygame.transform.average_color — finds the average color of a surface
pygame.transform.threshold — finds which, and how many pixels in a surface are within a threshold of a color.
A Surface transform is an operation that moves or resizes the pixels. All these functions take a Surface to operate on and return a new Surface with the results.
Some of the transforms are considered destructive. These means every time they are performed they lose pixel data. Common examples of this are resizing and rotating. For this reason, it is better to retransform the original surface than to keep transforming an image multiple times. (For example, suppose you are animating a bouncing spring which expands and contracts. If you applied the size changes incrementally to the previous images, you would lose detail. Instead, always begin with the original image and scale to the desired size.)
pygame.transform.flip()
flip vertically and horizontally
flip(Surface, xbool, ybool) -> Surface
This can flip a Surface either vertically, horizontally, or both. Flipping a Surface is nondestructive and returns a new Surface with the same dimensions.
pygame.transform.scale()
resize to new resolution
scale(Surface, (width, height), DestSurface = None) -> Surface
Resizes the Surface to a new resolution. This is a fast scale operation that does not sample the results.
An optional destination surface can be used, rather than have it create a new one. This is quicker if you want to repeatedly scale something. However the destination must be the same size as the (width, height) passed in. Also the destination surface must be the same format.
pygame.transform.rotate()
rotate an image
rotate(Surface, angle) -> Surface
Unfiltered counterclockwise rotation. The angle argument represents degrees and can be any floating point value. Negative angle amounts will rotate clockwise.
Unless rotating by 90 degree increments, the image will be padded larger to hold the new size. If the image has pixel alphas, the padded area will be transparent. Otherwise pygame will pick a color that matches the Surface colorkey or the topleft pixel value.
pygame.transform.rotozoom()
filtered scale and rotation
rotozoom(Surface, angle, scale) -> Surface
This is a combined scale and rotation transform. The resulting Surface will be a filtered 32-bit Surface. The scale argument is a floating point value that will be multiplied by the current resolution. The angle argument is a floating point value that represents the counterclockwise degrees to rotate. A negative rotation angle will rotate clockwise.
pygame.transform.scale2x()
specialized image doubler
scale2x(Surface, DestSurface = None) -> Surface
This will return a new image that is double the size of the original. It uses the AdvanceMAME Scale2X algorithm which does a ‘jaggie-less’ scale of bitmap graphics.
This really only has an effect on simple images with solid colors. On photographic and antialiased images it will look like a regular unfiltered scale.
An optional destination surface can be used, rather than have it create a new one. This is quicker if you want to repeatedly scale something. However the destination must be twice the size of the source surface passed in. Also the destination surface must be the same format.
pygame.transform.smoothscale()
scale a surface to an arbitrary size smoothly
smoothscale(Surface, (width, height), DestSurface = None) -> Surface
Uses one of two different algorithms for scaling each dimension of the input surface as required. For shrinkage, the output pixels are area averages of the colors they cover. For expansion, a bilinear filter is used. For the amd64 and i686 architectures, optimized MMX routines are included and will run much faster than other machine types. The size is a 2 number sequence for (width, height). This function only works for 24-bit or 32-bit surfaces. An exception will be thrown if the input surface bit depth is less than 24.
pygame.transform.get_smoothscale_backend()
return smoothscale filter version in use: ‘GENERIC’, ‘MMX’, or ‘SSE’
get_smoothscale_backend() -> String
Shows whether or not smoothscale is using MMX or SSE acceleration. If no acceleration is available then “GENERIC” is returned. For a x86 processor the level of acceleration to use is determined at runtime.
This function is provided for Pygame testing and debugging.
pygame.transform.set_smoothscale_backend()
set smoothscale filter version to one of: ‘GENERIC’, ‘MMX’, or ‘SSE’
set_smoothscale_backend(type) -> None
Sets smoothscale acceleration. Takes a string argument. A value of ‘GENERIC’ turns off acceleration. ‘MMX’ uses MMX instructions only. ‘SSE’ allows SSE extensions as well. A value error is raised if type is not recognized or not supported by the current processor.
This function is provided for Pygame testing and debugging. If smoothscale causes an invalid instruction error then it is a Pygame/SDL bug that should be reported. Use this function as a temporary fix only.
pygame.transform.chop()
gets a copy of an image with an interior area removed
chop(Surface, rect) -> Surface
Extracts a portion of an image. All vertical and horizontal pixels surrounding the given rectangle area are removed. The corner areas (diagonal to the rect) are then brought together. (The original image is not altered by this operation.)
NOTE: If you want a “crop” that returns the part of an image within a rect, you can blit with a rect to a new surface or copy a subsurface.
pygame.transform.laplacian()
find edges in a surface
laplacian(Surface, DestSurface = None) -> Surface
Finds the edges in a surface using the laplacian algorithm.
pygame.transform.average_surfaces()
find the average surface from many surfaces.
average_surfaces(Surfaces, DestSurface = None, palette_colors = 1) -> Surface
Takes a sequence of surfaces and returns a surface with average colors from each of the surfaces.
palette_colors - if true we average the colors in palette, otherwise we average the pixel values. This is useful if the surface is actually greyscale colors, and not palette colors.
Note, this function currently does not handle palette using surfaces correctly.
pygame.transform.average_color()
finds the average color of a surface
average_color(Surface, Rect = None) -> Color
Finds the average color of a Surface or a region of a surface specified by a Rect, and returns it as a Color.
pygame.transform.threshold()
finds which, and how many pixels in a surface are within a threshold of a color.
threshold(DestSurface, Surface, color, threshold = (0,0,0,0), diff_color = (0,0,0,0), change_return = 1, Surface = None, inverse = False) -> num_threshold_pixels
Finds which, and how many pixels in a surface are within a threshold of a color.
It can set the destination surface where all of the pixels not within the threshold are changed to diff_color. If inverse is optionally set to True, the pixels that are within the threshold are instead changed to diff_color.
If the optional second surface is given, it is used to threshold against rather than the specified color. That is, it will find each pixel in the first Surface that is within the threshold of the pixel at the same coordinates of the second Surface.
If change_return is set to 0, it can be used to just count the number of pixels within the threshold if you set
If change_return is set to 1, the pixels set in DestSurface will be those from the color.
If change_return is set to 2, the pixels set in DestSurface will be those from the first Surface.
You can use a threshold of (r,g,b,a) where the r,g,b can have different thresholds. So you could use an r threshold of 40 and a blue threshold of 2 if you like.
.END
C270 15cm small object (5mm screws and nuts) test
Now I am testing C270 again, this time adding small objects of 5mm screws, washers, and nuts.
.END
ftwebcam.TestWebcamCamera09(cameraNumber = 0, imageSize = (1280, 720), imageCount = 3000, timerSecond = 0.25, fileName = "file20130618hkt1725.bmp")
ef TestWebcamCamera10(cameraNumber, imageSize, imageCount, timerSecond, fileName):
ftprint.PrintDoubleSpaceLine("*** Sample run begin - TestWebCamCamera08() 2013jun18hkt10:54 ***")
pygame.init()
pygame.camera.init()
webCamList = pygame.camera.list_cameras()
webCamCount = len(webCamList)
print "Number of cameras found = ", webCamCount
print "Camera used = ", cameraNumber
print "Image size = (", imageSize[0], ", ", imageSize[1], ")"
print "Number of images to get = ", imageCount
print "Time between images (in seconds) = ", timerSecond
imageSize = Size1280x720 # debug only !!!
webCamCamera = pygame.camera.Camera(webCamList[int(cameraNumber)], imageSize)
webCamCamera.start()
pygame.display.init()
webCamDisplay = pygame.display.set_mode(imageSize, 0)
webCamSurface = pygame.surface.Surface(imageSize, 0, webCamDisplay)
for count in range(imageCount):
for event in pygame.event.get(): # Escape key from local keyboard, NOT PuTTY!!!
if (event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE)):
webCamCamera.stop()
pygame.quit()
return
if (event.type == KEYDOWN and event.key == K_x):
webCamCamera.stop()
pygame.quit()
return
if (event.type == KEYDOWN and event.key == K_s):
fullPathFileName = os.path.join('/home/pi/fongtoy/', fileName)
pygame.image.save(webCamSurface, fullPathFileName)
print "Image saved"
if webCamCamera.query_image():
webCamSurface = webCamCamera.get_image(webCamSurface)
webCamDisplay.blit(webCamSurface, (0,0))
pygame.display.flip()
time.sleep(timerSecond)
print "Image number = ", count
pygame.image.save(webCamSurface, "/home/pi/fongtoy/testImage.bmp")
print "File name of last image = /home/pi/fongtoy/testImage.bmp",
ftprint.PrintDoubleSpaceLine("*** Sample run end ***")
# *** sample call ***
# ftwebcam.TestWebcamCamera09(cameraNumber = 1,
# imageSize = (1280, 720),
# imageCount = 10,
# timerSecond = 1,
# fileName = "file20130618hkt1725.bmp")
def TestWebcamCamera09(cameraNumber, imageSize, imageCount, timerSecond, fileName):
ftprint.PrintDoubleSpaceLine("*** Sample run begin - TestWebCamCamera08() 2013jun18hkt10:54 ***")
pygame.init()
pygame.camera.init()
webCamList = pygame.camera.list_cameras()
webCamCount = len(webCamList)
print "Number of cameras found = ", webCamCount
print "Camera used = ", cameraNumber
print "Image size = (", imageSize[0], ", ", imageSize[1], ")"
print "Number of images to get = ", imageCount
print "Time between images (in seconds) = ", timerSecond
imageSize = Size1280x720 # debug only !!!
webCamCamera = pygame.camera.Camera(webCamList[int(cameraNumber)], imageSize)
webCamCamera.start()
pygame.display.init()
webCamDisplay = pygame.display.set_mode(imageSize, 0)
webCamSurface = pygame.surface.Surface(imageSize, 0, webCamDisplay)
for count in range(imageCount):
for event in pygame.event.get(): # Escape key from local keyboard, NOT PuTTY!!!
if (event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE)):
webCamCamera.stop()
pygame.quit()
return
if (event.type == KEYDOWN and event.key == K_x):
webCamCamera.stop()
pygame.quit()
return
if (event.type == KEYDOWN and event.key == K_s):
fullPathFileName = os.path.join('/home/pi/fongtoy/', fileName)
pygame.image.save(webCamSurface, fullPathFileName)
print "Image saved"
if webCamCamera.query_image():
webCamSurface = webCamCamera.get_image(webCamSurface)
webCamDisplay.blit(webCamSurface, (0,0))
pygame.display.flip()
time.sleep(timerSecond)
print "Image number = ", count
pygame.image.save(webCamSurface, "/home/pi/fongtoy/testImage.bmp")
print "File name of last image = /home/pi/fongtoy/testImage.bmp",
ftprint.PrintDoubleSpaceLine("*** Sample run end ***")
def TestWebcamCamera08(cameraNumber, imageSize, imageCount, timerSecond):
ftprint.PrintDoubleSpaceLine("*** Sample run begin - TestWebCamCamera09() 2013jun18hkt16:48 ***")
pygame.init()
pygame.camera.init()
webCamList = pygame.camera.list_cameras()
webCamCount = len(webCamList)
print "Number of cameras found = ", webCamCount
print "Camera used = ", cameraNumber
print "Image size = (", imageSize[0], ", ", imageSize[1], ")"
print "Number of images to get = ", imageCount
print "Time between images (in seconds) = ", timerSecond
webCamCamera = pygame.camera.Camera(webCamList[int(cameraNumber)], imageSize)
webCamCamera.start()
pygame.display.init()
webCamDisplay = pygame.display.set_mode(imageSize, 0)
webCamSurface = pygame.surface.Surface(imageSize, 0, webCamDisplay)
for count in range(imageCount):
for event in pygame.event.get(): # Escape key from local keyboard, NOT PuTTY!!!
if (event.type == QUIT or
(event.type == KEYDOWN and event.key == K_ESCAPE) or
(event.type == KEYDOWN and event.key == K_x)):
webCamCamera.stop()
pygame.quit()
return
if webCamCamera.query_image():
webCamSurface = webCamCamera.get_image(webCamSurface)
webCamDisplay.blit(webCamSurface, (0,0))
pygame.display.flip()
time.sleep(timerSecond)
print "Image number = ", count
pygame.image.save(webCamSurface, "/home/pi/fongtoy/testImage.bmp")
print "File name of last image = /home/pi/fongtoy/testImage.bmp",
ftprint.PrintDoubleSpaceLine("*** Sample run end ***")
.END2
Logitech C920 testing notes
Now I am testing Logitech C920, with and without LED lighting. I found this time C920 auto focus results appears to be worse than C270. This is a bit strange.
.END
Logitech C270 testing notes
Now I have not hung the LED lamp on the lamp post. Instead, I placed the lamp behind the 2 cameras. I tested C270 with and without LED lighting. I found that indoor with indirect sunlight, the images captured are good, and even better than with the a bit too strong LED light.
.END
Webcam and lighting 3 in 1 setup up notes
I found it tedious to adjust the webcam and lighting positions every time. So I am thinking of placing 3 things into 1 group.
.END
# *****************************************************************************
# Function - TestWebcam()
# Notes -
# * BenQ 737s 17" LCD monitor native Resolution 1280 x 1024
# * C920 image size = HD1080 (1920 x 1080), HD720 (1280 x 720)
# * pygame error: No video mode large enough for 1920 x 1080
# * $ lsusb -d 046d:0825 Logitech Webcam C270
# * $ lsusb -d 046d:082d Logitech Webcam C920
# *****************************************************************************
Size640x480 = (640, 480)
Size1280x720 = (1280, 720)
Size1280x1024 = (1280, 1024) # ValueError: Destination surface not the correct width or height
Size1920x1080 = (1920, 1080) # pygame.error: No video mode large enough for 1920x1080
def TestWebcamCamera10(cameraNumber, imageSize, imageCount, timerSecond, fileName):
ftprint.PrintDoubleSpaceLine("*** Sample run begin - TestWebCamCamera08() 2013jun18hkt10:54 ***")
pygame.init()
pygame.camera.init()
webCamList = pygame.camera.list_cameras()
webCamCount = len(webCamList)
print "Number of cameras found = ", webCamCount
print "Camera used = ", cameraNumber
print "Image size = (", imageSize[0], ", ", imageSize[1], ")"
print "Number of images to get = ", imageCount
print "Time between images (in seconds) = ", timerSecond
imageSize = Size1280x720 # debug only !!!
webCamCamera = pygame.camera.Camera(webCamList[int(cameraNumber)], imageSize)
webCamCamera.start()
pygame.display.init()
webCamDisplay = pygame.display.set_mode(imageSize, 0)
webCamSurface = pygame.surface.Surface(imageSize, 0, webCamDisplay)
for count in range(imageCount):
for event in pygame.event.get(): # Escape key from local keyboard, NOT PuTTY!!!
if (event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE)):
webCamCamera.stop()
pygame.quit()
return
if (event.type == KEYDOWN and event.key == K_x):
webCamCamera.stop()
pygame.quit()
return
if (event.type == KEYDOWN and event.key == K_s):
fullPathFileName = os.path.join('/home/pi/fongtoy/', fileName)
pygame.image.save(webCamSurface, fullPathFileName)
print "Image saved"
if webCamCamera.query_image():
webCamSurface = webCamCamera.get_image(webCamSurface)
webCamDisplay.blit(webCamSurface, (0,0))
pygame.display.flip()
time.sleep(timerSecond)
print "Image number = ", count
pygame.image.save(webCamSurface, "/home/pi/fongtoy/testImage.bmp")
print "File name of last image = /home/pi/fongtoy/testImage.bmp",
ftprint.PrintDoubleSpaceLine("*** Sample run end ***")
# *** sample call ***
# ftwebcam.TestWebcamCamera09(cameraNumber = 1,
# imageSize = (1280, 720),
# imageCount = 10,
# timerSecond = 1,
# fileName = "file20130618hkt1725.bmp")
.END
Logitech web cam and UVC, V4L2, Pygame, OpenCV etc
Now I have skimmed through some pages about UVC, V4L2, and confirmed the following.
1. C270, Quickcam Sphere AF are supported by UVC
2. C910 is supported, but not sure about C920
Pygame and OpenCV support V4L2.
Linux UVC driver and tools Supported devices
http://www.ideasonboard.org/uvc/
The following table lists the UVC features supported by the Linux UVC driver.
UVC Feature Status Comments
Camera input terminal Yes
USB Streaming terminal Yes
Extension units can be accessed directly or mapped to V4L2 controls. They require a user-space management tool.
Still image capture No
Video streaming Yes
046d:0825 Logitech HD Webcam C270
046d:0826 Logitech HD Webcam C525
046d:0821 Logitech Portable Webcam C910
046d:0994 Logitech Quickcam Sphere AF
Identifying Your Webcam
Although webcams may look quite different from one another, they often share similar or identical major components. This means that drivers may work for many different makes and models with little or no modification.
To definitively identify your webcam, the following information will be of use:
Make and Model Name/Number.
Any version or revision information. This is often printed on the device or its packaging and will read something similar to REV 01 or VER. 2.2. This information can be particularly useful.
The output of the lsusb. Type lsusb -v into a terminal window.
This program prints information about the devices connected to the USB bus. If you scroll through it, you should find some information about your webcam.
Type lsusb -n into a terminal window. This version of the command lists the device's USB ID; a number that is unique to every device.
Video4Linux From Wikipedia
https://en.wikipedia.org/wiki/Video4Linux
Video4Linux or V4L is a video capture application programming interface for Linux, supporting many USB webcams ...
V4L2
V4L2 is the second version of V4L. ...
Software supporting Video4Linux
...
OpenCV
PyGame
.END
1. C270, Quickcam Sphere AF are supported by UVC
2. C910 is supported, but not sure about C920
Pygame and OpenCV support V4L2.
Linux UVC driver and tools Supported devices
http://www.ideasonboard.org/uvc/
The following table lists the UVC features supported by the Linux UVC driver.
UVC Feature Status Comments
Camera input terminal Yes
USB Streaming terminal Yes
Extension units can be accessed directly or mapped to V4L2 controls. They require a user-space management tool.
Still image capture No
Video streaming Yes
046d:0825 Logitech HD Webcam C270
046d:0826 Logitech HD Webcam C525
046d:0821 Logitech Portable Webcam C910
046d:0994 Logitech Quickcam Sphere AF
Identifying Your Webcam
Although webcams may look quite different from one another, they often share similar or identical major components. This means that drivers may work for many different makes and models with little or no modification.
To definitively identify your webcam, the following information will be of use:
Make and Model Name/Number.
Any version or revision information. This is often printed on the device or its packaging and will read something similar to REV 01 or VER. 2.2. This information can be particularly useful.
The output of the lsusb. Type lsusb -v into a terminal window.
This program prints information about the devices connected to the USB bus. If you scroll through it, you should find some information about your webcam.
Type lsusb -n into a terminal window. This version of the command lists the device's USB ID; a number that is unique to every device.
Video4Linux From Wikipedia
https://en.wikipedia.org/wiki/Video4Linux
Video4Linux or V4L is a video capture application programming interface for Linux, supporting many USB webcams ...
V4L2
V4L2 is the second version of V4L. ...
Software supporting Video4Linux
...
OpenCV
PyGame
.END
Tuesday, June 18, 2013
Computer vision tutorial for pygame.camera newbies - Nirav Patel
Now that I know how to capture images using pygame.camera, I am moving on the computer vision for newbies. I read the following thing and found too many technical jargons. I guess I should first read colour basics, then tutorials of the pygame.transformation, pygame.threshold etc.
Perhaps I should also read the following or other articles about v4l, uvc etc
https://help.ubuntu.com/community/Webcam
I call it a day.
Camera Module Introduction by Nirav Patel nrp@eclecti.cc Revision 1.0, May 25th, 2009
http://www.pygame.org/docs/tut/camera/CameraIntro.html
Import and Init
...
Capturing a Single Image
...
Capturing a Live Stream
...
Basic Computer Vision
By using the camera, transform, and mask modules, pygame can do some basic computer vision.
Colorspaces
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")
self.cam = pygame.camera.Camera(self.clist[0], self.size, "YUV")
self.cam = pygame.camera.Camera(self.clist[0], self.size, "HSV")
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.
self.thresholded = pygame.surface.Surface(self.size, 0, self.display)
self.snapshot = self.cam.get_image(self.snapshot)
pygame.transform.threshold(self.thresholded,self.snapshot,(0,255,0),(90,170,170),(0,0,0),2)
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)
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)
Using the Mask Module
The stuff above is great if you just want to display images, but with the mask module, you can also use a camera as an input device for a game. For example, going back to the example of thresholding out a specific object, we can find the position of that object and use it to control an on screen object.
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()
This is just the most basic example. You can track multiple different colored blobs, find the outlines of objects, have collision detection between real life and in game objects, get the angle of an object to allow for even finer control, and more. Have fun!
.END
Perhaps I should also read the following or other articles about v4l, uvc etc
https://help.ubuntu.com/community/Webcam
I call it a day.
Camera Module Introduction by Nirav Patel nrp@eclecti.cc Revision 1.0, May 25th, 2009
http://www.pygame.org/docs/tut/camera/CameraIntro.html
Import and Init
...
Capturing a Single Image
...
Capturing a Live Stream
...
Basic Computer Vision
By using the camera, transform, and mask modules, pygame can do some basic computer vision.
Colorspaces
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")
self.cam = pygame.camera.Camera(self.clist[0], self.size, "YUV")
self.cam = pygame.camera.Camera(self.clist[0], self.size, "HSV")
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.
self.thresholded = pygame.surface.Surface(self.size, 0, self.display)
self.snapshot = self.cam.get_image(self.snapshot)
pygame.transform.threshold(self.thresholded,self.snapshot,(0,255,0),(90,170,170),(0,0,0),2)
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)
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)
Using the Mask Module
The stuff above is great if you just want to display images, but with the mask module, you can also use a camera as an input device for a game. For example, going back to the example of thresholding out a specific object, we can find the position of that object and use it to control an on screen object.
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()
This is just the most basic example. You can track multiple different colored blobs, find the outlines of objects, have collision detection between real life and in game objects, get the angle of an object to allow for even finer control, and more. Have fun!
.END