TrafficProcess.java
8.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.viontech.process;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.viontech.model.BaseModel;
import com.viontech.model.TrafficModel;
import com.viontech.utils.DateUtil;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.Base64;
import java.util.Optional;
public class TrafficProcess implements Process {
@Override
public BaseModel process(JSONObject jsonObject) throws Exception {
TrafficModel model = new TrafficModel();
JSONObject eventData = jsonObject.getJSONObject("event_data");
if (eventData != null) {
JSONObject vehicle = eventData.getJSONObject("vehicle");
JSONObject lane = eventData.getJSONObject("lane");
JSONObject location = eventData.getJSONObject("location");
JSONObject illegal = eventData.getJSONObject("illegal");
JSONArray pics = eventData.getJSONArray("pics");
// ------------------- 车速 ------------------
Float speedF = eventData.getFloat("speed");
model.setSpeed(Optional.ofNullable(speedF).orElse(0F).intValue());
// ------------------------ 车道 --------------
if (lane != null) {
Integer laneNo = lane.getInteger("number");
model.setLane(laneNo);
}
// ------------------------ 车辆信息 车牌信息 --------------
String eventTimeStr = jsonObject.getString("event_dt");
long eventTime = DateUtil.parse(DateUtil.FORMAT_FULL, eventTimeStr).getTime();
model.setDiscernTime(BaseModel.toInt(eventTime / 1000));
model.setDiscernMillisecondTime(BaseModel.toInt(eventTime % 1000));
vehicle(model, vehicle);
// ------------------------ 图片 --------------
pic(model, pics);
// ---------------- 车速,方向,地点 --------------
location(model, location);
// ------------------ 违章 --------------------
illegal(model, illegal);
return model;
} else {
return null;
}
}
private void pic(TrafficModel model, JSONArray pics) {
if (pics != null && pics.size() > 0) {
// --------------- 图片2 ----------
if (pics.size() > 1) {
JSONObject pic2 = pics.getJSONObject(1);
Integer width = pic2.getInteger("width");
Integer height = pic2.getInteger("hight");
model.setSecondPictureHeight(height);
model.setSecondPictureWidth(width);
model.setSecondPictureTime(model.getDiscernTime());
model.setSecondPictureMillisecondTime(model.getDiscernMillisecondTime());
String picBase64 = pic2.getString("pic_base64");
byte[] pic2Bytes = Base64.getDecoder().decode(picBase64);
model.setPicture2(pic2Bytes);
model.setSecondPictureSize(pic2Bytes.length);
}
// ------------------ 图片1 -------------
JSONObject pic1 = pics.getJSONObject(0);
Integer width = pic1.getInteger("width");
Integer height = pic1.getInteger("hight");
model.setFirstPictureWidth(width);
model.setFirstPictureHeight(height);
String picBase64 = pic1.getString("pic_base64");
byte[] pic1Bytes = Base64.getDecoder().decode(picBase64);
model.setPicture1(pic1Bytes);
model.setFirstPictureSize(pic1Bytes.length);
byte[] pic1UrlBytes = pic1.getString("src_url").getBytes();
System.arraycopy(pic1UrlBytes, 0, model.getPicturePath(), 0, Math.min(128, pic1UrlBytes.length));
}
}
private void vehicle(TrafficModel model, JSONObject vehicle) {
if (vehicle != null) {
JSONObject plate = vehicle.getJSONObject("plate");
// ----------------- 车牌 ------------------
if (plate != null) {
byte[] text = plate.getString("text").getBytes(StandardCharsets.UTF_8);
System.arraycopy(text, 0, model.getPlateText(), 0, Math.min(text.length, 16));
// todo 车牌类型待实现
model.setPlateType(1);
JSONObject rect = plate.getJSONObject("rect");
if (rect != null) {
Float top = rect.getFloat("top");
Float left = rect.getFloat("left");
Float bottom = rect.getFloat("bottom");
Float right = rect.getFloat("right");
model.setPatternTop((int) (top * 10)).setPatternLeft((int) (left * 10))
.setPatternBottom((int) (bottom * 10)).setPatternRight((int) (right * 10));
}
Integer plateScore = plate.getInteger("score");
model.setConfidence(plateScore);
// todo 车牌单双行没有
model.setPlateStructure(1);
}
JSONObject body = vehicle.getJSONObject("body");
// -------------------- 车身 --------------------
if (body != null) {
JSONObject type = body.getJSONObject("type");
if (type != null) {
// todo 大车小车 (vehicleType) 待实现
model.setVehicleType(4);
// todo 车型细分
String categoryCode = type.getString("code");
model.setVehicleCategory(1);
}
JSONObject color = body.getJSONObject("color");
if (color != null) {
// todo 车身颜色待转换
String colorCode = color.getString("code");
model.setColor(0);
}
JSONObject logo = body.getJSONObject("logo");
if (logo != null) {
String code = logo.getString("code");
// todo 车辆品牌待转换
model.setManufacturerLogo(Integer.parseInt(code));
}
}
}
}
private void location(TrafficModel model, JSONObject location) {
if (location != null) {
JSONObject driveDirection = location.getJSONObject("drive_direction");
if (driveDirection != null) {
Integer directionCode = driveDirection.getInteger("code");
// todo 方向待转换
model.setDirection(directionCode);
}
String locationName = location.getString("name");
byte[] locationNameBytes = locationName.getBytes(StandardCharsets.UTF_8);
System.arraycopy(locationNameBytes, 0, model.getRouteLocation(), 0, Math.min(locationNameBytes.length, 32));
// todo 录像路径
byte[] videoPathBytes = "testVideoPath".getBytes(StandardCharsets.UTF_8);
System.arraycopy(videoPathBytes, 0, model.getVideoPath(), 0, videoPathBytes.length);
}
}
private void illegal(TrafficModel model, JSONObject illegal) throws ParseException {
if (illegal != null && illegal.getInteger("state") == 1) {
String illegalCode = illegal.getString("code");
// todo 需要交通违法类型转换 illegalCode --> illegalType
model.setIllegalType(0);
JSONObject detail = illegal.getJSONObject("detail");
if (detail != null) {
Float limitSpeed = detail.getFloat("limit_speed");
Float sparkSpeed = detail.getFloat("spark_speed");
String redStartStr = detail.getString("start_dt");
long redStart = 0;
long redEnd = 0;
if (redStartStr != null && !"".equals(redStartStr.trim())) {
redStart = DateUtil.parse(DateUtil.FORMAT_FULL, redStartStr).getTime();
}
String redEndStr = detail.getString("end_dt");
if (redEndStr != null && !"".equals(redEndStr.trim())) {
redEnd = DateUtil.parse(DateUtil.FORMAT_FULL, redEndStr).getTime();
}
model.setRedLightBeginTime((int) (redStart / 1000));
model.setRedLightBeginMillisecondTime((int) (redStart % 1000));
model.setRedLightEndTime((int) (redEnd / 1000));
model.setRedLightEndMillisecondTime((int) (redEnd % 1000));
model.setSpeedLimit(Optional.ofNullable(limitSpeed).orElse(0F).intValue());
model.setOverSpeedThreshold(Optional.ofNullable(sparkSpeed).orElse(0F).intValue());
}
}
}
}