JeVoisBase  1.18
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
PythonTest.py
Go to the documentation of this file.
1 import pyjevois
2 if pyjevois.pro: import libjevoispro as jevois
3 else: import libjevois as jevois
4 
5 ## Simple test of programming JeVois modules in Python
6 #
7 # This module by default simply draws a cricle and a text message onto the grabbed video frames.
8 #
9 # Feel free to edit it and try something else. Note that this module does not import OpenCV, see the PythonOpenCV for a
10 # minimal JeVois module written in Python that uses OpenCV.
11 #
12 # See http://jevois.org/tutorials for tutorials on getting started with programming JeVois in Python without having
13 # to install any development software on your host computer.
14 #
15 # @author Laurent Itti
16 #
17 # @videomapping YUYV 640 480 15.0 YUYV 640 480 15.0 JeVois PythonTest
18 # @email itti\@usc.edu
19 # @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
20 # @copyright Copyright (C) 2017 by Laurent Itti, iLab and the University of Southern California
21 # @mainurl http://jevois.org
22 # @supporturl http://jevois.org/doc
23 # @otherurl http://iLab.usc.edu
24 # @license GPL v3
25 # @distribution Unrestricted
26 # @restrictions None
27 # @ingroup modules
28 class PythonTest:
29  # ###################################################################################################
30  ## Constructor
31  def __init__(self):
32  jevois.LINFO("PythonTest Constructor")
33  self.frame = 0 # a simple frame counter used to demonstrate sendSerial()
34 
35  # ###################################################################################################
36  ## JeVois optional extra init once the instance is fully constructed
37  def init(self):
38  jevois.LINFO("PythonTest JeVois init")
39 
40  # Examples of adding user-tunable parameters to the module. Users can modify these using the JeVois console,
41  # over a serial connection, using JeVois-Inventor, or using the JeVois-Pro GUI:
42  pc = jevois.ParameterCategory("PythonTest Parameters", "")
43  self.cx = jevois.Parameter(self, 'cx', 'int', "Circle horizontal center, in pixels", 320, pc)
44  self.cy = jevois.Parameter(self, 'cy', 'int', "Circle vertical center, in pixels", 240, pc)
45  self.radius = jevois.Parameter(self, 'radius', 'byte', "Circle radius, in pixels", 50, pc)
46  self.thickness = jevois.Parameter(self, 'thickness', 'byte', "Circle thickness, in pixels", 2, pc)
47 
48  # ###################################################################################################
49  ## Process function with no USB output
50  def processNoUSB(self, inframe):
51  jevois.LFATAL("process no usb not implemented")
52 
53  # ###################################################################################################
54  ## Process function with USB output
55  def process(self, inframe, outframe):
56  # Get the next camera image (may block until it is captured):
57  inimg = inframe.get()
58  if self.frame == 0:
59  jevois.LINFO("Input image is {} {}x{}".format(jevois.fccstr(inimg.fmt), inimg.width, inimg.height))
60 
61  # Get the next available USB output image:
62  outimg = outframe.get()
63  if self.frame == 0:
64  jevois.LINFO("Output image is {} {}x{}".format(jevois.fccstr(outimg.fmt), outimg.width, outimg.height))
65 
66  # Example of getting pixel data from the input and copying to the output:
67  jevois.paste(inimg, outimg, 0, 0)
68 
69  # We are done with the input image:
70  inframe.done()
71 
72  # Example of in-place processing:
73  jevois.hFlipYUYV(outimg)
74 
75  # Example of simple drawings and of accessing parameter values (users can change them via console, JeVois
76  # Inventor, or JeVois-Pro GUI):
77  jevois.writeText(outimg, "Hi from Python!", 20, 20, jevois.YUYV.White, jevois.Font.Font10x20)
78 
79  jevois.drawCircle(outimg, self.cx.get(), self.cy.get(), self.radius.get(), self.thickness.get(),
80  jevois.YUYV.White)
81 
82  # We are done with the output, ready to send it to host over USB:
83  outframe.send()
84 
85  # Send a string over serial (e.g., to an Arduino). Remember to tell the JeVois Engine to display those messages,
86  # as they are turned off by default. For example: 'setpar serout All' in the JeVois console:
87  if self.frame % 100 == 0:
88  jevois.sendSerial("DONE frame {}".format(self.frame));
89  self.frame += 1
90 
91  # ###################################################################################################
92  ## Process function with GUI output on JeVois-Pro
93  def processGUI(self, inframe, helper):
94  # Start a new display frame, gets its size and also whether mouse/keyboard are idle:
95  idle, winw, winh = helper.startFrame()
96 
97  # Draw full-resolution input frame from camera:
98  x, y, w, h = helper.drawInputFrame("c", inframe, False, False)
99 
100  # Get the next camera image (may block until it is captured):
101  #inimg = inframe.getCvBGRp()
102 
103  # Start measuring image processing time (NOTE: does not account for input conversion time):
104  self.timer.start()
105 
106  # Some drawings:
107  helper.drawCircle(self.cx.get(), self.cy.get(), self.radius.get(), 0xffffffff, True)
108 
109  # Write frames/s info from our timer:
110  fps = self.timer.stop()
111  helper.iinfo(inframe, fps, winw, winh);
112 
113  # End of frame:
114  helper.endFrame()
115 
116  # ###################################################################################################
117  ## Parse a serial command forwarded to us by the JeVois Engine, return a string
118  def parseSerial(self, str):
119  jevois.LINFO("parseserial received command [{}]".format(str))
120  if str == "hello":
121  return self.hello()
122  return "ERR Unsupported command"
123 
124  # ###################################################################################################
125  ## Return a string that describes the custom commands we support, for the JeVois help message
126  def supportedCommands(self):
127  # use \n separator if your module supports several commands
128  return "hello - print hello using python"
129 
130  # ###################################################################################################
131  ## Internal method that can get invoked by users from the JeVois console as a custom command
132  def hello(self):
133  return "Hello from python!"
134 
135  # ###################################################################################################
136  ## JeVois optional extra uninit before the instance is destroyed
137  def uninit(self):
138  jevois.LINFO("PythonTest JeVois uninit")
139 
PythonTest.PythonTest.supportedCommands
def supportedCommands(self)
Return a string that describes the custom commands we support, for the JeVois help message.
Definition: PythonTest.py:126
PythonTest.PythonTest.cx
cx
Definition: PythonTest.py:43
PythonTest.PythonTest.radius
radius
Definition: PythonTest.py:45
PythonTest.PythonTest.frame
frame
Definition: PythonTest.py:33
PythonTest.PythonTest.cy
cy
Definition: PythonTest.py:44
PythonTest.PythonTest
Simple test of programming JeVois modules in Python.
Definition: PythonTest.py:28
PythonTest.PythonTest.thickness
thickness
Definition: PythonTest.py:46
PythonTest.PythonTest.__init__
def __init__(self)
Constructor.
Definition: PythonTest.py:31
jevois::ParameterCategory
PythonTest.PythonTest.process
def process(self, inframe, outframe)
Process function with USB output.
Definition: PythonTest.py:55
PythonTest.PythonTest.init
def init(self)
JeVois optional extra init once the instance is fully constructed.
Definition: PythonTest.py:37
jevois::fccstr
std::string fccstr(unsigned int fcc)
PythonTest.PythonTest.parseSerial
def parseSerial(self, str)
Parse a serial command forwarded to us by the JeVois Engine, return a string.
Definition: PythonTest.py:118
PythonTest.PythonTest.hello
def hello(self)
Internal method that can get invoked by users from the JeVois console as a custom command.
Definition: PythonTest.py:132
PythonTest.PythonTest.processGUI
def processGUI(self, inframe, helper)
Process function with GUI output on JeVois-Pro.
Definition: PythonTest.py:93
PythonTest.PythonTest.uninit
def uninit(self)
JeVois optional extra uninit before the instance is destroyed.
Definition: PythonTest.py:137
PythonTest.PythonTest.processNoUSB
def processNoUSB(self, inframe)
Process function with no USB output.
Definition: PythonTest.py:50