Currently, I am using OpenCV to record a lifestream from my webcam. I now want to display that in a browser. I was thinking of using VideoWriter to write the video to a file, then somehow access that file from HTML5. Is this possible? Any other suggestions?
The following is the code I have.
int main(int argc, const char * argv[]) {
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) { // check if we succeeded
std::cout << "No camera found!\n";
}
namedWindow("Window",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("Window", frame);
char keypress;
keypress = waitKey(30);
if(keypress==27) break;
}
return 0;
}
As can be seen, I am displaying the live stream in a window, but, as said, I want a browser lifestream. Thanks.
Related
I am trying to open a window of my webcam using opencv in c++ but it seems the webcam does not want to open. I tested before in other apps like cheese and it works.
#include <opencv2/highgui.hpp>
#include <iostream>
int main() {
int PORT = 0;
cv::Mat image;
cv::namedWindow("Webcam window", cv::WINDOW_AUTOSIZE);
cv::VideoCapture cap(PORT);
cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);
cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
if (!cap.isOpened()) {
std::cout << "Could not open the camera" << std::endl;
return -1;
}
while (true) {
cap >> image;
if (!image.empty())
cv::imshow("Webcam window", image);
if (cv::waitKey(10) >= 0) {
break;
}
}
return 0;
}
I am using ubuntu budgie 21.10.
Well this might sound silly, but I don't see a call to cv::open function. You mentioned that when you call it it simply returns false, but I don't see it in the code you provided.
You should also try to explicitly open default camera device.
Have you tried official examples from OpenCV? (such as https://docs.opencv.org/3.4/d8/dfe/classcv_1_1VideoCapture.html)
Do they work?
Also, you should turn on logging on the most verbose level, it often provide additional info about what's going on: How to enable logging for OpenCV
I have created a program reading images in the sequence. Here is my code:
int main(int argc, char** argv) {
cv::namedWindow("Example3", cv::WINDOW_AUTOSIZE);
cv::VideoCapture cap;
cap.open(string("F:/8TH SEMESTER/trial/%05d.ppm"));
//"F:/8TH SEMESTER/Traffic Sign Dectection/GTSDB/FullIJCNN2013/FullIJCNN2013/01/%05d.ppm"
cv::Mat frame;
for (;;) {
cap >> frame;
if (frame.empty()) break; // Ran out of film
cv::imshow("Example3", frame);
if (cv::waitKey(2000) >= 0) break;
}
return 0;
}
it works perfectly with those images which have traffic sign and cars etc. But when I run this code with sample data, for example only with a 30-speed sign then it runs first time and stops? Why does this happen?
With this image, it works perfectly:
When I run it using the following image, it gives error.
I'm using OpenCV 3.1, I try to run a simple code as the following one (main function):
cv::VideoCapture cam;
cv::Mat matTestingNumbers;
cam.open(0);
if (!cam.isOpened()) { printf("--(!)Error opening video capture\n"); return -1; }
while (cam.read(matTestingNumbers))
{
cv::imshow("matTestingNumbers", matTestingNumbers);
cv::waitKey(5000);
}
When I move the camera it seems that the code does not capture and show the current frame but shows all the captured frames from the previous position and only then from the new one.
So when I capture the wall it shows the correct frames (the wall itself) in the correct delay, but, when I twist the camera to my computer, I first see about 3 frames of the wall and only then the computer, it seems that the frames are stuck.
I've tried to use videoCapture.set() functions and set the FPS to 1, and I tried to switch the method of capturing to cam >> matTestingNumbers (and the rest of the main function according to this change) but nothing helped, I still got "stuck" frames.
BTW, These are the solutions I found on web.
What can I do to fix this problem?
Thank you, Dan.
EDIT:
I tried to retrieve frames as the following:
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat frame;
namedWindow("edges",1);
for(;;)
{
cap.grab();
if (waitKey(11) >= 0)
{
cap.retrieve(frame);
imshow("edges", frame);
}
}
return 0;
}
But, it gave the result (when I pointed the camera on one spot and pressed a key it showed one more of the previous frames that were captured of the other point).
It is just like you're trying to picture one person then another but when you picture the second you get the photo of the first person what doesn't make sense.
Then, I tried the following:
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat frame;
namedWindow("edges",1);
for(;;)
{
cap >> frame;
if (waitKey(33) >= 0)
imshow("edges", frame);
}
return 0;
}
And it worked as expected.
One of the problems is that you are not calling cv::waitKey(X) to properly freeze the window for X amount of milliseconds. Get rid of usleep()!
I'm new to opencv and its development. I'm taking camera feed and that feed i convert into avi format's video file. When i try to open that file using VLC player. Nothing is shown to me. please find below the code i used. Any help is appreciated.( File is writing into file but i think its problem with file formats )
int main(int argc, char** argv){
VideoCapture vcap(0);
if(!vcap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
// VideoWriter video("/Users/venushka/Desktop/pre/ou.avi",vcap.get(CV_CAP_PROP_FOURCC),vcap.get(CV_CAP_PROP_FPS),
// cv::Size(vcap.get(CV_CAP_PROP_FRAME_WIDTH), vcap.get(CV_CAP_PROP_FRAME_HEIGHT)));
const int fps = 30.0;
vcap.set(CV_CAP_PROP_FPS, fps);
VideoWriter video("/Users/venushka/Desktop/pre/ou.avi", vcap.get(CV_CAP_PROP_FOURCC), fps,
cv::Size(vcap.get(CV_CAP_PROP_FRAME_WIDTH), vcap.get(CV_CAP_PROP_FRAME_HEIGHT)));
for(;;){
Mat frame;
vcap >> frame;
video.write(frame);
imshow( "Frame", frame );
char c = (char)waitKey(33);
if( c == 27 ) break;
}
return 0;
}
CV_CAP_PROP_FPS only works on videos as far as I know, from camera it will return 0.
Set your FPS manually as follows:
const int fps = 30.0;
vcap.set(CV_CAP_PROP_FPS, fps);
VideoWriter video("<path to avi>", vcap.get(CV_CAP_PROP_FOURCC), fps,
cv::Size(vcap.get(CV_CAP_PROP_FRAME_WIDTH), vcap.get(CV_CAP_PROP_FRAME_HEIGHT)));
I use VS 2010, with opencv. Whatever i try when i want to use my wecamera (on my laptop), i get this: "r6010 abort() has been called".
And a gray window shows up.
Here is the code:
#include<opencv\cv.h>
#include<opencv\highgui.h>
using namespace cv;
int main()
{
Mat image;
VideoCapture cap;
cap.open(0);
namedWindow("Window", 1);
while (1)
{
cap >> image;
imshow("Windwow",image);
waitKey(33);
}
}
Btw, in another program, what i got from youtube, it says "Error: Frame is NULL".
On my laptop, I have to use device 1 to access the webcam. You should also check whether cap is opened, e.g.
cap.open(1);
if(!cap.isOpened())
return -1;