嵌入式设备或者移动端设备由于没有GPU,是NPU、TPU、CPU导致模型推理速度跟不上

此时就需要抽帧跳帧检测推理识别

原理:假设视频是30帧,则目标画面在毫秒内也就是3帧之内变化不大,只需要识别第一帧,第二帧和第三帧使用第一帧的识别结果进行标注画锚框

    ## 嵌入式设备深度学习AI推理跳帧检测原理
    cap = cv2.VideoCapture(0)
    # 跳几帧推理检测:推荐2或者3,优先推荐设置为2(跳过一帧:间隔1帧推理一次),如果性能达不到可以设置3(跳过2帧:间隔2帧推理一次)
    # 但是市面上大部分设置的是3
    detect_skip = 3
    # 跳帧计数
    detect_skip_index = 1;
    # 最新一帧也就是上一帧推理结果
    dict_result = None
    while True:
        ret, img = cap.read()
        if img.shape[-1]==4:
            img=cv2.cvtColor(img,cv2.COLOR_BGRA2BGR)
        # 余数为0
        if detect_skip_index % detect_skip == 0 or dict_result == None:
            detect_skip_index = 1
            # 模型推理
            dict_result = detect(model, img, device)
        else:
            detect_skip_index = detect_skip_index + 1
        cv2.imshow("result", draw_result(img,dict_result))
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()

百度paddle官方原文

人脸识别可以使用python里的cv2库,该库可以实现提取图片或者视频中的人脸信息。本文使用的是cv2库配合百度智能云的人脸识别接口准确率比较高,最后会附带代码 cv2库安装: pip install opencv-python 百度智能云的人脸识别:百度智能云 需要安装百度的库,这是接口文档 pip install baidu-aip 2.从视频提取图片 由于视频是无法直接进行人脸识别的,我们只需要使用cv2库将图片切割为一帧一帧的就可以使用图片进行人脸识别了 import
基于yolov5+deepsort+slowfast算法的视频实时行为检测。 1. yolov5实现目标检测,确定目标坐标 2. deepsort实现目标跟踪,持续标注目标坐标 3. slowfast实现动作识别,并给出置信率 4. 用框持续框住目标,并将动作类别以及置信度显示在框上
# Create a blob from the image blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False) # Feed the blob to the model net.setInput(blob) # Get the output layer names of the model layer_names = net.getLayerNames() layer_names = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] # Perform forward pass to get detections detections = net.forward(layer_names) # Get the detections from the output detections = np.squeeze(detections) # Get the class IDs and confidences for each detection class_ids, confidences, boxes = [], [], [] for detection in detections: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5: center_x, center_y, w, h = (detection[0:4] * np.array([img.shape[1], img.shape[0], img.shape[1], img.shape[0]])).astype('int') x, y = int(center_x - w / 2), int(center_y - h / 2) boxes.append([x, y, int(w), int(h)]) confidences.append(float(confidence)) class_ids.append(class_id) # Perform non-maximum suppression to get the final detections indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) # Draw the detections on the image for i in indices: i = i[0] box = boxes[i] x, y, w, h = box[0], box[1], box[2], box[3] cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) # Display the image cv2.imshow("detections", img) cv2.waitKey() cv2.destroyAllWindows() 希望这对你有帮助。 ### 回答2: YOLOv8是一个实时目标检测算法,它是YOLO(You Only Look Once)系列的最新版本。下面是一个示例Python代码,用于进行YOLOv8实时检测推理: ```python import cv2 import torch from torchvision import transforms from yolov5.models.experimental import attempt_load from yolov5.utils.general import non_max_suppression, scale_coords from yolov5.utils.torch_utils import select_device # 加载模型 weights = 'yolov5s.pt' # 替换为YOLOv8的权重文件路径 device = select_device('') model = attempt_load(weights, map_location=device) stride = int(model.stride.max()) # 计算特征提取步长 # 准备图像 img_size = 640 # 设置输入图像的大小 transform = transforms.Compose([ transforms.ToPILImage(), transforms.Resize(img_size), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # 设置阈值和类别 conf_thres = 0.3 # 检测置信度阈值 iou_thres = 0.5 # 非最大值抑制的IOU阈值 names = model.module.names if hasattr(model, 'module') else model.names # 开启实时摄像头 cap = cv2.VideoCapture(0) while cap.isOpened(): success, frame = cap.read() if not success: break # 图像预处理 img = transform(frame).unsqueeze(0).to(device) img /= 255.0 # 像素值归一化到[0, 1] if img.ndimension() == 3: img = img.unsqueeze(0) # 检测推理 pred = model(img)[0] pred = non_max_suppression(pred, conf_thres, iou_thres)[0] pred = scale_coords(img_size, pred[:, :4], frame.shape).round() # 绘制边界框和标签 for x1, y1, x2, y2, conf, cls in pred: cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2) cv2.putText(frame, f'{names[int(cls)]} {conf:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 0), 2) # 展示结果 cv2.imshow('YOLOv8 - Real-time Object Detection', frame) if cv2.waitKey(1) == 27: break # 释放资源 cap.release() cv2.destroyAllWindows() 这段代码使用了OpenCV和PyTorch,首先加载YOLOv8模型的权重文件,然后设置输入图像的大小和预处理方式。接下来,通过调用摄像头获取实时图像,并将其进行预处理。然后通过模型进行推理,获得目标检测结果。最后,将检测结果绘制在原始图像上,并实时展示在窗口中。用户可以按下ESC键停止检测。 注意,这段代码仅提供了一个基本的框架,可能需要根据具体情况进行适当的调整和优化。 ### 回答3: YOLOv8是一种实时目标检测算法,它在Python中有相应的推理代码。YOLOv8的推理代码可以用来将模型训练所得的权重文件加载到内存中,并使用这些权重来进行实时目标检测。 在Python中,我们首先需要安装相应的库和依赖项,如PyTorch、OpenCV等。安装完成后,我们可以编写推理代码推理代码的第一步是加载YOLOv8的模型权重文件。通过调用PyTorch提供的相关函数,我们可以将预训练好的权重文件加载到内存中。 接下来,我们需要定义模型的输入和输出。对于YOLOv8,输入是一张图像,输出是检测到的目标的边界框和类别信息。 在推理过程中,我们将输入图像传递给模型,模型将返回一组预测框。然后,我们可以根据预测框的置信度和类别信息进行筛选和筛除,以得到最终的检测结果。 为了实现实时检测,我们可以将推理过程放入一个循环中。在每一次循环中,我们读取一帧图像,并将其传递给模型进行检测。然后,我们可以将检测结果可视化或保存下来。 需要注意的是,YOLOv8的推理代码需要在具备足够计算资源的机器上运行,因为它需要高性能的GPU来实现实时检测。 总之,YOLOv8的实时检测推理Python代码包括加载权重文件、定义输入输出、进行推理过程,以及在循环中实现实时检测。这些代码可以帮助我们实现基于YOLOv8的目标检测应用。
luchen4396: 用root权限修改配置文件总会报错.config/code-server/config.yaml" E212: Can't open file for writing ,导致无法保存配置文件,这该怎么解决 深度学习目标检测在游戏领域的应用 zfw5850: 我想要数据集,麻烦发一下 718059569@qq.com,谢谢大佬 python使用onnx模型进行推理 深度物联网: jupyter运行一下,获取看我另一篇文章