-
Notifications
You must be signed in to change notification settings - Fork 7
/
webcam.cpp
51 lines (33 loc) · 988 Bytes
/
webcam.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <jpeglib.h>
//#include "ext/fmemopen/fmemopen.h"
#include "webcam.hpp"
using namespace std;
using namespace cv;
vector<uchar> jpegbuf;
vector<int> jpegbuf_params;
VideoCapture camera;
void webcam_init(unsigned short int webcam_index) {
camera.open(webcam_index);
if(!camera.isOpened()) {
perror("Failed to connect to a webcam.");
exit(1);
}
jpegbuf_params.push_back(CV_IMWRITE_JPEG_QUALITY);
jpegbuf_params.push_back(100);
}
FILE *webcam_read() {
Mat frame, edges;
camera >> frame;
flip(frame, frame, +1);
cvtColor(frame, edges, CV_BGR2GRAY);
imencode(".jpg", frame, jpegbuf, jpegbuf_params);
FILE *jpegfile = fmemopen(jpegbuf.data(), jpegbuf.size()+1, "r");
// FIXME: do I even need this?
waitKey(16); // FPS
return jpegfile;
}