目标检测一些函数

2023-05-16

1. 计算bbox的iou

def myiou(bbox_A, bbox_B, mode='xyxy'): #xyxy
    x0_a,y0_a,x1_a,y1_a = bbox_A
    x0_b,y0_b,x1_b,y1_b = bbox_B
    areaA = (y1_a-y0_a) * (x1_a-x0_a)
    areaB = (y1_b-y0_b) * (x1_b-x0_b)
    overlap = max(min(x1_a,x1_b)-max(x0_a,x0_b), 0) * (min(y1_a,y1_b)-max(y0_a,y0_b))

    iou = overlap / (areaA+areaB-overlap+1e-6)
    return iou

2. 对文件夹中的图片按照整数序排序(非字典序)

def sort_files(imgs):
    sorted_imgs = []
    videos = []
    for img_name in imgs:
        videos.append(int(img_name[:-4]))
    sorted_video = (np.array(videos)[np.argsort(videos)])
    for img_s in sorted_video:
        sorted_imgs.append('{}.jpg'.format(img_s))
    return sorted_imgs

3. 绘制bbox可视化

def draw_yolox_predictions(img, bboxes, scores, bbclasses, confthre, classes_dict, gt=False, color=(0, 255, 0)):
    for i in range(len(bboxes)):
        box = bboxes[i]
        if not gt:
            score = scores[i]
            if score < confthre:
                continue
        x0 = int(box[0])
        y0 = int(box[1])
        x1 = int(box[2])
        y1 = int(box[3])

        cv2.rectangle(img, (x0, y0), (x1, y1), color, 2)
        if not gt:
            cls_id = int(bbclasses[i])
            cv2.putText(img, '{}:{:.1f}%'.format(0, score * 100), (x0, y0 - 3), cv2.FONT_HERSHEY_PLAIN, 1.3, color, thickness = 1)
    return img

4. 上下、左右、镜像翻转增强

#img size 720,1280,3
bboxes2, confs2  = predict(model2, img, size=IMG_SIZE, augment=AUGMENT)
bboxes3, confs3  = predict(model2, img[:,::-1,:], size=IMG_SIZE, augment=AUGMENT) #左右
bboxes4, confs4  = predict(model2, img[::-1,:,:], size=IMG_SIZE, augment=AUGMENT) #上下
bboxes5, confs5  = predict(model2, img[::-1,::-1,:], size=IMG_SIZE, augment=AUGMENT) #镜像
for bbox3, conf3 in zip(bboxes3, confs3):
     if conf3 >= 0.7:
         x3, y3, w3, h3 = bbox3
         bboxes_ensemble.append([1279-x3-w3, y3, 1279-x3, y3+h3])
         scores_ensemble.append(conf3)
    
for bbox4, conf4 in zip(bboxes4, confs4):
    if conf4 >= 0.7:
        x4, y4, w4, h4 = bbox4
        bboxes_ensemble.append([x4, 719-y4-h4, x4+w4, 719-y4])
        scores_ensemble.append(conf4)
        
for bbox5, conf5 in zip(bboxes5, confs5):
    if conf5 >= 0.75:
        x5, y5, w5, h5 = bbox5
        bboxes_ensemble.append([1279-x5-w5, 720-y5-h5, 1279-x5, 720-y5])
        scores_ensemble.append(conf5)

手动nms,输出最终结果

argsort = np.argsort(scores_ensemble)
argsort = argsort[::-1]
bboxes_sort = [bboxes_ensemble[p] for p in argsort]
scores_sort = [scores_ensemble[p] for p in argsort]

for a_i in range(len(bboxes_sort)):
    for b_j in range(a_i+1, len(bboxes_sort)):
        if scores_sort[b_j] > 0.15:
            bbox_A = bboxes_sort[a_i]
            bbox_B = bboxes_sort[b_j]
            x0_A, y0_A, x1_A,  y1_A = bbox_A
            x0_B, y0_B, x1_B,  y1_B = bbox_B
            iou = myiou(bbox_A, bbox_B)
            if iou > 0.3:
                scores_sort[b_j] = 0
                
take =  np.array(scores_sort) > 0.15
bboxes_nms = np.array(bboxes_sort)[take]
scores_nms = np.array(scores_sort)[take]
bboxes_nms = bboxes_nms.tolist()
scores_nms = scores_nms.tolist()

根据bbox裁剪正方形区域

