[Python] resizing an image before overlay causes problems

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following code:

    def def showData(frame, extracted_boxes, scaleFactor):
    # frame = cv2.resize(frame, (0,0), fx=scaleFactor, fy=scaleFactor)
    overlay = frame.copy()
    for idx, detection in enumerate(extracted_boxes, 1):
    bbox, text, confidence = detection
    cv2.rectangle(overlay, (self.scoreBoxCoords['x']+int(bbox[0][0]), self.scoreBoxCoords['y']+int(bbox[0][1])),
    (self.scoreBoxCoords['x']+int(bbox[2][0]), self.scoreBoxCoords['y']+int(bbox[2][1])), (255, 0, 0), thickness=2)
    cv2.putText(overlay, str(text), (int(self.scoreBoxCoords['x']+int(bbox[0][0])), int(self.scoreBoxCoords['y']+int(bbox[0][1])-8)),
    cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 255), 1, cv2.LINE_AA)

    # Blend the overlay with the original frame
    alpha = 0.6 # Transparency factor
    frame = cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0)

    # Show the frame with text overlay
    cv2.imshow("Video Text Overlay", frame)


    Example call:

    e.showData(frame, [
    ([[138, 26], [294, 26], [294, 59], [138, 59]], 'Some long text', 0.9195668923551638),
    ([[329, 29], [349, 29], [349, 55], [329, 55]], '5', 0.9999988079074598),
    ([[387, 33], [443, 33], [443, 51], [387, 51]], 'Ist Rnd', 0.7930378510572831),
    ([[476, 28], [562, 28], [562, 58], [476, 58]], '00:48', 0.8575270134267072),
    ([[589, 31], [653, 31], [653, 51], [589, 51]], 'Rdol 64', 0.21039095574692898),
    ([[684, 28], [708, 28], [708, 56], [684, 56]], '8', 0.9997217848717952),
    ([[743, 28], [920, 28], [920, 54], [743, 54]], 'Some other text', 0.9998729670627251)], 0.75)


    It works fine with one caveat: the results are for a scaled image. However, after removing the comment in the first line to scale the image accordingly, only the image is shown while no overlay is visible. What is the reason for this? Thanks for your help!

    Continue reading...

Compartilhe esta Página