基于u版Yolo v3
detect_save_crop.py

import argparse
from sys import platformfrom models import *  # set ONNX_EXPORT in models.py
from utils.datasets import *
from utils.utils import *def detect(save_txt=True, save_img=False):img_size = (320, 192) if ONNX_EXPORT else opt.img_size  # (320, 192) or (416, 256) or (608, 352) for (height, width)out, source, weights, half, view_img = opt.output, opt.source, opt.weights, opt.half, opt.view_imgwebcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt')# Initializedevice = torch_utils.select_device(device='cpu' if ONNX_EXPORT else opt.device)if os.path.exists(out):shutil.rmtree(out)  # delete output folderos.makedirs(out)  # make new output folder# Initialize modelmodel = Darknet(opt.cfg, img_size)# Load weightsattempt_download(weights)if weights.endswith('.pt'):  # pytorch formatmodel.load_state_dict(torch.load(weights, map_location=device)['model'])else:  # darknet format_ = load_darknet_weights(model, weights)# Fuse Conv2d + BatchNorm2d layers# model.fuse()# Eval modemodel.to(device).eval()# Export modeif ONNX_EXPORT:img = torch.zeros((1, 3) + img_size)  # (1, 3, 320, 192)torch.onnx.export(model, img, 'weights/export.onnx', verbose=True)return# Half precisionhalf = half and device.type != 'cpu'  # half precision only supported on CUDAif half:model.half()# Set Dataloadervid_path, vid_writer = None, Noneif webcam:view_img = Truetorch.backends.cudnn.benchmark = True  # set True to speed up constant image size inferencedataset = LoadStreams(source, img_size=img_size, half=half)else:save_img = Truedataset = LoadImages(source, img_size=img_size, half=half)# Get classes and colorsclasses = load_classes(parse_data_cfg(opt.data)['names'])colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(classes))]# Run inferencet0 = time.time()with open( 'xyxy_cls_loose_nut.txt', 'w') as file:   #保存坐标的txt文件for path, img, im0s, vid_cap in dataset:t = time.time()# Get detectionsimg = torch.from_numpy(img).to(device)if img.ndimension() == 3:img = img.unsqueeze(0)pred, _ = model(img)if opt.half:pred = pred.float()for i, det in enumerate(non_max_suppression(pred, opt.conf_thres, opt.nms_thres)):  # detections per imageif webcam:  # batch_size >= 1p, s, im0 = path[i], '%g: ' % i, im0s[i]else:p, s, im0 = path, '', im0ssave_path = str(Path(out) / Path(p).name)s += '%gx%g ' % img.shape[2:]  # print stringif det is not None and len(det):# Rescale boxes from img_size to im0 sizedet[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()# Print resultsfor c in det[:, -1].unique():n = (det[:, -1] == c).sum()  # detections per classs += '%g %ss, ' % (n, classes[int(c)])  # add to string# Write resultsfor *xyxy, conf, _, cls in det:if save_txt:  # Write to file# with open(save_path + '.txt', 'a') as file:# with open( 'crop_xyxy.txt', 'a') as file:file.write(('%g ' * 6 + '\n') % (*xyxy, cls, int(path[18:-4])))     #保存检测坐标、类别和图片名if save_img or view_img:  # Add bbox to imagelabel = '%s %.2f' % (classes[int(cls)], conf)plot_one_box(xyxy, im0, label=label, color=colors[int(cls)])print('%sDone. (%.3fs)' % (s, time.time() - t))# Stream resultsif view_img:cv2.imshow(p, im0)# Save results (image with detections)if save_img:if dataset.mode == 'images':cv2.imwrite(save_path, im0)else:if vid_path != save_path:  # new videovid_path = save_pathif isinstance(vid_writer, cv2.VideoWriter):vid_writer.release()  # release previous video writerfps = vid_cap.get(cv2.CAP_PROP_FPS)w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*opt.fourcc), fps, (w, h))vid_writer.write(im0)if save_txt or save_img:print('Results saved to %s' % os.getcwd() + os.sep + out)if platform == 'darwin':  # MacOSos.system('open ' + out + ' ' + save_path)print('Done. (%.3fs)' % (time.time() - t0))if __name__ == '__main__':parser = argparse.ArgumentParser()parser.add_argument('--cfg', type=str, default='cfg/yolov3-spp.cfg', help='cfg file path')parser.add_argument('--data', type=str, default='data/coco.data', help='coco.data file path')parser.add_argument('--weights', type=str, default='weights/yolov3-spp.weights', help='path to weights file')parser.add_argument('--source', type=str, default='data/samples', help='source')  # input file/folder, 0 for webcamparser.add_argument('--output', type=str, default='output', help='output folder')  # output folderparser.add_argument('--img-size', type=int, default=416, help='inference size (pixels)')parser.add_argument('--conf-thres', type=float, default=0.3, help='object confidence threshold')parser.add_argument('--nms-thres', type=float, default=0.5, help='iou threshold for non-maximum suppression')parser.add_argument('--fourcc', type=str, default='mp4v', help='output video codec (verify ffmpeg support)')parser.add_argument('--half', action='store_true', help='half precision FP16 inference')parser.add_argument('--device', default='', help='device id (i.e. 0 or 0,1) or cpu')parser.add_argument('--view-img', action='store_true', help='display results')opt = parser.parse_args()print(opt)with torch.no_grad():detect()

