I have found many examples in opencv of sending a mat through socket from java to java or c++, but I can't get it to work on python.
The server code:
MatOfByte bytemat = new MatOfByte();
Highgui.imencode(".jpg", out, bytemat);
byte[] bytes = bytemat.toArray();
r.write(String.valueOf(bytes.length));
Log.d(TAG, String.valueOf(bytes.length));
r.write(bytes);
The python code:
def recvall(sock, count):
buf = b''
while count:
newbuf = sock.recv(count)
if not newbuf: return None
buf += newbuf
count -= len(newbuf)
return buf
length = recvall(camera_socket, 5)
if not length:
continue
print length
data = recvall(camera_socket, int(length))
if not data:
continue
nparr = np.fromstring(data, np.uint8)
frame = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_UNCHANGED)
window = cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
cv2.imshow('frame', frame)
The weird part is that imdecode returns None always. I just can't get it to work.
PS: the java client works using ObjectInputStream
----EDIT----
Thanks all for advices, I've replaced the byte stream with predefined bytes and discovered that Java was sending some headers when sending bytes because it was using ObjectOutputStream.
Now the java code for writing to socket is:
DataOutputStream oos = null;
try {
oos = new DataOutputStream(os);
oos.write(byteImage);
} catch (Exception e) {
Log.e(TAG, "Error while writing to OutputStream", e);
cancel();
setState(STATE_NONE, this.type);
}
Try using np.uint8(nparr) for conversion as in:
frame = np.uint8(nparr)
This example works:
import numpy as np
import cv2
nparr = np.zeros((512, 512))
nparr[200:300, 400:450]=255
cv2.imshow("Result", np.uint8(nparr))
cv2.waitKey()
[EDIT] In case of a colour image please keep in mind that OpenCV2 images are BGR instaed of RGB, so you may vae to use
rgb = cv2.cvtColor(frame_in_bgr, cv2.COLOR_BGR2RGB)
Thanks all for advices, I've replaced the byte stream with predefined bytes and discovered that Java was sending some headers when sending bytes because it was using ObjectOutputStream.
Now the java code for writing to socket is:
DataOutputStream oos = null;
try {
oos = new DataOutputStream(os);
oos.write(byteImage);
} catch (Exception e) {
Log.e(TAG, "Error while writing to OutputStream", e);
cancel();
setState(STATE_NONE, this.type);
}
This now works. The only problem left is that the colors are inverted. Any tip on how to invert them again?
Related
I am trying to receive the video stream of my Tello drone using Processing as stated in its SDK documentation:
To receive the video stream via UDP I have tried to use a script by The Coding Train:
import java.awt.image.*;
import javax.imageio.*;
import java.net.*;
import java.io.*;
// Port we are receiving.
int port = 11111;
DatagramSocket ds;
// A byte array to read into (max size of 65536, could be smaller)
byte[] buffer = new byte[65536];
PImage video;
void setup() {
size(400,300);
try {
ds = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
video = createImage(320,240,RGB);
}
void draw() {
// checkForImage() is blocking, stay tuned for threaded example!
checkForImage();
// Draw the image
background(0);
imageMode(CENTER);
image(video,width/2,height/2);
}
void checkForImage() {
DatagramPacket p = new DatagramPacket(buffer, buffer.length);
try {
ds.receive(p);
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = p.getData();
println("Received datagram with " + data.length + " bytes." );
// Read incoming data into a ByteArrayInputStream
ByteArrayInputStream bais = new ByteArrayInputStream( data );
// We need to unpack JPG and put it in the PImage video
video.loadPixels();
try {
// Make a BufferedImage out of the incoming bytes
BufferedImage img = ImageIO.read(bais);
// Put the pixels into the video PImage
img.getRGB(0, 0, video.width, video.height, video.pixels, 0, video.width);
} catch (Exception e) {
e.printStackTrace();
}
// Update the PImage pixels
video.updatePixels();
}
The only thing I changed in the script is the variable port as the SDK documentation (see above) specifies, that it should be 11111.
When I run the script (after successfully sending command and streamon to the drone), I get the message Received datagram with 65536 bytes. and a Null Pointer Exception. I've found out, that the Null Pointer Exception is shown, because the following line of code returns null:
BufferedImage img = ImageIO.read(bais);
So in the end, my question is, why this line of code or more specifically ImageIO.read(bais) returns null. However, the script works correctly, if you use it combined with the Video Sender script of the Coding Train. So, what's the problem here?
I've read a few posts on how to send a picture using sockets in Python, and how to send a picture using sockets in Java, I was wanting to combine the two and send a picture from Python to Java using sockets on both ends. Most of my code is taken from the posts I read but here is the python client:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.0.12",4141))
try:
file = open("subbed.jpg", 'rb')
bytes = file.read()
print "{0:b}".format(len(bytes))
size = len(bytes)
s.sendall(size)
answer = s.recv(4096)
print "Answer = %s" %answer
if answer == 'GOT SIZE':
s.sendall(bytes)
answer = s.recv(4096)
if answer == 'GOT IMAGE' :
s.sendall("byte")
file.close()
finally:
s.close()
the code for the Java server is:
public static void main(String[] args) {
while(true) {
try (
ServerSocket server = new ServerSocket(PORT_NUMBER);
Socket client = server.accept();
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
InputStream in = client.getInputStream()) {
System.out.println("GOT CONNECTION FROM: " + client.getInetAddress().toString());
byte[] sizeAr = new byte[4];
in.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
System.out.println(Integer.toBinaryString(size));
out.println("GOT SIZE");
byte[] imageAr = new byte[size];
in.read(imageAr);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));
ImageIO.write(image, "jpg", new File("C:\\myprivatelocation\\test.jpg"));
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
}
The initial problem comes from sending the size I think. I'm no python expert, nor am I a Java expert, but I think what's happening is Python is sending the size as a string and Java is receiving it as a byte array and converting it to an integer, and there are some differences in the way they are stored in the two languages. Can anyone offer any assistance with this issue?
Although I would approach your problem slightly differently, the following code works:
Python Sender
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(("127.0.0.1", 8888))
with open("C:\\temp\\test-input.jpg", 'rb') as f:
content = f.read()
size = len(content)
print("File bytes:", size)
s.sendall(size.to_bytes(4, byteorder='big'))
buff = s.recv(4)
resp = int.from_bytes(buff, byteorder='big')
print("Response:", resp)
if size == resp:
s.sendall(content)
buff = s.recv(2)
print(buff)
print("Complete.")
Java Receiver
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
class Server{
public static void main(String[] args) {
int PORT_NUMBER = 8888;
try (
ServerSocket server = new ServerSocket(PORT_NUMBER);
Socket client = server.accept();
OutputStream sout = client.getOutputStream();
InputStream sin = client.getInputStream();
){
System.out.println("GOT CONNECTION FROM: " + client.getInetAddress().toString());
// Get length
byte[] size_buff = new byte[4];
sin.read(size_buff);
int size = ByteBuffer.wrap(size_buff).asIntBuffer().get();
System.out.format("Expecting %d bytes\n", size);
// Send it back (?)
sout.write(size_buff);
// Create Buffers
byte[] msg_buff = new byte[1024];
byte[] img_buff = new byte[size];
int img_offset = 0;
while(true) {
int bytes_read = sin.read(msg_buff, 0, msg_buff.length);
if(bytes_read == -1) { break; }
// Copy bytes into img_buff
System.arraycopy(msg_buff, 0, img_buff, img_offset, bytes_read);
img_offset += bytes_read;
System.out.format("Read %d / %d bytes...\n", img_offset, size);
if(img_offset >= size) { break; }
}
BufferedImage image = ImageIO.read(new ByteArrayInputStream(img_buff));
ImageIO.write(image, "jpg", new File("C:\\temp\\test-output.jpg"));
// Send "OK"
byte[] OK = new byte[] {0x4F, 0x4B};
sout.write(OK);
}
catch (IOException ioe) { ioe.printStackTrace(); }
}
}
The sender opens a socket, reads the file, and sends the receiver the length. The receiver gets the length, parses the bytes and sends it back. Upon receipt of the "confirmation", the sender then sends the file contents. The receiver will then repeatedly read 1024 byte chunks from the socket input stream, inserting the bytes into img_data. When there are no more bytes expected (or the socket is closed), the receiver will send "OK" to the sender (unconditionally) and exit. The sender will just print that "OK" (in bytes), and exit.
Some of this could be cleaned up with a ByteArrayOutputStream, but I wanted to get as close to the functionality of your code as possible.
Something is off - you should be getting some kind of error when trying to send some integer in the socket:
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect(('localhost', 7777))
>>> s.sendall(len(b'some bytes'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'int'
Sockets can only send bytes, you have to convert the int object containing the size to bytes somehow, python won't do it automatically for you. Your code should fail with the error above.
In the java code you are using asIntBuffer() and Integer.toBinaryString to convert your values, in the python code you just try to send the data without converting, you should get error.
Now, to convert the int to bytes, you can use the struct module; It will convert to a binary representation used by the C language - I think that's what your java code expect
size_in_bytes = struct.pack('I', len(data_to_send))
In the same way, you should use struct.unpack to convert the bytes back to a integer object. See the documentation for more details and a table of possible conversions.
I am transmitting image from server to client. Server capture the screen and convert it to byte array and client receive byte array and convert back it to image. but transmission happens only for few frame and then error occurred.
Receiver side:
while(true) {
DataInputStream dis = new DataInputStream(csocket.getInputStream());
int len = dis.readInt();
System.out.println(len);
byte data[] = null;
data = new byte[len];
dis.read(data);
ByteArrayInputStream in = new ByteArrayInputStream(data);
BufferedImage image1=ImageIO.read(in);
ImageIcon imageIcon= new ImageIcon(image1);
Image image = imageIcon.getImage();
image = image.getScaledInstance(cPanel.getWidth(),cPanel.getHeight(),Image.SCALE_FAST);
//Draw the recieved screenshot
Graphics graphics = cPanel.getGraphics();
graphics.drawImage(image, 0, 0, cPanel.getWidth(),cPanel.getHeight(),cPanel);
}
Sender Side:
while(continueLoop) {
try {
BufferedImage image = robot.createScreenCapture(rectangle);
byte[] imageInByte;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image,"jpg", baos);
baos.flush();
imageInByte = baos.toByteArray();
baos.close();
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
//PrintWriter out = new PrintWriter(socket.getOutputStream());
//out.flush();
dos.writeInt(imageInByte.length);
System.out.println(imageInByte.length);
dos.write(imageInByte);
Thread.sleep(1000);
dos.flush();
}
catch(Exception e) {
}
}
Output of Receiver:
1177222283
-297418067
1228900861
-412483840
189486847
10536391
-33405441
12898815
740182
-16736067
-805436987
-16726825
258150991
2137853087
1917408603
512024791
-1227886373
-1034512766
1772271848
157387
Exception in thread "Thread-3" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:228)
at remoteclient.ClientScreenReciever.run(ClientScreenReciever.java:65)
Please help me..what to do for continuous transmission of image from server to client over socket in faster way in java.
You are using
DataInputStream#read(byte[])
which (check its Javadoc) does not guarantee that a full arrays-worth of data will be read. This method is used for buffered reading and not to fully read the requested amount of bytes.
Instead you must call
DataInputStream#readFully(byte[])
which has a contract suiting your purpose (again check the Javadoc).
I need a very simple function that allows me to read the first 1k bytes of a file through FTP. I want to use it in MATLAB to read the first lines and, according to some parameters, to download only files I really need eventually. I found some examples online that unfortunately do not work. Here I'm proposing the sample code where I'm trying to download one single file (I'm using the Apache libraries).
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try {
client.connect("data.site.org");
// filename to be downloaded.
String filename = "filename.Z";
fos = new FileOutputStream(filename);
// Download file from FTP server
InputStream stream = client.retrieveFileStream("/pub/obs/2008/021/ab120210.08d.Z");
byte[] b = new byte[1024];
stream.read(b);
fos.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
the error is in stream which is returned empty. I know I'm passing the folder name in a wrong way, but I cannot understand how I have to do. I've tried in many way.
I've also tried with the URL's Java classes as:
URL url;
url = new URL("ftp://data.site.org/pub/obs/2008/021/ab120210.08d.Z");
URLConnection con = url.openConnection();
BufferedInputStream in =
new BufferedInputStream(con.getInputStream());
FileOutputStream out =
new FileOutputStream("C:\\filename.Z");
int i;
byte[] bytesIn = new byte[1024];
if ((i = in.read(bytesIn)) >= 0) {
out.write(bytesIn);
}
out.close();
in.close();
but it is giving an error when I'm closing the InputStream in!
I'm definitely stuck. Some comments about would be very useful!
Try this test
InputStream is = new URL("ftp://test:test#ftp.secureftp-test.com/bookstore.xml").openStream();
byte[] a = new byte[1000];
int n = is.read(a);
is.close();
System.out.println(new String(a, 0, n));
it definitely works
From my experience when you read bytes from a stream acquired from ftpClient.retrieveFileStream, for the first run it is not guarantied that you get your byte buffer filled up. However, either you should read the return value of stream.read(b); surrounded with a cycle based on it or use an advanced library to fill up the 1024 length byte[] buffer:
InputStream stream = null;
try {
// Download file from FTP server
stream = client.retrieveFileStream("/pub/obs/2008/021/ab120210.08d.Z");
byte[] b = new byte[1024];
IOUtils.read(stream, b); // will call periodically stream.read() until it fills up your buffer or reaches end-of-file
fos.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(inputStream);
}
I cannot understand why it doesn't work. I found this link where they used the Apache library to read 4096 bytes each time. I read the first 1024 bytes and it works eventually, the only thing is that if completePendingCommand() is used, the program is held for ever. Thus I've removed it and everything works fine.
I have done some research, also checked answer on stackoverflow. However i just cant get my code right, please help. the code can run, but i cant get the image, it shows 0kb.
Socket socket = new Socket(addr, port);
byte [] buffer = new byte[1024];
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
writer.println("GET " + url.getFile() + " HTTP/1.0\r\n");
writer.println("HOST:" + url.getHost() + "\r\n");
writer.println("\r\n");
DataInputStream in = new DataInputStream(socket.getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
int n = 0;
while (-1!=(n=in.read(buffer)))
{
out.write(buffer, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream("0.jpeg");
fos.write(response);
fos.close();
}catch (Exception e){
System.out.println(e.toString());
}
Using raw sockets to perform an HTTP GET is much more complicated than necessary.
I recommend using an HTTP client like the one from Apache or you can use java.net.URLConnection. See How do I do a HTTP GET in Java? or Using java.net.URLConnection to fire and handle HTTP requests
Your code doesn't have any obvious flaws. If you're getting a zero length file, it's because you aren't sending anything.
BTW you don't need the ByteArrayOutputStream. You can write everything you read directly to the FileOutputStream. Saves both time and space.