技术标签: python 计算机视觉 目标检测 yolov4 opencv 开发语言
我在进行目标检测时候,比如红绿灯检测,目标区域很小,样本杂乱。
想要筛选错误样本的话,很困难。可以把目标区域裁剪出来。人大脑处理对于这样的异己比较敏感。样本量较少的话可以自己筛一筛。样本量较大的话,可以训练一个分类模型帮你筛一下。
它就可以实现一个目标检测的数据集可以转化为一个分类的数据集。
这个是根据txt标签文件进行筛选的,大同小异,别的文件类型也就是登录读取信息不一样。
它最终会读取你目标检测的每一个类,然后每个类会创建一个就叫这个类名的文件夹,然后裁剪出来的这一类的图片。最后图片的文件名就是你的”原文件名+num“。
#根据预测出来的txt文件裁剪图片
import os
import cv2
from tqdm import tqdm
image_input = '/home/xys/CloundShiProjects/traffic_light/trafficlight_dect/data/JPEGImages/'
txt_input = '/home/xys/CloundShiProjects/traffic_light/trafficlight_dect/data/labels/'
path_output = "/home/xys/CloundShiProjects/traffic_light/trafficlight_dect/crop/" # 裁剪出来的小图保存的根目录
class_names_path = '/home/xys/CloundShiProjects/traffic_light/trafficlight_dect/data/classes.txt'
img_total = []
txt_total = []
def read_class_name(path): #读取path下的类别民
f = open(path,'r')
classes_name = []
for i in f.readlines():
classes_name.append(i.strip())
return classes_name
classes_name = read_class_name(class_names_path)
file_image = os.listdir(image_input)
for filename in file_image:#在做jpg文件名列表
first,last = os.path.splitext(filename)
img_total.append(first)
file_txt = os.listdir(txt_input)
for filename in file_txt:#在做txt文件名列表
first,last = os.path.splitext(filename)
txt_total.append(first)
for img_ in tqdm(img_total):
if img_ in txt_total:
filename_img = img_+".jpg"
path1 = os.path.join(image_input,filename_img)
img = cv2.imread(path1)
filename_txt = img_+'.txt' #预测出来的txt文件没有后缀名,有则加 {+".txt"}
h = img.shape[0]
w = img.shape[1]
n = 1
with open(os.path.join(txt_input,filename_txt),"r+",encoding="utf-8",errors="ignore") as f:
for line in f:
aa = line.split(" ")
# if not int(aa[0]) == 0: continue #判断需要裁剪的类别:0--vehicle
x_center = w * float(aa[1]) # aa[1]左上点的x坐标
y_center = h * float(aa[2]) # aa[2]左上点的y坐标
width = int(w*float(aa[3])) # aa[3]图片width
height = int(h*float(aa[4])) # aa[4]图片height
lefttopx = int(x_center-width/2.0)
lefttopy = int(y_center-height/2.0)
roi = img[lefttopy+1:lefttopy+height+3,lefttopx+1:lefttopx+width+1] # [左上y:右下y,左上x:右下x]
# (y1:y2,x1:x2)需要调参,否则裁剪出来的小图可能不太好
if roi.size == 0: continue
filename_last = img_+"_"+str(n)+".jpg" # 裁剪出来的小图文件名
x = int(aa[0])
path2 = os.path.join(path_output,classes_name[x]) # 需要在path_output路径下创建一个cut_txt文件夹
if not os.path.exists(path2):
os.mkdir(path2)
# print('path2:', path2) # 裁剪小图的保存位置
cv2.imwrite(os.path.join(path2,filename_last),roi)
n = n+1
else:
continue
希望可以帮到你们!
【单选题】下面关于Java语言特点的描述中,错误的是()。【单选题】下面()是合法标识符。【单选题】编译Java程序后生成的面向JVM的字节码文件的扩展名是【填空题】一个try代码段后面必须跟着若干个( )代码段或者一个( )代码段。【填空题】Java规定,如果子类中定义的成员方法与父类中定义的成员方法同名,并且参数的( )和...
http://blog.sina.com.cn/s/blog_4b9ee9e501011xx1.html 1、QMouseEvent中的坐标QMouseEvent中保存了两个坐标,一个是全局坐标,当然另外一个是局部坐标。全局坐标(globalPos())即是桌面屏幕坐标(screen coordinates),这个跟windows下的调用getCursorPos函数得到的结果一致。
http://www.codeceo.com/article/android-glide-usage.html在图片加载库烂大街的今天,选择一个适合自己使用的图片加载库已经成为了每一个Android开发者的必经之路。现在市面上知名的图片加载库有UIL,Picasso,Volley ImageLoader,Fresco以及我们今天的主角Glide。它们各有千秋,不能评定谁一定比谁好,
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char a[4][100]; int i;//第i个字符串 int size_1, size_2, size_3, size_4; int size_m...
Java学习路线图的框架部分分为两个阶段,第一阶段的Java框架包含六个内容:MyBatis,Spring,SpringMVC,Maven高级,Git,Dubbo。在Java学习路线图中掌握框架的使用,对企业项目的构建有着关键性的作用。Java框架规定了应用体系结构,构成了某类特定软件的可复用设计。学完这一部分的Java框架后,可以使Java开发人员只关注软件的业务功能,这是迈向中级程序员的重要一...
conda安装tensorflow教程,亲测成功!1、打开Anaconda Prompt,输入命令:conda create -n "conda环境名” python=3.6记住一定要选择3.6的python版本,亲测最稳定的版本!2、激活子环境,输入命令:conda activate "刚刚创建的环境名"3、设置镜像源,加快下载速度conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/clo
代码:import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Handler;import android.os.Message;import android.util.AttributeS
opencv3.0 xphoto模块包含了简单白平衡算法。该sua
E8 开发资料大全 E9 开发资料大全
MX Player - Docking of Battle Games (双人对战游戏接入)1. Game Init (游戏初始化)After the game is loaded, execute it in the entry class: (游戏加载完成后在入口类中执行)onGameInit();onGameStart();/** * !#en Game initialization * !#zh 游戏初始化 */export let onGameInit = function
1 abm activity-based management 基于作业活动管理 2 ao application outsourcing 应用程序外包 3 apics american production and inventory c society,inc 美国生产与库存管理协会 4 apics applied manufacturin...
测试使用路径图实现代码#!/usr/bin/env python# -*- coding: utf-8 -*-"""Information: @author : enrico @contact : [email protected] @Site : @software : PyCharm @file : Dijkstra.py @time : ...