一:复制下面java,代码,到一个类里
package com.yueqiu.images;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class A {
public static void main(String[] args) throws Exception {
//返回字符串
System.out.println("开始了....");
String fileUrlName="/home/pi/dev/dev01/images/horse.jpg"; //图片路径",
String accessToken ="自己应用apikey&sercetkey生成的AccessToken";
//生成accessToken的办法 http://ai.baidu.com/forum/topic/show/496505
while(true) {
String result = getScenerAndObjectResult(fileUrlName,accessToken);
System.out.println(result);
new Thread().sleep(2000);
}
}
public static String getScenerAndObjectResult(String imagePath,String accessToken) throws Exception{
byte[] imgData = readFileByBytes(imagePath);
String imgStr = encode(imgData);
String param = "image=" + URLEncoder.encode(imgStr,"UTF-8");
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
String result = post("https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general", accessToken, param);
return result;
}
public static String post(String requestUrl, String accessToken, String params) throws Exception {
System.out.println(params);
String generalUrl = "";
generalUrl = requestUrl + "?access_token=" + accessToken;
System.out.println("发送的连接为:"+generalUrl);
URL url = new URL(generalUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
System.out.println("打开链接,开始发送请求"+new Date().getTime()/1000);
connection.setRequestMethod("POST");
// 设置通用的请求属性
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 得到请求的输出流对象
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(params);
out.flush();
out.close();
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> headers = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : headers.keySet()) {
System.out.println(key + "--->" + headers.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = null;
if (requestUrl.contains("nlp"))
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
else
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String result = "";
String getLine;
while ((getLine = in.readLine()) != null) {
result += getLine;
}
in.close();
System.out.println("请求结束"+new Date().getTime()/1000);
System.out.println("result:" + result);
return result;
}
private static final char last2byte = (char) Integer.parseInt("00000011", 2);
private static final char last4byte = (char) Integer.parseInt("00001111", 2);
private static final char last6byte = (char) Integer.parseInt("00111111", 2);
private static final char lead6byte = (char) Integer.parseInt("11111100", 2);
private static final char lead4byte = (char) Integer.parseInt("11110000", 2);
private static final char lead2byte = (char) Integer.parseInt("11000000", 2);
private static final char[] encodeTable = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
public static String encode(byte[] from) {
StringBuilder to = new StringBuilder((int) ((double) from.length * 1.34D) + 3);
int num = 0;
char currentByte = 0;
int i;
for (i = 0; i < from.length; ++i) {
for (num %= 8; num < 8; num += 6) {
switch (num) {
case 0:
currentByte = (char) (from[i] & lead6byte);
currentByte = (char) (currentByte >>> 2);
case 1:
case 3:
case 5:
default:
break;
case 2:
currentByte = (char) (from[i] & last6byte);
break;
case 4:
currentByte = (char) (from[i] & last4byte);
currentByte = (char) (currentByte << 2);
if (i + 1 < from.length) {
currentByte = (char) (currentByte | (from[i + 1] & lead2byte) >>> 6);
}
break;
case 6:
currentByte = (char) (from[i] & last2byte);
currentByte = (char) (currentByte << 4);
if (i + 1 < from.length) {
currentByte = (char) (currentByte | (from[i + 1] & lead4byte) >>> 4);
}
}
to.append(encodeTable[currentByte]);
}
}
if (to.length() % 4 != 0) {
for (i = 4 - to.length() % 4; i > 0; --i) {
to.append("=");
}
}
return to.toString();
}
/**
* 根据文件路径读取byte[] 数组
*/
public static byte[] readFileByBytes(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
short bufSize = 1024;
byte[] buffer = new byte[bufSize];
int len1;
while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
bos.write(buffer, 0, len1);
}
byte[] var7 = bos.toByteArray();
return var7;
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException var14) {
var14.printStackTrace();
}
bos.close();
}
}
}
}
二: 把路径 换了
这时在eclipse中直接运行就会发现已经可以识别图的内容了
原图
识别 结果:
三:把文件上传到服务 器之中,这个应该都会上传
上传以后,进行测试
执行命令 : java A
原图:
识别结果:
四:创建dockerfile
执行命令: vi Dockerfile
写以下内容:
五:通过dockerfile文件来创建镜像
执行命令: docker build -t 镜像名 . (注意后有个点)
六: 创建容器
因为我们的图片是服务器上的,也就是主机上的,所以容器是无法访问到图片,
这时要在创建容器时,要挂载宿主机的图片地址到镜像中
docker run --name photo_one -it -v /home/pi/dev/dev01/images:/home/pi/dev/dev01/images photo_huazy
可以看容器已经正常运行了
这时如果 执行ctrl + c 直接退出容器了,,如果 在创建容器时,在镜像前面 加上 - d 就不会退出了
六:查看
1:docker ps -a 就可以看到已经刚才的容器了
2:启动: docker start 容器名
3:查看状态 :docker ps
4:查看容器实时 日志 docker logs 容器名 -f -t