JeVoisBase  1.7
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
PythonSandbox.py
Go to the documentation of this file.
1 import libjevois as jevois
2 import cv2
3 import numpy as np
4 
5 ## Simple example of image processing using OpenCV in Python on JeVois
6 #
7 # This module is here for you to experiment with Python OpenCV on JeVois.
8 #
9 # By default, we get the next video frame from the camera as an OpenCV BGR (color) image named 'inimg'.
10 # We then apply some image processing to it to create an output BGR image named 'outimg'.
11 # We finally add some text drawings to outimg and send it to host over USB.
12 #
13 # See http://jevois.org/tutorials for tutorials on getting started with programming JeVois in Python without having
14 # to install any development software on your host computer.
15 #
16 # @author Laurent Itti
17 #
18 # @videomapping YUYV 352 288 30.0 YUYV 352 288 30.0 JeVois PythonSandbox
19 # @email itti\@usc.edu
20 # @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
21 # @copyright Copyright (C) 2017 by Laurent Itti, iLab and the University of Southern California
22 # @mainurl http://jevois.org
23 # @supporturl http://jevois.org/doc
24 # @otherurl http://iLab.usc.edu
25 # @license GPL v3
26 # @distribution Unrestricted
27 # @restrictions None
28 # @ingroup modules
29 class PythonSandbox:
30  # ###################################################################################################
31  ## Constructor
32  def __init__(self):
33  # Instantiate a JeVois Timer to measure our processing framerate:
34  self.timer = jevois.Timer("sandbox", 100, jevois.LOG_INFO)
35 
36  # ###################################################################################################
37  ## Process function with USB output
38  def process(self, inframe, outframe):
39  # Get the next camera image (may block until it is captured) and here convert it to OpenCV BGR by default. If
40  # you need a grayscale image instead, just use getCvGRAY() instead of getCvBGR(). Also supported are getCvRGB()
41  # and getCvRGBA():
42  inimg = inframe.getCvBGR()
43 
44  # Start measuring image processing time (NOTE: does not account for input conversion time):
45  self.timer.start()
46 
47  # Detect edges using the Laplacian algorithm from OpenCV:
48  #
49  # Replace the line below by your own code! See for example
50  # - http://docs.opencv.org/trunk/d4/d13/tutorial_py_filtering.html
51  # - http://docs.opencv.org/trunk/d9/d61/tutorial_py_morphological_ops.html
52  # - http://docs.opencv.org/trunk/d5/d0f/tutorial_py_gradients.html
53  # - http://docs.opencv.org/trunk/d7/d4d/tutorial_py_thresholding.html
54  #
55  # and so on. When they do "img = cv2.imread('name.jpg', 0)" in these tutorials, the last 0 means they want a
56  # gray image, so you should use getCvGRAY() above in these cases. When they do not specify a final 0 in imread()
57  # then usually they assume color and you should use getCvBGR() here.
58  #
59  # The simplest you could try is:
60  # outimg = inimg
61  # which will make a simple copy of the input image to output.
62  outimg = cv2.Laplacian(inimg, -1, ksize=5, scale=0.25, delta=127)
63 
64  # Write a title:
65  cv2.putText(outimg, "JeVois Python Sandbox", (3, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255),
66  1, cv2.LINE_AA)
67 
68  # Write frames/s info from our timer into the edge map (NOTE: does not account for output conversion time):
69  fps = self.timer.stop()
70  height, width, channels = outimg.shape # if outimg is grayscale, change to: height, width = outimg.shape
71  cv2.putText(outimg, fps, (3, height - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)
72 
73  # Convert our BGR output image to video output format and send to host over USB. If your output image is not
74  # BGR, you can use sendCvGRAY(), sendCvRGB(), or sendCvRGBA() as appropriate:
75  outframe.sendCvBGR(outimg)
def process(self, inframe, outframe)
Process function with USB output.
def __init__(self)
Constructor.