地质所 沉降监测网建设项目
chenhuan
2024-05-16 f992b4e508b358eba4170b1e9b1bb21319f7a3cd
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package com.javaweb.third.controller;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.javaweb.common.config.Global;
import com.javaweb.common.constant.Constants;
import com.javaweb.common.core.controller.BaseController;
import com.javaweb.common.core.domain.AjaxResult;
import com.javaweb.common.utils.ServletUtils;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.common.utils.file.FileUploadUtils;
import com.javaweb.framework.util.ShiroUtils;
import com.javaweb.system.domain.SysUser;
import com.javaweb.third.baidu.api.BaiduAi;
import com.javaweb.third.baidu.bean.face.FaceDetectResult;
import com.javaweb.third.baidu.bean.imgClassify.AnimalResult;
import com.javaweb.third.baidu.bean.imgClassify.CarResult;
import com.javaweb.third.baidu.bean.imgClassify.DishResult;
import com.javaweb.third.baidu.bean.imgClassify.PlantResult;
import com.javaweb.third.baidu.bean.ocr.*;
import com.javaweb.third.baidu.service.FaceService;
import com.javaweb.third.baidu.service.ImageClassifyService;
import com.javaweb.third.baidu.service.OcrService;
import com.javaweb.third.domain.AiHis;
import com.javaweb.third.service.IAiHisService;
import com.sun.jna.platform.win32.Guid;
import org.apache.commons.fileupload.util.Streams;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
 
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author wujiyue
 */
@Controller
@RequestMapping("/third/ai")
public class BaiduAiController extends BaseController {
 
    @Autowired
    IAiHisService aiHisService;
    private static String prefix="/third/ai/";
    public static String DEFAULT_UPLOAD_BASE_PATH=FileUploadUtils.getDefaultBaseDir()+"/temp";
 
    @RequestMapping(value={"","/"})
    @RequiresPermissions("third:ai")
    public String index(){
        return prefix+"/baiduAi";
    }
 
