View Single Post
  #3   Spotlight this post!  
Unread 10-01-2017, 15:59
Thad House Thad House is online now
Volunteer, WPILib Contributor
no team (Waiting for 2021)
Team Role: Mentor
 
Join Date: Feb 2011
Rookie Year: 2010
Location: Thousand Oaks, California
Posts: 1,106
Thad House has a reputation beyond reputeThad House has a reputation beyond reputeThad House has a reputation beyond reputeThad House has a reputation beyond reputeThad House has a reputation beyond reputeThad House has a reputation beyond reputeThad House has a reputation beyond reputeThad House has a reputation beyond reputeThad House has a reputation beyond reputeThad House has a reputation beyond reputeThad House has a reputation beyond repute
Re: MJPG Streamer input from opencv in C++

Quote:
Originally Posted by jreneew2 View Post
Hello!

I am setting up a vision co-processor with a raspberry pi 3 and I am having a trouble getting image data from opencv to mjpg-streamer. The reason I want to do this is so I can see the output of the opencv processing right on the smart dashboard.

The specific issue is that there is no image showing up in the web server for mjpg-streamer. The page is continuously loading so I think its just not displaying the stream.

I can even watch the avi file if I run omxplayer on the file.

Here is my opencv code:
Code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <ntcore.h>
#include <cscore.h>
#include <networktables/NetworkTable.h>

void VisionThread() {

	cv::VideoCapture vidCap(0);
	
	cv::Mat frame;
	cv::Mat output;

	cv::namedWindow("Display Window", cv::WINDOW_AUTOSIZE);
	
	if(!vidCap.isOpened()) {
		std::cout << "Can't open video stream!" << "\n";
	}
	else {
		vidCap.set(CV_CAP_PROP_FRAME_WIDTH,640);
		vidCap.set(CV_CAP_PROP_FRAME_HEIGHT,480);
	}

	vidCap >> frame; 
		
	cv::VideoWriter outStream("output/out.avi", CV_FOURCC('M','J','P','G'), 30, frame.size(), true);

	while(true) {
		vidCap >> frame;
		cv::circle(frame, cv::Point(320, 240), 5, cv::Scalar(255, 0, 0));
		
		if(outStream.isOpened()) {
			outStream.write(frame);
		} 
		else {
			std::cout<<"Can't write to file!" << "\n";
		}
		
		cv::imshow("Display Window", frame);
		if(cv::waitKey(30) >= 0) break;
	}	
}

int main(int argc, const char** argv) {
	
	VisionThread();
	
    return 0;
}
Here is the command I am running to start mjpg streamer:
Code:
mjpg_streamer -i "input_file.so -f /home/pi/vision_code/output"  -o "output_http.so -w /usr/local/www"
Any help is appreciated!

Thanks,
Drew
You should be using the cscore classes to handle the USB cameras and the opencv streams. Especially since you already have cscore linked.They are much more reliable then the stock opencv vidcapture objects. Take a look at the UsbCamera, CvSink, CvSource and MjpegServer classes in cscore. I can post an example how how to use them in code in a bit, but it's fairly simple. Create the UsbCamera and a CvSink, set the cvSink source to be the USB camera. Create a cvsource and an MjpegServer. Set the MjpegServer source to the cvsource. Then in your loop use CvSink grabframe to get the frame, and Cvsource putframe to stream your final opencv image
__________________
All statements made are my own and not the feelings of any of my affiliated teams.
Teams 1510 and 2898 - Student 2010-2012
Team 4488 - Mentor 2013-2016
Co-developer of RobotDotNet, a .NET port of the WPILib.
Reply With Quote