Using camera in DIA

I would like to use camera in my DIA application. I’m using SimpleLogger as my starting point for image log. I have also checked similar attempt with WEB presentation code where digiweb is used if you decide to use already running HTTP server (use_default_httpserver=True).

The code first imports camera module and then uses get_image() function. The problem is that while code is executing the environment reported problem

NameError: global name ‘get_image’ is not defined

If I test camera capture Python script as standalone script without DIA environment the script works well.

Any suggestions? I’m also attaching the code.

Hi dms.engine,

Without seeing the code in particular, it sounds like you may have imported the camera module with the syntax of:

import camera

Yet reference the get_image function like so:

my_image = get_image()

Please make sure you’re referencing the camera.get_image() properly in your code. If the problem still persists, can you post a small code example where it demonstrates the problem?

Max

Here are the peaces of the code:

#import camera module
try:
import camera
except:
print “Camera module is not available!”

.
.
.

#Check if the get_image() is present
def start(self):
if not globals().has_key(‘get_image’):
self.__camera_access = False;
print “get_image() is not present!”
else:
self.__camera_access = True;
print “get_image() is present!”

  return True

Here I get the message if I run the code:

Determining platform type…Digi Python environment found.

Attempting to read “dia.pyr” in “WEB/python/dia.zip”…
iDigi Device Integration Application Version 1.2.19
Using settings file: dia.pyr
Core: initial garbage collection of 0 objects.
Core: post-settings garbage collection of 10 objects.
Starting Scheduler…
Starting Channel Manager…
get_image() is not present!
Starting Device Driver Manager…
.
.
.

What could be wrong? It seems that the camera module is imported but thu function is not available.

Hi dms.engine,

It looks like you’re testing for get_image() directly, instead of the module where the function exists. Try this import statement instead:

from camera import get_image

The result is the same:

Determining platform type…Digi Python environment found.

Attempting to read “dia.pyr” in “WEB/python/dia.zip”…
iDigi Device Integration Application Version 1.2.19
Using settings file: dia.pyr
Core: initial garbage collection of 0 objects.
Core: post-settings garbage collection of 10 objects.
Starting Scheduler…
Starting Channel Manager…
get_image() is not present!
Starting Device Driver Manager…

I’m attaching full source if there is some mistake. The code is based on SimpleLogger module.

imports

from settings.settings_base import SettingsBase, Setting

from channels.logging.logger_base import LoggerBase
from channels.channel_publisher import ChannelDoesNotExist
from channels.logging.logging_events import
LoggingEventNewSample, LoggingEventChannelNew, LoggingEventChannelRemove
from channels.logging.camera_logger.camera_logger_channel_dbi import
CameraLoggerChannelDBI

try:
from camera import get_image
except:
print “Camera module is not available!”

constants

interface

classes

class CameraLogger(LoggerBase):
def init(self, name, core_services):
self.__name = name
self.__core_services = core_services

    self.__camera_access = False;
    
    self.monitored_channel = None
    
    settings_list = [Setting(name="monitored_channel", type=str, required=True)]
    
    LoggerBase.__init__(self, name=name, settings_list=settings_list)


def log_event(self, logging_event):
	"""
	Called when we should log a new event

	Keyword arguments:
	event -- the new event
	"""

    if isinstance(logging_event, LoggingEventNewSample):
        print "CameraLogger(%s): new sample %s)" % (
            self.__name,
            repr(logging_event.channel.get()))
        if logging_event.channel.name() == SettingsBase.get_setting(self, "monitored_channel") and logging_event.channel.get().value:
            self.take_picture(logging_event.channel.name())
        else:
            pass
    elif isinstance(logging_event, LoggingEventChannelNew):
        print "CameraLogger(%s): new channel '%s'" % (
            self.__name,
            logging_event.channel.name())
    elif isinstance(logging_event, LoggingEventChannelRemove):
        print "CameraLogger(%s): remove channel '%s'" % (
            self.__name,
            logging_event.channel.name())            
    else:
        print "CameraLogger(%s): unknown log event '%s'" % (
            self.__name,
            logging_event.__class__.__name__)            

def channel_database_get(self):
	return CameraLoggerChannelDBI()

def apply_settings(self):
	SettingsBase.merge_settings(self)
	accepted, rejected, not_found = SettingsBase.verify_settings(self)

    if len(rejected) or len(not_found):
        print ("%s(%s): settings rejected/not found: %s/%s" % 
               (self.__class__.__name__, self.__name, rejected, not_found))
  
    SettingsBase.commit_settings(self, accepted)
	return (accepted, rejected, not_found)

def start(self):
  if not globals().has_key('get_image'):
      self.__camera_access = False;
      print "get_image() is not present!"
  else:
      self.__camera_access = True;
      print "get_image() is present!"
       
  return True

def take_picture(self, channel):
    """ Take picture from camera"""
    if self.__camera_access:
        #Camera: BORIS 6.2.2010
        ##Destination of the image
        fh = open('A/demo_image.jpg', 'wb') 
        ##image, and timestamp
        image, timestamp = camera.get_image()
        ##write buffer and close
        fh.write(image)
        fh.close()
        print "Picture saved (%s)!" % (timestamp)
    else:
        print "Camera can't be accessed!"
        
    return True
        
def stop(self):
	return True

Interesting implementation. I was able to bring your source into my copy of Dia 1.2.19 (minus the import CameraLoggerChannelDBI), and get it to run to the point where it would try to save pictures to the ‘A/demo_image.jpg’.

The camera API was being correctly imported, and even without a camera attached to the device the check for ‘get_image’ in globals returns True.

What platform are you trying to run it on? I can run it against a ConnectPort X8 and ConnectPort X4. I may be running a different firmware version then you, so could you also post the results from sending the CLI command ‘info device’?

Max

Here is my info device:

Device Information:

Product : ConnectPort X8
MAC Address : 00:40:9D:37:04:21
Firmware Version : 2.9.0.7 (Version 82001115_G1 10/30/2009)
Boot Version : 1.1.3 (release_82001113_C)
Post Version : 1.1.3 (release_82001114_C)
Product VPD Version : release_82001512_A
Product ID : 0x004B
Hardware Strapping : 0x8003
CPU Utilization : 9 %
Uptime : 2 days, 23 hours, 49 minutes, 49 seconds
Current Date/Time : Sat Feb 13 19:20:53 2010
Total Memory : 16777216
Used Memory : 15747348
Free Memory : 1029868

I’ve loaded my device up the same way you have it there, and still can’t get the camera python module to fail its import.

Two more things to look at I guess: Are you running another python application in addition to the Dia? I know the version of X8 you’re using starts with the embedded gateway kit autostarting on it, which can consume quite a bit of resources. To determine this you can either view the auto start settings at http:///config/applications/python_autostart_config.htm

or at the CLI: ‘set python’

Another thing to look at in your python code is removing the try/except block from around the camera import statement. I’m curious as to what reason the device is giving when it fails to import there.

Sorry for my late response. I was absent for 2 weeks but I’m back now.

I checked autostart options and everything was checked out.

The I clean up all unnecessary files from WEB/Python directory and magically it starts working as you said it will. I’m not sure what it was, but there was whole bunch of different demo files and some old files with python interpreter. It seems that one of them was very sensitive for dia.py.

It is working now.

Thank you for pointing me to the right direction.