Comments (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.
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.
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.
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:
I'm still digging in this issue, will update if I found the solution.
from yolo_v3_tutorial_from_scratch.
Related Issues (20)
- In function prep_image
- What is loading batch?
- yolov3-tiny model image dimensions error
- how to run detect.py
- google colab
- testing object detector
- bounding boxes not correct HOT 1
- How to solve this runtime error problem? HOT 1
- darknet spp maxpool
- 'NoneType' object has no attribute 'shape' HOT 1
- cv2.imwrite doesn't output image HOT 1
- the problem of function "predict_transform" HOT 1
- Why does batch norm layer has the parameter of weight and bias? HOT 2
- Why do we reverse the final dim of the image in "prep_image"? HOT 1
- Quick question
- RuntimeError: Expected object of device type cuda but got device type cpu for argument #1 'self' in call to _thnn_conv2d_forward
- Anchors scaling for each feature map output HOT 1
- Object detection algorithms
- why no images saved in the directory named 'det' HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yolo_v3_tutorial_from_scratch.