WongKinYiu / yolov7

Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
GNU General Public License v3.0
13.18k stars 4.17k forks source link

Printed text goes beyond image #1548

Open ghost opened 1 year ago

ghost commented 1 year ago

Hi, I made yolo v7 model for detecting one class only. Also, i added ocr so when it detects the object, it crops detection from bounding boxes and it send that to ocr for text detection. But, when plotting the results, detected text is too large and it goes beyond image. I tried edditing the def plot_one_box function, but with no success.

Here is how my function looks now:

def plot_one_box(x, img, color=None, label=None, text=None, line_thickness=3):

Plots one bounding box on image img

tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1  # line/font thickness
color = color or [random.randint(0, 255) for _ in range(3)]
c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
if label:
    tf = max(tl - 1, 1)  # font thickness
    t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
    c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
    cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA)  # filled
    cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
if text:
    t_size = cv2.getTextSize(text, 0, fontScale=tl / 3, thickness=1)[0]
    c3 = c1[0], c1[1] - t_size[1] - 3
    cv2.rectangle(img, c1, c3, color, -1, cv2.LINE_AA)  # filled
    cv2.putText(img, text, (c1[0], c1[1] + t_size[1] + 2), 0, tl / 3, [255, 255, 255], thickness=1, lineType=cv2.LINE_AA)

Anyway, I want to print detected label above bounding box as it is now, and then the detected text, but when it reaches the width of the bounding box width, it goes into a new line with new filled rectangle. Can somebody help me.

Andy-Jovi commented 1 year ago

color = color or [random.randint(0, 255) for _ in range(3)] c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)

How can I change this setting?

ghost commented 1 year ago

i dont understand your question. please, be more specific.