Pages

Wednesday, June 12, 2013

pygame blit and flip program more or less working!

So I first ran the test function TestWebCam01() in text mode, making sure there is no bug, before running it in Python GUI shell.

After some 5 minutes debug, the program is syntax error free.  Then I open IDLE and try to run fongtoy.py in the shell.  However, I forgot how to run a script in Python shell.

I thought I could run the script something like the following, but it did not work.

>>>> execscript"\home\pi\fongtoy\fongtoy.py"

Then I tried to import the fongtoy.py module, also no luck.  I also forgot how to switch between program shell and output window.  This is annoying.

Then I tried to go back to PuTTY and run fongtoy.py there.  This time luck is with me.  The program indeed outputs an image, seemingly capture by the Logitech C920.  But it immediately exits, so I could not see clearly the image.  

Anyway, the program seems to work.

I thought I could use time.sleep(4) to hold the display for 4 seconds, or while True: to hold the display forever.  However, neither works.  

This is strange.  Jogging time.




# fongtoy v1.8 tlfong01 2013jun12

ProgramTitle = "FongToy v1.8 tlfong01 2013jun12"

import sys 
import time 
import smbus 
import pdb 
import spidev 
import wiringpi
import wiringpi2
import RPIO as GPIO  
from RPIO import PWM 
from enum import Enum 
from subprocess import call

import ftgpio
import ftprint
import ftspi
import ftiox
import fteeprom
import ftguzuntypi
import ftdemux
import fttest
import ftadc
import ftwebcam

# *** Troubleshooting functions ***

# *** GPIO tests v1.3 tlfong01 2013may23 ***
# ftgpio.TestLed()
# ftgpio.TestBuzzer()
# ftgpio.TestButtonEchoBuzzer()
# ftgpio.TestButtonEchoLed()

# *** SPI Tests v1.3 tlfong01 2013may23 ***

# ftdemux.TestSelectSpiSlaveDevice(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 5)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 2, testStartAddress = 0x0123, testWriteDataByte = 0x5a)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 1, testStartAddress = 0x0123, testWriteDataByte = 0x3b)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 0, testStartAddress = 0x0123, testWriteDataByte = 0x3b)

# ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber = 0, spiChipSubAddress = 0)
# ftdemux.TestSelectSpiSlaveDevice(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 5)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 0, testStartAddress = 0x0123, testWriteDataByte = 0x3b)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 1, testStartAddress = 0x0411, testWriteDataByte = 0x4c)
# fttest.TestDemuxGuzuntyClock(mcp23s17SubAddress = 0, guzuntyClockDemuxAddress = 2, secondCount = 10)

# *** Main program ***

# *** Start program message ***
ftprint.StartProgram(ProgramTitle)

# *** Main tests ***

# *** SPI loop back ***
# ftspi.TestSpiLoopBackV01(spiChannelNumber = 0, spiChipEnableNumber = 1, testDataByte = 0x55, testTime = 0.001, testCount = 60000)

# *** ADC ***
# ftadc.TestMcp3208v03() 
# ftadc.TestMcp3201v04()

# *** EEPROM ***
# fteeprom.TestEeporm25Lc256v01(spiChannelNumber = 0, spiChipEnableNumber = 1, startAddress = 0x4100, testDataByte = 0x55)

# *** Mcp23s17 ***
# ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber = 0, spiChipSubAddress = 0)

# *** Demux ***
# ftdemux.TestDemuxV01(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 5)

# *** Guzunty Pi Step Motor ***
# ... test functions already moved to ftguzunti.py tlfong01 2013jun10 ...

# *** Webcam ***

# ftwebcam.TestWebcam()

ftwebcam.TestWebcam01()

# *** Stop program message ***

ftprint.StopProgram()

# .END


# ftwebcam v0.4 tlfong01 2013jun12

# *****************************************************************************
# Module - ftwebcam.py
# Description - Test Logitech webcam C920 and C270 
# Reference
#   Pygame.camera Reference - http://www.pygame.org/docs/ref/camera.html
#   Pygame.camera Turorial -  http://www.pygame.org/docs/tut/camera/CameraIntro.html
# Notes
#   Image zise 2MP 16:9 = 1920 * 1080 = 2,073,600
#   /etc/motion/motion.conf setting
#     Daemon = off
#     webcam_localhost = on
#   /etc/default/motion setting
#     start_motion_daemon = no
# *****************************************************************************

# *****************************************************************************
# Imports
# *****************************************************************************

import time

import pygame
import pygame.camera
from pygame.locals import *

import ftprint

# *****************************************************************************
# Function - TestWebcam()
# Description - 
#   Test Webcam 
# *****************************************************************************

def TestWebcam():

    ftprint.PrintDoubleSpaceLine("*** Sample run begin - Test web cam 2013jun12hkt16:23 ***")    
    
    webCamWidth  = 640
    webCamHeight = 480

    webCamWidth  = 1920
    webCamHeight = 1024

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

    webCamList = pygame.camera.list_cameras()
    if webCamList:
        print "List of available cameras = ", webCamList
        webCam = pygame.camera.Camera(webCamList[0],(webCamWidth, webCamHeight))

    webCam.start()

    imageSurface = webCam.get_image()

    pygame.image.save(imageSurface, "/home/pi/fongtoy/testImage.bmp")

    webCam.stop()
    pygame.quit()

    ftprint.PrintDoubleSpaceLine("*** Sample run end ***")


def TestWebcam01():

    # *** Program begin message ***

    ftprint.PrintDoubleSpaceLine("*** Sample run begin - Test web cam 2013jun12hkt16:23 ***")    
       
    # *** Image/Display size ***

    # * Vga *

    VgaSize = (640, 480)

    # * Logitech C920 *

    LogitechC920size = (1920, 1024)

    # *** initialize pygame.camera module ***

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

    # *** Create and start webcam object ***

    webCamList = pygame.camera.list_cameras()
    if webCamList:
        print "List of available cameras = ", webCamList
        webCam = pygame.camera.Camera(webCamList[0],VgaSize)
  webCam.start()

    # *** Create a display  ***

    pygame.display.init()
    webCamDisplay = pygame.display.set_mode(VgaSize, 0)

    # *** Create a surface  ***

    webCamSurface = pygame.surface.Surface(VgaSize, 0, webCamDisplay)     
    
    # *** Get surface ***

    webCamSurface = webCam.get_image(webCamSurface)

    time.sleep(0.1)

    # *** Save surface as a BMP file ***

    pygame.image.save(webCamSurface, "/home/pi/fongtoy/testImage.bmp")

    # *** blit and flip ***

    webCamDisplay.blit(webCamSurface, (0,0))
    pygame.display.flip()

    webCam.stop()
    pygame.quit()

    # *** Print program end message ***

    ftprint.PrintDoubleSpaceLine("*** Sample run end ***")

# .END



pi@raspberrypi ~/fongtoy $ sudo python fongtoy.py

*** Start Program - FongToy v1.8 tlfong01 2013jun12 ***


*** Sample run begin - Test web cam 2013jun12hkt16:23 ***

List of available cameras =  ['/dev/video0']

*** Sample run end ***


*** Stop Program ***

.END


No comments:

Post a Comment