Coder Social home page Coder Social logo

Comments (4)

Dawn11041107 avatar Dawn11041107 commented on May 28, 2024 4

You can change c1,c2 from tensor to int type,such as

c1 = (int(c1[0].item()), int(c1[1].item()))
c2 = (int(c2[0].item()), int(c2[1].item()))

Therefore, the whole function is

def write(x, results):
    c1 = tuple(x[1:3].int())
    c2 = tuple(x[3:5].int())

    # 将c1和c2的值转换为整数类型
    c1 = (int(c1[0].item()), int(c1[1].item()))
    c2 = (int(c2[0].item()), int(c2[1].item()))

    img = results[int(x[0])]
    cls = int(x[-1])
    color = random.choice(colors)
    label = "{0}".format(classes[cls])
    cv2.rectangle(img, c1, c2, color, 2)
    t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 1 , 1)[0]
    c2 = c1[0] + t_size[0] + 3, c1[1] + t_size[1] + 4
    cv2.rectangle(img, c1, c2,color, -1)
    cv2.putText(img, label, (c1[0], c1[1] + t_size[1] + 4), cv2.FONT_HERSHEY_PLAIN, 1, [225,255,255], 1);
    return img

from yolo_v3_tutorial_from_scratch.

WenqingZong avatar WenqingZong commented on May 28, 2024 3

Update:
The 5th parameter of cv2.rectangle is thickness, according to OpenCV documentation, negative values, like FILLED, mean that the function has to draw a filled rectangle. The bug is caused by the second cv2.rectangle call, which intends to draw a small filled rectangle only for the label text. The right bottom coordinate of the filled rectangle is not updated.

Here is the modified (and should be bug-free) code:

def write(x, results):
    c1 = tuple(x[1:3].int())
    c2 = tuple(x[3:5].int())
    d1 = (int(c1[0]), int(c1[1]))
    d2 = (int(c2[0]), int(c2[1]))
    img = results[int(x[0])]
    cls = int(x[-1])
    color = random.choice(colors)
    label = "{0}".format(classes[cls])
    cv2.rectangle(img, d1, d2, color, 1)
    t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 1, 1)[0]
    c2 = c1[0] + t_size[0] + 3, c1[1] + t_size[1] + 4
    d2 = (int(c2[0]), int(c2[1]))
    cv2.rectangle(img, d1, d2, color, -1)
    cv2.putText(img, label, (int(c1[0]), int(c1[1] + t_size[1] + 4)), cv2.FONT_HERSHEY_PLAIN, 1, [225, 255, 255], 1)
    return img

from yolo_v3_tutorial_from_scratch.

EZ4BRUCE avatar EZ4BRUCE commented on May 28, 2024 1

this is because cv2.rectangle(). only take int ,u can copy my write(x, results) below

def write(x, results):
    c1 = tuple(x[1:3].int())
    c2 = tuple(x[3:5].int())
    d1=(int(c1[0]), int(c1[1]))
    d2=(int(c2[0]), int(c2[1]))
    img = results[int(x[0])]
    cls = int(x[-1])
    color = random.choice(colors)
    label = "{0}".format(classes[cls])
    cv2.rectangle(img, d1, d2, color, 1)
    t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 1, 1)[0]
    c2 = c1[0] + t_size[0] + 3, c1[1] + t_size[1] + 4
    cv2.rectangle(img, d1, d2, color, -1)
    cv2.putText(img, label, (int(c1[0]), int(c1[1] + t_size[1] + 4)),
                cv2.FONT_HERSHEY_PLAIN, 1, [225, 255, 255], 1)
    return img

from yolo_v3_tutorial_from_scratch.

WenqingZong avatar WenqingZong commented on May 28, 2024

Got the same problem and applied @EZ4BRUCE suggestion. The program runs smoothly, but the output image is a bit strange, the detected bounding boxes are no longer boxes, they become some pure colour rectangles.

Here is the det image I have now:
det_dog-cycle-car

I'm still digging in this issue, will update if I found the solution.

from yolo_v3_tutorial_from_scratch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.