python调用摄像头拍摄数据集

yipeiwu_com6年前Python基础

之前需要做一些目标检测的训练,需要自己采集一些数据集,写了一个小demo来实现图片的采集

使用方法:

  • 指定name的名称,name为分类的标签
  • 按n键拍摄图片
  • 程序会在当前目录下生成一个pictures的文件夹,图片存放在其中
print("正在初始化摄像头...")
import cv2
import os
import datetime
cap = cv2.VideoCapture(0)
print("初始化成功!")
 
# name='play_phone'
# name='haqian'
# name='spleeing'
# name='zhengchang'
# name="zhedang"
name="waitou"
 
savedpath =r'./pictures/'+name
isExists = os.path.exists(savedpath)
if not isExists:
  os.makedirs(savedpath)
  print('path of %s is build' % (savedpath))
else:
  print('path of %s already exist and rebuild' % (savedpath))
print("按N键拍摄图片")
i=0
while(True):
  ret, frame = cap.read()
  gray = cv2.cvtColor(frame, 1)
  cv2.imshow('test',frame)
  now = datetime.datetime.now()
  now = now.strftime('%m-%d-%H-%M-%S')
  savedname = '/'+name+ '_' + str(i) + '_{0}' '.jpg'.format(now)
  if cv2.waitKey(1) & 0xFF == ord('n'):  #按N拍摄
    i += 1
    cv2.imwrite(savedpath + savedname, frame)
    cv2.namedWindow("Image")
    cv2.imshow("Image", frame)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
 
cap.release()
cv2.destroyAllWindows()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python如何派生内置不可变类型并修改实例化行为

本文实例为大家分享了python派生内置不可变类型并修改实例化行为的具体代码,供大家参考,具体内容如下 案例:   我们想要自定义新类型的元组,对传入的可迭代对象我们只保留其中的int类...

浅谈python中对于json写入txt文件的编码问题

最近一直在研究python+selenium+beautifulsoup的爬虫,但是存入数据库还有写入txt文件里面的时候一直都是unicode编码的格式。 接下来就是各种翻阅文档,查找...

Numpy对数组的操作:创建、变形(升降维等)、计算、取值、复制、分割、合并

1. 简介 NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。最主要的数据结...

python list 合并连接字符串的方法

比如下面一个list复制代码 代码如下:binfo = ['lao','wang','python']我们通过help方法得知,可以用string的join方法来解决。 下面我们通过空格...

Python2和Python3中urllib库中urlencode的使用注意事项

前言 在Python中,我们通常使用urllib中的urlencode方法将字典编码,用于提交数据给url等操作,但是在Python2和Python3中urllib模块中所提供的urle...