Created
June 12, 2019 13:18
-
-
Save ravenxrz/f214196192cc5eaaa94a901f25f3b05a to your computer and use it in GitHub Desktop.
Socket通信,客户端发送图片给服务器,服务器进行一定的处理后返回给客户端新图片
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.raven.mnist_recon; | |
import android.util.Log; | |
import com.bumptech.glide.Glide; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.Socket; | |
/** | |
* 上传图片类 | |
*/ | |
public class UpLoadImgModule { | |
private final String IP = "103.46.128.45"; | |
private final int PORT= 13204; | |
/** | |
* 上传回调接口 | |
*/ | |
public interface CallBackListener{ | |
/** | |
* 完成上传并分析后,回调这个函数 | |
* @param score 分析得到的分数 | |
*/ | |
void onScoreFinished(String score); | |
/** | |
* 中途出错,回调这个函数 | |
*/ | |
void onScoreError(String msg); | |
} | |
private CallBackListener callBackListener; | |
public UpLoadImgModule(CallBackListener callBackListener) { | |
this.callBackListener = callBackListener; | |
} | |
/** | |
* 上传图片函数,在相册选择图片后,会自动调动这个方法。filePath是你选择的图片的绝对路径 | |
* @param filePath1 选择图片1的绝对路径 | |
* @param filePath2 选择图片2的绝对路径 | |
*/ | |
public void uploadImg(String filePath1,String filePath2){ | |
/* 在这里写你的上传代码,写完后回调callBackListener中的相关方法 | |
* 注意可能需要调用线程,不然程序可能卡死,但是我没写过python混编,这部分得到时候来看了 | |
* */ | |
new SocketThread(filePath1,filePath2).start(); | |
} | |
/** | |
* Socket通信线程,主要实现功能: | |
* 1. 发送试卷正反面到服务端 | |
* 2. 发送完成后建立接受分析结果socket | |
*/ | |
private class SocketThread extends Thread { | |
private String path1, path2; | |
Socket socket; | |
SocketThread(String imgPath1, String imgPath2) { | |
this.path1 = imgPath1; | |
this.path2 = imgPath2; | |
} | |
/** | |
* 发送图片 | |
* @param path | |
* @throws IOException | |
*/ | |
private void sendImg(String path) throws IOException { | |
// 创建一个Socket对象,并指定服务端的IP及端口号 | |
socket = new Socket(IP, PORT); | |
InputStream inputStream = new FileInputStream(path); | |
// 获取Socket的OutputStream对象用于发送数据。 | |
OutputStream outputStream = socket.getOutputStream(); | |
// 创建一个byte类型的buffer字节数组,用于存放读取的本地文件 | |
byte buffer[] = new byte[4 * 1024]; | |
int temp = 0; | |
// 循环读取文件 | |
while ((temp = inputStream.read(buffer)) != -1) { | |
// 把数据写入到OuputStream对象中 | |
outputStream.write(buffer, 0, temp); | |
} | |
// 发送读取的数据到服务端 | |
outputStream.flush(); | |
socket.close(); | |
} | |
/** | |
* 接收图片 | |
*/ | |
private void recieveImg() throws IOException { | |
// 发送完成,等待结果 | |
socket = new Socket(IP, PORT); | |
byte buffer[] = new byte[512]; | |
int temp = 0; | |
InputStream inputStream = socket.getInputStream(); | |
// 保存图片 | |
// 获取雾图图片文件名和目录 | |
File hazeFile = new File(this.path1); | |
String dirName = hazeFile.getParent(); | |
Log.i("dir name",dirName); | |
// 下行稍有错误 | |
String fileName = hazeFile.getName().substring(0,hazeFile.getName().indexOf('.')); | |
Log.i("file name",fileName); | |
File dehazeImg = new File(new File(dirName),fileName+"_dehaze.jpg"); | |
//创建图片字节流 | |
FileOutputStream fos = new FileOutputStream(dehazeImg); | |
byte[] buf = new byte[1024]; | |
int len = 0; | |
//往字节流里写图片数据 | |
while ((len = inputStream.read(buf)) != -1) | |
{ | |
fos.write(buf,0,len); | |
} | |
} | |
@Override | |
public void run() { | |
super.run(); | |
try { | |
sendImg(this.path1); | |
Log.i(MainActivity.class.getSimpleName(),"finish img 1"); | |
sendImg(this.path2); | |
Log.i(MainActivity.class.getSimpleName(),"finish img 2"); | |
recieveImg(); | |
callBackListener.onScoreFinished(""); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
Log.e(MainActivity.class.getSimpleName(),"连接失败"); | |
callBackListener.onScoreError("Socket 连接失败"); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
使用Socket通信,完成: | |
手机上传雾图,服务器分析后回传 | |
""" | |
from deblurgan.model import generator_model, discriminator_model, generator_containing_discriminator_multiple_outputs | |
import os | |
from PIL import Image, ImageFile | |
import numpy as np | |
import socket | |
import sys | |
haze_path = './img_from_socket/haze.jpg' | |
dehaze_path = './img_from_socket/dehaze.jpg' | |
g = None | |
def init_net_module(): | |
""" | |
初始话网络模块 | |
:return: 生成器 | |
""" | |
# 模型保存目录 | |
model_save_dir = './model_save_res_block_256_vgg16' | |
def load_saved_weight(g, d=None): | |
""" | |
加载已训练好的权重 | |
:param g: 生成器 | |
:param d: 判别器 | |
:return: | |
""" | |
# TODO: 这里需要做细化处理。判定文件是否存在。多个权重文件找到最新的权重文件 | |
g.load_weights(os.path.join(model_save_dir, 'generator_49_33.h5')) | |
if d is None: | |
return | |
d.load_weights(os.path.join(model_save_dir, 'discriminator_49.h5')) | |
# 构建网络模型 | |
global g | |
g = generator_model() | |
# 加载模型权重 | |
load_saved_weight(g) | |
def dehaze(): | |
""" | |
实现去雾 | |
:return: | |
""" | |
haze_img = np.array(Image.open(haze_path).convert('RGB')) | |
generated_img = g.predict(haze_img.reshape((1, 256, 256, 3)) / 127.5 - 1) | |
generated_img = (generated_img + 1) * 127.5 | |
dehazed_img = Image.fromarray(generated_img[0].astype('uint8')) | |
# 保存下来 | |
dehazed_img.save(dehaze_path) | |
def socket_service(): | |
""" | |
开启socket服务 | |
:return: | |
""" | |
# 开启socket服务 | |
try: | |
s = socket.socket() | |
s.bind(('192.168.42.200', 6666)) | |
s.listen(10) | |
except socket.error as msg: | |
print(msg) | |
sys.exit(1) | |
print("Wait") | |
def save_haze_file(sock): | |
""" | |
从sock中获取数据,并保存下来 | |
:param sock: | |
:return: | |
""" | |
with open(haze_path, 'wb') as f: | |
print('file opened') | |
while True: | |
data = sock.recv(1024) | |
# print() | |
if not data: | |
break | |
elif 'EOF' in str(data): | |
f.write(data[:-len('EFO')]) | |
break | |
# write data to a file | |
f.write(data) | |
# sock.close() | |
print('pic received finished') | |
def send_img(sock): | |
# 发送处理后的图片 | |
with open(dehaze_path, 'rb') as f: | |
for data in f: | |
sock.send(data) | |
print('send finished') | |
# 等待连接并处理 | |
while True: | |
sock, _ = s.accept() | |
try: | |
save_haze_file(sock) | |
dehaze() | |
send_img(sock) | |
except Exception as reason: | |
print(reason) | |
finally: | |
sock.close() | |
if __name__ == '__main__': | |
init_net_module() | |
socket_service() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment