cv2.putText(frame, “FPS:“+str(fps), (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)

今天给大家分享一个简单快速在cv2的可视化窗口生成文字的方法,只需要一行代码即可实现,简单快捷。需要你们的点赞关注支持,老样子,代码以开源。

下面就以最近在做的基于姿势估计的人员摔倒为例,给大家展示一下。

首先大家先看效果图:

 基于姿势估计的检测方法,大家可以自行阅读我另一篇博客,这里主要介绍如何在cv2调出的窗口界面添加文字。在

cv2.imshow()

后面调用cv2内置的cv2.puttext()函数,官方的文档如下所示:

def putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None): # real signature unknown; restored from __doc__
    """
    putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> img
    .   @brief Draws a text string.
    .   
    .   The function cv::putText renders the specified text string in the image. Symbols that cannot be rendered
    .   using the specified font are replaced by question marks. See #getTextSize for a text rendering code
    .   example.
    .   
    .   @param img Image.
    .   @param text Text string to be drawn.
    .   @param org Bottom-left corner of the text string in the image.
    .   @param fontFace Font type, see #HersheyFonts.
    .   @param fontScale Font scale factor that is multiplied by the font-specific base size.
    .   @param color Text color.
    .   @param thickness Thickness of the lines used to draw a text.
    .   @param lineType Line type. See #LineTypes
    .   @param bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise,
    .   it is at the top-left corner.
    """
    pass

 使用方法参数举例如下:

cv2.putText(frame, "FPS:"+str(fps), (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)

注意一点:这个函数的第二个位置默认传入的是字符串类型,要注意传入的数据类型匹配或者强制转换成String类型。完整实验代码示例如下:

import cv2
import time
import mediapipe as mp
from tqdm import tqdm
# 导入solution
mp_pose = mp.solutions.pose
mp_drawing = mp.solutions.drawing_utils
pose = mp_pose.Pose(static_image_mode=False,
            #    model_complexity=1,
                    smooth_landmarks=True,
           #        enable_segmentation=True,
                    min_detection_confidence=0.7,
                    min_tracking_confidence=0.7)
while cap.isOpened():
    # 获取画面
    success, frame = cap.read()

    if not success:
        print('Error')
        break

    ## !!!处理帧函数
    start=time.time()
    frame = process_frame(frame)
    end=time.time()
    fps=int(1/(end-start))

    # 图片 添加的文字 位置 字体 字体大小 字体颜色 字体粗细
    cv2.putText(frame, "FPS:"+str(fps), (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
    # 展示处理后的三通道图像
    cv2.imshow('Posture Recognition', frame)
    if cv2.waitKey(1) in [ord('q'), 27]:
        break
cap.release()
cv2.destroyAllWindows()

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我了解您的要求,并将帮助您解决问题。以下是代码示例: ``` python import cv2 # 加载视频文件 cap = cv2.VideoCapture('coin04.mp4') # 定义输出视频的编解码器、帧率、大小等参数 fourcc = cv2.VideoWriter_fourcc(*'mp4v') fps = cap.get(cv2.CAP_PROP_FPS) size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) # 创建输出视频文件对象 out = cv2.VideoWriter('coin_output.mp4', fourcc, fps, size) # 读取视频帧 while(cap.isOpened()): ret, frame = cap.read() if ret == True: # 对每一帧图像进行处理 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (11, 11), 0) circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, dp=1, minDist=50, param1=100, param2=30, minRadius=20, maxRadius=80) # 检测到硬币,则将其位置标注在图像中 if circles is not None: circles = np.round(circles[0, :]).astype("int") for (x, y, r) in circles: cv2.circle(frame, (x, y), r, (0, 255, 0), 4) cv2.putText(frame, "Coin #" + str(len(circles)), (x - 50, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) # 将帧写入输出视频文件对象 out.write(frame) # 显示处理后的图像 cv2.imshow('Coin Detection',frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break # 释放资源 cap.release() out.release() cv2.destroyAllWindows() ``` 以上代码使用OpenCV读取给定的coin04.mp4视频文件,并从视频帧中提取每个硬币的位置,然后将其标注在图像中,并输出到coin_output.mp4文件中。您可以根据您自己的需求调整代码细节。祝您好运!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值