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