亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python opencv通過按鍵采集圖片源碼

瀏覽:4日期:2022-06-19 09:30:22
一、python版本

寫了個python opencv的小demo,可以通過鍵盤按下字母s進行采集圖像。

功能說明

“N” 新建文件夾 data/ 用來存儲圖像“S” 開始采集圖像,將采集到的圖像放到 data/ 路徑下“Q” 退出窗口

python opencv源碼

’’’“N” 新建文件夾 data/ 用來存儲圖像'S' 開始采集圖像,將采集到的圖像放到 data/ 路徑下“Q” 退出窗口’’’import numpy as np # 數據處理的庫 Numpyimport cv2 # 圖像處理的庫 OpenCvimport os # 讀寫文件import shutil # 讀寫文件from PIL import Image, ImageDraw, ImageFont# # OpenCv 調用攝像頭 / Use cameracap = 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:字體大小’’’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)) # 創建一個可以在給定圖像上繪圖的對象 draw = ImageDraw.Draw(img) # 字體的格式 fontStyle = ImageFont.truetype('font/simsun.ttc', textSize, encoding='utf-8') # 繪制文本 draw.text((left, top), text, textColor, font=fontStyle) # 轉換回OpenCV格式 return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)# 存儲圖像的文件夾 current_dir = ''# 保存 圖像 的路徑 path_photos_from_camera = 'data/'press_n_flag = 0cnt_ss=0while cap.isOpened(): flag, img_rd = cap.read() #print(img_rd.shape) kk = cv2.waitKey(2) # 待會要寫的字體 / Font to write font = cv2.FONT_ITALIC # 4. 按下 ’n’ 新建存儲人臉的文件夾 / 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): passelse: os.mkdir(current_dir)print(’n’)print('新建的保存圖像的文件夾 / Create folders: ', current_dir)press_n_flag = 1# 已經按下 ’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('請在按 ’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, '圖片采集系統', 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: 創建保存圖像文件夾', 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: 保存當前圖片', 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 # 如果需要攝像頭窗口大小可調 / 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 windowscap.release()cv2.destroyAllWindows()效果圖

python opencv通過按鍵采集圖片源碼

安裝相關庫

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 1080int main(){//測試視頻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);//設置寬度capture.set(CAP_PROP_FRAME_HEIGHT, SRC_HEIGHT); //設置長度Mat frame;int n = 0;char* cstr = new char[120];while (true){capture >> frame;if (frame.data == NULL){printf('Image is emptyn');//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);//參數為零,則可以自由拖動imshow('111', frame);waitKey(2);}return 0;}

效果圖

python opencv通過按鍵采集圖片源碼

到此這篇關于opencv通過按鍵采集圖片源碼的文章就介紹到這了,更多相關opencv按鍵采集圖片內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 精品欧美成人高清视频在线观看 | 午夜精品福利在线 | 日本免费黄网站 | 久久中文字幕久久久久 | 18岁禁免费网站 | 国产一区二区三区在线观看影院 | 免费能直接在线观看黄的视频 | 免费成人黄色网址 | 成人自拍视频 | 成人免费在线视频观看 | 95视频在线观看免费 | 国产在线观看xxxx免费 | 亚洲精品久久久久综合网 | 极品国模私拍福利在线观看 | 妖精www视频在线观看高清 | 黄色大片在线观看 | 亚洲色图日韩精品 | 99这里只有精品66视频 | 久草在线中文888 | 青青草国产三级精品三级 | 亚洲精品久中文字幕 | 亚洲黄色自拍 | 一级女性全黄久久生活片 | 中国一级黄色毛片 | 欧美亚洲国产一区二区三区 | 成人免费男女视频网站慢动作 | 免费国产一级特黄久久 | 亚洲欧美在线综合一区二区三区 | 久9视频这里只有精品8 | 尤物视频免费在线观看 | 亚洲精品乱码国产精品乱码 | 日韩一级欧美一级毛片在线 | 欧美精品一区二区三区视频 | 午夜久草| 国产精品久久毛片 | 欧美日韩一级大片 | 久久国产精品自线拍免费 | 国产黄色a三级三级三级 | 精品一区二区三区的国产在线观看 | 亚洲成色999久久网站 | 中文字幕一区二区三区四区 |