测试的代码

# 导入必要模块

import cv2
import numpy as np
import time
start = time.time()
vc = cv2.VideoCapture(0)#('data/data.mp4')
c = 1
d = 0
if vc.isOpened():  # 判断是否正常打开
    rval, frame = vc.read()
else:
    rval = False

timeF = 5  # 视频帧计数间隔频率
# try:
lower_colorYellow = np.array([26, 43, 46])  # 分别对应着HSV中的最小值
upper_colorYellow = np.array([34, 255, 255])  # 分别对应着HSV中的最大值
lower_colorRed = np.array([0, 43, 46])
upper_colorRed = np.array([10, 255, 255])
lower_colorRed2 = np.array([156, 43, 46])
upper_colorRed2 = np.array([180, 255, 255])
while rval:  # 循环读取视频帧
    rval, frame = vc.read()
    if (c % timeF == 0):  # 每隔timeF帧进行存储操作
        d = d + 1
        img = frame

        tag = 0
        res = ''
        b, g, r = cv2.split(img)

        cv2.waitKey(0)
        # img = cv2.imread("data/red1.png")  # 读取图片路径
        img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)  # 将BGR图像转换为HSV格式

        YellowMask = cv2.inRange(img_hsv, lower_colorYellow, upper_colorYellow)  # inrange函数将根据最小最大HSV值检测出自己想要的颜色部分
        RedMask = cv2.inRange(img_hsv, lower_colorRed, upper_colorRed)  # inrange函数将根据最小最大HSV值检测出自己想要的颜色部分
        RedMask2 = cv2.inRange(img_hsv, lower_colorRed2, upper_colorRed2)  # inrange函数将根据最小最大HSV值检测出自己想要的颜色部分
        if YellowMask.max() == 255:
            print(' 检测到黄色!')
            tag = 1
            res = ' yellow '
        elif RedMask.max() == 255:

            print('检测到红色!')
            tag = 1
            res = ' red '
        elif RedMask2.max() == 255:
            print('检测到红色!')

            tag = 1
            res = ' red '
        if tag == 1:
            cv2.imwrite('data/res/' + str(d) +res+''+str(c) + '.jpg', frame)  # 存储为图像-----Ubuntu下路径
        # cv2.imwrite('E:/yolov5-pytorch-main-attention/1/' + str(d) + '.jpg', frame)  # 存储为图像-----Windows下路径
            #print(d)

    c = c + 1
    cv2.waitKey(1)
vc.release()
end = time.time()
print(end - start, 's')
#cv2.imshow("mask", RedMask2)  # 通过imshow显示
cv2.waitKey(0)  # 等待键盘按下按键退出

你可能感兴趣的:(Deep,Learning,python,计算机视觉,opencv)