hunglc007 / tensorflow-yolov4-tflite

YOLOv4, YOLOv4-tiny, YOLOv3, YOLOv3-tiny Implemented in Tensorflow 2.0, Android. Convert YOLO v4 .weights tensorflow, tensorrt and tflite
https://github.com/hunglc007/tensorflow-yolov4-tflite
MIT License
2.23k stars 1.24k forks source link

Android app rework using Kotlin and more modern approach #352

Open Gunock opened 3 years ago

paschalis-mpeis commented 2 years ago

There must be something wrong with the configuration of the camera activity.

Gunock commented 2 years ago

There must be something wrong with the configuration of the camera activity.

Could you provide more details (logs, screenshots, etc.)?

paschalis-mpeis commented 2 years ago

Sure, here you are:

![Screenshot_20210914-154736](https://user-images.githubusercontent.com/1271164/133265926-7eeaa4c1-cc17-4b8b-adf1-a08cf2815c69.png | width=200)

Do you want anything more specific?

Gunock commented 2 years ago

As for now I'll try to replicate this on one of my devices.

Gunock commented 2 years ago

Ok, it's not happening on my devices. Could you provide some informations about the device you're using? And were you holding the device horizontally or vertically? (the app is locked in single position so I can't tell from the screenshot)

paschalis-mpeis commented 2 years ago

Hmm.. I tested this on another device (Samsung S20 I think). On that one it works fine!

But on my other device (CAT S62) it works just like the screenshot I've attached.

More stats: CAT S62:

Samsung S20:

Gunock commented 2 years ago

Ok, the CAT is not using 16:9 for camera images. Maybe I can force similar resolution on my phone.

paschalis-mpeis commented 2 years ago

On the original java code (not the Kotlin MVVM) I get the below:

S62: (the device that had issue

Samsung S20:

And it works well on both devices. So my guess is that the image gets distorted on the CAT S62.

Gunock commented 2 years ago

You could put a breakpoint in YoloV4Detector class and view the scaledBitmap variable in android studio. Colors will be shifted but otherwise the image should look normal. If not then I guess something is not right with the Image to Bitmap conversion.

I attach a screenshot where to put the breakpoint image

paschalis-mpeis commented 2 years ago

I'll try it now.

BTW was this a porting from hunglc007 or a fresh implementation based on it? Because that one is using a different frame size and works OK.

Gunock commented 2 years ago

It's mostly fresh implementation with parts from hunglc007. The old implementation was a pain in the ass to convert to Kotlin and it used archaic camera APIs.

paschalis-mpeis commented 2 years ago

Samsung S20: Looks fine

CAT S62 its just garbage: bitmap:

Screenshot 2021-09-14 at 17 05 36

scaled bitmap:

Screenshot 2021-09-14 at 17 03 45
Gunock commented 2 years ago

Ok, that got to be conversion problem. The androidx camera provides images in YUV format and there is a code which converts it to RGB. Something must be wrong with the YUV buffer (I had these problems previously). I'll take a look a it later. The problem is most probably in ImageToBitmapConverter class, imageToBitmap method.

paschalis-mpeis commented 2 years ago

Could it be that the frame size (864x480) is messing around with the YUV?

Gunock commented 2 years ago

Most probably yes. Usually it has something to do with the buffer size being some funny number.

paschalis-mpeis commented 2 years ago

Forcing the rationto AspectRatio.RATIO_4_3 in DetectorActivity seems to be working. Thank you so much for the help so far.

BTW the below distortion between the bitmap and the scaledBitmap is intended?

Screenshot 2021-09-14 at 17 37 18 Screenshot 2021-09-14 at 17 37 11
Gunock commented 2 years ago

The image is resized without maintaining aspect ratio. The same thing was done in the old implementation. I could replace it with padding instead but I don't know if it is an idea worth pursuing.

Gunock commented 2 years ago

I didn't factor the stride in image conversion, thats why for some resolution images are corrupted. I'm working on a fix

Gunock commented 2 years ago

@Paschalis Check my fix. There should be no problems for 16:9 on your CAT phone. Still 4:3 is usually native format for cameras so stick to that for later use. Please let me know if the fix works on your devices.

paschalis-mpeis commented 2 years ago

Thanks @Gunock for the update! It does work, I can confirm that:

Screenshot 2021-09-20 at 08 06 00 Screenshot 2021-09-20 at 08 06 06

BTW, as a follow-up to my previous comment, yolo accepts the resized image without aspect ratio? That is the bottom image? As it needs square images?

If so, and given that training happens on unstretched images, do you believe that a left/right white padding would improve detection? I'm asking as I'm thinking of implementing this feature if it improves inference.

Gunock commented 2 years ago

You can either train yolo on non square images or add padding. I guess padding would improve inference but that's just guessing.

EDIT: Maybe right/bottom padding would be better. Result position transposing should be easier and adding padding also should be easier (put image in position 0,0).

paschalis-mpeis commented 2 years ago

Thanks for the tip! But then, with right/bottom padding wouldn't the getDetections return Detections on offset positions?

Or is nms supposed to be "normalizing" Detections again? (because, the current approach could make the Detection rectangles wider).

Gunock commented 2 years ago

NMS stands for Non-maximum Suppression. It's a way of removing overlapping boxes.

YOLO operates on normalized coords (i.e. positions and dimensions are expressed in percent's). There's some code in getDetections that translates YOLO bounding box to rectangle (https://developer.android.com/reference/android/graphics/RectF).

Whatever you do with padding there will be needed some offset correction in bounding boxes.

paschalis-mpeis commented 2 years ago

Hi @Gunock ,

It's me again! Unfortunately with a custom model I get zero detections on your MVVM fork (commit @1ecc22c), whereas the original (non-mvvm) work is OK.

In particular, I tested 3 models:

  1. coco (has 80 classes): works on both versions
  2. custom model (has 29 classes): works on both versions
  3. custom model (has 99 classes): works only on the non-mvvm version

In contrast with the last time, the above issue persists on both devices (i.e., it does not work on both a CAT S62 and a Samsung S20)

Can you guide me or give me some hints (once again) on how to resolve this? Also, it seems I can't open an issue on your repository.

Cheers, Paschalis

Gunock commented 2 years ago

Please contact me by mail ( asz.czyk.dev@gmail.com ). I will need more information.

Gunock commented 1 year ago

@Paschalis I have migrated from YUV to ARGB in CameraX's ImageAnalysis (latest version added support for it). This should resolve any issues with image color format conversion. I have also added image scaling that retains aspect ratio. This resolved issues with zero detections when using your custom model.