JeVoisBase  1.18
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
PythonSandbox.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 import cv2
5 import 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 USB output
40  def process(self, inframe, outframe):
41  # Get the next camera image (may block until it is captured) and here convert it to OpenCV BGR by default. If
42  # you need a grayscale image instead, just use getCvGRAY() instead of getCvBGR(). Also supported are getCvRGB()
43  # and getCvRGBA():
44  inimg = inframe.getCvBGR()
45 
46  # Start measuring image processing time (NOTE: does not account for input conversion time):
47  self.timer.start()
48 
49  # Detect edges using the Laplacian algorithm from OpenCV:
50  #
51  # Replace the line below by your own code! See for example
52  # - http://docs.opencv.org/trunk/d4/d13/tutorial_py_filtering.html
53  # - http://docs.opencv.org/trunk/d9/d61/tutorial_py_morphological_ops.html
54  # - http://docs.opencv.org/trunk/d5/d0f/tutorial_py_gradients.html
55  # - http://docs.opencv.org/trunk/d7/d4d/tutorial_py_thresholding.html
56  #
57  # and so on. When they do "img = cv2.imread('name.jpg', 0)" in these tutorials, the last 0 means they want a
58  # gray image, so you should use getCvGRAY() above in these cases. When they do not specify a final 0 in imread()
59  # then usually they assume color and you should use getCvBGR() here.
60  #
61  # The simplest you could try is:
62  # outimg = inimg
63  # which will make a simple copy of the input image to output.
64  outimg = cv2.Laplacian(inimg, -1, ksize=5, scale=0.25, delta=127)
65 
66  # Write a title:
67  cv2.putText(outimg, "JeVois Python Sandbox", (3, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
68 
69  # Write frames/s info from our timer into the edge map (NOTE: does not account for output conversion time):
70  fps = self.timer.stop()
71  outheight = outimg.shape[0]
72  outwidth = outimg.shape[1]
73  cv2.putText(outimg, fps, (3, outheight - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
74 
75  # Convert our OpenCv output image to video output format and send to host over USB:
76  outframe.sendCv(outimg)
77 
78  # ###################################################################################################
79  ## Process function with GUI output
80  def processGUI(self, inframe, helper):
81  # Start a new display frame, gets its size and also whether mouse/keyboard are idle:
82  idle, winw, winh = helper.startFrame()
83 
84  # Draw full-resolution input frame from camera:
85  x, y, w, h = helper.drawInputFrame("c", inframe, False, False)
86 
87  # Get the next camera image (may block until it is captured):
88  #inimg = inframe.getCvBGRp()
89 
90  # Start measuring image processing time (NOTE: does not account for input conversion time):
91  self.timer.start()
92 
93  # Some drawings:
94  helper.drawCircle(50, 50, 20, 0xffffffff, True)
95  helper.drawRect(100, 100, 300, 200, 0xffffffff, True)
96 
97  # Write frames/s info from our timer:
98  fps = self.timer.stop()
99  helper.iinfo(inframe, fps, winw, winh);
100 
101  # End of frame:
102  helper.endFrame()
103 
PythonSandbox.PythonSandbox.__init__
def __init__(self)
Constructor.
Definition: PythonSandbox.py:34
PythonSandbox.PythonSandbox.timer
timer
Definition: PythonSandbox.py:36
PythonSandbox.PythonSandbox.processGUI
def processGUI(self, inframe, helper)
Process function with GUI output.
Definition: PythonSandbox.py:80
PythonSandbox.PythonSandbox.process
def process(self, inframe, outframe)
Process function with USB output.
Definition: PythonSandbox.py:40
PythonSandbox.PythonSandbox
Simple example of image processing using OpenCV in Python on JeVois.
Definition: PythonSandbox.py:31
jevois::Timer