本文整理汇总了Python中cv2.putText方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.putText方法的具体用法?Python cv2.putText怎么用?Python cv2.putText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.putText方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: drawBoundingBox
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import putText [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 putText [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 putText [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_det_and_mask
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import putText [as 别名]
def vis_det_and_mask(im, class_name, dets, masks, thresh=0.8):
"""Visual debugging of detections."""
num_dets = np.minimum(10, dets.shape[0])
colors_mask = random_colors(num_dets)
colors_bbox = np.round(np.random.rand(num_dets, 3) * 255)
# sort rois according to the coordinates, draw upper bbox first
draw_mask = np.zeros(im.shape[:2], dtype=np.uint8)
for i in range(1):
bbox = tuple(int(np.round(x)) for x in dets[i, :4])
mask = masks[i, :, :]
full_mask = unmold_mask(mask, bbox, im.shape)
score = dets[i, -1]
if score > thresh:
word_width = len(class_name)
cv2.rectangle(im, bbox[0:2], bbox[2:4], colors_bbox[i], 2)
cv2.rectangle(im, bbox[0:2], (bbox[0] + 18 + word_width*8, bbox[1]+15), colors_bbox[i], thickness=cv2.FILLED)
apply_mask(im, full_mask, draw_mask, colors_mask[i], 0.5)
draw_mask += full_mask
cv2.putText(im, '%s' % (class_name), (bbox[0]+5, bbox[1] + 12), cv2.FONT_HERSHEY_PLAIN,
1.0, (255,255,255), thickness=1)
return im
示例5: ProcessFrame
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import putText [as 别名]
def ProcessFrame(self, frame):
# segment arm region
segment = self.SegmentArm(frame)
# make a copy of the segmented image to draw on
draw = cv2.cvtColor(segment, cv2.COLOR_GRAY2RGB)
# draw some helpers for correctly placing hand
cv2.circle(draw,(self.imgWidth/2,self.imgHeight/2),3,[255,102,0],2)
cv2.rectangle(draw, (self.imgWidth/3,self.imgHeight/3), (self.imgWidth*2/3, self.imgHeight*2/3), [255,102,0],2)
# find the hull of the segmented area, and based on that find the
# convexity defects
[contours,defects] = self.FindHullDefects(segment)
# detect the number of fingers depending on the contours and convexity defects
# draw defects that belong to fingers green, others red
[nofingers,draw] = self.DetectNumberFingers(contours, defects, draw)
# print number of fingers on image
cv2.putText(draw, str(nofingers), (30,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255))
return draw
示例6: draw_outputs
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import putText [as 别名]
def draw_outputs(img, outputs, class_names=None):
boxes, objectness, classes = outputs
#boxes, objectness, classes = boxes[0], objectness[0], classes[0]
wh = np.flip(img.shape[0:2])
if img.ndim == 2 or img.shape[2] == 1:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
min_wh = np.amin(wh)
if min_wh <= 100:
font_size = 0.5
else:
font_size = 1
for i in range(classes.shape[0]):
x1y1 = tuple((np.array(boxes[i][0:2]) * wh).astype(np.int32))
x2y2 = tuple((np.array(boxes[i][2:4]) * wh).astype(np.int32))
img = cv2.rectangle(img, x1y1, x2y2, (255, 0, 0), 1)
img = cv2.putText(img, '{}'.format(int(classes[i])), x1y1, cv2.FONT_HERSHEY_COMPLEX_SMALL, font_size,
(0, 0, 255), 1)
return img
示例7: draw_labels
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import putText [as 别名]
def draw_labels(x, y, class_names=None):
img = x.numpy()
if img.ndim == 2 or img.shape[2] == 1:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
boxes, classes = tf.split(y, (4, 1), axis=-1)
classes = classes[..., 0]
wh = np.flip(img.shape[0:2])
min_wh = np.amin(wh)
if min_wh <= 100:
font_size = 0.5
else:
font_size = 1
for i in range(len(boxes)):
x1y1 = tuple((np.array(boxes[i][0:2]) * wh).astype(np.int32))
x2y2 = tuple((np.array(boxes[i][2:4]) * wh).astype(np.int32))
img = cv2.rectangle(img, x1y1, x2y2, (255, 0, 0), 1)
if class_names:
img = cv2.putText(img, class_names[classes[i]], x1y1, cv2.FONT_HERSHEY_COMPLEX_SMALL, font_size,
(0, 0, 255), 1)
else:
img = cv2.putText(img, str(classes[i]), x1y1, cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
return img
示例8: detect
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import putText [as 别名]
def detect(imgfile):
origimg = cv2.imread(imgfile)
img = preprocess(origimg)
img = img.astype(np.float32)
img = img.transpose((2, 0, 1))
net.blobs['data'].data[...] = img
out = net.forward()
box, conf, cls = postprocess(origimg, out)
for i in range(len(box)):
p1 = (box[i][0], box[i][1])
p2 = (box[i][2], box[i][3])
cv2.rectangle(origimg, p1, p2, (0,255,0))
p3 = (max(p1[0], 15), max(p1[1], 15))
title = "%s:%.2f" % (COCO_CLASSES[int(cls[i])], conf[i])
cv2.putText(origimg, title, p3, cv2.FONT_ITALIC, 0.6, (0, 255, 0), 1)
cv2.imshow("SSD", origimg)
k = cv2.waitKey(0) & 0xff
#Exit if ESC pressed
if k == 27 : return False
return True
示例9: detect
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import putText [as 别名]
def detect(imgfile):
origimg = cv2.imread(imgfile)
img = preprocess(origimg)
img = img.astype(np.float32)
img = img.transpose((2, 0, 1))
net.blobs['data'].data[...] = img
out = net.forward()
box, conf, cls = postprocess(origimg, out)
for i in range(len(box)):
p1 = (box[i][0], box[i][1])
p2 = (box[i][2], box[i][3])
cv2.rectangle(origimg, p1, p2, (0,255,0))
p3 = (max(p1[0], 15), max(p1[1], 15))
title = "%s:%.2f" % (CLASSES[int(cls[i])], conf[i])
cv2.putText(origimg, title, p3, cv2.FONT_ITALIC, 0.6, (0, 255, 0), 1)
cv2.imshow("SSD", origimg)
k = cv2.waitKey(0) & 0xff
#Exit if ESC pressed
if k == 27 : return False
return True
示例10: draw_value_reward_score
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import putText [as 别名]
def draw_value_reward_score(self, img, value, reward, score):
img = img.copy()
# Average with 0.5 for semi-transparent background
img[:14] = img[:14] * 0.5 + 0.25
img[50:] = img[50:] * 0.5 + 0.25
if self.cfg.gan == 'ls':
red = -np.tanh(float(score) / 1) * 0.5 + 0.5
else:
red = -np.tanh(float(score) / 10.0) * 0.5 + 0.5
top = '%+.2f %+.2f' % (value, reward)
cv2.putText(img, top, (3, 7), cv2.FONT_HERSHEY_SIMPLEX, 0.25,
(1.0, 1.0 - red, 1.0 - red))
score = '%+.3f' % score
cv2.putText(img, score, (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.35,
(1.0, 1.0 - red, 1.0 - red))
return img
示例11: vis_images_and_indexs
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import putText [as 别名]
def vis_images_and_indexs(images, features, dir, name):
# indexs = np.reshape(indexs, (len(indexs),))
# print('visualizing images and indexs: ', images.shape, indexs.shape)
id_imgs = []
for feature in features:
img = np.ones((64, 64, 3))
cv2.putText(img,
str(feature), (4, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.25,
(1.0, 0.0, 0.0))
id_imgs.append(img)
id_imgs = np.stack(id_imgs, axis=0)
# print('id imgs: ', id_imgs.shape)
vis_imgs = np.vstack([images, id_imgs])
image = make_image_grid(vis_imgs, per_row=images.shape[0])
vis_dir = dir
try:
os.mkdir(vis_dir)
except:
pass
cv2.imwrite(os.path.join(vis_dir, name + '.png'), image[:, :, ::-1] * 255.0)
示例12: overlay_class_names
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import putText [as 别名]
def overlay_class_names(self, image, predictions):
"""
Adds detected class names and scores in the positions defined by the
top-left corner of the predicted bounding box
Arguments:
image (np.ndarray): an image as returned by OpenCV
predictions (BoxList): the result of the computation by the model.
It should contain the field `scores` and `labels`.
"""
scores = predictions.get_field("scores").tolist()
labels = predictions.get_field("labels").tolist()
labels = [self.CATEGORIES[i] for i in labels]
boxes = predictions.bbox
template = "{}: {:.2f}"
for box, score, label in zip(boxes, scores, labels):
x, y = box[:2]
s = template.format(label, score)
cv2.putText(
image, s, (x, y), cv2.FONT_HERSHEY_SIMPLEX, .5, (255, 255, 255), 1
)
return image
示例13: update
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import putText [as 别名]
def update(self, radarData):
self.img = np.zeros((self.height, self.width, self.channels), np.uint8)
cv2.line(self.img, (10, 0), (self.width/2 - 5, self.height), (100, 255, 255))
cv2.line(self.img, (self.width - 10, 0), (self.width/2 + 5, self.height), (100, 255, 255))
for track_number in range(1, 65):
if str(track_number)+'_track_range' in radarData:
track_range = radarData[str(track_number)+'_track_range']
track_angle = (float(radarData[str(track_number)+'_track_angle'])+90.0)*math.pi/180
x_pos = math.cos(track_angle)*track_range*4
y_pos = math.sin(track_angle)*track_range*4
cv2.circle(self.img, (self.width/2 + int(x_pos), self.height - int(y_pos) - 10), 5, (255, 255, 255))
#cv2.putText(self.img, str(track_number),
# (self.width/2 + int(x_pos)-2, self.height - int(y_pos) - 10), self.font, 1, (255,255,255), 2)
cv2.imshow("Radar", self.img)
cv2.waitKey(2)
示例14: test_yellow_white_thresh_images
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import putText [as 别名]
def test_yellow_white_thresh_images(src, dst, y_low=(10,50,0), y_high=(30,255,255), w_low=(180,180,180), w_high=(255,255,255)):
"""
apply the thresh to images in a src folder and output to dst foler
"""
image_files = glob.glob(src+"*.jpg")
for idx, file in enumerate(image_files):
print(file)
img = mpimg.imread(file)
image_threshed = yellow_white_thresh(img, y_low, y_high, w_low, w_high)
file_name = file.split("\\")[-1]
print(file_name)
out_image = dst+file_name
print(out_image)
# convert binary to RGB, *255, to visiual, 1 will not visual after write to file
image_threshed = cv2.cvtColor(image_threshed*255, cv2.COLOR_GRAY2RGB)
# HSV = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
# V = HSV[:,:,2]
# brightness = np.mean(V)
# info_str = "brightness is: {}".format(int(brightness))
# cv2.putText(image_threshed, info_str, (50,700), cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,255),2)
cv2.imwrite(out_image, image_threshed)
示例15: vis_class
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import putText [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