一、Google Cloud Vision API简介
Google Cloud Vision API 是 Google 提供的一项强大的视觉分析服务,它通过先进的机器学习模型,帮助开发者轻松地在应用程序中集成图像识别功能。该API支持多种功能,包括但不限于:
-
图像标记:自动为图像生成描述性标签,帮助快速理解图像内容。
-
面部检测:识别图像中的人脸,并提供面部特征的详细信息。
-
地标检测:识别图像中的地标,并提供详细的地理位置信息。
-
文本检测:从图像中提取文本内容,支持多种语言。
-
安全搜索检测:检测图像中是否包含成人内容、暴力内容等,帮助过滤不适当的内容。
二、Google Cloud Vision API的核心功能
1. 图像标记(Label Detection)
通过分析图像内容,自动为其生成描述性标签。这对于内容分类、搜索优化等场景非常有用。
2. 面部检测(Face Detection)
能够识别图像中的人脸,并提供面部特征的详细信息,如年龄、性别、表情等。这在社交媒体、安防等领域有广泛应用。
3. 地标检测(Landmark Detection)
识别图像中的地标建筑或自然景观,并提供详细的地理位置信息。这对于旅游应用、地理信息系统等非常有价值。
4. 文本检测(Text Detection)
从图像中提取文本内容,支持多种语言。这对于文档扫描、图像内容理解等场景非常有用。
5. 安全搜索检测(Safe Search Detection)
检测图像中是否包含成人内容、暴力内容等,帮助过滤不适当的内容。这对于内容审核、儿童保护等场景非常重要。
三、Google Cloud Vision API的使用案例
1. 社交媒体平台的自动图像标签和分类
通过图像标记功能,社交媒体平台可以自动为用户上传的图片生成标签,提高内容的可发现性和用户体验。
2. 新闻和广告行业的视觉内容分析
利用地标检测和文本检测功能,新闻和广告行业可以快速分析图像内容,提取关键信息,提高工作效率。
3. 文档扫描和电子阅读器的文本提取功能
通过文本检测功能,文档扫描和电子阅读器可以提取图像中的文本内容,方便用户阅读和编辑。
4. 安防领域的面部识别
利用面部检测功能,安防系统可以实时监控和识别人员,提高安全性和效率。
5. 旅游类应用的地标识别
通过地标检测功能,旅游应用可以为用户提供详细的地标信息,增强用户体验。
四、在Spring Boot中使用Google Cloud Vision API
1. 项目背景
在Spring Boot项目中,我们可以通过集成Google Cloud Vision API来实现对URL的安全检测。具体来说,我们可以通过以下步骤实现:
-
使用Selenium无头浏览器访问URL并获取网页截图。
-
将截图发送到Google Cloud Vision API进行安全搜索检测。
-
根据API返回的结果判断URL是否包含恶意内容。
2. 代码实现
package net.xdclass.service.impl;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.vision.v1.*;
import com.google.protobuf.ByteString;
import net.xdclass.service.MachineLearningService;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
@Service
@EnableAsync
public class MachineLearningServiceImpl implements MachineLearningService {
private static final String REDIS_CACHE_KEY_PREFIX = "url:malicious:";
@Autowired
private ExecutorService executorService;
@Autowired
private StringRedisTemplate redisTemplate;
@Value("${google.cloud.vision.credentials.path}")
private String googleCloudVisionCredentialsPath;
static {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
}
@Override
@Async
public CompletableFuture<Boolean> detectMac(String url) {
return CompletableFuture.supplyAsync(() -> {
String cachedResult = redisTemplate.opsForValue().get(REDIS_CACHE_KEY_PREFIX + url);
if (cachedResult != null) {
return Boolean.parseBoolean(cachedResult);
}
WebDriver driver = null;
try {
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--window-size=1920x1080");
driver = new ChromeDriver(options);
driver.get(url);
TakesScreenshot screenshot = (TakesScreenshot) driver;
byte[] screenshotBytes = screenshot.getScreenshotAs(OutputType.BYTES);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(screenshotBytes));
boolean isMalicious = detectMaliciousContent(image);
redisTemplate.opsForValue().set(REDIS_CACHE_KEY_PREFIX + url, String.valueOf(isMalicious));
return isMalicious;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (driver != null) {
driver.quit();
}
}
}, executorService);
}
private boolean detectMaliciousContent(BufferedImage image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(image, "png", baos);
} catch (IOException e) {
e.printStackTrace();
return false;
}
byte[] imageBytes = baos.toByteArray();
try (ImageAnnotatorClient visionClient = ImageAnnotatorClient.create(getCredentials())) {
ByteString imgBytes = ByteString.copyFrom(imageBytes);
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.SAFE_SEARCH_DETECTION).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
.addFeatures(feat)
.setImage(img)
.build();
BatchAnnotateImagesResponse response = visionClient.batchAnnotateImages(Arrays.asList(request));
AnnotateImageResponse annotateImageResponse = response.getResponsesList().get(0);
if (annotateImageResponse.hasError()) {
System.out.printf("Error: %s%n", annotateImageResponse.getError().getMessage());
return false;
}
SafeSearchAnnotation annotation = annotateImageResponse.getSafeSearchAnnotation();
if (annotation.getAdult() == Likelihood.LIKELY || annotation.getAdult() == Likelihood.VERY_LIKELY ||
annotation.getMedical() == Likelihood.LIKELY || annotation.getMedical() == Likelihood.VERY_LIKELY ||
annotation.getRacy() == Likelihood.LIKELY || annotation.getRacy() == Likelihood.VERY_LIKELY ||
annotation.getSpoof() == Likelihood.LIKELY || annotation.getSpoof() == Likelihood.VERY_LIKELY ||
annotation.getViolence() == Likelihood.LIKELY || annotation.getViolence() == Likelihood.VERY_LIKELY) {
return true;
}
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private ImageAnnotatorSettings getCredentials() throws IOException {
return ImageAnnotatorSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(GoogleCredentials.fromStream(new FileInputStream(googleCloudVisionCredentialsPath))))
.build();
}
}
3. 代码解析
-
异步处理:使用
@Async
注解和CompletableFuture
实现异步检测,避免阻塞主线程,提高系统性能。 -
缓存机制:通过Redis缓存检测结果,避免重复检测,减少资源消耗和API调用次数。
-
无头浏览器截图:使用Selenium的无头浏览器(ChromeDriver)访问URL并获取网页截图,为后续的图像检测提供数据。
-
图像检测:将截图转换为字节数组,并调用Google Cloud Vision API进行安全搜索检测。根据API返回的结果判断图像是否包含恶意内容。
Google Cloud Vision API 提供了强大的视觉分析功能,能够帮助开发者轻松地在应用程序中集成图像识别功能。在Spring Boot项目中,通过结合Selenium无头浏览器和Redis缓存,我们可以实现对URL的安全检测,保护用户免受恶意网站的侵害。