2个二维码按钮分别是红色二维码识别和多个二维码识别
本项目是将yolov5训练好的模型转换为tflite模型进行部署
python export.py --weights yolov5s.pt --include tflite --img 640
这段代码采用了多线程的方式,使得模型的加载过程可以在后台进行,不影响主线程的UI交互。这样能够保持应用的流畅性和响应性。
//===============================模型初始化========================================
private void Preload(){
executor.execute(new Runnable() {
@Override
public void run() {
try {
detector_shape=DetectorFactory.getDetector(getAssets(),
"graphics/yolov5s-fp16.tflite");//图形模型
detector_shape.useCPU();
detector_light = DetectorFactory.getDetector(getAssets(), "light/yolov5s-fp16.tflite");//红绿灯模型
detector_light.useCPU();
detector_flag = DetectorFactory.getDetector(getAssets(), "Trafflc_Flag/yolov5s-fp16.tflite");//交通标识物模型
detector_flag.useCPU();
detector_tv=DetectorFactory.getDetector(getAssets(),"Tv/yolov5s-fp16.tflite");//红色二维码模型
detector_tv.useCPU();
detector_qr=DetectorFactory.getDetector(getAssets(),"qr/qr.tflite");//二维码模型
detector_qr.useCPU();
}catch (final IOException e){
e.printStackTrace();
}
}
});
}
detector_tv是要使用的模型 bitmap:要识别的图片
最终返回的results是识别后的类别名称,坐标,准确率
handleResult类用于在bitmap图片上画出识别到的框以便观察是否识别正确
//开始识别
List<Classifier.Recognition>results=detector_tv.recognizeImage(bitmap);
System.out.println(results);
// 2显示识别结果
handleResult(bitmap, results);
想要代码可私聊