之前需要做一些目标检测的训练,需要自己采集一些数据集,写了一个小demo来实现图片的采集
使用方法:
- 指定name的名称,name为分类的标签
- 按n键拍摄图片
- 程序会在当前目录下生成一个pictures的文件夹,图片存放在其中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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