001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.demos.sandbox.hand;
031
032import javax.swing.JFrame;
033
034import jssc.SerialPort;
035
036import org.openimaj.hardware.serial.SerialDevice;
037import org.openimaj.image.DisplayUtilities;
038import org.openimaj.image.FImage;
039import org.openimaj.image.MBFImage;
040import org.openimaj.image.colour.RGBColour;
041import org.openimaj.image.connectedcomponent.ConnectedComponentLabeler;
042import org.openimaj.image.pixel.ConnectedComponent;
043import org.openimaj.image.pixel.ConnectedComponent.ConnectMode;
044import org.openimaj.math.geometry.shape.Polygon;
045import org.openimaj.video.capture.VideoCapture;
046
047public class Fingers2 {
048        public static void main(String[] args) throws Exception {
049                final VideoCapture vc = new VideoCapture(320, 240);
050
051                final JFrame frame = DisplayUtilities.displaySimple(vc.getNextFrame(), "capture");
052                final ConnectedComponentLabeler ccl = new ConnectedComponentLabeler(ConnectMode.CONNECT_4);
053
054                final String dev = "/dev/tty.usbmodemfd121";
055                final SerialDevice device = new SerialDevice(dev, 9600, SerialPort.DATABITS_8,
056                                SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
057
058                while (true) {
059                        final MBFImage cimg = vc.getNextFrame();
060                        final FImage gimg = cimg.flatten();
061                        // gimg.processInplace(new OtsuThreshold());
062                        gimg.threshold(0.6f);
063
064                        ccl.analyseImage(gimg);
065                        final ConnectedComponent hand = Fingers.findBiggest(ccl.getComponents());
066
067                        if (hand != null) {
068                                final Polygon poly = hand.toPolygon();
069                                cimg.drawPolygon(poly, 2, RGBColour.RED);
070
071                                double ratio = hand.calculateRegularBoundingBoxAspectRatio();
072
073                                System.out.println(ratio);
074
075                                ratio = 1 - ((ratio - 3.0) / (5.5 - 3));
076                                ratio = ratio < 0 ? 0 : ratio > 1 ? 1 : ratio;
077
078                                final int amt = (int) (ratio * 180);
079                                sendCommand(device, amt);
080                                System.out.println(ratio + " " + amt);
081                        }
082
083                        DisplayUtilities.display(cimg, frame);
084                }
085        }
086
087        private static void sendCommand(SerialDevice device, int angle) {
088                try {
089                        device.getOutputStream().write((angle + "\n").getBytes("US-ASCII"));
090                        Thread.sleep(60);
091                } catch (final Exception e) {
092                        throw new RuntimeException(e);
093                }
094        }
095}