使用opencv帮助绘图过程的库。想给图片加标签。图像分类等。

opencv-draw-tools-fernaperg的Python项目详细描述


opencv绘图工具

使用opencv帮助绘图过程的库。想给图片加标签。图像等的分类

image

图像由Intel OpenVino工具包生成,由OpenCVDraw Tools v0.1.9绘制

安装

先决条件

您需要安装:

  • 打开cv>;=3.6.2
  • numpy=1.13.3
  • python约束>;=1.4.0

您只需执行: pip install -r requirements.txt

最后,您可以使用以下命令安装库:

pip install opencv-draw-tools-fernaperg

安装opencv-draw-tools时,它会自动下载numpy,但不会下载opencv,因为在某些情况下,您需要另一个版本。

用法

测试

import opencv_draw_tools as cv2_tools

print('Name: {}\nVersion:{}\nHelp:{}'.format(cv2_tools.name,cv2_tools.__version__,cv2_tools.help))
cv2_tools.webcam_test()

面向对象编程方法

首先创建一个selectorcv2对象。您可以传递可选参数来配置输出。

selector = cv2_tools.SelectorCV2(color=(200,90,0), filled=True)

也可以稍后使用方法(所有可选参数)对其进行配置:

selector.set_properties()

现在,每次要添加选定区域时,请调用以下方法:

"""
Coordinates:

(x1,y1)____(x2,y1)
   |          |
   |          |
(x1,y2)____(x2,y2)

Tags (optional parameter):
* It could be a normal string
* A string with '\n'
* A list of strings
* None / '' / [] / False
"""
selector.add_zone((x1,y1,x2,y2),tags=tag)

最后,当您要绘制所有矩形时,请执行:

edited_frame = selector.draw(frame)

如果您想多次使用同一个对象,可以很容易地更改其中的内容:

# This method could help change rectangles to
selector.set_range_valid_rectangles( origin, destination)

# This method could help if you know exactly the indexes that you want to keep
# Default = [], so if you just want to clean the buffer call this method without parameters
set_valid_rectangles(indexes)

如果需要,可以看到示例detect_faces.py,它还使用一个名为face_recognition的开源库。

手动法

import opencv_draw_tools as cv2_tools


"""
  Draw better rectangles to select zones.
  Keyword arguments:
  frame -- opencv frame object where you want to draw
  position -- touple with 4 elements (x1, y1, x2, y2)
              This elements must be between 0 and 1 in case it is normalized
              or between 0 and frame height/width.
  tags -- list of strings/tags you want to associate to the selected zone (default [])
  tag_position -- position where you want to add the tags, relatively to the selected zone (default None)
                  If None provided it will auto select the zone where it fits better:
                      - First try to put the text on the Bottom Rigth corner
                      - If it doesn't fit, try to put the text on the Bottom Left corner
                      - If it doesn't fit, try to put the text Inside the rectangle
                      - Finally if it doesn't fit, try to put the text On top of the rectangle
  alpha -- transparency of the selected zone on the image (default 0.9)
           1 means totally visible and 0 totally invisible
  color -- color of the selected zone, touple with 3 elements BGR (default (110,70,45) -> dark blue)
           BGR = Blue - Green - Red
  normalized -- boolean parameter, if True, position provided normalized (between 0 and 1) else you should provide concrete values (default False)
  thickness -- thickness of the drawing in pixels (default 2)
  filled -- boolean parameter, if True, will draw a filled rectangle with one-third opacity compared to the rectangle (default False)
  peephole -- boolean parameter, if True, also draw additional effect, so it looks like a peephole
"""
frame = cv2_tools.select_zone(frame, position, tags=[])

网络摄像头示例

import opencv_draw_tools as cv2_tools
cv2_tools.webcam_test()

参见webcam_test()代码:

def webcam_test():
    """Reproduce Webcam in real time with a selected zone."""
    print('Launching webcam test')
    cap = cv2.VideoCapture(0)
    f_width = cap.get(3)
    f_height = cap.get(4)
    window_name = 'opencv_draw_tools'
    while True:
        ret, frame = cap.read()
        frame = cv2.flip(frame, 1)
        if ret:
            keystroke = cv2.waitKey(1)
            position = (0.33,0.2,0.66,0.8)
            tags = ['MIT License', '(C) Copyright\n    Fernando\n    Perez\n    Gutierrez']
            frame = select_zone(frame, position, tags=tags, color=(130,58,14), thickness=2, filled=True, normalized=True)
            cv2.imshow(window_name, frame)
            # True if escape 'esc' is pressed
            if keystroke == 27:
                break
    cv2.destroyAllWindows()
    cv2.VideoCapture(0).release()

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java从JSP或HTML向servlet发送多个参数   java方法来查看字符是否在字符数组中   使用带有java的MAC地址连接到设备   java如何将csv文件中的数据打印到secondactivity?   java如何从netbean 7.0.1连接到数据库   java考虑所有可能的类值,用于输出测试分割的预测值。   java我的actionListener调用有什么问题   swing在Java中实现粒子过滤器最有效的方法是什么?   java运行。getFontFamily()为返回null。使用apachepoi的docx文件   一个事务中的java领域循环与每个步骤循环中的一个事务   java日期格式与Spring Boot不兼容   java类冲突。处理   java GridBagLayout不工作   java将图像发送到另一个应用程序