这篇文章主要介绍“python如何通过按键采集图片源码”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“python如何通过按键采集图片源码”文章能帮助大家解决问题。

一、python版本功能说明

“N” 新建文件夹 data/ 用来存储图像
“S” 开始采集图像,将采集到的图像放到 data/ 路径下
“Q” 退出窗口

python opencv源码

'''“N”新建文件夹data/用来存储图像"S"开始采集图像,将采集到的图像放到data/路径下“Q”退出窗口'''importnumpyasnp#数据处理的库Numpyimportcv2#图像处理的库OpenCvimportos#读写文件importshutil#读写文件fromPILimportImage,ImageDraw,ImageFont##OpenCv调用摄像头/Usecameracap=cv2.VideoCapture(0)cap.set(cv2.CAP_PROP_FRAME_WIDTH,1920)cap.set(cv2.CAP_PROP_FRAME_HEIGHT,1080)'''#功能函数,只是用来往图片中显示汉字#示例img=cv2ImgAddText(cv2.imread('img1.jpg'),"大家好,我是片天边的云彩",10,65,(0,0,139),20)参数说明:img:OpenCV图片格式的图片text:要写入的汉字left:字符坐标x值top:字符坐标y值textColor:字体颜色:textSize:字体大小'''defcv2ImgAddText(img,text,left,top,textColor=(0,255,0),textSize=20):if(isinstance(img,np.ndarray)):#判断是否OpenCV图片类型img=Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))#创建一个可以在给定图像上绘图的对象draw=ImageDraw.Draw(img)#字体的格式fontStyle=ImageFont.truetype("font/simsun.ttc",textSize,encoding="utf-8")#绘制文本draw.text((left,top),text,textColor,font=fontStyle)#转换回OpenCV格式returncv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)#存储图像的文件夹current_dir=""#保存图像的路径path_photos_from_camera="data/"press_n_flag=0cnt_ss=0whilecap.isOpened():flag,img_rd=cap.read()#print(img_rd.shape)kk=cv2.waitKey(2)#待会要写的字体/Fonttowritefont=cv2.FONT_ITALIC#4.按下'n'新建存储人脸的文件夹/press'n'tocreatethefoldersforsavingfacesifkk==ord('N')orkk==ord('n'):current_dir=path_photos_from_camera#os.makedirs(current_dir)ifos.path.isdir(current_dir):passelse:os.mkdir(current_dir)print('')print("新建的保存图像的文件夹/Createfolders:",current_dir)press_n_flag=1#已经按下'n'/havepressed'n'#5.按下's'保存摄像头中的图像到本地/Press's'tosaveimageintolocalimagesifkk==ord('S')orkk==ord('s'):#检查有没有先按'n'新建文件夹/checkifyouhavepressed'n'ifpress_n_flag:cnt_ss+=1cv2.imwrite(current_dir+"/img_"+str(cnt_ss)+".jpg",img_rd)print("写入本地/Saveinto:",str(current_dir)+"/img_face_"+str(cnt_ss)+".jpg")else:print("请在按'S'之前先按'N'来建文件夹/Pleasepress'N'before'S'")#添加说明/Addsomestatements#cv2.putText(img_rd,"FaceRegister",(20,40),font,1,(0,255,0),1,cv2.LINE_AA)img_rd=cv2ImgAddText(img_rd,"图片采集系统",160,25,(0,255,0),30)#cv2.putText(img_rd,"N:Createfacefolder",(20,350),font,0.8,(0,255,0),1,cv2.LINE_AA)img_rd=cv2ImgAddText(img_rd,"N:创建保存图像文件夹",20,350,(0,255,0),20)#cv2.putText(img_rd,"S:Savecurrentface",(20,400),font,0.8,(0,255,0),1,cv2.LINE_AA)img_rd=cv2ImgAddText(img_rd,"S:保存当前图片",20,400,(0,255,0),20)#cv2.putText(img_rd,"Q:Quit",(20,450),font,0.8,(0,0,0),1,cv2.LINE_AA)img_rd=cv2ImgAddText(img_rd,"Q:退出",20,450,(0,255,0),20)#6.按下'Q'键退出/Press'q'toexitifkk==ord('Q')orkk==ord('q'):break#如果需要摄像头窗口大小可调/Uncommentthislineifyouwantthecamerawindowisresizeablecv2.namedWindow("camera",0)cv2.imshow("camera",img_rd)#释放摄像头/Releasecameraanddestroyallwindowscap.release()cv2.destroyAllWindows()效果图


安装相关库

windows安装

pipinstallpillow

tx2/linux/…

sudoapt-getinstallpython3-pillow二、c语言版本c语言源码

/*****************************************************2021.5.18:按键采集图像******************************************************/#include"opencv2/core/core.hpp"#include"opencv2/imgproc/imgproc.hpp"#include"opencv2/calib3d/calib3d.hpp"#include"opencv2/highgui/highgui.hpp"#include<iostream>#include<fstream>usingnamespacecv;usingnamespacestd;#defineSRC_WIDTH1920#defineSRC_HEIGHT1080intmain(){//测试视频VideoCapturecapture;capture.open(1);//capture.open("v4l2srcdevice=/dev/video4!video/x-raw,width=1920,height=1020,framerate=30/1!videoconvert!appsink");if(!capture.isOpened()){printf("文件打开失败");}capture.set(CAP_PROP_FRAME_WIDTH,SRC_WIDTH);//设置宽度capture.set(CAP_PROP_FRAME_HEIGHT,SRC_HEIGHT);//设置长度Matframe;intn=0;char*cstr=newchar[120];while(true){capture>>frame;if(frame.data==NULL){printf("Imageisempty");//writer.write(frame);break;//continue;}charkk=waitKey(2);if(kk=='S'||kk=='s'){sprintf(cstr,"%s%d%s","caliberation/",n++,".jpg");imwrite(cstr,frame);printf("保存了图片");}namedWindow("111",0);//参数为零,则可以自由拖动imshow("111",frame);waitKey(2);}return0;}

关于“python如何通过按键采集图片源码”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。