运行:
python detect_save_crop.py --cfg cfg/yolov3_rail.cfg --data data/rail.data --weights base130.weights --source data/ub/loose_nut/ --device 2
其中source为批量测试图片所在的文件夹,将检测出来的坐标写入txt文件夹。需要修改适配自己的图片文件名,即int(path[18:-4]),我这取的是18代表path路径的data/ub/loose_nut/,-4代表省去图片后缀 .jpg。最后生成的txt文件类似838 650 1323 861 0 157 ,前四个数字为xyxy坐标。第5个为类别,第6为图片名。

新建crop_loose_nut.py

import os
import cv2
# import numpy as np
# from PIL import Image
# import matplotlib.pyplot as plt
import shutildef save(cropImg, framenum, tracker):  crop_img = pathnew + framenum + '_' + tracker + '.jpg'if (os.path.exists(crop_img)):cv2.imwrite(pathnew + framenum + '_' + tracker + '(1).jpg', cropImg, [int(cv2.IMWRITE_JPEG_QUALITY), 100])else:cv2.imwrite(crop_img, cropImg, [int(cv2.IMWRITE_JPEG_QUALITY), 100])# if (os.path.exists(pathnew)):#     cv2.imwrite(pathnew + framenum + '_' + tracker + '.jpg', cropImg, [int(cv2.IMWRITE_JPEG_QUALITY), 100])# else:#     os.makedirs(pathnew)#      cv2.imwrite(pathnew + framenum + '_' + tracker + '.jpg', cropImg, [int(cv2.IMWRITE_JPEG_QUALITY), 100])f = open("xyxy_cls_loose_nut.txt", "r")   #标签txt文件
lines = f.readlines()
print(len(lines))
pathnew = "./crop_loose_nut/"  #裁剪后文件夹
if os.path.exists(pathnew):shutil.rmtree(pathnew)  # delete output folder
os.makedirs(pathnew)  # make new output folder
for line in lines:print(line)li = line.split(' ')# print(li[0], li[1], li[2], li[3],)  #根据自己的txt内容格式进行修改filename = li[5] + '.jpg'# img = cv2.imread("./data/ub/te") #原始图片文件夹img = cv2.imread("./data/ub/loose_nut/" + filename)a = max(0, int(float(li[0])) - 2) # xmin b = int(float(li[2])) + 2 # xmaxc = int(float(li[1])) - 2 # ymin d = int(float(li[3])) + 2 # ymax cropImg = img[c:d, a:b]  # 裁剪# cv2.imwrite("1_1.jpg", cropImg)# save(cropImg, li[0], str(li[1]).split('.')[1][:3])  #保留小数save(cropImg, li[5], li[4])

