JeVoisBase  1.21
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
PythonSandbox.py
Go to the documentation of this file.
1import pyjevois
2if pyjevois.pro: import libjevoispro as jevois
3else: import libjevois as jevois
4import cv2
5import numpy as np
6
7## Simple example of image processing using OpenCV in Python on JeVois
8#
9# This module is here for you to experiment with Python OpenCV on JeVois.
10#
11# By default, we get the next video frame from the camera as an OpenCV BGR (color) image named 'inimg'.
12# We then apply some image processing to it to create an output BGR image named 'outimg'.
13# We finally add some text drawings to outimg and send it to host over USB.
14#
15# See http://jevois.org/tutorials for tutorials on getting started with programming JeVois in Python without having
16# to install any development software on your host computer.
17#
18# @author Laurent Itti
19#
20# @videomapping YUYV 352 288 30.0 YUYV 352 288 30.0 JeVois PythonSandbox
21# @email itti\@usc.edu
22# @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
23# @copyright Copyright (C) 2017 by Laurent Itti, iLab and the University of Southern California
24# @mainurl http://jevois.org
25# @supporturl http://jevois.org/doc
26# @otherurl http://iLab.usc.edu
27# @license GPL v3
28# @distribution Unrestricted
29# @restrictions None
30# @ingroup modules
32 # ###################################################################################################
33 ## Constructor
34 def __init__(self):
35 # Instantiate a JeVois Timer to measure our processing framerate:
36 self.timer = jevois.Timer("sandbox", 100, jevois.LOG_INFO)
37
38 # ###################################################################################################
39 ## Process function with no USB or GUI output (headless)
40 def processNoUSB(self, inframe):
41 # Here we will just report video capture framerate
42
43 # Stop our timer here, then start it, so that we will measure the delay between two calls to processNoUSB(),
44 # which will be limited by video capture framerate:
45 self.timer.stop()
46 self.timer.start()
47
48 # Grab a frame from the camera sensor. Will block until the next video frame has been captured by the hardware
49 # and is available:
50 inrawimg = inframe.get()
51
52 # Could process the raw image here...
53
54
55 # ###################################################################################################
56 ## Process function with USB output
57 def process(self, inframe, outframe):
58 # Get the next camera image (may block until it is captured) and here convert it to OpenCV BGR by default. If
59 # you need a grayscale image instead, just use getCvGRAY() instead of getCvBGR(). Also supported are getCvRGB()
60 # and getCvRGBA():
61 inimg = inframe.getCvBGR()
62
63 # Start measuring image processing time (NOTE: does not account for input conversion time):
64 self.timer.start()
65
66 # Detect edges using the Laplacian algorithm from OpenCV:
67 #
68 # Replace the line below by your own code! See for example
69 # - http://docs.opencv.org/trunk/d4/d13/tutorial_py_filtering.html
70 # - http://docs.opencv.org/trunk/d9/d61/tutorial_py_morphological_ops.html
71 # - http://docs.opencv.org/trunk/d5/d0f/tutorial_py_gradients.html
72 # - http://docs.opencv.org/trunk/d7/d4d/tutorial_py_thresholding.html
73 #
74 # and so on. When they do "img = cv2.imread('name.jpg', 0)" in these tutorials, the last 0 means they want a
75 # gray image, so you should use getCvGRAY() above in these cases. When they do not specify a final 0 in imread()
76 # then usually they assume color and you should use getCvBGR() here.
77 #
78 # The simplest you could try is:
79 # outimg = inimg
80 # which will make a simple copy of the input image to output.
81 outimg = cv2.Laplacian(inimg, -1, ksize=5, scale=0.25, delta=127)
82
83 # Write a title:
84 cv2.putText(outimg, "JeVois Python Sandbox", (3, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
85
86 # Write frames/s info from our timer into the edge map (NOTE: does not account for output conversion time):
87 fps = self.timer.stop()
88 outheight = outimg.shape[0]
89 outwidth = outimg.shape[1]
90 cv2.putText(outimg, fps, (3, outheight - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
91
92 # Convert our OpenCv output image to video output format and send to host over USB:
93 outframe.sendCv(outimg)
94
95 # ###################################################################################################
96 ## Process function with GUI output
97 def processGUI(self, inframe, helper):
98 # Start a new display frame, gets its size and also whether mouse/keyboard are idle:
99 idle, winw, winh = helper.startFrame()
100
101 # Draw full-resolution input frame from camera:
102 x, y, w, h = helper.drawInputFrame("c", inframe, False, False)
103
104 # Get the next camera image (may block until it is captured):
105 #inimg = inframe.getCvBGRp()
106
107 # Start measuring image processing time (NOTE: does not account for input conversion time):
108 self.timer.start()
109
110 # Some drawings:
111 helper.drawCircle(50, 50, 20, 0xffffffff, True)
112 helper.drawRect(100, 100, 300, 200, 0xffffffff, True)
113
114 # Write frames/s info from our timer:
115 fps = self.timer.stop()
116 helper.iinfo(inframe, fps, winw, winh);
117
118 # End of frame:
119 helper.endFrame()
120
Simple example of image processing using OpenCV in Python on JeVois.
processGUI(self, inframe, helper)
Process function with GUI output.
processNoUSB(self, inframe)
Process function with no USB or GUI output (headless)
process(self, inframe, outframe)
Process function with USB output.
__init__(self)
Constructor.