tensorflow-yolov4-tflite icon indicating copy to clipboard operation
tensorflow-yolov4-tflite copied to clipboard

Android app rework using Kotlin and more modern approach

Open ThomasKiljanczykDev opened this issue 4 years ago • 26 comments

ThomasKiljanczykDev avatar May 20 '21 18:05 ThomasKiljanczykDev

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

paschalis-mpeis avatar Sep 14 '21 12:09 paschalis-mpeis

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

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

ThomasKiljanczykDev avatar Sep 14 '21 13:09 ThomasKiljanczykDev

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?

paschalis-mpeis avatar Sep 14 '21 13:09 paschalis-mpeis

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

ThomasKiljanczykDev avatar Sep 14 '21 13:09 ThomasKiljanczykDev

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)

ThomasKiljanczykDev avatar Sep 14 '21 13:09 ThomasKiljanczykDev

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:

  • Frame: 864x480
  • Crop: 416x416

Samsung S20:

  • 1280 x 720
  • Crop: 416, 416

paschalis-mpeis avatar Sep 14 '21 13:09 paschalis-mpeis

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

ThomasKiljanczykDev avatar Sep 14 '21 13:09 ThomasKiljanczykDev

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

S62: (the device that had issue

  • Frame 640 x 480

Samsung S20:

  • Frame: 1280 x 720

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

paschalis-mpeis avatar Sep 14 '21 13:09 paschalis-mpeis

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

ThomasKiljanczykDev avatar Sep 14 '21 13:09 ThomasKiljanczykDev

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.

paschalis-mpeis avatar Sep 14 '21 13:09 paschalis-mpeis

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.

ThomasKiljanczykDev avatar Sep 14 '21 14:09 ThomasKiljanczykDev

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

paschalis-mpeis avatar Sep 14 '21 14:09 paschalis-mpeis

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.

ThomasKiljanczykDev avatar Sep 14 '21 14:09 ThomasKiljanczykDev

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

paschalis-mpeis avatar Sep 14 '21 14:09 paschalis-mpeis

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

ThomasKiljanczykDev avatar Sep 14 '21 14:09 ThomasKiljanczykDev

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

paschalis-mpeis avatar Sep 14 '21 14:09 paschalis-mpeis

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.

ThomasKiljanczykDev avatar Sep 14 '21 15:09 ThomasKiljanczykDev

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

ThomasKiljanczykDev avatar Sep 19 '21 15:09 ThomasKiljanczykDev

@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.

ThomasKiljanczykDev avatar Sep 19 '21 16:09 ThomasKiljanczykDev

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.

paschalis-mpeis avatar Sep 20 '21 05:09 paschalis-mpeis

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).

ThomasKiljanczykDev avatar Sep 20 '21 06:09 ThomasKiljanczykDev

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).

paschalis-mpeis avatar Sep 20 '21 07:09 paschalis-mpeis

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.

ThomasKiljanczykDev avatar Sep 20 '21 09:09 ThomasKiljanczykDev

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

paschalis-mpeis avatar Nov 29 '21 12:11 paschalis-mpeis

Please contact me by mail ( [email protected] ). I will need more information.

ThomasKiljanczykDev avatar Nov 30 '21 08:11 ThomasKiljanczykDev

@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.

ThomasKiljanczykDev avatar Sep 24 '22 06:09 ThomasKiljanczykDev