내 작은 로봇이 비디오를 캡처하지만 이미지 파일을 로컬에서 처리 할만큼 성능이 떨어집니다. 필자는 데스크탑 크롬 브라우저에서 원격으로 볼 수있는 간단한 비디오 스트리밍 코드를 선물했습니다. 아래에 표시된 로봇의이 "서버 코드"는 정상적으로 작동합니다.자바 OpenCV HTTP URL에서 비디오를 캡처하는 방법
public class HttpStreamOpenCV {
public static void main(String[] args) throws Exception {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat = new Mat();
VideoCapture vid = new VideoCapture(0);
vid.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 160);
vid.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 120);
vid.open(0);
System.out.println("Camera open");
ServerSocket ss = new ServerSocket(8080);
Socket sock = ss.accept();
System.out.println("Socket connected");
String boundary = "Thats it folks!";
writeHeader(sock.getOutputStream(), boundary);
System.out.println("Written header");
long stime = System.currentTimeMillis();
int cnt = 0;
while (Button.ESCAPE.isUp()) {
vid.read(mat);
if (!mat.empty()) {
writeJpg(sock.getOutputStream(), mat, boundary);
System.out.println("Written jpg");
if (cnt++ >= 100)
{
long stop = System.currentTimeMillis();
System.out.println("Frame rate: " + (cnt*1000/(stop - stime)));
cnt = 0;
stime = stop;
}
} else System.out.println("No picture");
}
sock.close();
ss.close();
}
private static void writeHeader(OutputStream stream, String boundary) throws IOException {
stream.write(("HTTP/1.0 200 OK\r\n" +
"Connection: close\r\n" +
"Max-Age: 0\r\n" +
"Expires: 0\r\n" +
"Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n" +
"Pragma: no-cache\r\n" +
"Content-Type: multipart/x-mixed-replace; " +
"boundary=" + boundary + "\r\n" +
"\r\n" +
"--" + boundary + "\r\n").getBytes());
}
private static void writeJpg(OutputStream stream, Mat img, String boundary) throws IOException {
MatOfByte buf = new MatOfByte();
Highgui.imencode(".jpg", img, buf);
byte[] imageBytes = buf.toArray();
stream.write(("Content-type: image/jpeg\r\n" +
"Content-Length: " + imageBytes.length + "\r\n" +
"\r\n").getBytes());
stream.write(imageBytes);
stream.write(("\r\n--" + boundary + "\r\n").getBytes());
}
}
이 스트림을 읽으려면 데스크톱 컴퓨터에서 Java OpenCV를 사용하고 싶습니다. OpenCV의의 조각에서 나는 .. 나는이 같은 URL로 비디오 캡처를 열 수 있어야
String url = "http://192.168.1.86:8080/?dummy=param.jpg";
VideoCapture capture =new VideoCapture(url);
을 추정했지만이 "클라이언트 코드는"아무것도하지 않는다. 나는 오류를 던지지 않으며 소켓을 열지도 않는다 (Wireshark를 사용하여 검사). 나는 openCV 2411과 331을 모두 시험해 보았습니다. 나는 주변을 수색 했으므로 아무도 작동하지 않는다는 증거를 보지 못 했으므로 작동해야합니까? 그렇지 않다면 위의 writeJpg 메서드 (ReadJpg)에 상호 클라이언트 코드를 작성하여 현재 OpenCV 메서드에 버퍼링하고 이미지를 전달해야합니다.