[Bounding box] Print label in center of generated image.#9
[Bounding box] Print label in center of generated image.#9aelkhour wants to merge 1 commit intoarterys:masterfrom
Conversation
|
@aelkhour When I tried it out, I get the following error:
|
|
I think you just need to include a font file in the |
Sorry for the late reply @bsblaw , I somehow missed your comments. |
|
@bsblaw If you have time and think this PR could be useful, could you retest the updated version please? |
|
|
||
| draw.line(points, fill='red', width=5) | ||
| label = box['label'] | ||
| font = ImageFont.truetype('OpenSans-Regular.ttf', br[1] // 20) |
There was a problem hiding this comment.
In this case br[1] // 20 is the font size right? Now, br[1] is the y coordinate of the bottom right point. That means that boxes that are further down on the image will have a bigger text than boxes at the top. This might work fine for classification but let's make it work for bounding boxes as well.
Maybe dcm.Rows // 20?
There was a problem hiding this comment.
I agree that this is not very robust.
I made a slightly different proposal relying on the bounding box size instead of the image size, the rationale being that if in the future we want to support multiple bounding boxes in the same image, the labels should be positioned accordingly.
There was a problem hiding this comment.
We actually support multiple bounding boxes in the same image
There was a problem hiding this comment.
The problem I see with using the text size relative to bbox size is that for small boxes with, say 20px height, the text would be 1px height and unreadable. That is why a fixed font size would be better.
Other implementations I have seen show the text above the bbox, instead of inside. In that case there would be no issue if the text is bigger than the box
There was a problem hiding this comment.
I agree with you that this PR handles poorly all edge cases, as my intent was to focus on the most common use case (IMHO) of creating a single bbox which has the same size as the image. In that specific case, placing the label above the bbox sadly leads to a text lying outside of the image.
The worst case scenario for this PR would be having a text label being too small to read, somewhat equivalent to running the inference without this PR. What do you think of keeping it this way as I don't see any negative effect?
There was a problem hiding this comment.
I see your point regarding placing the label on top of the bounding box. You are right there. However, we are using bboxes for classification as a workaround currently. Ideally we should agree on a format for classification which does not need a bbox. @bsblaw What do you think?
| label = box['label'] | ||
| font = ImageFont.truetype('OpenSans-Regular.ttf', br[1] // 20) | ||
| text_width, text_height = draw.textsize(label, font) | ||
| text_position = ((br[0] - text_width) / 2, (br[1] - text_height) / 2) |
There was a problem hiding this comment.
Same happens to this calculation. It does not work for bounding boxes that do not span the whole image.
1d06a86 to
b67d535
Compare
b67d535 to
f214752
Compare
This is quite handy for binary classifiers.