Permalink
Browse files

Rozpoznavanie ruky uz plusminus funguje

  • Loading branch information...
1 parent e383b58 commit 8dd36fabd44bf24cc084b0250cb110b9b0e90595 @mhozza committed Jun 20, 2011
Showing with 457 additions and 9 deletions.
  1. +231 −0 NeuralNet/Makefile
  2. +24 −0 NeuralNet/NeuralNet.pro
  3. +169 −0 NeuralNet/main.cpp
  4. +0 −1 NeuralNet/neurallayer.h
  5. +2 −2 handrecognizer.cpp
  6. +17 −6 mainwindow.cpp
  7. +1 −0 mainwindow.h
  8. +13 −0 mainwindow.ui
View
231 NeuralNet/Makefile
@@ -0,0 +1,231 @@
+#############################################################################
+# Makefile for building: NeuralNet
+# Generated by qmake (2.01a) (Qt 4.7.2) on: Mon Jun 20 22:06:33 2011
+# Project: NeuralNet.pro
+# Template: app
+# Command: /usr/bin/qmake -o Makefile NeuralNet.pro
+#############################################################################
+
+####### Compiler, tools and options
+
+CC = gcc
+CXX = g++
+DEFINES = -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
+CFLAGS = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)
+CXXFLAGS = -pipe -O2 -O3 -Wall -W -D_REENTRANT $(DEFINES)
+INCPATH = -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I.
+LINK = g++
+LFLAGS = -Wl,-O1
+LIBS = $(SUBLIBS) -L/usr/lib -lQtGui -lQtCore -lpthread
+AR = ar cqs
+RANLIB =
+QMAKE = /usr/bin/qmake
+TAR = tar -cf
+COMPRESS = gzip -9f
+COPY = cp -f
+SED = sed
+COPY_FILE = $(COPY)
+COPY_DIR = $(COPY) -r
+STRIP = strip
+INSTALL_FILE = install -m 644 -p
+INSTALL_DIR = $(COPY_DIR)
+INSTALL_PROGRAM = install -m 755 -p
+DEL_FILE = rm -f
+SYMLINK = ln -f -s
+DEL_DIR = rmdir
+MOVE = mv -f
+CHK_DIR_EXISTS= test -d
+MKDIR = mkdir -p
+
+####### Output directory
+
+OBJECTS_DIR = ./
+
+####### Files
+
+SOURCES = main.cpp \
+ perceptron.cpp \
+ continuous_perceptron.cpp \
+ neuralnetwork.cpp \
+ neurallayer.cpp
+OBJECTS = main.o \
+ perceptron.o \
+ continuous_perceptron.o \
+ neuralnetwork.o \
+ neurallayer.o
+DIST = /usr/share/qt4/mkspecs/common/g++.conf \
+ /usr/share/qt4/mkspecs/common/unix.conf \
+ /usr/share/qt4/mkspecs/common/linux.conf \
+ /usr/share/qt4/mkspecs/qconfig.pri \
+ /usr/share/qt4/mkspecs/modules/qt_webkit_version.pri \
+ /usr/share/qt4/mkspecs/features/qt_functions.prf \
+ /usr/share/qt4/mkspecs/features/qt_config.prf \
+ /usr/share/qt4/mkspecs/features/exclusive_builds.prf \
+ /usr/share/qt4/mkspecs/features/default_pre.prf \
+ /usr/share/qt4/mkspecs/features/release.prf \
+ /usr/share/qt4/mkspecs/features/default_post.prf \
+ /usr/share/qt4/mkspecs/features/warn_on.prf \
+ /usr/share/qt4/mkspecs/features/qt.prf \
+ /usr/share/qt4/mkspecs/features/unix/thread.prf \
+ /usr/share/qt4/mkspecs/features/moc.prf \
+ /usr/share/qt4/mkspecs/features/resources.prf \
+ /usr/share/qt4/mkspecs/features/uic.prf \
+ /usr/share/qt4/mkspecs/features/yacc.prf \
+ /usr/share/qt4/mkspecs/features/lex.prf \
+ /usr/share/qt4/mkspecs/features/include_source_dir.prf \
+ NeuralNet.pro
+QMAKE_TARGET = NeuralNet
+DESTDIR =
+TARGET = NeuralNet
+
+first: all
+####### Implicit rules
+
+.SUFFIXES: .o .c .cpp .cc .cxx .C
+
+.cpp.o:
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
+
+.cc.o:
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
+
+.cxx.o:
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
+
+.C.o:
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
+
+.c.o:
+ $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<"
+
+####### Build rules
+
+all: Makefile $(TARGET)
+
+$(TARGET): $(OBJECTS)
+ $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)
+
+Makefile: NeuralNet.pro /usr/share/qt4/mkspecs/linux-g++/qmake.conf /usr/share/qt4/mkspecs/common/g++.conf \
+ /usr/share/qt4/mkspecs/common/unix.conf \
+ /usr/share/qt4/mkspecs/common/linux.conf \
+ /usr/share/qt4/mkspecs/qconfig.pri \
+ /usr/share/qt4/mkspecs/modules/qt_webkit_version.pri \
+ /usr/share/qt4/mkspecs/features/qt_functions.prf \
+ /usr/share/qt4/mkspecs/features/qt_config.prf \
+ /usr/share/qt4/mkspecs/features/exclusive_builds.prf \
+ /usr/share/qt4/mkspecs/features/default_pre.prf \
+ /usr/share/qt4/mkspecs/features/release.prf \
+ /usr/share/qt4/mkspecs/features/default_post.prf \
+ /usr/share/qt4/mkspecs/features/warn_on.prf \
+ /usr/share/qt4/mkspecs/features/qt.prf \
+ /usr/share/qt4/mkspecs/features/unix/thread.prf \
+ /usr/share/qt4/mkspecs/features/moc.prf \
+ /usr/share/qt4/mkspecs/features/resources.prf \
+ /usr/share/qt4/mkspecs/features/uic.prf \
+ /usr/share/qt4/mkspecs/features/yacc.prf \
+ /usr/share/qt4/mkspecs/features/lex.prf \
+ /usr/share/qt4/mkspecs/features/include_source_dir.prf \
+ /usr/lib/libQtGui.prl \
+ /usr/lib/libQtCore.prl
+ $(QMAKE) -o Makefile NeuralNet.pro
+/usr/share/qt4/mkspecs/common/g++.conf:
+/usr/share/qt4/mkspecs/common/unix.conf:
+/usr/share/qt4/mkspecs/common/linux.conf:
+/usr/share/qt4/mkspecs/qconfig.pri:
+/usr/share/qt4/mkspecs/modules/qt_webkit_version.pri:
+/usr/share/qt4/mkspecs/features/qt_functions.prf:
+/usr/share/qt4/mkspecs/features/qt_config.prf:
+/usr/share/qt4/mkspecs/features/exclusive_builds.prf:
+/usr/share/qt4/mkspecs/features/default_pre.prf:
+/usr/share/qt4/mkspecs/features/release.prf:
+/usr/share/qt4/mkspecs/features/default_post.prf:
+/usr/share/qt4/mkspecs/features/warn_on.prf:
+/usr/share/qt4/mkspecs/features/qt.prf:
+/usr/share/qt4/mkspecs/features/unix/thread.prf:
+/usr/share/qt4/mkspecs/features/moc.prf:
+/usr/share/qt4/mkspecs/features/resources.prf:
+/usr/share/qt4/mkspecs/features/uic.prf:
+/usr/share/qt4/mkspecs/features/yacc.prf:
+/usr/share/qt4/mkspecs/features/lex.prf:
+/usr/share/qt4/mkspecs/features/include_source_dir.prf:
+/usr/lib/libQtGui.prl:
+/usr/lib/libQtCore.prl:
+qmake: FORCE
+ @$(QMAKE) -o Makefile NeuralNet.pro
+
+dist:
+ @$(CHK_DIR_EXISTS) .tmp/NeuralNet1.0.0 || $(MKDIR) .tmp/NeuralNet1.0.0
+ $(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/NeuralNet1.0.0/ && $(COPY_FILE) --parents perceptron.h continuous_perceptron.h neuralnetwork.h neurallayer.h .tmp/NeuralNet1.0.0/ && $(COPY_FILE) --parents main.cpp perceptron.cpp continuous_perceptron.cpp neuralnetwork.cpp neurallayer.cpp .tmp/NeuralNet1.0.0/ && (cd `dirname .tmp/NeuralNet1.0.0` && $(TAR) NeuralNet1.0.0.tar NeuralNet1.0.0 && $(COMPRESS) NeuralNet1.0.0.tar) && $(MOVE) `dirname .tmp/NeuralNet1.0.0`/NeuralNet1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/NeuralNet1.0.0
+
+
+clean:compiler_clean
+ -$(DEL_FILE) $(OBJECTS)
+ -$(DEL_FILE) *~ core *.core
+
+
+####### Sub-libraries
+
+distclean: clean
+ -$(DEL_FILE) $(TARGET)
+ -$(DEL_FILE) Makefile
+
+
+check: first
+
+mocclean: compiler_moc_header_clean compiler_moc_source_clean
+
+mocables: compiler_moc_header_make_all compiler_moc_source_make_all
+
+compiler_moc_header_make_all:
+compiler_moc_header_clean:
+compiler_rcc_make_all:
+compiler_rcc_clean:
+compiler_image_collection_make_all: qmake_image_collection.cpp
+compiler_image_collection_clean:
+ -$(DEL_FILE) qmake_image_collection.cpp
+compiler_moc_source_make_all:
+compiler_moc_source_clean:
+compiler_uic_make_all:
+compiler_uic_clean:
+compiler_yacc_decl_make_all:
+compiler_yacc_decl_clean:
+compiler_yacc_impl_make_all:
+compiler_yacc_impl_clean:
+compiler_lex_make_all:
+compiler_lex_clean:
+compiler_clean:
+
+####### Compile
+
+main.o: main.cpp neuralnetwork.h \
+ neurallayer.h \
+ continuous_perceptron.h \
+ perceptron.h
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp
+
+perceptron.o: perceptron.cpp perceptron.h
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o perceptron.o perceptron.cpp
+
+continuous_perceptron.o: continuous_perceptron.cpp continuous_perceptron.h \
+ perceptron.h
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o continuous_perceptron.o continuous_perceptron.cpp
+
+neuralnetwork.o: neuralnetwork.cpp neuralnetwork.h \
+ neurallayer.h \
+ continuous_perceptron.h \
+ perceptron.h
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o neuralnetwork.o neuralnetwork.cpp
+
+neurallayer.o: neurallayer.cpp neurallayer.h \
+ continuous_perceptron.h \
+ perceptron.h
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o neurallayer.o neurallayer.cpp
+
+####### Install
+
+install: FORCE
+
+uninstall: FORCE
+
+FORCE:
+
View
24 NeuralNet/NeuralNet.pro
@@ -0,0 +1,24 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2011-03-24T17:36:52
+#
+#-------------------------------------------------
+TARGET = NeuralNet
+CONFIG += console
+CONFIG -= app_bundle
+
+TEMPLATE = app
+
+SOURCES += main.cpp \
+ perceptron.cpp \
+ continuous_perceptron.cpp \
+ neuralnetwork.cpp \
+ neurallayer.cpp
+
+HEADERS += \
+ perceptron.h \
+ continuous_perceptron.h \
+ neuralnetwork.h \
+ neurallayer.h
+
+QMAKE_CXXFLAGS_RELEASE += -O3
View
169 NeuralNet/main.cpp
@@ -0,0 +1,169 @@
+#include <iostream>
+#include "neuralnetwork.h"
+#include <cstdlib>
+#include <algorithm>
+#include <fstream>
+#include <csignal>
+#include <cstdio>
+#include <sys/types.h>
+#include <dirent.h>
+
+
+using namespace std;
+using namespace NeuralNET;
+
+#define FOR(i,n) for(int i=0;i<(n);i++)
+#define N 6400
+#define N_SIDE 80
+#define OUT_N 1
+
+inline void print(string s)
+{
+ cout << s << endl;
+}
+
+volatile int stop = 0;
+
+void ctrlc(int signal __attribute__((unused)))
+{
+ stop = 1;
+}
+
+float getError(float output,bool target)
+{
+ return 0.5*(target-output)*(target-output);
+}
+
+vector<string> listDirectory(string path)
+{
+ vector<string> files;
+ if(path[path.size()-1]!='/') path+='/';
+ DIR *dp;
+ struct dirent *ep;
+
+ dp = opendir(path.c_str());
+ if (dp != NULL)
+ {
+ while (ep = readdir (dp))
+ {
+ string s = ep->d_name;
+ if(s=="." || s == "..") continue;
+ files.push_back(path+s);
+ }
+ closedir(dp);
+ }
+ else
+ perror ("Couldn't open the directory");
+ return files;
+}
+
+template<class T> vector<T> make_vector(T t)
+{
+ vector<T> vt;
+ vt.push_back(t);
+ return vt;
+}
+
+vector<float> loadImage(string path)
+{
+ print(path);
+ ifstream ifs(path.c_str());
+ vector<float> res;
+ res.resize(N);
+ //ppm without comments
+ //head
+ string hdr;
+ ifs >> hdr;
+ if(hdr!="P1") throw 1;
+ //w, h
+ int w,h;
+ ifs >> w >> h;
+ if(w!=N_SIDE || h!=N_SIDE) throw 1;
+ int i = 0;
+ FOR(y,h)
+ {
+ FOR(x,w)
+ {
+ int t;
+ ifs >> t;
+ res[i++] = t;
+ }
+ }
+ ifs.close();
+ return res;
+}
+
+int main(int argc, char *argv[])
+{
+ srand (time(NULL) );
+ float alpha = 0.2;
+ int verbose = 10;
+ int mode = 0;
+ if(argc>1) mode = atoi(argv[1]);
+
+ unsigned sizes[] = {12,OUT_N};
+ NeuralNetwork *net = new NeuralNetwork(2,sizes,N,alpha);
+ signal(SIGINT, ctrlc);
+
+ vector<pair<vector<float>,vector<int > > > tests;
+
+ string hands_path, nonhands_path;
+ if(mode==0)
+ {
+ hands_path = "../hand_images/hands";
+ nonhands_path = "../hand_images/other";
+ }
+ else
+ {
+ hands_path = "../hand_images/test/hands";
+ nonhands_path = "../hand_images/test/other";
+ net->loadWeights("vahy");
+ }
+
+ //nacitaj
+ vector<string> hands = listDirectory(hands_path);
+ vector<string> nonhands = listDirectory(nonhands_path);
+
+ for(int i = 0;i<hands.size();i++)
+ {
+ tests.push_back(make_pair(loadImage(hands[i]),make_vector(1)));
+ }
+
+ for(int i = 0;i<nonhands.size();i++)
+ {
+ tests.push_back(make_pair(loadImage(nonhands[i]),make_vector(0)));
+ }
+
+ float E = 100;
+ int epoche = 0;
+ while(epoche<200 && (mode==0 || epoche<1))
+ {
+ epoche++;
+ cerr << epoche << endl;
+ E=0;
+ random_shuffle(tests.begin(),tests.end());
+
+ FOR(i,tests.size())
+ {
+ cout << i << ": ";
+ cout << tests[i].second[0] << " ";
+ vector<float> tc = net->classify(tests[i].first);
+ float c = tc[0];
+ cout << (c>0.5) << " (" << c << ")" << endl;
+
+ float e = 0;
+ if(mode==0)
+ e = net->train(tests[i].first,tests[i].second);
+ else
+ e = getError(c,tests[i].second[0]);
+
+ E += e;
+ }
+
+ cout << E << endl;
+ if(stop) break;
+ }
+ if(verbose)cout << epoche << endl;
+ if(mode == 0) net->saveWeights("vahy");
+
+}
View
1 NeuralNet/neurallayer.h
@@ -2,7 +2,6 @@
#define NEURALLAYER_H
#include "continuous_perceptron.h"
-#include "binary_perceptron.h"
namespace NeuralNET
{
View
4 handrecognizer.cpp
@@ -76,7 +76,7 @@ void HandRecognizer::processRects(queue<pair<QRect,uint> > * q, QImage * imgRef,
}
//zapis do suboru
- stringstream fname;
+ /*stringstream fname;
//index = 0;
fname << "hand_images/"<< ((hand>0.5) ? "hand" : "other") << "_" << index << ".pbm";
index++;
@@ -105,6 +105,6 @@ void HandRecognizer::processRects(queue<pair<QRect,uint> > * q, QImage * imgRef,
}
}
ofs << endl;
- }
+ }*/
}
}
View
23 mainwindow.cpp
@@ -113,19 +113,30 @@ void MainWindow::getImage()
if(!imageFromCamera.isNull())
{
- QImage img = imageProcessor->processImage(imageFromCamera);
- /*HandDetector d;
- img.setPixel(d.getHand(&img),0xFFFF0000);*/
+ QImage img =
+ imageProcessor->processImage(imageFromCamera);
+ //QImage img(VIDEO_WIDTH,VIDEO_HEIGHT,QImage::Format_RGB32);
+ QPainter p;
+ p.begin(&img);
+ /*p.setPen(QPen(QColor(Qt::white)));
+ p.setBrush(QBrush(QColor(Qt::white)));
+ p.drawRect(0,0,VIDEO_WIDTH,VIDEO_HEIGHT);*/
if(handRecognizer->isHand())
{
- QPainter p;
- p.begin(&img);
+
+ p.setPen(QPen(QColor(Qt::blue)));
+ p.setBrush(QBrush(QColor(Qt::red)));
+ QRect r = handRecognizer->getHandRect();
+ p.drawRect(r.x()+r.width()/2-2,r.y()+r.height()/2-2,4,4);
+
p.setPen(QPen(QColor(Qt::red)));
p.setBrush(QBrush(QColor(Qt::color0), Qt::NoBrush));
p.drawRect(handRecognizer->getHandRect());
- p.end();
+
ui->label_2->setText(QString::number(handRecognizer->getHandP()));
}
+ p.end();
+
pixmap = QPixmap::fromImage(img);
}
View
1 mainwindow.h
@@ -30,6 +30,7 @@
#define VIDEO_DEVICE "/dev/video0"
#define VIDEO_WIDTH 320
#define VIDEO_HEIGHT 240
+#define MAX_EMPTY_SPAN 10
namespace Ui {
View
13 mainwindow.ui
@@ -62,6 +62,19 @@
<bool>false</bool>
</property>
</widget>
+ <widget class="QLabel" name="label_2">
+ <property name="geometry">
+ <rect>
+ <x>640</x>
+ <y>220</y>
+ <width>56</width>
+ <height>15</height>
+ </rect>
+ </property>
+ <property name="text">
+ <string>TextLabel</string>
+ </property>
+ </widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">

0 comments on commit 8dd36fa

Please sign in to comment.