在工地场景的禁烟区域,传统的人工巡查方式难以实现全天候监管,抽烟行为往往无法及时发现和处理。如何利用智能监控设备实时识别抽烟行为,成为安全管理的重要需求。
萤石开放平台提供了一套完整的AI智能检测方案,结合摄像头设备接入、实时抓拍和AI算法分析,能够快速搭建抽烟检测系统,实现自动化的违规行为识别和预警。
1
技术架构
整体方案分为四个核心模块:
2
详细开发步骤
第1步:获取AccessToken
首先需要获取萤石云API的访问令牌,这是调用所有接口的前提条件
接口地址
POST https://open.ys7.com/api/lapp/token/get
请求参数
appKey:应用Key(在萤石开放平台创建应用时获取)
appSecret:应用Secret
代码实现
publicclassEzvizTokenClient { privatestaticfinal String TOKEN_API_URL = "https://open.ys7.com/api/lapp/token/get";public TokenResponse getToken(String appKey, String appSecret) throws Exception { // 构建请求体 (form-urlencoded格式) String requestBody = "appKey=" + appKey + "&appSecret=" + appSecret;
HttpRequest httpRequest = HttpRequest.newBuilder() .uri(URI.create(TOKEN_API_URL)) .header("Content-Type", "application/x-www-form-urlencoded") .POST(HttpRequest.BodyPublishers.ofString(requestBody)) .build();
HttpResponse<String> response = httpClient.send(httpRequest, BodyHandlers.ofString()); return objectMapper.readValue(response.body(), TokenResponse.class); }}
响应示例
{ "code": "200", "msg": "success", "data": { "accessToken": "at.xxxxx", "expireTime": 7200000 }}第2步:添加设备到账号
将摄像头设备绑定到你的萤石账号,才能进行后续的抓拍操作
接口地址
POST https://open.ys7.com/api/lapp/device/add
请求参数
accessToken:访问令牌
deviceSerial:设备序列号(在设备机身标签上可找到)
validateCode:设备验证码(设备初始化时设置)
代码实现
publicclassEzvizDeviceClient { privatestaticfinal String DEVICE_ADD_API_URL = "https://open.ys7.com/api/lapp/device/add";public DeviceAddResponse addDevice(String accessToken, String deviceSerial, String validateCode) throws Exception { String requestBody = "accessToken=" + accessToken + "&deviceSerial=" + deviceSerial + "&validateCode=" + validateCode;
HttpRequest httpRequest = HttpRequest.newBuilder() .uri(URI.create(DEVICE_ADD_API_URL)) .header("Content-Type", "application/x-www-form-urlencoded") .POST(HttpRequest.BodyPublishers.ofString(requestBody)) .build();
HttpResponse<String> response = httpClient.send(httpRequest, BodyHandlers.ofString()); return objectMapper.readValue(response.body(), DeviceAddResponse.class); }}
第3步:设备实时抓拍
调用摄像头抓取当前画面,获取图片URL供后续AI分析使用。
接口地址
POST https://open.ys7.com/api/lapp/device/capture
请求参数
accessToken:访问令牌
deviceSerial:设备序列号
channelNo:通道号(默认为1)
代码实现
publicclassEzvizCaptureClient { privatestaticfinal String CAPTURE_API_URL = "https://open.ys7.com/api/lapp/device/capture";public CaptureResponse capture(String accessToken, String deviceSerial, int channelNo) throws Exception { String requestBody = "accessToken=" + accessToken + "&deviceSerial=" + deviceSerial + "&channelNo=" + channelNo;
HttpRequest httpRequest = HttpRequest.newBuilder() .uri(URI.create(CAPTURE_API_URL)) .header("Content-Type", "application/x-www-form-urlencoded") .POST(HttpRequest.BodyPublishers.ofString(requestBody)) .build();
HttpResponse<String> response = httpClient.send(httpRequest, BodyHandlers.ofString()); return objectMapper.readValue(response.body(), CaptureResponse.class); }}
响应示例
{ "code": "200", "msg": "success", "data": { "deviceSerial": "ABC123456", "channelNo": "1", "picUrl": "
第4步:调用AI抽烟检测接口
将抓拍的图片URL发送到AI检测接口,识别抽烟行为
接口地址
POST https://open.ys7.com/api/service/intelligence/algo/analysis/smoking_detection
请求参数
accessToken:访问令牌(放在Header中)
imageUrl:图片URL
imgWidth:图片宽度
imgHeight:图片高度
detectMode:检测模式(target_detect 目标检测 或 alert_detect 告警检测)
代码实现
publicclassSmokingDetectionClient { privatestaticfinal String API_URL = "https://open.ys7.com/api/service/intelligence/algo/analysis/smoking_detection";public DetectionResponse detect(String imageUrl, int imgWidth, int imgHeight, String detectMode) throws Exception { DetectionRequest request = new DetectionRequest(); request.setRequestId(UUID.randomUUID().toString()); request.setTaskType("smoking_detection"); request.setMark(true);
DataInfo dataInfo = new DataInfo(); dataInfo.setModal("image"); dataInfo.setType("url"); dataInfo.setData(imageUrl); request.setDataInfo(List.of(dataInfo));
DataParam dataParam = new DataParam(); dataParam.setModal("image"); dataParam.setImgWidth(imgWidth); dataParam.setImgHeight(imgHeight); dataParam.setDetectMode(detectMode); request.setDataParams(List.of(dataParam));
String jsonBody = objectMapper.writeValueAsString(request);
HttpRequest httpRequest = HttpRequest.newBuilder() .uri(URI.create(API_URL)) .header("Content-Type", "application/json") .header("accessToken", accessToken) .POST(HttpRequest.BodyPublishers.ofString(jsonBody)) .build();
HttpResponse<String> response = httpClient.send(httpRequest, BodyHandlers.ofString()); return objectMapper.readValue(response.body(), DetectionResponse.class); }}
响应示例
{ "meta": { "code": 200, "message": "success" }, "data": { "requestId": "uuid-xxxxx", "taskType": "smoking_detection", "images": [{ "imageWidth": 1920, "imageHeight": 1080, "contentAnn": { "bboxes": [{ "weight": 0.95, "tagInfo": { "tag": "person", "labels": [{ "key": "action", "label": "smoking", "labelWeight": 0.92 }] } }] } }] }}第5步:完整流程集成
将以上4步串联起来,形成完整的检测流程
完整代码示例
publicclassCompleteFlowDemo { publicstaticvoidmain(String[] args){ // 配置参数 String appKey = "your_appKey"; String appSecret = "your_appSecret"; String deviceSerial = "your_device_serial"; String validateCode = "your_validate_code";// 1. 获取Token EzvizTokenClient tokenClient = new EzvizTokenClient(); TokenResponse tokenResponse = tokenClient.getToken(appKey, appSecret); String accessToken = tokenResponse.getData().getAccessToken();
// 2. 添加设备 EzvizDeviceClient deviceClient = new EzvizDeviceClient(); deviceClient.addDevice(accessToken, deviceSerial, validateCode);
// 3. 设备抓拍 EzvizCaptureClient captureClient = new EzvizCaptureClient(); CaptureResponse captureResponse = captureClient.capture(accessToken, deviceSerial, 1); String picUrl = captureResponse.getData().getPicUrl();
// 4. 抽烟检测 SmokingDetectionClient detectionClient = new SmokingDetectionClient(accessToken); DetectionResponse detectionResponse = detectionClient.detect(picUrl, 1920, 1080, "target_detect");
// 5. 处理检测结果 if (detectionResponse.isSuccess()) { for (ImageResult image : detectionResponse.getData().getImages()) { for (Bbox bbox : image.getContentAnn().getBboxes()) { System.out.println("检测到: " + bbox.getTagInfo().getTag()); System.out.println("置信度: " + bbox.getWeight() * 100 + "%"); for (Label label : bbox.getTagInfo().getLabels()) { System.out.println("行为: " + label.getLabel() + " (" + label.getLabelWeight() * 100 + "%)"); } } } } }}
3
项目依赖配置
使用Maven管理项目依赖,仅需引入Jackson用于JSON处理
<dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.0</version> </dependency></dependencies>项目使用Java 11原生HttpClient,无需额外HTTP依赖库
4
运行项目
# 编译打包mvn clean package# 运行完整流程java -jar target/smoking-detection-1.0-SNAPSHOT.jar
5
运行项目扩展建议
1.定时巡检:结合定时任务,每隔一段时间自动抓拍检测
2.告警联动:检测到抽烟行为后,触发声光报警或发送通知
3.证据留存:保存检测图片和结果数据,用于后续追溯
4.多设备管理:批量接入多个摄像头,实现区域全覆盖监控
6
相关资源
萤石开放平台
https://open.ys7.com
Token接口文档
https://open.ys7.com/help/84
设备添加文档
https://open.ys7.com/help/85 -
设备抓拍文档
https://open.ys7.com/help/687
通过萤石开放平台的设备接入能力和AI检测算法,只需5步即可快速搭建一套抽烟检测系统。整个流程从设备接入到AI分析,代码简洁清晰,不仅适合工地场景也能匹配各类禁烟场所的智能化管理需求。
/ END /
企业微信小助手
扫码咨询更多问题
往期推荐
5步搞定|宠物AI识别与智能剪辑开发实践
开发实践 | 5 步搞定台球厅精彩瞬间智能识别和自动剪辑
5步搞定 | 烟火AI检测与智能告警开发实践
点击下方跳转萤石开放平台小程序
股票
板块
用户
资讯
评论 (0)