JeVoisBase  1.21
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
PythonTest.py
Go to the documentation of this file.
1import pyjevois
2if pyjevois.pro: import libjevoispro as jevois
3else: 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
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 self.timer = jevois.Timer("pytest", 100, jevois.LOG_INFO)
35
36 # ###################################################################################################
37 ## JeVois optional extra init once the instance is fully constructed
38 def init(self):
39 jevois.LINFO("PythonTest JeVois init")
40
41 # Examples of adding user-tunable parameters to the module. Users can modify these using the JeVois console,
42 # over a serial connection, using JeVois-Inventor, or using the JeVois-Pro GUI:
43 pc = jevois.ParameterCategory("PythonTest Parameters", "")
44 self.cx = jevois.Parameter(self, 'cx', 'int', "Circle horizontal center, in pixels", 320, pc)
45 self.cy = jevois.Parameter(self, 'cy', 'int', "Circle vertical center, in pixels", 240, pc)
46 self.radius = jevois.Parameter(self, 'radius', 'byte', "Circle radius, in pixels", 50, 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(), 2, jevois.YUYV.White)
80
81 # We are done with the output, ready to send it to host over USB:
82 outframe.send()
83
84 # Send a string over serial (e.g., to an Arduino). Remember to tell the JeVois Engine to display those messages,
85 # as they are turned off by default. For example: 'setpar serout All' in the JeVois console:
86 if self.frame % 100 == 0:
87 jevois.sendSerial("DONE frame {}".format(self.frame));
88 self.frame += 1
89
90 # ###################################################################################################
91 ## Process function with GUI output on JeVois-Pro
92 def processGUI(self, inframe, helper):
93 # Start a new display frame, gets its size and also whether mouse/keyboard are idle:
94 idle, winw, winh = helper.startFrame()
95
96 # Draw full-resolution input frame from camera:
97 x, y, w, h = helper.drawInputFrame("c", inframe, False, False)
98
99 # Get the next camera image (may block until it is captured):
100 #inimg = inframe.getCvBGRp()
101
102 # Start measuring image processing time (NOTE: does not account for input conversion time):
103 self.timer.start()
104
105 # Some drawings:
106 helper.drawCircle(self.cx.get(), self.cy.get(), self.radius.get(), 0xffffffff, True)
107
108 # Write frames/s info from our timer:
109 fps = self.timer.stop()
110 helper.iinfo(inframe, fps, winw, winh);
111
112 # End of frame:
113 helper.endFrame()
114
115 # ###################################################################################################
116 ## Parse a serial command forwarded to us by the JeVois Engine, return a string
117 def parseSerial(self, str):
118 jevois.LINFO("parseserial received command [{}]".format(str))
119 if str == "hello":
120 return self.hello()
121 return "ERR Unsupported command"
122
123 # ###################################################################################################
124 ## Return a string that describes the custom commands we support, for the JeVois help message
126 # use \n separator if your module supports several commands
127 return "hello - print hello using python"
128
129 # ###################################################################################################
130 ## Internal method that can get invoked by users from the JeVois console as a custom command
131 def hello(self):
132 return "Hello from python!"
133
134 # ###################################################################################################
135 ## JeVois optional extra uninit before the instance is destroyed
136 def uninit(self):
137 jevois.LINFO("PythonTest JeVois uninit")
138
Simple test of programming JeVois modules in Python.
Definition PythonTest.py:28
supportedCommands(self)
Return a string that describes the custom commands we support, for the JeVois help message.
hello(self)
Internal method that can get invoked by users from the JeVois console as a custom command.
uninit(self)
JeVois optional extra uninit before the instance is destroyed.
processGUI(self, inframe, helper)
Process function with GUI output on JeVois-Pro.
Definition PythonTest.py:92
__init__(self)
Constructor.
Definition PythonTest.py:31
process(self, inframe, outframe)
Process function with USB output.
Definition PythonTest.py:55
processNoUSB(self, inframe)
Process function with no USB output.
Definition PythonTest.py:50
init(self)
JeVois optional extra init once the instance is fully constructed.
Definition PythonTest.py:38
parseSerial(self, str)
Parse a serial command forwarded to us by the JeVois Engine, return a string.
std::string fccstr(unsigned int fcc)