Opencv学习笔记(八):如何通过cv2读取视频和摄像头来进行人脸检测(jetson nano)

简介: 如何使用OpenCV库通过cv2模块读取视频和摄像头进行人脸检测,并提供了相应的代码示例。

1.视频

通过视频每一帧的时间戳保存人脸图片
cap.get(cv2.CAP_PROP_POS_MSEC)/1000

vidio_path='/home/lqs/Documents/retinaface_lightweight.pytorch-master/1.mp4'
cap = cv2.VideoCapture(vidio_path) # open video
while True:
# read frame
    ret,frame=cap.read()
    if not ret: # if the camera over return false
        break
    start=time.time()
    boxes, landms, scores = detector(frame)
    if any(scores) and boxes[0][0]:
        end=time.time()
        time1 = round(end - start, 3)  # 保留一位小数 2为两位
        boxes = boxes.astype(np.int)
        landms = landms.astype(np.int)

        for i in range(len(boxes)):
            text = "{:.4f}".format(scores[i])
            faces=[boxes[i][0],boxes[i][1],boxes[i][2],boxes[i][3]]
            face_roi=align_process(frame,faces,landms[i],(112,112))
            a=round(cap.get(cv2.CAP_PROP_POS_MSEC)/1000,2) 
            cv2.imwrite(output1_path+str(a)+'.jpg',face_roi)
            cv2.rectangle(frame, (boxes[i][0], boxes[i][1]), (boxes[i][2], boxes[i][3]), (0, 0, 255), 2)
            cx = boxes[i][0]
            cy = boxes[i][1] + 12
            cv2.putText(frame, text, (cx, cy),
                        cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255))
            cv2.putText(frame, 'Cost: {}ms'.format(time1),
                                (10, 25), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.75, (0, 23, 255), 1)
            cv2.putText(frame,
                                'FPS: {:2.2f}'.format(1 / time1) if time1 > 0 else 'FPS: --',
                                (10, 50), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.75, (0, 23, 255), 1)
            # landms
            cv2.circle(frame, (landms[i][0][0], landms[i][0][1]), 1, (0, 0, 255), 4)
            cv2.circle(frame, (landms[i][1][0], landms[i][1][1]), 1, (0, 255, 255), 4)
            cv2.circle(frame, (landms[i][2][0], landms[i][2][1]), 1, (255, 0, 255), 4)
            cv2.circle(frame, (landms[i][3][0], landms[i][3][1]), 1, (0, 255, 0), 4)
            cv2.circle(frame, (landms[i][4][0], landms[i][4][1]), 1, (255, 0, 0), 4)
        cv2.imshow('video',frame)
        if ord('q')==cv2.waitKey(40):
            break
    else:
        print('no face')
        continue
# release resource
cv2.destroyAllWindows()
cap.release

----------------------------------------
# the offcial code
import cv2

def vidread(path):
    cap = cv2.VideoCapture(path)
    frames = []
    while(cap.isOpened()):
        ret, frame = cap.read()
        if ret:
            frames.append(frame)
        else:
            break
    cap.release()
    return frames

# save video
def vidwrite(path, frames):
    fourcc = cv2.VideoWriter_fourcc(*'MP4V')
    out = cv2.VideoWriter(path, fourcc, 30.0, (frames[0].shape[1], frames[0].shape[0]))
    for frame in frames:
        out.write(frame)
    out.release()

2.摄像头

通过CSI摄像头每一帧的系统时间保存图片

#import time 获取绝对时间年月日时分秒
#a=time.strftime('%Y%m%d%H%M%S', time.localtime())
#print(a)
import cv2,os,time
import argparse
import numpy as np
from align_faces import align_process
from retinaface import RetinaDetector
#设置gstreamer管道参数
def gstreamer_pipeline(
    capture_width=1280, #摄像头预捕获的图像宽度
    capture_height=720, #摄像头预捕获的图像高度
    display_width=1280, #窗口显示的图像宽度
    display_height=720, #窗口显示的图像高度
    framerate=60,       #捕获帧率
    flip_method=0,      #是否旋转图像
):
    return (
        "nvarguscamerasrc ! "
        "video/x-raw(memory:NVMM), "
        "width=(int)%d, height=(int)%d, "
        "format=(string)NV12, framerate=(fraction)%d/1 ! "
        "nvvidconv flip-method=%d ! "
        "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
        "videoconvert ! "
        "video/x-raw, format=(string)BGR ! appsink"
        % (
            capture_width,
            capture_height,
            framerate,
            flip_method,
            display_width,
            display_height,
        )
    )