#img size: 720,1280,3
def mycrop(box, enlarge):
    x0 = int(box[0])
    y0 = int(box[1])
    x1 = int(box[2])
    y1 = int(box[3])
    w = x1 - x0
    h = y1 - y0

    side = max(w, h)

    # enlarge = min(int(2*side), 719)
    enlarge_w = max(int((enlarge - w) / 2), 0)
    enlarge_h = max(int((enlarge - h) / 2), 0)

    xx0 = 0
    xx1 = 1279
    yy0 = 0
    yy1 = 719
    #超出边界
    if x0 < enlarge_w:
        xx0 = 0
        xx1 = enlarge
    if x1 > 1279 - enlarge_w:
        xx1 = 1279
        xx0 = 1279 - enlarge

    if y0 < enlarge_h:
        yy0 = 0
        yy1 = enlarge
    if y1 > 719 - enlarge_h:
        yy0 = 719 - enlarge
        yy1 = 719

    if x0 >= enlarge_w and x1 <= 1279 - enlarge_w:
        #larger
        xx0 = x0 - enlarge_w
        xx1 = x1 + enlarge_w

    if y0 >= enlarge_h and y1 <= 719 - enlarge_h:
        yy0 = y0 - enlarge_h
        yy1 = y1 + enlarge_h

    return xx0, xx1, yy0, yy1

计算相邻两帧相似度,判断是否属于同一个视频

def calculate(image1, image2):
    hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
    hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
    # plt.plot(hist1, color="r")
    # plt.plot(hist2, color="g")
    # plt.show()
    # 计算直方图的重合度
    degree = 0
    for i in range(len(hist1)):
        if hist1[i] != hist2[i]:
            degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
        else:
            degree = degree + 1
    degree = degree / len(hist1)
    return degree

def similarity_img(img1, img2):
    sub_image1 = cv2.split(img1)
    sub_image2 = cv2.split(img2)
    sub_data = 0
    for im1, im2 in zip(sub_image1, sub_image2):
        sub_data += calculate(im1, im2)
    sub_data = sub_data / 3
    return sub_data

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

