一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - python opencv通過按鍵采集圖片源碼

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

2021-11-08 10:16翟羽嚄 Python

OpenCV是一個基于BSD許可(開源)發(fā)行的跨平臺計算機(jī)視覺和機(jī)器學(xué)習(xí)軟件庫,可以運行在Linux、Windows、Android和Mac OS操作系統(tǒng)上,本文給大家分享python opencv通過按鍵采集圖片源碼,感興趣的朋友一起看看吧

一、python版本

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

功能說明

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

python opencv源碼

"""

“N”  新建文件夾 data/  用來存儲圖像
"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)建一個可以在給定圖像上繪圖的對象
    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)

# 存儲圖像的文件夾 
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)
    # 待會要寫的字體 / 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):
            pass
        else:
            os.mkdir(current_dir)
        print("
")
        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("請在按 "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()

效果圖

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

安裝相關(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()
{
	//測試視頻
	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è)置長度
	Mat frame;
	int n = 0;
	char* cstr = new char[120];
	while (true)
	{
		
		capture >> frame;
		if (frame.data == NULL)
		{
			printf("Image is empty
");
			//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("保存了圖片
");

		}

		
		namedWindow("111", 0);//參數(shù)為零,則可以自由拖動
		imshow("111", frame);
		waitKey(2);
	}

	return 0;

}

效果圖

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

到此這篇關(guān)于opencv通過按鍵采集圖片源碼的文章就介紹到這了,更多相關(guān)opencv按鍵采集圖片內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/mao_hui_fei/article/details/116985867

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩毛片高清在线看 | 国产一级在线观看 | 国产伦码精品一区二区三区 | 亚色九九九全国免费视频 | 亚洲天堂在线视频观看 | 午夜五月天 | 女人把扒开给男人爽的 | 给我免费的视频在线观看 | 人皮高跟鞋在线观看 | 蜜桃久久久亚洲精品成人 | 国模娜娜一区二区三区 | 91桃花| 我要色色网| 免费黄色片网站 | 日韩美毛片 | 免费一级毛片完整版在线看 | 男人和女人全黄一级毛片 | 国产成人福利美女观看视频 | 久久黄色精品视频 | 女人肮脏的交易中文字幕未删减版 | 日本人妖在线 | 99久久爱热6在线播放 | 黑人破中国女人处 | 欧美国产日本精品一区二区三区 | 免费看片aⅴ免费大片 | 天作谜案免费完整版在线观看 | 女学生被老师调教在教室 | 果冻传媒天美传媒在线小视频播放 | 免费观看a毛片一区二区不卡 | 歪歪漫画a漫入口 | 91九色视频无限观看免费 | 乌克兰粉嫩摘花第一次 | 国产精品反差婊在线观看 | 国产精品一二区 | 99热碰 | 国产欧美一区二区三区久久 | 欧美日韩国产手机在线观看视频 | 精品一区heyzo在线播放 | 久久99热狠狠色一区二区 | 精品午夜寂寞影院在线观看 | 国内视频一区二区三区 |