From: Sam Moore Date: Sun, 15 Sep 2013 05:23:54 +0000 (+0800) Subject: Add "image" module X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=ff5831931c289cd740609eaad0d36d768751a6d3;p=matches%2FMCTX3420.git Add "image" module - Use OpenCV to take image from webcam - Use FastCGI and write image back to client - Works! - Currently has a memory leak, but that can be easily fixed - ~50 FPS (on my laptop though, really need to start using the BBB for things) --- diff --git a/server/Makefile b/server/Makefile index 51dfd82..3838876 100644 --- a/server/Makefile +++ b/server/Makefile @@ -2,7 +2,7 @@ CXX = gcc FLAGS = -std=c99 -Wall -pedantic -g -I/usr/include/opencv -I/usr/include/opencv2/highgui -L/usr/lib LIB = -lfcgi -lssl -lcrypto -lpthread -lm -lopencv_highgui -lopencv_core -lopencv_ml -lopencv_imgproc -OBJ = log.o control.o data.o fastcgi.o main.o sensor.o actuator.o +OBJ = log.o control.o data.o fastcgi.o main.o sensor.o actuator.o image.o RM = rm -f BIN = server diff --git a/server/fastcgi.c b/server/fastcgi.c index f4b0f2d..092eaed 100644 --- a/server/fastcgi.c +++ b/server/fastcgi.c @@ -15,6 +15,7 @@ #include "actuator.h" #include "control.h" #include "options.h" +#include "image.h" /**The time period (in seconds) before the control key expires @ */ #define CONTROL_TIMEOUT 180 @@ -433,6 +434,17 @@ void FCGI_PrintRaw(const char *format, ...) va_end(list); } + +/** + * Write binary data + * See fwrite + */ +void FCGI_WriteBinary(void * data, size_t size, size_t num_elem) +{ + Log(LOGDEBUG,"Writing!"); + FCGI_fwrite(data, size, num_elem, FCGI_stdout); +} + /** * Main FCGI request loop that receives/responds to client requests. * @param data Reserved. @@ -470,6 +482,8 @@ void * FCGI_RequestLoop (void *data) module_handler = Sensor_Handler; } else if (!strcmp("actuators", module)) { module_handler = Actuator_Handler; + } else if (!strcmp("image", module)) { + module_handler = Image_Handler; } context.current_module = module; diff --git a/server/fastcgi.h b/server/fastcgi.h index f0b8bd9..adb1db9 100644 --- a/server/fastcgi.h +++ b/server/fastcgi.h @@ -58,6 +58,8 @@ extern char *FCGI_EscapeJSON(char *buf); extern void FCGI_RejectJSONEx(FCGIContext *context, StatusCodes status, const char *description); extern void *FCGI_RequestLoop (void *data); +extern void FCGI_WriteBinary(void * data, size_t size, size_t num_elem); + /** * Shortcut to calling FCGI_RejectJSONEx. Sets the error code * to STATUS_ERROR. diff --git a/server/image.c b/server/image.c new file mode 100644 index 0000000..81c692b --- /dev/null +++ b/server/image.c @@ -0,0 +1,25 @@ +#include "cv.h" +#include "highgui_c.h" +#include "image.h" +#include +#include + +void Image_Handler(FCGIContext * context, const char * params) +{ + static CvCapture * capture = NULL; + if (capture == NULL) + capture = cvCreateCameraCapture(-1); + + static int p[] = {CV_IMWRITE_JPEG_QUALITY, 100, 0}; + + IplImage * frame = cvQueryFrame(capture); + assert(frame != NULL); + CvMat * jpg = cvEncodeImage(".jpg", frame, p); + + // Will this work? + Log(LOGNOTE, "Sending image!"); + FCGI_PrintRaw("Content-type: image/jpg\r\n\r\n"); + //FCGI_PrintRaw("Content-Length: %d", jpg->rows*jpg->cols); + FCGI_WriteBinary(jpg->data.ptr,1,jpg->rows*jpg->cols); + +} diff --git a/server/image.h b/server/image.h new file mode 100644 index 0000000..7cec1ed --- /dev/null +++ b/server/image.h @@ -0,0 +1,15 @@ +/** + * @file image.h + * @purpose Helper functions for image processing + */ + +#ifndef _IMAGE_H +#define _IMAGE_H + +#include "common.h" + +extern void Image_Handler(FCGIContext * context, const char * params); + +#endif //_IMAGE_H + +//EOF