jetson nano 人体姿态识别

该博客展示了如何结合GStreamer捕获CSI摄像头的视频流,并利用GluonCV的预训练模型进行人体姿态估计。通过定义GStreamer管道来处理摄像头输入,然后使用SSD检测器定位人员,最后使用简单姿势估计器获取关键点坐标,实现图像中人物的实时姿态识别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul  1 17:17:29 2021

@author: ledi
"""

import cv2
 
 
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,
        )
    )
 
 
# def show_camera():
#     cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
 
#     while cap.isOpened():
#         flag, img = cap.read()
#         cv2.imshow("CSI Camera", img)
#         kk = cv2.waitKey(1)
 
#         # do other things
 
#         if kk == ord('q'):  # 按下 q 键,退出
#             break
 
#     cap.release()
#     cv2.destroyAllWindows()
    
    


#from __future__ import division
import argparse, time, logging, os, math, tqdm, cv2

import numpy as np
import mxnet as mx
from mxnet import gluon, nd, image
from mxnet.gluon.data.vision import transforms

import matplotlib.pyplot as plt

import gluoncv as gcv
from gluoncv import data
from gluoncv.data import mscoco
from gluoncv.model_zoo import get_model
from gluoncv.data.transforms.pose import detector_to_simple_pose, heatmap_to_coord
from gluoncv.utils.viz import cv_plot_image, cv_plot_keypoints



ctx = mx.cpu()
detector_name = "ssd_512_mobilenet1.0_coco"
detector = get_model(detector_name, pretrained=True, ctx=ctx)




detector.reset_class(classes=['person'], reuse_weights={'person':'person'})
detector.hybridize()



estimator = get_model('simple_pose_resnet18_v1b', pretrained='ccd24037', ctx=ctx)
estimator.hybridize()

#如果是linux 免驱摄像头,用这个
#cap = cv2.VideoCapture(0)
time.sleep(1)  ### letting the camera autofocus



axes = None
num_frames = 100

#csi 摄像头
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
 
while cap.isOpened():
    flag, frame = cap.read()
    print(11111)
    # cv2.imshow("CSI Camera", img)
    # kk = cv2.waitKey(1)
 
    # # do other things
 
    # if kk == ord('q'):  # 按下 q 键,退出
    #     break

    # ret, frame = cap.read()
    frame = mx.nd.array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)).astype('uint8')
    
    print(2222)

    x, frame = gcv.data.transforms.presets.ssd.transform_test(frame, short=512, max_size=350)
    
    print(333)
    x = x.as_in_context(ctx)
    class_IDs, scores, bounding_boxs = detector(x)
    print(444)

    pose_input, upscale_bbox = detector_to_simple_pose(frame, class_IDs, scores, bounding_boxs,
                                                       output_shape=(128, 96), ctx=ctx)
    
    print(pose_input)
    if len(upscale_bbox) > 0:
        predicted_heatmap = estimator(pose_input)
        pred_coords, confidence = heatmap_to_coord(predicted_heatmap, upscale_bbox)

        img = cv_plot_keypoints(frame, pred_coords, confidence, class_IDs, bounding_boxs, scores,
                                box_thresh=0.5, keypoint_thresh=0.2)
        cv_plot_image(img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        # 存储图片
        cv2.imwrite("camera.jpeg", frame)
        break

#python cam_demo.py --num-frames 100




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值