Add "image" module
authorSam Moore <[email protected]>
Sun, 15 Sep 2013 05:23:54 +0000 (13:23 +0800)
committerSam Moore <[email protected]>
Sun, 15 Sep 2013 05:23:54 +0000 (13:23 +0800)
- 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)

server/Makefile
server/fastcgi.c
server/fastcgi.h
server/image.c [new file with mode: 0644]
server/image.h [new file with mode: 0644]

index 51dfd82..3838876 100644 (file)
@@ -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
index f4b0f2d..092eaed 100644 (file)
@@ -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;
index f0b8bd9..adb1db9 100644 (file)
@@ -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 (file)
index 0000000..81c692b
--- /dev/null
@@ -0,0 +1,25 @@
+#include "cv.h"
+#include "highgui_c.h"
+#include "image.h"
+#include <string.h>
+#include <stdio.h>
+
+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 (file)
index 0000000..7cec1ed
--- /dev/null
@@ -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

UCC git Repository :: git.ucc.asn.au