Skip to content

Commit

Permalink
update english comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mymagicpower committed Mar 21, 2023
1 parent 6e20414 commit 9c57080
Show file tree
Hide file tree
Showing 857 changed files with 4,940 additions and 54,165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* @author Calvin
*
* @email [email protected]
*/
public final class AnimalsClassExample {

private static final Logger logger = LoggerFactory.getLogger(AnimalsClassExample.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import java.io.InputStream;
import java.util.List;

/**
* @author Calvin
* @email [email protected]
*/
public class AnimalTranslator implements Translator<Image, Classifications> {
List<String> classes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import java.nio.file.Path;
import java.nio.file.Paths;

// https://www.paddlepaddle.org.cn/hubdetail?name=resnet50_vd_animals&en_category=ImageClassification
//
// Users/calvin/Desktop/Download/browser/PaddleHub-release-v2.1/modules/image/classification/resnet50_vd_animals

/**
* Animals Classification
* https://www.paddlepaddle.org.cn/hubdetail?name=resnet50_vd_animals&en_category=ImageClassification
* @author Calvin
* @email [email protected]
*/
public final class AnimalsClassification {

private static final Logger logger = LoggerFactory.getLogger(AnimalsClassification.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
import java.nio.file.Paths;

// https://www.paddlepaddle.org.cn/hubdetail?name=mobilenet_v2_animals&en_category=ImageClassification
//
// /Users/calvin/Desktop/Download/browser/PaddleHub-release-v2.1/modules/image/classification/mobilenet_v2_animals

/**
* @author Calvin
* @email [email protected]
*/
public final class LightAnimalsClassification {

private static final Logger logger = LoggerFactory.getLogger(LightAnimalsClassification.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* @author Calvin
*
* @email [email protected]
*/
public final class DishesClassificationExample {
private static final Logger logger = LoggerFactory.getLogger(DishesClassificationExample.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* @author Calvin
*
* @email [email protected]
*/
public final class LightDishesClassExample {

private static final Logger logger = LoggerFactory.getLogger(LightDishesClassExample.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import java.io.InputStream;
import java.util.List;

/**
* @author Calvin
*
* @email [email protected]
*/
public class DishTranslator implements Translator<Image, Classifications> {
List<String> classes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
import java.nio.file.Path;
import java.nio.file.Paths;

// https://www.paddlepaddle.org.cn/hubdetail?name=resnet50_vd_dishes&en_category=ImageClassification
/**
* @author Calvin
*
* @email [email protected]
*/
public final class DishesClassification {
// https://www.paddlepaddle.org.cn/hubdetail?name=resnet50_vd_dishes&en_category=ImageClassification

private static final Logger logger = LoggerFactory.getLogger(DishesClassification.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import java.nio.file.Paths;

// https://www.paddlepaddle.org.cn/hubdetail?name=mobilenet_v2_dishes&en_category=ImageClassification
//
// /Users/calvin/Desktop/Download/browser/PaddleHub-release-v2.1/modules/image/classification/mobilenet_v2_dishes

/**
* @author Calvin
*
* @email [email protected]
*/
public final class LightDishesClassification {

private static final Logger logger = LoggerFactory.getLogger(LightDishesClassification.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import java.nio.file.Paths;

/**
* 图片比对 - 1:1.
*
* Feature Comparison - 1:1.
* 特征比对 - 1:1.
* @author Calvin
* @date 2021-07-10
* @email [email protected]
Expand All @@ -43,11 +43,13 @@ public static void main(String[] args) throws IOException, ModelException, Trans
float[] feature1 = predictor.predict(img1);
float[] feature2 = predictor.predict(img2);

//欧式距离
// 欧式距离
// Euclidean distance
float dis = FeatureComparison.dis(feature1, feature2);
logger.info(Float.toString(dis));

//余弦相似度
// 余弦相似度
// Cosine similarity
float cos = FeatureComparison.cosineSim(feature1, feature2);
logger.info(Float.toString(cos));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

/**
* 图片特征提取 (512维特征)
*
* Image feature extraction (512-dimensional features)
* @author Calvin
* @date 2021-07-10
* @email [email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
/**
* 特征相似度计算
* https://zhuanlan.zhihu.com/p/88117781?utm_source=wechat_session
*/
* Feature Similarity Calculation
* @author Calvin
* @date 2021-07-10
* @email [email protected]
**/
public final class FeatureComparison {
private FeatureComparison() {}

/**
* 余弦相似度
* Cosine similarity
* @param feature1
* @param feature2
* @return
Expand All @@ -29,6 +34,7 @@ public static float cosineSim(float[] feature1, float[] feature2) {

/**
* 欧式距离
* Euclidean distance
* @param feature1
* @param feature2
* @return
Expand All @@ -44,6 +50,7 @@ public static float dis(float[] feature1, float[] feature2) {

/**
* 内积
* Inner product
* @param feature1
* @param feature2
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

/**
* 图片特征提取模型
* Image Feature Extraction Model
*
* @author Calvin
* @date 2021-12-19
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
*
* @author Calvin
* @date 2021-12-19
**/
public class ImageTranslator implements Translator<Image, float[]> {

private static final Logger logger = LoggerFactory.getLogger(ImageTranslator.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,27 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* First Order Example
*
* @author Calvin
*/
public final class FirstOrderExample {

private static final Logger logger = LoggerFactory.getLogger(FirstOrderExample.class);

private FirstOrderExample() {}

public static void main(String[] args) throws Exception {
// 源图
// 源图 - Source image
Path imageFile = Paths.get("src/test/resources/beauty.jpg");
// 驱动视频
// 驱动视频 - Driving video
Path videoPath = Paths.get("src/test/resources/driver.mp4");
// 生成视频
// 生成视频 - Generated video
Path outPath = Paths.get("build/output/result.mp4");

Image image = ImageFactory.getInstance().fromFile(imageFile);
// 获取视频关键帧
// 获取视频关键帧 - Get key frames from the video
List<Image> driverFrames = VideoUtils.getKeyFrame(videoPath.toString());
List<BufferedImage> imgList = new ArrayList();

Expand All @@ -49,10 +53,10 @@ public static void main(String[] args) throws Exception {
Map kpDrivingInitial = detector.predict(driverFrames.get(0));

int total = driverFrames.size();
// 进度条打印
// 进度条打印 - Progress bar printing
try (ProgressBar bar =
new ProgressBarBuilder()
.setTaskName("视频合成")
.setTaskName("视频合成 - Video Generation")
.setStyle(ProgressBarStyle.ASCII)
.setInitialMax(total)
.build(); ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* @author Calvin
*
* @email [email protected]
**/
public class FirstOrder {

public FirstOrder() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;

/**
* Image Utils
* @author Calvin
*
* @email [email protected]
**/
public class ImageUtils {

public static Image bufferedImage2DJLImage(BufferedImage img) {
return ImageFactory.getInstance().fromImage(img);
}

public static void saveImage(BufferedImage img, String name, String path) {
Image newImage = ImageFactory.getInstance().fromImage(img); // 支持多种图片格式,自动适配
// 支持多种图片格式,自动适配
// Supports multiple image formats and automatically adapts
Image newImage = ImageFactory.getInstance().fromImage(img);
Path outputDir = Paths.get(path);
Path imagePath = outputDir.resolve(name);
// OpenJDK 不能保存 jpg 图片的 alpha channel
// OpenJDK cannot save the alpha channel of jpg images
try {
newImage.save(Files.newOutputStream(imagePath), "png");
} catch (IOException e) {
Expand All @@ -38,6 +46,7 @@ public static void saveImage(Image img, String name, String path) {
Path outputDir = Paths.get(path);
Path imagePath = outputDir.resolve(name);
// OpenJDK 不能保存 jpg 图片的 alpha channel
// OpenJDK cannot save the alpha channel of jpg images
try {
img.save(Files.newOutputStream(imagePath), "png");
} catch (IOException e) {
Expand All @@ -58,10 +67,12 @@ public static void saveBoundingBoxImage(

public static void drawImageRect(BufferedImage image, int x, int y, int width, int height) {
// 将绘制图像转换为Graphics2D
// Convert the drawing image to Graphics2D
Graphics2D g = (Graphics2D) image.getGraphics();
try {
g.setColor(new Color(246, 96, 0));
// 声明画笔属性 :粗 细(单位像素)末端无修饰 折线处呈尖角
// Declare pen attributes: thick (in pixels), no decoration at the end, pointed at the corner of the broken line
BasicStroke bStroke = new BasicStroke(4, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
g.setStroke(bStroke);
g.drawRect(x, y, width, height);
Expand All @@ -74,10 +85,12 @@ public static void drawImageRect(BufferedImage image, int x, int y, int width, i
public static void drawImageRect(
BufferedImage image, int x, int y, int width, int height, Color c) {
// 将绘制图像转换为Graphics2D
// Convert the drawing image to Graphics2D
Graphics2D g = (Graphics2D) image.getGraphics();
try {
g.setColor(c);
// 声明画笔属性 :粗 细(单位像素)末端无修饰 折线处呈尖角
// Declare pen attributes: thick (in pixels), no decoration at the end, pointed at the corner of the broken line
BasicStroke bStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
g.setStroke(bStroke);
g.drawRect(x, y, width, height);
Expand All @@ -101,20 +114,29 @@ public static void drawImageText(BufferedImage image, String text) {
}
}

/** 返回外扩人脸 factor = 1, 100%, factor = 0.2, 20% */
/**
* 返回外扩人脸 factor = 1, 100%, factor = 0.2, 20%
* Return the expanded face with factor = 1, 100%, factor = 0.2, 20%
* @param img
* @param box
* @param factor
* @return
*/
public static Image getSubImage(Image img, BoundingBox box, float factor) {
Rectangle rect = box.getBounds();
// 左上角坐标
// Upper left corner coordinates
int x1 = (int) (rect.getX() * img.getWidth());
int y1 = (int) (rect.getY() * img.getHeight());
// 宽度,高度
// width, height
int w = (int) (rect.getWidth() * img.getWidth());
int h = (int) (rect.getHeight() * img.getHeight());
// 左上角坐标

int x2 = x1 + w;
int y2 = y1 + h;

// 外扩大100%,防止对齐后人脸出现黑边
// Expand by 100% to prevent black edges from appearing on the aligned face
int new_x1 = Math.max((int) (x1 + x1 * factor / 2 - x2 * factor / 2), 0);
int new_x2 = Math.min((int) (x2 + x2 * factor / 2 - x1 * factor / 2), img.getWidth() - 1);
int new_y1 = Math.max((int) (y1 + y1 * factor / 2 - y2 * factor / 2), 0);
Expand Down Expand Up @@ -154,14 +176,14 @@ public static void drawBoundingBoxImage(Image img, DetectedObjects detection) {

public static int getX(Image img, BoundingBox box) {
Rectangle rect = box.getBounds();
// 左上角x坐标
// Upper left corner x coordinate
int x = (int) (rect.getX() * img.getWidth());
return x;
}

public static int getY(Image img, BoundingBox box) {
Rectangle rect = box.getBounds();
// 左上角y坐标
// Upper left corner y coordinate
int y = (int) (rect.getY() * img.getHeight());
return y;
}
Expand Down
Loading

0 comments on commit 9c57080

Please sign in to comment.