if __name__ == "__main__":
    vidio_path='/home/z/Documents/retinaface_lightweight.pytorch-master/1.mp4'
    output_path = '/home/z/Documents/retinaface_lightweight.pytorch-master/retainmp4_result/'
    output1_path = '/home/z/Documents/retinaface_lightweight.pytorch-master/retainmp4_roi_result/'
    os.makedirs(os.path.dirname(output_path),exist_ok=True)
    os.makedirs(os.path.dirname(output1_path),exist_ok=True)

    capture_width = 1280
    capture_height = 720
    display_width = 1280
    display_height = 720
    framerate = 60
    flip_method = 0

    # 创建管道
    print(gstreamer_pipeline(capture_width,capture_height,display_width,display_height,framerate,flip_method))

    #管道与视频流绑定
    cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
    detector = RetinaDetector()
    if cap.isOpened():
        window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)

        # 逐帧显示
        while cv2.getWindowProperty("CSI Camera", 0) >= 0:
            ret_val, img = cap.read()
            if not ret_val: # if the camera over return false
                break
          # 图像太大需要调整
            height, width = img.shape[0:2]
            print("height=",height,"width=",width)
            if width > 800:
                new_width = 640
                new_height = int(new_width/width*height)
                img = cv2.resize(img, (new_width, new_height))
            print("new_height=",new_height,"new_width=",new_width)

            cv2.imshow("CSI Camera", img)
            #print("img.shape=",img.shape)
            keyCode = cv2.waitKey(30) & 0xFF         
            if keyCode == 27:# ESC键退出
                break
        #print("img.shape=",img.shape)
        #释放资源
        cap.release()
        cv2.destroyAllWindows()
    else:
        print("打开摄像头失败")
目录
相关文章
|
1天前
|
编解码 数据安全/隐私保护 计算机视觉
Opencv学习笔记(十):同步和异步(多线程)操作打开海康摄像头
如何使用OpenCV进行同步和异步操作来打开海康摄像头,并提供了相关的代码示例。
11 1
Opencv学习笔记(十):同步和异步(多线程)操作打开海康摄像头
|
1天前
|
计算机视觉 Python
Opencv学习笔记(十):opencv和base64之间的转换
本文介绍了如何使用Python和OpenCV库将图像在Base64编码和OpenCV可读格式之间进行转换。
6 0
|
1天前
|
编解码 计算机视觉 Python
Opencv学习笔记(九):通过CV2将摄像头视频流保存为视频文件
使用OpenCV库通过CV2将摄像头视频流保存为视频文件,包括定义视频编码格式、设置保存路径、通过write写入视频文件,并提供了相应的Python代码示例。
8 0
|
1月前
|
算法 计算机视觉
opencv图像形态学
图像形态学是一种基于数学形态学的图像处理技术,它主要用于分析和修改图像的形状和结构。
37 4
|
20天前
|
存储 计算机视觉
Opencv的基本操作(一)图像的读取显示存储及几何图形的绘制
本文介绍了使用OpenCV进行图像读取、显示和存储的基本操作,以及如何绘制直线、圆形、矩形和文本等几何图形的方法。
Opencv的基本操作(一)图像的读取显示存储及几何图形的绘制
|
2天前
|
计算机视觉
Opencv学习笔记(三):图像二值化函数cv2.threshold函数详解
这篇文章详细介绍了OpenCV库中的图像二值化函数`cv2.threshold`,包括二值化的概念、常见的阈值类型、函数的参数说明以及通过代码实例展示了如何应用该函数进行图像二值化处理,并展示了运行结果。
23 0
Opencv学习笔记(三):图像二值化函数cv2.threshold函数详解
|
2月前
|
算法 计算机视觉 Python
python利用opencv进行相机标定获取参数,并根据畸变参数修正图像附有全部代码(流畅无痛版)
该文章详细介绍了使用Python和OpenCV进行相机标定以获取畸变参数,并提供了修正图像畸变的全部代码,包括生成棋盘图、拍摄标定图像、标定过程和畸变矫正等步骤。
python利用opencv进行相机标定获取参数,并根据畸变参数修正图像附有全部代码(流畅无痛版)
WK
|
2月前
|
编解码 计算机视觉 Python
如何在OpenCV中进行图像转换
在OpenCV中,图像转换涉及颜色空间变换、大小调整及类型转换等操作。常用函数如`cvtColor`可实现BGR到RGB、灰度图或HSV的转换;`resize`则用于调整图像分辨率。此外,通过`astype`或`convertScaleAbs`可改变图像数据类型。对于复杂的几何变换,如仿射或透视变换,则可利用`warpAffine`和`warpPerspective`函数实现。这些技术为图像处理提供了强大的工具。
WK
82 1
|
4月前
|
算法 计算机视觉
【Qt&OpenCV 图像的感兴趣区域ROI】
【Qt&OpenCV 图像的感兴趣区域ROI】
125 1
|
4月前
|
运维 算法 计算机视觉
【Qt&OpenCV 图像的模板匹配 matchTemplate/minMaxLoc】
【Qt&OpenCV 图像的模板匹配 matchTemplate/minMaxLoc】
62 1