最近在为一个比赛做准备,打算搞个C++和OpenCV相关的。但是我只学了C++,没学过OpenCV。有人能提供点学习OpenCV(C++向的)的教程吗?最好是适合我这种只学过C++,对OpenCV一点都不懂的人的教程。
另外再问一下YoloV5是什么?谢谢大家🙏🙏
都不懂的话不建议看文字教程,直接看视频,B站直接搜索opencv入门,先多看几个,找一个适合你节奏和你觉得可以跟的上的看下去。看完入门差不多有个概念了,后面一些进阶的操作什么的看你自己有没有兴趣学了。
yolov5是一个yolo系列目标检测网络之一,目前从yoloV1到V8都有,各自有一些不同和创新,这里面比较好用的是V5,对新手是友好。
代码地址:
https://github.com/luohenyueji/OpenCV-Practical-Exercisehttps://download.csdn.net/download/luohenyj/11025216
如果没有积分(系统自动设定资源分数)看看参考链接。我搬运过来的,大修改没有。
OpenImage获取Python3代码:
linux:
import csv
import subprocess
import os
runMode = "train"
classes = ["Snowman"]
with open('class-descriptions-boxable.csv', mode='r') as infile:
reader = csv.reader(infile)
dict_list = {rows[1]:rows[0] for rows in reader}
subprocess.run(['rm', '-rf', 'JPEGImages'])
subprocess.run([ 'mkdir', 'JPEGImages'])
subprocess.run(['rm', '-rf', 'labels'])
subprocess.run([ 'mkdir', 'labels'],)
for ind in range(0, len(classes)):
className = classes[ind]
print("Class " + str(ind) + " : " + className)
commandStr = "grep " + dict_list[className] + " " + runMode + "-annotations-bbox.csv"
print(commandStr)
class_annotations = subprocess.run(commandStr.split(), stdout=subprocess.PIPE).stdout.decode('utf-8')
class_annotations = class_annotations.splitlines()
totalNumOfAnnotations = len(class_annotations)
print("Total number of annotations : "+str(totalNumOfAnnotations))
cnt = 0
for line in class_annotations[0:totalNumOfAnnotations]:
cnt = cnt + 1
print("annotation count : " + str(cnt))
lineParts = line.split(',')
subprocess.run([ 'aws', 's3', '--no-sign-request', '--only-show-errors', 'cp', 's3://open-images-dataset/'+runMode+'/'+lineParts[0]+".jpg", 'JPEGImages/'+lineParts[0]+".jpg"])
with open('labels/%s.txt'%(lineParts[0]),'a') as f:
f.write(' '.join([str(ind),str((float(lineParts[5]) + float(lineParts[4]))/2), str((float(lineParts[7]) + float(lineParts[6]))/2), str(float(lineParts[5])-float(lineParts[4])),str(float(lineParts[7])-float(lineParts[6]))])+'\n')
windows:
import csv
import subprocess
import os
#要下的数据集rain,test,valid
runMode = "train"
#类别
classes = ["Snowman"]
with open('class-descriptions-boxable.csv', mode='r') as infile:
reader = csv.reader(infile)
dict_list = {rows[1]:rows[0] for rows in reader}
#删除以前下载的
subprocess.run(['rd', '/s/q', 'JPEGImages'],shell=True)
subprocess.run(['mkdir', 'JPEGImages'],shell=True)
subprocess.run(['rd', '/s/q', 'labels'],shell=True)
subprocess.run(['mkdir', 'labels'],shell=True)
for ind in range(0, len(classes)):
className = classes[ind]
print("Class " + str(ind) + " : " + className)
strs = dict_list[className]
commandStr = "findstr /r "+ '"\<' + strs + '\>"' + " " + runMode + "-annotations-bbox.csv"
class_annotations = subprocess.run(commandStr, stdout=subprocess.PIPE,shell=True).stdout.decode('utf-8')
class_annotations = class_annotations.splitlines()
print(commandStr.split(','))
#多少张图像被下载
totalNumOfAnnotations = len(class_annotations)
print("Total number of annotations : "+str(totalNumOfAnnotations))
cnt = 0
for line in class_annotations[0:totalNumOfAnnotations]:
cnt = cnt + 1
print("annotation count : " + str(cnt))
lineParts = line.split(',')
subprocess.run([ 'aws', 's3', '--no-sign-request', '--only-show-errors', 'cp', 's3://open-images-dataset/'+runMode+'/'+lineParts[0]+".jpg", 'JPEGImages/'+lineParts[0]+".jpg"],shell=True)
with open('labels/%s.txt'%(lineParts[0]),'a') as f:
f.write(' '.join([str(ind),str((float(lineParts[5]) + float(lineParts[4]))/2), str((float(lineParts[7]) + float(lineParts[6]))/2), str(float(lineParts[5])-float(lineParts[4])),str(float(lineParts[7])-float(lineParts[6]))])+'\n')
数据集分割:
import random
import os
import subprocess
import sys
image_dir='/home/your/darknet/OpenImage/JPEGImages'
def split_data_set():
f_val = open("snowman_test.txt", 'w')
f_train = open("snowman_train.txt", 'w')
path, dirs, files = next(os.walk(image_dir))
data_size = len(files)
ind = 0
data_test_size = int(0.1 * data_size)
test_array = random.sample(range(data_size), k=data_test_size)
for f in os.listdir(image_dir):
if(f.split(".")[1] == "jpg"):
ind += 1
if ind in test_array:
f_val.write(image_dir+'/'+f+'\n')
else:
f_train.write(image_dir+'/'+f+'\n')
split_data_set()
draw loss:
import matplotlib.pyplot as plt
log='train.log'
lines = []
for line in open(log):
if "avg" in line:
lines.append(line)
iterations = []
avg_loss = []
print('Retrieving data and plotting training loss graph...')
for i in range(len(lines)):
try:
lineParts = lines[i].split(',')
iterations.append(int(lineParts[0].split(':')[0]))
avg_loss.append(float(lineParts[1].split()[0]))
except:
continue
fig = plt.figure()
for i in range(0, len(lines)):
plt.plot(iterations[i:i+2], avg_loss[i:i+2], 'r.-')
plt.xlabel('Batch Number')
plt.ylabel('Avg Loss')
fig.savefig('training_loss_plot.png', dpi=300)
print('Done! Plot saved as training_loss_plot.png')