[openCV/Python]基于openCV的图片素材采集系统V1

import cv2 as cv
import numpy as np
import os
import sys
import pygame


# 图像预处理
def PROCESSING(img_ori):
    img_processed = cv.resize(img_ori, (512, 512))
    return img_processed


# 遍历文件函数
def getFileList(dir, Filelist, ext=None):
    """
    获取文件夹及其子文件夹中文件列表
    输入 dir:文件夹根目录
    输入 ext: 扩展名
    返回: 文件路径列表
    """
    newDir = dir
    if os.path.isfile(dir):
        if ext is None:
            Filelist.append(dir)
        else:
            if ext in dir[-3:]:
                Filelist.append(dir)

    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            newDir = os.path.join(dir, s)
            getFileList(newDir, Filelist, ext)

    return Filelist


ID_sum = 0
flag = 0
while (flag == 0):
    cap = cv.VideoCapture(ID_sum)
    ret, frame = cap.read()
    if ret == True:
        ID_sum += 1
    else:
        print("\n检测到" + str(ID_sum) + "个摄像头")
        break

flag = 0
ID_tmp = 0
while flag == 0:
    # 模块初始化
    pygame.init()
    pygame.joystick.init()
    # 若只连接了一个手柄,此处带入的参数一般都是0
    if (pygame.joystick.get_count() == 0):
        print("\n未成功连接到手柄")
    else:
        print("\n已成功连接到手柄")
        joystick = pygame.joystick.Joystick(0)
        # 手柄对象初始化
        joystick.init()
    print("\n正在读取摄像头")
    camera = cv.VideoCapture(ID_tmp)  # 摄像头读取
    path_image = 'D:\pythonProject4_camera\image_capture'  # 路径读取

    if camera.get(cv.CAP_PROP_FPS) == 0.0:
        print("\n摄像头读取失败,请检查摄像头是否连接")
        break
    else:
        print("\n摄像头读取成功")

    print("\n视频中图像的宽度为{}".format(camera.get(cv.CAP_PROP_FRAME_WIDTH)))
    print("视频中图像的高度为{}".format(camera.get(cv.CAP_PROP_FRAME_HEIGHT)))
    print("视频帧率为{}".format(camera.get(cv.CAP_PROP_FPS)))

    if camera.isOpened():
        print("\n视频播放中")
        print("\n输入Esc键或A键(手柄)以退出窗口")
        print("输入p键或B键(手柄)以捕获图像")
        print("输入d键或X键(手柄)以删除图像")
        print("输入c键或Y键(手柄)以切换摄像头")
        print("输入a键或LB键(手柄)以打开或关闭摇杆显示")
        print("\n请勿在程序运行时手动修改捕获图像文件夹")

    # 遍历获取图片数量
    imgfolder = path_image  # 搜索路径,会自动搜索子文件夹
    imglist = getFileList(imgfolder, [], 'jpg')  # 取出jpg图像文件路径,为字符串类型数组
    print("\n检测到" + imgfolder + "下有" + str(len(imglist)) + '张图像')  # 输出图像数量
    capture_num = len(imglist)

    while camera.isOpened():
        ret, frame = camera.read()
        if ret is True:
            frame = cv.resize(frame, (0, 0), None, 1, 1)  # 视频大小调整
            frame = cv.flip(frame, -1)  # 中心对称
            cv.imshow("camera", frame)
            cv.imshow("processed camera", PROCESSING(frame))

            flag_esc = 0
            flag_capture = 0
            flag_del = 0
            flag_cam = 0
            flag_axis = 0

            # pygame.event.get()可以获得当前用户所做动作的事件列表
            for event_ in pygame.event.get():
                if event_.type == pygame.JOYBUTTONDOWN or event_.type == pygame.JOYBUTTONUP:
                    # joystick.get_button(3)为获取按键3(Y键)动作,返回1为按下,0为抬起
                    if joystick.get_button(3) == 1:
                        print("\n检测到Y键已按下")
                        flag_cam = 1
                    # joystick.get_button(2)为获取按键2(X键)动作,返回1为按下,0为抬起
                    if joystick.get_button(2) == 1:
                        print("\n检测到X键已按下")
                        flag_del = 1
                    # joystick.get_button(1)为获取按键1(B键)动作,返回1为按下,0为抬起
                    if joystick.get_button(1) == 1:
                        print("\n检测到B键已按下")
                        flag_capture = 1
                    # joystick.get_button(0)为获取按键0(A键)动作,返回1为按下,0为抬起
                    if joystick.get_button(0) == 1:
                        print("\n检测到A键已按下")
                        flag_esc = 1
                        flag = 1

                    if joystick.get_button(4) == 1:
                        print("\n检测到LB键已按下")
                        flag_axis = 1
                    if joystick.get_button(5) == 1:
                        print("\n检测到RB键已按下")

            cv.waitKey(int(1000 / camera.get(cv.CAP_PROP_FPS)))

            if cv.waitKey(1) == 99 or flag_cam == 1:
                if (ID_tmp < ID_sum - 1):
                    ID_tmp += 1
                else:
                    ID_tmp = 0
                print("已成功更换到索引为" + str(ID_tmp) + "的摄像头")
                break
            if cv.waitKey(1) == 27 or flag_esc == 1:
                flag = 1
                print("已安全退出窗口")
                break
            if cv.waitKey(1) == 112 or flag_capture == 1:
                capture_num += 1
                if not os.path.exists(path_image):
                    print(path_image + "路径不存在")
                if os.path.exists(path_image + "\\" + str(capture_num) + ".jpg"):
                    os.remove(path_image + "\\" + str(capture_num) + ".jpg")
                    print(path_image + "下的" + str(capture_num) + ".jpg已被成功替换")
                cv.imwrite(path_image + "\\" + str(capture_num) + ".jpg", frame)
                print("已成功捕获" + str(capture_num) + ".jpg")
            if cv.waitKey(1) == 100 or flag_del == 1:
                if os.path.exists(path_image + "\\" + str(capture_num) + ".jpg"):
                    os.remove(path_image + "\\" + str(capture_num) + ".jpg")
                    print("已删除" + str(capture_num) + ".jpg")
                    capture_num -= 1
                else:
                    print("删除" + str(capture_num) + ".jpg失败,请检查参数")

            if cv.waitKey(1) == 97 or flag_axis == 1:
                print("左摇杆x值:" + str(joystick.get_axis(0)))
                print("左摇杆y值:" + str(-joystick.get_axis(1)))
                print("右摇杆x值:" + str(joystick.get_axis(2)))
                print("右摇杆y值:" + str(-joystick.get_axis(3)))

            del (ret)
            del (frame)

        else:
            break
    camera.release()
    cv.destroyAllWindows()

    pygame.quit()

(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)

你可能感兴趣的:(Python,openCV,opencv,pygame,python,计算机视觉,人工智能)