目标检测一些函数 的相关文章

  • Linux之网络相关命令——ping、tranceroute、netstat、ftp、lftp、wget、ssh、scp、sftp

    网络连接方面 xff0c Linux可以说是万能的 Linux工具可以建立各种网络系统及应用 xff0c 包括防火墙 路由器 域名服务器 NAS xff08 网络附加存储 xff09 盒等 这里主要讲一些经常用到的命令 xff0c 涉及网络
  • Linux文件搜索命令介绍——locate、find、xargs、touch、stat

    本文主要介绍两个用在Linux系统中搜索文件的工具 locate 通过文件名查找文件find 在文件系统目录框架中查找文件 同时 xff0c 我们也会介绍一个通常与文件搜索命令一起使用 处理搜索结果文件列表的命令 xargs 从标准输入中建
  • ubuntu使用bash脚本+gnome实现开机自启python程序和崩溃重启

    这里以tx2的ubuntu18 04为例 xff0c 对ubuntu系统是有效的 例如我们要实现开机自动启动 home me test main py程序 xff0c 并且当main py出现任何意料之外的错误报错时 xff0c 系统可以重
  • http请求转串口通信系统开发者文档

    http请求转串口通信系统介绍 系统价值和功能与口号 让所有单片机联网通信 1 系统使用c语言mqtt协议开发esp8266为硬件载体 xff0c 调用者只需要任意编程语言的串口通信即可 xff01 2 是一个好用的免费的稳定的单片机网络通
  • ubuntu实现屏幕的旋转和开启自动旋转屏幕

    1 旋转屏幕 有两种方法 xff0c 一种是命令行 xff0c 一种是图形界面 这里只介绍命令行 xff0c 因为其简单 xrandr o left 向左旋转90度 xff0c 用于横屏转竖屏 xrandr o right 向右旋转90度
  • MaskRCNN在Jetson tx2上的测速结果

    博主测试了在不同模式 精度下降MaskRCNN部署到Jetson TX2上的测速结果 xff0c 与大家分享讨论 对FasterRCNN的测速可见FasterRcnn在Jetson TX2上测速 使用的MaskRCNN框架 matterpo
  • FasterRcnn在Jetson TX2上测速

    博主测试了在不同模式 精度下将FasterRCNN部署到Jetson TX2上的测速结果 xff0c 与大家分享讨论 对于MaskRCNN的部署结果可参见 MaskRCNN在Jetson tx2上的测速结果 使用的Caffe版本Faster
  • Linux学习笔记导航页

    本博客中与博主Linux学习相关的博文导航 xff0c 方便查看 Linux系统ls命令详解Linux系统中目录的内容详解 bin dev etc home lib opt usr varLinux操作文件与目录 cp mv mkdir r
  • Jetson TX2使用经验导航页

    本博客中与Jetson TX2使用相关的博文导航 xff0c 方便查看 JetsonTX2 之刷机 Jetpack 4 3TX2 ubuntu 18 04 更换清华镜像源Jetson TX2刷机后查看CUDA和CUDNN版本 以JetPac
  • Pytorch学习导航页

    本博客中与pytorch学习相关的博文 xff0c 方便查看 Pytorch源码学习之一 xff1a torchvision models alexnetPytorch源码学习之二 xff1a torchvision models vggP
  • Python小技巧导航页

    本博客中与Python使用技巧相关的博文 xff0c 方便查看 使用matplotlib绘图库的pyplot快速绘图Python调用face 43 43 API完成本地图片的人脸检测Python爬虫 按照关键词爬取视觉中国高清图像pytho
  • Linux归档与备份——gzip、gunzip、bzip2、bunzip2、tar、zip、unzip、rsync

    维护系统数据安全是计算机系统管理者的基本任务之一 xff0c 及时创建系统文件的备份文件是维度系统数据安全的一种常用方法 本节主要介绍以下命令 文件压缩程序 gzip 压缩和解压缩文件工具bzip2 块排序文件压缩工具 文件归档程序 tar
  • Linux之存储介质——mount、umount、fdisk、mkfs

    本节讨论设备级别的数据处理 对于诸如硬盘之类的物理存储器 网络存储器以及像RAID 独立冗余磁盘陈列 和LVM 逻辑卷管理 之类的虚拟存储器 xff0c Linux都有惊人的处理能力 本节主要用到以下命令 mount 挂载文件系统umoun
  • Jetson TX2挂载SD卡--亲测有效!

    不得不说 xff0c TX2用于深度学习算法的部署 xff0c 一个很大的问题是硬盘容量太小 xff0c 由于我的应用需求需要存储大量数据 xff0c 因此需要挂载一个SD卡 关于Linux挂载存储介质相关原理可参考我的博客 Linux之存
  • 实用的测试流程梳理总结(质量保障)

    废话不多说 xff0c 简明扼要的列出我认为测试最重要的几点 xff1a 1 测试思维 xff1a 优秀的测试思维对case设计的好坏起决定作用 xff0c case的好坏对测试效率和测试质量起决定作用 xff0c 所以测试思维非常重要 我
  • Linux之正则表达式---grep、元字符、任意字符、锚、中括号、否定、POSIX字符类

    正则表达式是一个非常重要的用于文本操作的工具 0 参考文献 Linux命令行大全 美 William E Shotts Jr 著 郭光伟 郝记生 译 xff0c 人民邮电出版社 更多有用的Linux知识详解 xff0c 可参加博主的Linu
  • Linux之文本处理---cat、sort、uniq、cut、paste、join、comm、diff、patch、tr、sed、aspell

    由于所有类UNIX操作系统都严重依赖于文本文件来进行某些数据类型的存储 所以需要很多可以进行文本操作的工具 常见的文本格式有 文件 xff1a 使用纯文本格式编辑的文件 在使用文本格式编辑较大文件时 xff0c 常用的方法是 xff0c 首
  • Linux之编译程序详细介绍---./configure、make、make install

    本节介绍如何通过源代码生成可执行程序 xff0c 在博主前期使用NVIDIA Jetson TX2时 由于Arm架构的各个包不完备 经常需要源码编译OpenCV等 为什么要编译软件呢 xff1f 可用性 尽管有些发行版已经包含了版本库中的一
  • 使用Visual Genome API + python3使用及数据集详情

    Visual Genome数据集 Visual Genome 主页Visual Genome APIVisual Genome Python DriverVisual Genome 论文 注意 xff0c API多为python2的实现 x
  • PIL:Python图像处理类库的基本用法

    span class token keyword from span PIL span class token keyword import span Image span class token keyword import span o

随机推荐