注:这里在abcd中我多设置了2个像素,主要是防止检测框未完全盖住目标物体。因此人为多加了2像素。

resize+pad
resize4.py

import cv2
import os
import shutildef imgToSize(img,size):''' imgToSize()# ----------------------------------------# Function:   将图像等比例缩放到 512x512 大小#             根据图像长宽不同分为两种缩放方式# Param img:  图像 Mat# Return img: 返回缩放后的图片# Example:    img = imgToSize(img)# ----------------------------------------'''# 测试点# cv2.imshow('metaImg.jpg', img)imgHeight, imgWidth = img.shape[:2]# cv.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])# src 原图像,dsize 输出图像的大小,# img = cv2.resize(img, (512,512))zoomHeight = sizezoomWidth = int(imgWidth*size/imgHeight)img = cv2.resize(img, (zoomWidth,zoomHeight))# 测试点# cv2.imshow('resizeImg', img)# 如果图片属于 Width<Height,那么宽度将达不到 512if imgWidth >= imgHeight:# 正常截取图像w1 = (zoomWidth-size)//2# 图像坐标为先 Height,后 Widthimg = img[0:size, w1:w1+size]else:# 如果宽度小于 512,那么对两侧边界填充为全黑色# 根据图像的边界的像素值,向外扩充图片,每个方向扩充50个像素,常数填充:# dst = cv2.copyMakeBorder(src, top, bottom, left, right, borderType[, dst[, value]])# dst = cv2.copyMakeBorder(img,50,50,50,50, cv2.BORDER_CONSTANT,value=[0,255,0])# 需要填充的宽度为 512-zoomWidthleft = (size-zoomWidth)//2# 避免余数取不到right = left+1img = cv2.copyMakeBorder(img, 0,0,left,right, cv2.BORDER_CONSTANT, value=[0,0,0])img = img[0:size, 0:size]# 测试点# cv2.imshow('size512', img)return imgimg_file = './crop_loose_nut_1'
out_file = "./resize_loose_nut/"
size = 256
if os.path.exists(out_file):shutil.rmtree(out_file)  # delete output folder
os.makedirs(out_file)  # make new output folder
for file in os.listdir(img_file):in_file  = img_file + '/' + fileout_file = "./resize_loose_nut"print(in_file)out_file = out_file + '/'+ fileprint(out_file)img = cv2.imread(in_file, 0)img = imgToSize(img, size)print(img.shape)cv2.imwrite(out_file, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
# img = cv2.imread('3_1.jpg', 0)
# img = imgToSize(img)
# cv2.imwrite('3_1_pad.jpg', img)

最后便可得到指定尺寸的图片且不变形。
还有一个需要注意的便是保存为jpg格式,即使你设置为100的quality,但还是有损压缩,如果想无损的话,建议改成png格式。

yolo测试图片批量裁剪+pad指定尺寸相关推荐

  1. 这个方法,可以实现图片批量裁剪尺寸

    大家在日常的生活或工作中,有没有遇到需要将大量图片进行裁剪的情况?一般面对这种难题,你们是选择如何去应对的呢?估计有大部分小伙伴选择最传统的方法,就是对图片一张一张的进行手动修改,但这也太费时费力了! ...

  2. python批量压缩图片_Python图片批量压缩到指定大小并将JPG转为PNG格式

    背景: 待压缩的图片大小有几十KB到近10M大小不等,且绝大部分图片为JPG格式.这些待压缩图片放在picture文件夹下 以及picture文件夹下的子文件夹中 现需要将picture文件夹下这些图 ...

  3. java批量上传图片源码_java 图片批量裁剪 源代码

    java 图片批量裁剪 源代码 package com.frank.dx; import java.awt.Image; import java.awt.Rectangle; import java. ...

  4. Python图片批量压缩到指定大小并将JPG转为PNG格式

    背景: 待压缩的图片大小有几十KB到近10M大小不等,且绝大部分图片为JPG格式.这些待压缩图片放在picture文件夹下 以及picture文件夹下的子文件夹中 现需要将picture文件夹下这些图 ...

  5. matplotlib显示图片与图片批量裁剪

    1.显示图片 import matplotlib.pyplot as plt # plt 用于显示图片 import matplotlib.image as mpimg # mpimg 用于读取图片 ...

  6. 微信小程序中裁剪图片以及压缩到指定尺寸并上传

    本文分为两个内容,分别是裁剪图片和压缩 引出问题 1.为何要裁剪图片 因为需要上传头像,但是每个型号的手机拍出来的照片尺寸都不太一样,不能统一,所以,希望在上传之前进行自主裁剪,保证上传到服务器上的尺 ...

  7. 将图片处理成圆形_如何把图片批量处理成指定/固定的文件大小/体积以内?

    这里介绍的是类似这样的需求: 需要把一组图片批量处理成 宽度1000px,大小在150kb以内的jpeg格式. 01.打开工具链接 webp2jpg-online-在线图片格式转化器(无需上传)-we ...

  8. python图片批量裁剪

    测试中对图片有的地方对大小有要求,有的需要裁剪边缘,花了一点时间做了个图片的批量裁剪,同时也学习了python对文件的简单操作 #coding=utf-8 # # 测试需要,裁剪图片,使用之前学过的技 ...

  9. 【R语言】对图片进行裁剪 图片批量裁剪

    对图片进行裁剪 / 批量裁剪 示例如图 # 对图片进行裁剪 library(magick) pic <- image_read('study.jpg') plot(pic) print(pic) ...

最新文章

  1. amazeui页面分析5
  2. Python正则表达式之编译正则表达式(2)
  3. C语言用数组(顺序表)实现大小固定的队列的方法
  4. html5基础知识点制作图片
  5. yii mysql 操作数据库_YII2中操作数据库的方式
  6. 折扇的保养方法是什么?
  7. NSIS:强制结束软件进程
  8. Oracle 安装教程
  9. java实现七种经典排序算法
  10. 云服务器搭建java服务器运行环境
  11. DVR,DVS,NVR,NVS 的区别
  12. 数据集:CIFAR-10、CIFAR-100、MNIST、SVHN、ImageNet、LSUN
  13. Android——打电话(选择指定手机卡)、发短信
  14. jmeter结果树为空_Jmeter查看结果树之查看响应的13种方法[详解]
  15. 零碎知识点之二:循环平稳信号
  16. CGTrader新赛CG Wildlife Challenge(CG野生生物竞赛)重磅推出
  17. 怎么让照片里的人嘴巴动起来_让嘴巴动起来的制作方法
  18. sql查询每科成绩的最高分_SQL查询每科分数最高的
  19. Cadence Allegro BGA扇出设置
  20. 【论文阅读】CVPR 2020:GaitPart: Temporal Part-based Model for Gait Recognition

热门文章

  1. Python机器学习基础之Matplotlib库的使用
  2. 电力系统导纳矩阵与短路计算matlab程序
  3. MYSQL数据库事务的隔离级别(详解)
  4. 【Redis学习笔记】13.Redis 主从复制
  5. java输入Scanner基本用法
  6. 前端之移动web开发学习
  7. 软件系统测试方法方案,软件测试方法及系统技术方案
  8. 云呐|固定资产管理系统功能包括哪些?
  9. jsp 网站 mysql 数据库连接 tomcat 服务器配置server
  10. 揭秘魅族日志分析平台的建设