网络编程
位置:首页>> 网络编程>> Python编程>> python调用摄像头拍摄数据集

python调用摄像头拍摄数据集

作者:天上飞下一毛雪  发布时间:2021-08-12 10:25:22 

标签: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()

来源:https://blog.csdn.net/qq_39622065/article/details/88594271

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com