    //上传单张图片资源
    @ResponseBody
    @RequestMapping("/queryAi")
    public Object upload(@RequestParam("type") String aiType,@RequestParam("file") MultipartFile multipartFile) throws Exception{
        Map resultMap =new HashMap<String,Object>();
        if(StringUtils.isEmpty(aiType)){
            return AjaxResult.error("type参数不能为空!");
        }
        String path=FileUploadUtils.upload(DEFAULT_UPLOAD_BASE_PATH,multipartFile);
        resultMap.put("path",path);
        //去掉映射路径前缀
        path= path.replace(Constants.RESOURCE_PREFIX,"");
        String localFilePath=Global.getProfile()+path;
        return queryAi(aiType,localFilePath);
    }
    public Object queryAi(String aiType,String localFilePath){
 
        String json="";//返回最原始的json
        String aiName="";
        if(BaiduAi.AiType.faceDetect.name().equals(aiType)){
            FaceDetectResult result = FaceService.faceDetect_localPath(localFilePath);
            json=result.getJson();
            aiName="人脸检测";
        }else if(BaiduAi.AiType.plant.name().equals(aiType)){
            PlantResult result= ImageClassifyService.plant(localFilePath);
            json=result.getJson();
            aiName="植物识别";
        }else if(BaiduAi.AiType.bankCard.name().equals(aiType)){
            BankCardOcrResult result = OcrService.bankCardOcr(localFilePath);
            json=result.getJson();
            aiName="银行卡识别";
        }else if(BaiduAi.AiType.idCard.name().equals(aiType)){
            IdCardOcrResult result = OcrService.idcardOcr_Z(localFilePath);
            json=result.getJson();
            aiName="身份证识别";
        }else if(BaiduAi.AiType.plate.name().equals(aiType)){
            //识别车牌号
            PlateNumberResult result = OcrService.license_plateOcr(localFilePath);
            json=result.getJson();
            aiName="识别车牌号";
        }else if(BaiduAi.AiType.driver.name().equals(aiType)){
            //驾驶证识别
            aiName="驾驶证识别";
            DriveLicenseOcrResult result = OcrService.drivingLicenseOcr(localFilePath);
            json=result.getJson();
        }else if(BaiduAi.AiType.animal.name().equals(aiType)){
            //动物识别
            AnimalResult result = ImageClassifyService.animal(localFilePath);
            json=result.getJson();
        }else if(BaiduAi.AiType.car.name().equals(aiType)){
            //车型识别
            aiName="车型识别";
            CarResult result = ImageClassifyService.car(localFilePath);
            json=result.getJson();
        }else if(BaiduAi.AiType.dish.name().equals(aiType)){
            //菜品识别
            aiName="菜品识别";
            DishResult result = ImageClassifyService.dish(localFilePath);
            json=result.getJson();
        }else if(BaiduAi.AiType.general_basic.name().toString().equals(aiType)){
            //通用文字识别
            aiName="通用文字识别";
            GeneralBasicIOcrResult result= OcrService.general_basic(localFilePath);
            json=result.getJson();
        }
        AiHis aiHis=new AiHis();
        JSONObject jsonObject= JSONObject.parseObject(json);
        String e_code=String.valueOf(jsonObject.get("error_code"));
        String e_msg=String.valueOf(jsonObject.get("error_msg"));
        String log_id=String.valueOf(jsonObject.get("log_id"));
        aiHis.setId(log_id);
        if(StringUtils.isNotEmpty(e_code)&&!"0".equals(e_code)){
            aiHis.setResult("0");
        }else{
            aiHis.setResult("1");
        }
        if(StringUtils.isNotEmpty(e_msg)){
            aiHis.setErrorMsg(e_msg);
        }
        aiHis.setAiType(aiType);
        aiHis.setTypeName(aiName);
        aiHis.setJsonResult(json);
        SysUser user=ShiroUtils.getSysUser();
        aiHis.setYhid(String.valueOf(user.getUserId()));
        aiHis.setYhmc(user.getUserName());
        aiHisService.insertAiHis(aiHis);
        return AjaxResult.success("",json);
    }
    public Object queryAi2(String aiType,String localFilePath,Map resultMap){
        if(BaiduAi.AiType.faceDetect.name().equals(aiType)){
            FaceDetectResult result = FaceService.faceDetect_localPath(localFilePath);
            if(result.isRequestOk()){
                resultMap.put("result","成功");
                resultMap.put("age",result.getAge());
                resultMap.put("gender","female".equals(result.getGender())?"女":"男");
                resultMap.put("beauty",result.getBeauty());
                resultMap.put("expression",result.getExpression());
                resultMap.put("hasGlasses",result.isHasGlasses()?"有":"无");
            }else{
                resultMap.put("result","失败"+result.getError_msg());
            }
        }else if(BaiduAi.AiType.plant.name().equals(aiType)){
            PlantResult result= ImageClassifyService.plant(localFilePath);
            if(result.isRequestOk()){
                //请求成功
                if(result.isPlant()){
                    //确定是植物
                    resultMap.put("result","成功");
                    resultMap.put("name",result.getResult_name());
                    resultMap.put("probability",result.getResult_probability());
                }else{
                    //不能判定是否植物
                    resultMap.put("result","非植物");
                    resultMap.put("resultJson", JSON.toJSONString(result.getResult()));
                }
            }else{
                //请求失败
                resultMap.put("result","识别失败!"+result.getError_msg());
            }
        }else if(BaiduAi.AiType.bankCard.name().equals(aiType)){
            BankCardOcrResult result = OcrService.bankCardOcr(localFilePath);
            if(result.isRequestOk()){
                resultMap.put("result","成功");
                resultMap.put("cardNum",result.getBank_card_number());
                resultMap.put("bankName",result.getBank_name());
            }else{
                resultMap.put("result","识别失败!"+result.getError_msg());
            }
        }else if(BaiduAi.AiType.idCard.name().equals(aiType)){
            IdCardOcrResult result = OcrService.idcardOcr_Z(localFilePath);
            if(result.isRequestOk()){
                resultMap.put("result","成功");
                resultMap.put("gender",result.getGender());
                resultMap.put("address",result.getAddress());
                resultMap.put("birthday",result.getBirthday());
                resultMap.put("ID",result.getID());
                resultMap.put("name",result.getName());
                resultMap.put("minzhu",result.getMinZhu());
            }else{
                resultMap.put("result","识别失败!"+result.getError_msg());
            }
        }else if(BaiduAi.AiType.plate.name().equals(aiType)){
            //识别车牌号
            PlateNumberResult result = OcrService.license_plateOcr(localFilePath);
            if(result.isRequestOk()){
                resultMap.put("result","成功");
                resultMap.put("number",result.getPlateNumber());
                resultMap.put("color",result.getColor());
            }else{
                resultMap.put("result","识别失败!"+result.getError_msg());
            }
        }else if(BaiduAi.AiType.driver.name().equals(aiType)){
            //驾驶证识别
            DriveLicenseOcrResult result = OcrService.drivingLicenseOcr(localFilePath);
            if(result.isRequestOk()){
                resultMap.put("result","成功");
                resultMap.put("ID",result.getID());
                resultMap.put("name",result.getName());
                resultMap.put("gender",result.getGender());
                resultMap.put("address",result.getAddress());
                resultMap.put("birthday",result.getBirthday());
                resultMap.put("country",result.getCountry());
                resultMap.put("time",result.getStarttime()+"-"+result.getEndtime());
                resultMap.put("type",result.getType());//C1
            }else{
                resultMap.put("result","识别失败!"+result.getError_msg());
            }
        }else if(BaiduAi.AiType.animal.name().equals(aiType)){
            //动物识别
            AnimalResult result = ImageClassifyService.animal(localFilePath);
            if(result.isRequestOk()){
                if(result.isAnimal()){
                    resultMap.put("result","成功");
                    resultMap.put("name",result.getResult_name());
                    resultMap.put("probability",result.getResult_probability());
                }else{
                    resultMap.put("result","非动物");
                    resultMap.put("resultJson", JSON.toJSONString(result.getResult()));
                }
            }else{
                resultMap.put("result","识别失败!"+result.getError_msg());
            }
        }else if(BaiduAi.AiType.car.name().equals(aiType)){
            //车型识别
            CarResult result = ImageClassifyService.car(localFilePath);
            if(result.isRequestOk()){
                if(result.isCar()){
                    resultMap.put("result","成功");
                    resultMap.put("name",result.getResult_name());
                    resultMap.put("probability",result.getResult_probability());
                    resultMap.put("year",result.getResult_year());
                }else{
                    resultMap.put("result","非车辆");
                    resultMap.put("resultJson", JSON.toJSONString(result.getResult()));
                }
            }else{
                resultMap.put("result","识别失败!"+result.getError_msg());
            }
        }else if(BaiduAi.AiType.dish.name().equals(aiType)){
            //菜品识别
            DishResult result = ImageClassifyService.dish(localFilePath);
            if(result.isRequestOk()){
                if(result.isDish()){
                    resultMap.put("result","成功");
                    resultMap.put("name",result.getResult_name());
                    resultMap.put("probability",result.getResult_probability());
                    resultMap.put("calorie",result.getResult_calorie());
                }else{
                    resultMap.put("result","非菜");
                    resultMap.put("resultJson", JSON.toJSONString(result.getResult()));
                }
            }else{
                resultMap.put("result","识别失败!"+result.getError_msg());
            }
        }else if(BaiduAi.AiType.general_basic.name().toString().equals(aiType)){
            //通用文字识别
            GeneralBasicIOcrResult result= OcrService.general_basic(localFilePath);
            if(result.isRequestOk()){
                resultMap.put("result","成功");
                resultMap.put("words_result_num",result.getWords_result_num());
                resultMap.put("words",result.getWords());
                resultMap.put("paragraph",result.getParagraph());
            }else{
                resultMap.put("result","识别失败!"+result.getError_msg());
            }
        }
        return resultMap;
    }
}