本文整理汇总了Python中cv2.getTextSize方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.getTextSize方法的具体用法?Python cv2.getTextSize怎么用?Python cv2.getTextSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.getTextSize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: drawBoundingBox
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def drawBoundingBox(self,imgcv,result):
for box in result:
# print(box)
x1,y1,x2,y2 = (box['topleft']['x'],box['topleft']['y'],box['bottomright']['x'],box['bottomright']['y'])
conf = box['confidence']
# print(conf)
label = box['label']
if conf < self.predictThresh:
continue
# print(x1,y1,x2,y2,conf,label)
cv2.rectangle(imgcv,(x1,y1),(x2,y2),(0,255,0),6)
labelSize=cv2.getTextSize(label,cv2.FONT_HERSHEY_COMPLEX,0.5,2)
# print('labelSize>>',labelSize)
_x1 = x1
_y1 = y1#+int(labelSize[0][1]/2)
_x2 = _x1+labelSize[0][0]
_y2 = y1-int(labelSize[0][1])
cv2.rectangle(imgcv,(_x1,_y1),(_x2,_y2),(0,255,0),cv2.FILLED)
cv2.putText(imgcv,label,(x1,y1),cv2.FONT_HERSHEY_COMPLEX,0.5,(0,0,0),1)
return imgcv
示例2: draw_boxes_frame
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def draw_boxes_frame(frame, frame_size, boxes_dicts, class_names, input_size):
"""Draws detected boxes in a video frame"""
boxes_dict = boxes_dicts[0]
resize_factor = (frame_size[0] / input_size[1], frame_size[1] / input_size[0])
for cls in range(len(class_names)):
boxes = boxes_dict[cls]
color = (0, 0, 255)
if np.size(boxes) != 0:
for box in boxes:
xy = box[:4]
xy = [int(xy[i] * resize_factor[i % 2]) for i in range(4)]
cv2.rectangle(frame, (xy[0], xy[1]), (xy[2], xy[3]), color[::-1], 2)
(test_width, text_height), baseline = cv2.getTextSize(class_names[cls],
cv2.FONT_HERSHEY_SIMPLEX,
0.75, 1)
cv2.rectangle(frame,
(xy[0], xy[1]),
(xy[0] + test_width, xy[1] - text_height - baseline),
color[::-1],
thickness=cv2.FILLED)
cv2.putText(frame, class_names[cls], (xy[0], xy[1] - baseline), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)
示例3: drawBoundingBox
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def drawBoundingBox(self,imgcv,result):
#finding max val
self.predictThresh=max([box['confidence'] for box in result])
for box in result:
# print(box)
x1,y1,x2,y2 = (box['topleft']['x'],box['topleft']['y'],box['bottomright']['x'],box['bottomright']['y'])
conf = box['confidence']
# print(conf)
label = box['label']
print("label",label,"confidence",conf)
if conf < self.predictThresh:
continue
# print(x1,y1,x2,y2,conf,label)
cv2.rectangle(imgcv,(x1,y1),(x2,y2),(0,255,0),6)
labelSize=cv2.getTextSize(label,cv2.FONT_HERSHEY_COMPLEX,0.5,2)
# print('labelSize>>',labelSize)
_x1 = x1
_y1 = y1#+int(labelSize[0][1]/2)
_x2 = _x1+labelSize[0][0]
_y2 = y1-int(labelSize[0][1])
cv2.rectangle(imgcv,(_x1,_y1),(_x2,_y2),(0,255,0),cv2.FILLED)
cv2.putText(imgcv,label,(x1,y1),cv2.FONT_HERSHEY_COMPLEX,0.5,(0,0,0),1)
return imgcv
示例4: vis_class
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def vis_class(img, pos, class_str, bg_color):
"""Visualizes the class."""
font_color = cfg.VIS.SHOW_CLASS.COLOR
font_scale = cfg.VIS.SHOW_CLASS.FONT_SCALE
x0, y0 = int(pos[0]), int(pos[1])
# Compute text size.
txt = class_str
font = cv2.FONT_HERSHEY_SIMPLEX
((txt_w, txt_h), _) = cv2.getTextSize(txt, font, font_scale, 1)
# Place text background.
back_tl = x0, y0 - int(1.3 * txt_h)
back_br = x0 + txt_w, y0
cv2.rectangle(img, back_tl, back_br, bg_color, -1)
# Show text.
txt_tl = x0, y0 - int(0.3 * txt_h)
cv2.putText(img, txt, txt_tl, font, font_scale, font_color, lineType=cv2.LINE_AA)
return img
示例5: create_text_mask
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def create_text_mask(text, font_face=cv2.FONT_HERSHEY_SIMPLEX, font_scale=2, thickness=2):
"""Build a binary image with text drawn in it"""
color = [1]
(w, h), _ = cv2.getTextSize(
text, fontFace=font_face, fontScale=font_scale, thickness=thickness
)
border = 30
dst = np.zeros((h + border, w + border), dtype='uint8')
cv2.putText(
dst, text=text, org=(border // 2, h + border // 2),
fontFace=font_face, fontScale=font_scale,
thickness=thickness, color=color
)
ymask = dst.any(1).cumsum()
ymask = (ymask != 0) & (ymask != ymask[-1])
xmask = dst.any(0).cumsum()
xmask = (xmask != 0) & (xmask != xmask[-1])
dst = dst[ymask][:, xmask]
return dst.astype(bool)
示例6: draw_bboxes
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def draw_bboxes(img, bbox, identities=None, offset=(0,0)):
for i,box in enumerate(bbox):
x1,y1,x2,y2 = [int(i) for i in box]
x1 += offset[0]
x2 += offset[0]
y1 += offset[1]
y2 += offset[1]
# box text and bar
id = int(identities[i]) if identities is not None else 0
color = COLORS_10[id%len(COLORS_10)]
label = '{} {}'.format("object", id)
t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 2 , 2)[0]
cv2.rectangle(img,(x1, y1),(x2,y2),color,3)
cv2.rectangle(img,(x1, y1),(x1+t_size[0]+3,y1+t_size[1]+4), color,-1)
cv2.putText(img,label,(x1,y1+t_size[1]+4), cv2.FONT_HERSHEY_PLAIN, 2, [255,255,255], 2)
return img
示例7: add_coco_bbox
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def add_coco_bbox(self, bbox, cat, conf=1, show_txt=True, img_id='default'):
bbox = np.array(bbox, dtype=np.int32)
# cat = (int(cat) + 1) % 80
cat = int(cat)
# print('cat', cat, self.names[cat])
c = self.colors[cat][0][0].tolist()
if self.theme == 'white':
c = (255 - np.array(c)).tolist()
txt = '{}{:.1f}'.format(self.names[cat], conf)
font = cv2.FONT_HERSHEY_SIMPLEX
cat_size = cv2.getTextSize(txt, font, 0.5, 2)[0]
cv2.rectangle(
self.imgs[img_id], (bbox[0], bbox[1]), (bbox[2], bbox[3]), c, 2)
if show_txt:
cv2.rectangle(self.imgs[img_id],
(bbox[0], bbox[1] - cat_size[1] - 2),
(bbox[0] + cat_size[0], bbox[1] - 2), c, -1)
cv2.putText(self.imgs[img_id], txt, (bbox[0], bbox[1] - 2),
font, 0.5, (0, 0, 0), thickness=1, lineType=cv2.LINE_AA)
示例8: vis_class
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def vis_class(img, pos, class_str, font_scale=0.35):
"""Visualizes the class."""
img = img.astype(np.uint8)
x0, y0 = int(pos[0]), int(pos[1])
# Compute text size.
txt = class_str
font = cv2.FONT_HERSHEY_SIMPLEX
((txt_w, txt_h), _) = cv2.getTextSize(txt, font, font_scale, 1)
# Place text background.
back_tl = x0, y0 - int(1.3 * txt_h)
back_br = x0 + txt_w, y0
cv2.rectangle(img, back_tl, back_br, _GREEN, -1)
# Show text.
txt_tl = x0, y0 - int(0.3 * txt_h)
cv2.putText(img, txt, txt_tl, font, font_scale, _GRAY, lineType=cv2.LINE_AA)
return img
示例9: drawPred
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def drawPred(frame,classId, conf, left, top, right, bottom):
# Draw a bounding box.
cv.rectangle(frame, (left, top), (right, bottom), (255, 178, 50), 3)
label = '%.2f' % conf
# Get the label for the class name and its confidence
if classes:
assert(classId < len(classes))
label = '%s:%s' % (classes[classId], label)
#Display the label at the top of the bounding box
labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
top = max(top, labelSize[1])
#cv.rectangle(frame, (left, top - round(1.5*labelSize[1])), (left + round(1.5*labelSize[0]), top + baseLine), (255, 255, 255), cv.FILLED)
cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0,0,0), 1)
# Remove the bounding boxes with low confidence using non-maxima suppression
示例10: vis_detections
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def vis_detections(img, class_name,c , dets, thresh=0.8):
for i in range(np.minimum(10, dets.shape[0])):
bbox = tuple(int(np.round(x)) for x in dets[i, :4])
score = dets[i, -1]
if score < thresh:
continue
txt = '{}:{:.2f}'.format( class_name,score)
font = cv2.FONT_HERSHEY_SIMPLEX
cat_size = cv2.getTextSize(txt, font, 0.5, 2)[0]
cv2.rectangle(
img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), c, 2)
cv2.rectangle(img,
(bbox[0], bbox[1] - cat_size[1] - 2),
(bbox[0] + cat_size[0], bbox[1] - 2), c, -1)
cv2.putText(img, txt, (bbox[0], bbox[1] - 2),
font, 0.5, (0, 0, 0), thickness=1, lineType=cv2.LINE_AA)
示例11: vis_class
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def vis_class(img, pos, class_str, font_scale=0.35):
"""Visualizes the class."""
img = img.astype(np.uint8)
x0, y0 = int(pos[0]), int(pos[1])
# Compute text size.
txt = class_str
font = cv2.FONT_HERSHEY_SIMPLEX
((txt_w, txt_h), _) = cv2.getTextSize(txt, font, font_scale, 1)
# Place text background.
if y0 - int(1.3 * txt_h) < 0:
y0 = y0 + int(1.6 * txt_h)
back_tl = x0, y0 - int(1.3 * txt_h)
back_br = x0 + txt_w, y0
cv2.rectangle(img, back_tl, back_br, _GREEN, -1)
# cv2.rectangle(img, back_tl, back_br, _GRAY, -1)
# Show text.
txt_tl = x0, y0 - int(0.3 * txt_h)
cv2.putText(img, txt, txt_tl, font, font_scale, _GRAY, lineType=cv2.LINE_AA)
# cv2.putText(img, txt, txt_tl, font, font_scale, (46, 52, 54), lineType=cv2.LINE_AA)
return img
示例12: draw_result
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def draw_result(image, confidences, faceboxes):
"""Draw the detection result on image"""
for result in zip(confidences, faceboxes):
conf = result[0]
facebox = result[1]
cv.rectangle(image, (facebox[0], facebox[1]),
(facebox[2], facebox[3]), (0, 255, 0))
label = "face: %.4f" % conf
label_size, base_line = cv.getTextSize(
label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
cv.rectangle(image, (facebox[0], facebox[1] - label_size[1]),
(facebox[0] + label_size[0],
facebox[1] + base_line),
(0, 255, 0), cv.FILLED)
cv.putText(image, label, (facebox[0], facebox[1]),
cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
示例13: write
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def write(x, batches, results):
c1 = tuple(x[1:3].int())
c2 = tuple(x[3:5].int())
img = results[int(x[0])]
cls = int(x[-1])
label = "{0}".format(classes[cls])
color = random.choice(colors)
cv2.rectangle(img, c1, c2,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, 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)
cv2.imshow("res", img)
cv2.waitKey(0)
#cv2.imwrite("",cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
return img
示例14: add_coco_bbox
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def add_coco_bbox(self, bbox, cat, conf=1, show_txt=True, img_id='default'):
bbox = np.array(bbox, dtype=np.int32)
# cat = (int(cat) + 1) % 80
cat = int(cat)
# print('cat', cat, self.names[cat])
c = self.colors[cat][0][0].tolist()
if self.theme == 'white':
c = (255 - np.array(c)).tolist()
txt = '{}{:.1f}'.format(self.names[cat], conf)
font = cv2.FONT_HERSHEY_SIMPLEX
cat_size = cv2.getTextSize(txt, font, 0.5, 2)[0]
cv2.rectangle(
self.imgs[img_id], (bbox[0], bbox[1]), (bbox[2], bbox[3]), c, 2)
if show_txt:
cv2.rectangle(self.imgs[img_id],
(bbox[0], bbox[1] - cat_size[1] - 2),
(bbox[0] + cat_size[0], bbox[1] - 2), c, -1)
cv2.putText(self.imgs[img_id], txt, (bbox[0], bbox[1] - 2),
font, 0.5, (0, 0, 0), thickness=1, lineType=cv2.LINE_AA)
示例15: draw_boxes
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import getTextSize [as 别名]
def draw_boxes(img, output, offset=(0, 0)):
for i, box in enumerate(output):
x1, y1, x2, y2, identity = [int(ii) for ii in box]
x1 += offset[0]
x2 += offset[0]
y1 += offset[1]
y2 += offset[1]
# box text and bar
color = compute_color_for_labels(identity)
label = '{}{:d}'.format("", identity)
t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 2, 2)[0]
cv2.rectangle(img, (x1, y1), (x2, y2), color, 3)
cv2.rectangle(img, (x1, y1), (x1 + t_size[0] + 3, y1 + t_size[1] + 4), color, -1)
cv2.putText(img, label, (x1, y1 + t_size[1] + 4), cv2.FONT_HERSHEY_PLAIN, 2, [255, 255, 255], 2)
return img