python opencv通過按鍵采集圖片源碼
一、python版本
寫了個(gè)python opencv的小demo,可以通過鍵盤按下字母s進(jìn)行采集圖像。
功能說明
“N” 新建文件夾 data/ 用來存儲(chǔ)圖像
“S” 開始采集圖像,將采集到的圖像放到 data/ 路徑下
“Q” 退出窗口
python opencv源碼
''' “N” 新建文件夾 data/ 用來存儲(chǔ)圖像 "S" 開始采集圖像,將采集到的圖像放到 data/ 路徑下 “Q” 退出窗口 ''' import numpy as np # 數(shù)據(jù)處理的庫 Numpy import cv2 # 圖像處理的庫 OpenCv import os # 讀寫文件 import shutil # 讀寫文件 from PIL import Image, ImageDraw, ImageFont # # OpenCv 調(diào)用攝像頭 / Use camera cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH,1920) cap.set(cv2.CAP_PROP_FRAME_HEIGHT,1080) ''' #功能函數(shù),只是用來往圖片中顯示漢字 #示例 img = cv2ImgAddText(cv2.imread('img1.jpg'), "大家好,我是片天邊的云彩", 10, 65, (0, 0, 139), 20) 參數(shù)說明: img:OpenCV圖片格式的圖片 text:要寫入的漢字 left:字符坐標(biāo)x值 top:字符坐標(biāo)y值 textColor:字體顏色 :textSize:字體大小 ''' def cv2ImgAddText(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)) # 創(chuàng)建一個(gè)可以在給定圖像上繪圖的對(duì)象 draw = ImageDraw.Draw(img) # 字體的格式 fontStyle = ImageFont.truetype( "font/simsun.ttc", textSize, encoding="utf-8") # 繪制文本 draw.text((left, top), text, textColor, font=fontStyle) # 轉(zhuǎn)換回OpenCV格式 return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) # 存儲(chǔ)圖像的文件夾 current_dir = "" # 保存 圖像 的路徑 path_photos_from_camera = "data/" press_n_flag = 0 cnt_ss=0 while cap.isOpened(): flag, img_rd = cap.read() #print(img_rd.shape) kk = cv2.waitKey(2) # 待會(huì)要寫的字體 / Font to write font = cv2.FONT_ITALIC # 4. 按下 'n' 新建存儲(chǔ)人臉的文件夾 / press 'n' to create the folders for saving faces if kk == ord('N') or kk == ord('n'): current_dir = path_photos_from_camera #os.makedirs(current_dir) if os.path.isdir(current_dir): pass else: os.mkdir(current_dir) print('\n') print("新建的保存圖像的文件夾 / Create folders: ", current_dir) press_n_flag = 1 # 已經(jīng)按下 'n' / have pressed 'n' # 5. 按下 's' 保存攝像頭中的圖像到本地 / Press 's' to save image into local images if kk == ord('S') or kk == ord('s'): # 檢查有沒有先按'n'新建文件夾 / check if you have pressed 'n' if press_n_flag: cnt_ss += 1 cv2.imwrite(current_dir + "/img_" + str(cnt_ss) + ".jpg", img_rd) print("寫入本地 / Save into:", str(current_dir) + "/img_face_" + str(cnt_ss) + ".jpg") else: print("請(qǐng)?jiān)诎?'S' 之前先按 'N' 來建文件夾 / Please press 'N' before 'S'") # 添加說明 / Add some statements #cv2.putText(img_rd, "Face Register", (20, 40), font, 1, (0, 255, 0), 1, cv2.LINE_AA) img_rd = cv2ImgAddText(img_rd, "圖片采集系統(tǒng)", 160, 25, (0, 255,0), 30) #cv2.putText(img_rd, "N: Create face folder", (20, 350), font, 0.8, (0, 255, 0), 1, cv2.LINE_AA) img_rd = cv2ImgAddText(img_rd, "N: 創(chuàng)建保存圖像文件夾", 20, 350, (0, 255, 0), 20) #cv2.putText(img_rd, "S: Save current face", (20, 400), font, 0.8, (0, 255, 0), 1, cv2.LINE_AA) img_rd = cv2ImgAddText(img_rd, "S: 保存當(dāng)前圖片", 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' to exit if kk == ord('Q') or kk == ord('q'): break # 如果需要攝像頭窗口大小可調(diào) / Uncomment this line if you want the camera window is resizeable cv2.namedWindow("camera", 0) cv2.imshow("camera", img_rd) # 釋放攝像頭 / Release camera and destroy all windows cap.release() cv2.destroyAllWindows()
效果圖
安裝相關(guān)庫
windows安裝
pip install pillow
tx2/linux/…
sudo apt-get install python3-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> using namespace cv; using namespace std; #define SRC_WIDTH 1920 #define SRC_HEIGHT 1080 int main() { //測(cè)試視頻 VideoCapture capture; capture.open(1); //capture.open("v4l2src device=/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); //設(shè)置寬度 capture.set(CAP_PROP_FRAME_HEIGHT, SRC_HEIGHT); //設(shè)置長(zhǎng)度 Mat frame; int n = 0; char* cstr = new char[120]; while (true) { capture >> frame; if (frame.data == NULL) { printf("Image is empty\n"); //writer.write(frame); break; //continue; } char kk=waitKey(2); if (kk == 'S' || kk == 's') { sprintf(cstr, "%s%d%s", "caliberation/", n++, ".jpg"); imwrite(cstr, frame); printf("保存了圖片\n"); } namedWindow("111", 0);//參數(shù)為零,則可以自由拖動(dòng) imshow("111", frame); waitKey(2); } return 0; }
效果圖
到此這篇關(guān)于opencv通過按鍵采集圖片源碼的文章就介紹到這了,更多相關(guān)opencv按鍵采集圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pycharm遠(yuǎn)程連接服務(wù)器并配置python interpreter的方法
這篇文章主要介紹了pycharm遠(yuǎn)程連接服務(wù)器并配置python interpreter的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12pyinstaller打包多個(gè)py文件和去除cmd黑框的方法
今天小編就為大家分享一篇pyinstaller打包多個(gè)py文件和去除cmd黑框的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06使用memory_profiler監(jiān)測(cè)python代碼運(yùn)行時(shí)內(nèi)存消耗方法
今天小編就為大家分享一篇使用memory_profiler監(jiān)測(cè)python代碼運(yùn)行時(shí)內(nèi)存消耗方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12如何使用Python JSON解析和轉(zhuǎn)換數(shù)據(jù)
JSON 是文本,使用 JavaScript 對(duì)象表示法編寫,Python 有一個(gè)內(nèi)置的 json 包,可用于處理 JSON 數(shù)據(jù),本文給大家介紹使用Python JSON解析和轉(zhuǎn)換數(shù)據(jù)的方法,感興趣的朋友跟隨小編一起看看吧2023-11-11跟老齊學(xué)Python之從格式化表達(dá)式到方法
上一講,主要介紹了用%表達(dá)的一種輸出格式化表達(dá)式。在那一講最后又拓展了一點(diǎn)東西,拓展的那點(diǎn),名曰:格式化方法。因?yàn)樗R(shí)上是使用了str的format方法。2014-09-09點(diǎn)云地面點(diǎn)濾波(Cloth Simulation Filter, CSF)
這篇文章主要介紹了點(diǎn)云地面點(diǎn)濾波(Cloth Simulation Filter, CSF)“布料”濾波算法介紹,本文從基本思想到實(shí)現(xiàn)思路一步步給大家講解的非常詳細(xì),需要的朋友可以參考下2021-08-08Tensorflow深度學(xué)習(xí)使用CNN分類英文文本
這篇文章主要為大家介紹了Tensorflow深度學(xué)習(xí)CNN實(shí)現(xiàn)英文文本分類示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11Python采集某度貼吧排行榜實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Python采集某度貼吧排行榜實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Python中條件語句、循環(huán)語句和pass語句的使用示例
Python條件語句是通過一條或多條語句的執(zhí)行結(jié)果(True或者False)來決定執(zhí)行的代碼塊,下面這篇文章主要給大家介紹了關(guān)于Python中條件語句、循環(huán)語句和pass語句使用的相關(guān)資料,需要的朋友可以參考下2022-06-06Python進(jìn)行數(shù)組的排序、倒序、截取方式
這篇文章主要介紹了Python進(jìn)行數(shù)組的排序、倒序、截取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02