Skip to content

Commit

Permalink
Merge pull request #88 from abdelaziz-mahdy/yolov11-test
Browse files Browse the repository at this point in the history
adding yolov11 example and model
  • Loading branch information
abdelaziz-mahdy authored Dec 1, 2024
2 parents 92d6f99 + 7eea703 commit 8ac54cd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Binary file added example/assets/models/yolo11n.torchscript
Binary file not shown.
58 changes: 57 additions & 1 deletion example/lib/run_model_by_image_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class _RunModelByImageDemoState extends State<RunModelByImageDemo> {
//CustomModel? _customModel;
late ModelObjectDetection _objectModel;
late ModelObjectDetection _objectModelYoloV8;

late ModelObjectDetection _objectModelYoloV11;
String? textToShow;
List? _prediction;
File? _image;
Expand All @@ -37,6 +37,7 @@ class _RunModelByImageDemoState extends State<RunModelByImageDemo> {
//String pathCustomModel = "assets/models/custom_model.ptl";
String pathObjectDetectionModel = "assets/models/yolov5s.torchscript";
String pathObjectDetectionModelYolov8 = "assets/models/yolov8s.torchscript";
String pathObjectDetectionModelYolov11 = "assets/models/yolo11n.torchscript";
try {
_imageModel = await PytorchLite.loadClassificationModel(
pathImageModel, 224, 224, 1000,
Expand All @@ -49,6 +50,14 @@ class _RunModelByImageDemoState extends State<RunModelByImageDemo> {
pathObjectDetectionModelYolov8, 80, 640, 640,
labelPath: "assets/labels/labels_objectDetection_Coco.txt",
objectDetectionModelType: ObjectDetectionModelType.yolov8);
_objectModelYoloV11 = await PytorchLite.loadObjectDetectionModel(
pathObjectDetectionModelYolov11,
80,
640,
640,
labelPath: "assets/labels/labels_objectDetection_Coco.txt",
objectDetectionModelType: ObjectDetectionModelType.yolov8
);
} catch (e) {
if (e is PlatformException) {
print("only supported for android, Error is $e");
Expand Down Expand Up @@ -157,6 +166,41 @@ class _RunModelByImageDemoState extends State<RunModelByImageDemo> {
});
}

Future runObjectDetectionYoloV11() async {
//pick a random image

final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
Stopwatch stopwatch = Stopwatch()..start();

objDetect = await _objectModelYoloV11.getImagePrediction(
await File(image!.path).readAsBytes(),
minimumScore: 0.1,
iOUThreshold: 0.3);
textToShow = inferenceTimeAsString(stopwatch);

print('object executed in ${stopwatch.elapsed.inMilliseconds} ms');
for (var element in objDetect) {
print({
"score": element?.score,
"className": element?.className,
"class": element?.classIndex,
"rect": {
"left": element?.rect.left,
"top": element?.rect.top,
"width": element?.rect.width,
"height": element?.rect.height,
"right": element?.rect.right,
"bottom": element?.rect.bottom,
},
});
}

setState(() {
//this.objDetect = objDetect;
_image = File(image.path);
});
}

String inferenceTimeAsString(Stopwatch stopwatch) =>
"Inference Took ${stopwatch.elapsed.inMilliseconds} ms";

Expand Down Expand Up @@ -295,6 +339,18 @@ class _RunModelByImageDemoState extends State<RunModelByImageDemo> {
),
),
),
TextButton(
onPressed: runObjectDetectionYoloV11,
style: TextButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Run object detection YoloV11 with labels",
style: TextStyle(
color: Colors.white,
),
),
),
TextButton(
onPressed: runObjectDetectionWithoutLabels,
style: TextButton.styleFrom(
Expand Down

0 comments on commit 8ac54cd

Please sign in to comment.