货品目标检测样本制作方法
最近公司要做无人超市项目,基本实现方式是通过摄像头检测货柜里面有多少个什么类别的货品,也就是目标检测算法。该方法所需数据样本为标记了位置的货品图片,如下所示。
为了制作该图片,设计方法如下:
1、拍摄该样品360度的视频
2、将视频抓帧
3、将图片通过opencv拼装成样本
拍摄视频如下并抓帧的程序如下:
import os
import cv2
path = "F:/objectdect/avi/"def openavifiletopic(filepath,index):cap = cv2.VideoCapture(filepath)if cap.isOpened(): # 判断是否正常打开rval, frame = cap.read()else:rval = Falsec = 0num = 0while rval: # 循环读取视频帧rval, frame = cap.read()if (c % 10 == 0):filesavepath = path + str(index) + "/" + str(num) + ".jpg"cv2.imwrite(filesavepath, frame) # 存储为图像num = num + 1c = c + 1cap.release()for index in range (1,21):pathavi = path + str(index)rootdir = pathavilist = os.listdir(rootdir) # 列出文件夹下所有的目录与文件for i in range(0, len(list)):filepath = os.path.join(rootdir, list[i])if os.path.isfile(filepath):print(list[i])filepath = pathavi + "/" + str(list[i])# 你想对文件的操作openavifiletopic(filepath,index)生成样本的程序如下:
import random
import cv2
path = "F:/objectdect/all"
for index in range(0,10001):filepath = path + "/" + str(index) + ".jpg"#产生一个1到5的随机数numlist = [1,2,4]rndnum = random.sample(numlist, 1)[0]imglist = []for numindex in range(1,rndnum+1):#print(numindex)#随机取出一个文件夹中的一个图片classnum = random.randint(1, 20)jpgpath = "F:/objectdect/avi/" + str(classnum) + "/" + str(random.randint(0, 200))+".jpg"imgtemp = cv2.imread(jpgpath)imgtemp = cv2.resize(imgtemp, (500, 500), interpolation=cv2.INTER_CUBIC)imglist.append(imgtemp)try:print(rndnum)if rndnum == 1:imgall = cv2.hconcat(imglist)res = cv2.resize(imgall, (500, 500), interpolation=cv2.INTER_CUBIC)cv2.imwrite(filepath, res) # 存储为图像if rndnum == 2:imgall = cv2.hconcat([imglist[0],imglist[1]])res = cv2.resize(imgall, (500, 500), interpolation=cv2.INTER_CUBIC)cv2.imwrite(filepath, res) # 存储为图像if rndnum == 4:imgall1 = cv2.hconcat([imglist[0],imglist[1]])imgall2 = cv2.hconcat([imglist[2], imglist[3]])imgall = cv2.vconcat([imgall1, imgall2])res = cv2.resize(imgall, (500, 500), interpolation=cv2.INTER_CUBIC)cv2.imwrite(filepath, res) # 存储为图像except:print("错误") 该程序保存了样本图片之后可以通过 voc数据集标记工具 生成对应的xml文件 即可开始目标检测算法的训练。
http://blog.csdn.net/hx921123/article/details/56484876
转载于:https://blog.51cto.com/yixianwei/2086358
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
