本文整理汇总了Python中cv2.FONT_HERSHEY_COMPLEX_SMALL属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.FONT_HERSHEY_COMPLEX_SMALL属性的具体用法?Python cv2.FONT_HERSHEY_COMPLEX_SMALL怎么用?Python cv2.FONT_HERSHEY_COMPLEX_SMALL使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.FONT_HERSHEY_COMPLEX_SMALL属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: draw_box
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [as 别名]
def draw_box(self, roi, img, linewidth, color, name=None):
"""
roi: rectangle or polygon
img: numpy array img
linewith: line width of the bbox
"""
if len(roi) > 6 and len(roi) % 2 == 0:
pts = np.array(roi, np.int32).reshape(-1, 1, 2)
color = tuple(map(int, color))
img = cv2.polylines(img, [pts], True, color, linewidth)
pt = (pts[0, 0, 0], pts[0, 0, 1]-5)
if name:
img = cv2.putText(img, name, pt, cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, color, 1)
elif len(roi) == 4:
if not np.isnan(roi[0]):
roi = list(map(int, roi))
color = tuple(map(int, color))
img = cv2.rectangle(img, (roi[0], roi[1]), (roi[0]+roi[2], roi[1]+roi[3]),
color, linewidth)
if name:
img = cv2.putText(img, name, (roi[0], roi[1]-5), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, color, 1)
return img
示例2: draw_outputs
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [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
示例3: draw_labels
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [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
示例4: show
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [as 别名]
def show(img, label, target):
img = np.transpose(img, (1, 2, 0))
img *= 128.
img += 127.5
img = img.astype(np.uint8)
lb = ""
for i in label:
lb += CHARS[i]
tg = ""
for j in target.tolist():
tg += CHARS[int(j)]
flag = "F"
if lb == tg:
flag = "T"
# img = cv2.putText(img, lb, (0,16), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.6, (0, 0, 255), 1)
img = cv2ImgAddText(img, lb, (0, 0))
cv2.imshow("test", img)
print("target: ", tg, " ### {} ### ".format(flag), "predict: ", lb)
cv2.waitKey()
cv2.destroyAllWindows()
示例5: draw_outputs
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [as 别名]
def draw_outputs(img, outputs, class_names, class_colors=None):
boxes, objectness, classes, nums = outputs
boxes, objectness, classes, nums = boxes[0], objectness[0], classes[0], nums[0]
wh = np.flip(img.shape[0:2])
for i in range(nums):
if class_colors:
box_color = class_colors[int(classes[i]) % len(class_colors)]
else:
box_color = (255, 0, 0)
label_color = (0, 0, 0)
font_face = cv2.FONT_HERSHEY_COMPLEX_SMALL
class_name = class_names[int(classes[i])]
label_size = cv2.getTextSize(class_name, font_face, 1, 1)[0]
top_left = tuple((np.array(boxes[i][0:2]) * wh).astype(np.int32))
bottom_right = tuple((np.array(boxes[i][2:4]) * wh).astype(np.int32))
label_bottom_right = tuple((np.array(top_left) + np.array(label_size)).astype(np.int32))
label_origin = tuple((np.array(top_left) + np.array((0, label_size[1]))).astype(np.int32))
img = cv2.rectangle(img, top_left, label_bottom_right, box_color, cv2.FILLED)
img = cv2.rectangle(img, top_left, bottom_right, box_color, 2)
img = cv2.putText(img, class_name, label_origin, font_face, 1, label_color, 2)
return img
示例6: draw_detection
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [as 别名]
def draw_detection(frame, det, class_names):
(klass, score, x0, y0, x1, y1) = det
klass_name = class_names[int(klass)]
h = frame.shape[0]
w = frame.shape[1]
# denormalize detections from [0,1] to the frame size
p0 = tuple(map(int, (x0*w,y0*h)))
p1 = tuple(map(int, (x1*w,y1*h)))
logging.info("detection: %s %s", klass_name, score)
cv2.rectangle(frame, p0, p1, (0,0,255), 2)
# Where to draw the text, a few pixels above the top y coordinate
tp0 = (p0[0], p0[1]-5)
draw_text = "{} {}".format(klass_name, score)
cv2.putText(frame, draw_text, tp0, cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.5, (0,0,255))
示例7: add_water_mask
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [as 别名]
def add_water_mask(video_path, mask_word):
"""
给视频增加水印
:param video_part3: 视频源
:param mask_word: 水印文字
:return:
"""
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
# 保证帧率不变
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
video_temp_path = get_temp_path(video_path, 'temp')
video_writer = cv2.VideoWriter(video_temp_path, fourcc, fps, img_size)
ret, frame = cap.read()
while ret:
# 文字在图中的坐标(注意:这里的坐标原点是图片左上角)
x, y = img_size[0] - 200, img_size[1] - 50
cv2.putText(img=frame, text=mask_word,
org=(x, y), fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL,
fontScale=1, color=(255, 255, 255))
video_writer.write(frame)
ret, frame = cap.read()
# 删除源文件,并重命名临时文件
os.remove(video_path)
os.rename(video_temp_path, video_path)
print('水印添加完成~')
video_writer.release()
cap.release()
示例8: draw
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [as 别名]
def draw(self, image, result):
image_h, image_w, _ = image.shape
colors = self.random_colors(len(result))
for i in range(len(result)):
xmin = max(int(result[i][1] - 0.5 * result[i][3]), 0)
ymin = max(int(result[i][2] - 0.5 * result[i][4]), 0)
xmax = min(int(result[i][1] + 0.5 * result[i][3]), image_w)
ymax = min(int(result[i][2] + 0.5 * result[i][4]), image_h)
color = tuple([rgb * 255 for rgb in colors[i]])
cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 1)
cv2.putText(image, result[i][0] + ':%.2f' % result[i][5], (xmin + 1, ymin + 8), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.5, color, 1)
print(result[i][0], ':%.2f%%' % (result[i][5] * 100 ))
示例9: _annotate_frame
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [as 别名]
def _annotate_frame(self,img, positions, tracking_units):
if img is None:
return
for track_u in tracking_units:
x,y = track_u.roi.offset
y += track_u.roi.rectangle[3]/2
cv2.putText(img, str(track_u.roi.idx), (int(x),int(y)), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,0))
black_colour = (0, 0,0)
roi_colour = (0, 255,0)
cv2.drawContours(img,[track_u.roi.polygon],-1, black_colour, 3, LINE_AA)
cv2.drawContours(img,[track_u.roi.polygon],-1, roi_colour, 1, LINE_AA)
try:
pos_list = positions[track_u.roi.idx]
except KeyError:
continue
for pos in pos_list:
colour = (0 ,0, 255)
try:
if pos["has_interacted"]:
colour = (255, 0,0)
except KeyError:
pass
cv2.ellipse(img,((pos["x"],pos["y"]), (pos["w"],pos["h"]), pos["phi"]),black_colour,3, LINE_AA)
cv2.ellipse(img,((pos["x"],pos["y"]), (pos["w"],pos["h"]), pos["phi"]),colour,1, LINE_AA)
示例10: draw_rois
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [as 别名]
def draw_rois(im, all_rois):
for roi in all_rois:
x,y = roi.offset
y += roi.rectangle[3]/2
x += roi.rectangle[2]/2
cv2.putText(im, str(roi.idx), (x,y), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,0))
black_colour,roi_colour = (0, 0,0), (0, 255,0)
cv2.drawContours(im,[roi.polygon],-1, black_colour, 3, cv2.CV_AA)
cv2.drawContours(im,[roi.polygon],-1, roi_colour, 1, cv2.CV_AA)
示例11: show
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [as 别名]
def show(self, pred_trajs={}, linewidth=2, show_name=False):
"""
pred_trajs: dict of pred_traj, {'tracker_name': list of traj}
pred_traj should contain polygon or rectangle(x, y, width, height)
linewith: line width of the bbox
"""
assert self.imgs is not None
video = []
cv2.namedWindow(self.name, cv2.WINDOW_NORMAL)
colors = {}
if len(pred_trajs) == 0 and len(self.pred_trajs) > 0:
pred_trajs = self.pred_trajs
for i, (roi, img) in enumerate(zip(self.gt_traj,
self.imgs[self.start_frame:self.end_frame+1])):
img = img.copy()
if len(img.shape) == 2:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
else:
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
img = self.draw_box(roi, img, linewidth, (0, 255, 0),
'gt' if show_name else None)
for name, trajs in pred_trajs.items():
if name not in colors:
color = tuple(np.random.randint(0, 256, 3))
colors[name] = color
else:
color = colors[name]
img = self.draw_box(trajs[0][i], img, linewidth, color,
name if show_name else None)
cv2.putText(img, str(i+self.start_frame), (5, 20),
cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 0), 2)
cv2.imshow(self.name, img)
cv2.waitKey(40)
video.append(img.copy())
return video
示例12: show
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [as 别名]
def show(self, pred_trajs={}, linewidth=2, show_name=False):
"""
pred_trajs: dict of pred_traj, {'tracker_name': list of traj}
pred_traj should contain polygon or rectangle(x, y, width, height)
linewith: line width of the bbox
"""
assert self.imgs is not None
video = []
cv2.namedWindow(self.name, cv2.WINDOW_NORMAL)
colors = {}
if len(pred_trajs) == 0 and len(self.pred_trajs) > 0:
pred_trajs = self.pred_trajs
for i, (roi, img) in enumerate(zip(self.gt_traj,
self.imgs[self.start_frame:self.end_frame+1])):
img = img.copy()
if len(img.shape) == 2:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
else:
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
img = self.draw_box(roi, img, linewidth, (0, 255, 0),
'gt' if show_name else None)
for name, trajs in pred_trajs.items():
if name not in colors:
color = tuple(np.random.randint(0, 256, 3))
colors[name] = color
else:
color = colors[name]
img = self.draw_box(traj[0][i], img, linewidth, color,
name if show_name else None)
cv2.putText(img, str(i+self.start_frame), (5, 20),
cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 0), 2)
cv2.imshow(self.name, img)
cv2.waitKey(40)
video.append(img.copy())
return video
示例13: save_final_state
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [as 别名]
def save_final_state(self,save_path):
#create color_dict
colors_dict_final={}
for pos,color in zip(self.p0,self.color):
colors_dict_final[tuple(color)]=pos
colors_dict_inital={}
for pos,color in zip(*self.initial_state):
colors_dict_inital[tuple(color)]=pos
#import ipdb;ipdb.set_trace()
last_frame=self.old_frame.copy()
for k in colors_dict_inital:
a,b=colors_dict_inital[k].ravel()
if k in colors_dict_final:
c,d=colors_dict_final[k].ravel()
last_frame = cv2.line(last_frame,(a,b),(c,d),(255,0,0),2)
else:
last_frame = cv2.circle(last_frame,(a,b),2,(0,0,255),-1)
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
text=' '.join(save_path.split('/')[-2:]).replace('_',' ')
textsize=cv2.getTextSize(text,font,1,1)[0]
cv2.putText(last_frame,text,(last_frame.shape[1]//2-textsize[0]//2,30), font,1, (0,0,255),1,cv2.LINE_AA)
cv2.imwrite(save_path+'/tracks.png',last_frame)
ret={}
ret['init_ftr_cnt']=len(self.initial_state[0])
示例14: draw_labels
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [as 别名]
def draw_labels(x, y, class_names):
img = x.numpy()
boxes, classes = tf.split(y, (4, 1), axis=-1)
classes = classes[..., 0]
wh = np.flip(img.shape[0:2])
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), 2)
img = cv2.putText(img, class_names[classes[i]],
x1y1, cv2.FONT_HERSHEY_COMPLEX_SMALL,
1, (0, 0, 255), 2)
return img
示例15: main
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import FONT_HERSHEY_COMPLEX_SMALL [as 别名]
def main(video_dir, gpu_id, model_path):
# load videos
filenames = sorted(glob.glob(os.path.join(video_dir, "img/*.jpg")),
key=lambda x: int(os.path.basename(x).split('.')[0]))
frames = [cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB) for filename in filenames]
gt_bboxes = pd.read_csv(os.path.join(video_dir, "groundtruth_rect.txt"), sep='\t|,| ',
header=None, names=['xmin', 'ymin', 'width', 'height'],
engine='python')
title = video_dir.split('/')[-1]
# starting tracking
tracker = SiamFCTracker(model_path, gpu_id)
for idx, frame in enumerate(frames):
if idx == 0:
bbox = gt_bboxes.iloc[0].values
tracker.init(frame, bbox)
bbox = (bbox[0]-1, bbox[1]-1,
bbox[0]+bbox[2]-1, bbox[1]+bbox[3]-1)
else:
bbox = tracker.update(frame)
# bbox xmin ymin xmax ymax
frame = cv2.rectangle(frame,
(int(bbox[0]), int(bbox[1])),
(int(bbox[2]), int(bbox[3])),
(0, 255, 0),
2)
gt_bbox = gt_bboxes.iloc[idx].values
gt_bbox = (gt_bbox[0], gt_bbox[1],
gt_bbox[0]+gt_bbox[2], gt_bbox[1]+gt_bbox[3])
frame = cv2.rectangle(frame,
(int(gt_bbox[0]-1), int(gt_bbox[1]-1)), # 0-index
(int(gt_bbox[2]-1), int(gt_bbox[3]-1)),
(255, 0, 0),
1)
if len(frame.shape) == 3:
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
frame = cv2.putText(frame, str(idx), (5, 20), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1)
cv2.imshow(title, frame)
cv2.waitKey(30)